From 7b22230e2c05aaaf0c5208098dfa38528315c521 Mon Sep 17 00:00:00 2001 From: aidenlx <31102694+aidenlx@users.noreply.github.com> Date: Tue, 3 Sep 2024 00:57:08 +0800 Subject: [PATCH] allow ":" or "=" inside metadata value --- src/vtt/vtt-parser.ts | 7 ++++-- tests/vtt/header.test.ts | 50 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/vtt/vtt-parser.ts b/src/vtt/vtt-parser.ts index 2587c2b..dbc5366 100644 --- a/src/vtt/vtt-parser.ts +++ b/src/vtt/vtt-parser.ts @@ -115,8 +115,11 @@ export class VTTParser implements CaptionsParser { protected _parseHeader(line: string, lineCount: number) { if (lineCount > 1) { if (SETTING_SEP_RE.test(line)) { - const [key, value] = line.split(SETTING_SEP_RE); - if (key) this._metadata[key] = (value || '').replace(SPACE_RE, ''); + // get index of first separator + const firstSepIndex = line.match(SETTING_SEP_RE)!.index!; + const key = line.substring(0, firstSepIndex).trim(); + const value = line.substring(firstSepIndex + 1).trim(); + this._metadata[key] = value; } } else if (line.startsWith(HEADER_MAGIC)) { this._block = VTTBlock.Header; diff --git a/tests/vtt/header.test.ts b/tests/vtt/header.test.ts index 736ec24..c9b09b3 100644 --- a/tests/vtt/header.test.ts +++ b/tests/vtt/header.test.ts @@ -14,9 +14,9 @@ test('BAD: should throw in strict mode if header is missing', async () => { ).rejects.toThrowError(); }); -test('GOOD: parse header metadata', async () => { +test('GOOD: parse header metadata with : and =', async () => { const { errors, metadata, cues } = await parseText( - ['WEBVTT', 'Kind: Language', 'Language:en-US'].join('\n'), + ['WEBVTT', 'Kind: Language', 'Language= en-US'].join('\n'), ); expect(errors).toHaveLength(0); @@ -29,3 +29,49 @@ test('GOOD: parse header metadata', async () => { } `); }); + +test('GOOD: parse header metadata with proper trimming', async () => { + const { errors, metadata, cues } = await parseText( + ['WEBVTT', 'Message:Hello World! ', ' My Property : Value '].join('\n'), + ); + + expect(errors).toHaveLength(0); + expect(cues).toHaveLength(0); + + expect(metadata).toStrictEqual({ + 'Message': 'Hello World!', + 'My Property': 'Value', + }); +}); + +test('GOOD: parse header metadata with value containing = or :', async () => { + const { errors, metadata, cues } = await parseText( + ['WEBVTT', 'Key1: Value with = sign', 'Key2: Value with : colon'].join('\n'), + ); + + expect(errors).toHaveLength(0); + expect(cues).toHaveLength(0); + + expect(metadata).toStrictEqual({ + "Key1": "Value with = sign", + "Key2": "Value with : colon", + }); +}); + +test('GOOD: parse header metadata with JSON and "=" in value', async () => { + const { errors, metadata, cues } = await parseText( + ['WEBVTT', + 'X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:0', + 'Json: {"a": 1, "b": 2}', + 'Eval: 1 + 1 = 2'].join('\n'), + ); + + expect(errors).toHaveLength(0); + expect(cues).toHaveLength(0); + + expect(metadata).toStrictEqual({ + "X-TIMESTAMP-MAP": "LOCAL:00:00:00.000,MPEGTS:0", + "Json": '{"a": 1, "b": 2}', + "Eval": "1 + 1 = 2", + }); +}); \ No newline at end of file