Skip to content

Commit 7b22230

Browse files
committed
allow ":" or "=" inside metadata value
1 parent 95eea28 commit 7b22230

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/vtt/vtt-parser.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ export class VTTParser implements CaptionsParser {
115115
protected _parseHeader(line: string, lineCount: number) {
116116
if (lineCount > 1) {
117117
if (SETTING_SEP_RE.test(line)) {
118-
const [key, value] = line.split(SETTING_SEP_RE);
119-
if (key) this._metadata[key] = (value || '').replace(SPACE_RE, '');
118+
// get index of first separator
119+
const firstSepIndex = line.match(SETTING_SEP_RE)!.index!;
120+
const key = line.substring(0, firstSepIndex).trim();
121+
const value = line.substring(firstSepIndex + 1).trim();
122+
this._metadata[key] = value;
120123
}
121124
} else if (line.startsWith(HEADER_MAGIC)) {
122125
this._block = VTTBlock.Header;

tests/vtt/header.test.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ test('BAD: should throw in strict mode if header is missing', async () => {
1414
).rejects.toThrowError();
1515
});
1616

17-
test('GOOD: parse header metadata', async () => {
17+
test('GOOD: parse header metadata with : and =', async () => {
1818
const { errors, metadata, cues } = await parseText(
19-
['WEBVTT', 'Kind: Language', 'Language:en-US'].join('\n'),
19+
['WEBVTT', 'Kind: Language', 'Language= en-US'].join('\n'),
2020
);
2121

2222
expect(errors).toHaveLength(0);
@@ -29,3 +29,49 @@ test('GOOD: parse header metadata', async () => {
2929
}
3030
`);
3131
});
32+
33+
test('GOOD: parse header metadata with proper trimming', async () => {
34+
const { errors, metadata, cues } = await parseText(
35+
['WEBVTT', 'Message:Hello World! ', ' My Property : Value '].join('\n'),
36+
);
37+
38+
expect(errors).toHaveLength(0);
39+
expect(cues).toHaveLength(0);
40+
41+
expect(metadata).toStrictEqual({
42+
'Message': 'Hello World!',
43+
'My Property': 'Value',
44+
});
45+
});
46+
47+
test('GOOD: parse header metadata with value containing = or :', async () => {
48+
const { errors, metadata, cues } = await parseText(
49+
['WEBVTT', 'Key1: Value with = sign', 'Key2: Value with : colon'].join('\n'),
50+
);
51+
52+
expect(errors).toHaveLength(0);
53+
expect(cues).toHaveLength(0);
54+
55+
expect(metadata).toStrictEqual({
56+
"Key1": "Value with = sign",
57+
"Key2": "Value with : colon",
58+
});
59+
});
60+
61+
test('GOOD: parse header metadata with JSON and "=" in value', async () => {
62+
const { errors, metadata, cues } = await parseText(
63+
['WEBVTT',
64+
'X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:0',
65+
'Json: {"a": 1, "b": 2}',
66+
'Eval: 1 + 1 = 2'].join('\n'),
67+
);
68+
69+
expect(errors).toHaveLength(0);
70+
expect(cues).toHaveLength(0);
71+
72+
expect(metadata).toStrictEqual({
73+
"X-TIMESTAMP-MAP": "LOCAL:00:00:00.000,MPEGTS:0",
74+
"Json": '{"a": 1, "b": 2}',
75+
"Eval": "1 + 1 = 2",
76+
});
77+
});

0 commit comments

Comments
 (0)