fix(hls): parse M3U8 attributes respecting quoted strings

The previous implementation split on commas without respecting quoted
values, causing subtitles with commas in names (e.g., "English, Forced")
or URIs with special characters to be incorrectly parsed.

Uses regex to properly match key=value pairs where values can be either
quoted (containing any characters except quotes) or unquoted (no commas).
This commit is contained in:
Fredrik Burmester
2026-01-05 21:28:00 +01:00
parent 964d53cc79
commit cbbac3c25c

View File

@@ -44,12 +44,14 @@ export async function parseM3U8ForSubtitles(
function parseAttributes(line: string): { [key: string]: string } {
const attributes: { [key: string]: string } = {};
const parts = line.split(",");
parts.forEach((part) => {
const [key, value] = part.split("=");
if (key && value) {
attributes[key.trim()] = value.replace(/"/g, "").trim();
}
});
const regex = /([A-Z-]+)=(?:"([^"]*)"|([^,]*))/g;
let match: RegExpExecArray | null;
while ((match = regex.exec(line)) !== null) {
const key = match[1];
const value = match[2] ?? match[3]; // quoted or unquoted
attributes[key] = value;
}
return attributes;
}