w3c / webvtt.js

WebVTT parser and validator
https://w3c.github.io/webvtt.js/parser.html
Creative Commons Zero v1.0 Universal
146 stars 49 forks source link

Entities object incorrectly passed to WebVTTCueTextParser #35

Closed RaminRabani closed 2 years ago

RaminRabani commented 2 years ago

Bug Description

In parser.js, the entities object is assigned to this on line 29:

line 29: this.entities = entities

If we console.log(this) right after line 29, we see that this corresponds to WebVTTParser.

Next on line 30, this.parse is assigned to a function. In the parse function on line 177, it is attempted to pass that same entities object to the WebVTTCueTextParser:

line 177: var cuetextparser = new WebVTTCueTextParser(cue.text, err, mode, this.entities)

However, if we do another console.log(this) here, we see that it corresponds to Window because it is being used inside the function.

So this on line 29 is not the same as this on line 177, meaning that the argument this.entities passed to the WebVTTCueTextParser on line 177 will always be undefined. This causes a bug later on line 639 in the parser

if(self.entities[buffer]) {

Uncaught TypeError: Cannot read properties of undefined (reading '&M') where the buffer here is &M and self.entities is undefined.


Proposed Change

The proposed solution is to update how entities is passed to the WebVTTCueTextParser constructor on line 177 of parser.js:

Before:

var cuetextparser = new WebVTTCueTextParser(cue.text, err, mode, this.entities)

After:

var cuetextparser = new WebVTTCueTextParser(cue.text, err, mode, entities)

Steps to Reproduce:

const vttData = `
WEBVTT

1
00:11:46.140 --> 00:11:48.380
Texas A&M`

const { parse } = new WebVTTParser();
const parsed = parse(vttData, "metadata");
// ===> Uncaught TypeError: Cannot read properties of undefined (reading '&M')