Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support HTML-escaped cues in VTT #4660

Merged
merged 4 commits into from
Nov 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion lib/text/vtt_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ shaka.text.VttTextParser = class {
end += timeOffset;

// Get the payload.
const payload = text.slice(1).join('\n').trim();
const payload = VttTextParser.htmlUnescape_(
text.slice(1).join('\n').trim());

let cue = null;
if (styles.has('global')) {
Expand Down Expand Up @@ -843,6 +844,35 @@ shaka.text.VttTextParser = class {

return (milliseconds / 1000) + seconds + (minutes * 60) + (hours * 3600);
}

/**
* This method converts the HTML entities &, <, >, ", and
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
* ' in string to their corresponding characters.
*
* @param {!string} input
* @return {string}
* @private
*/
static htmlUnescape_(input) {
/** Used to map HTML entities to characters. */
avelad marked this conversation as resolved.
Show resolved Hide resolved
const htmlUnescapes = {
'&': '&',
'&lt;': '<',
'&gt;': '>',
'&quot;': '"',
'&#39;': '\'',
};

/** Used to match HTML entities and HTML characters. */
avelad marked this conversation as resolved.
Show resolved Hide resolved
const reEscapedHtml = /&(?:amp|lt|gt|quot|#(0+)?39);/g;
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
const reHasEscapedHtml = RegExp(reEscapedHtml.source);
if (input && reHasEscapedHtml.test(input)) {
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
return input.replace(reEscapedHtml, (entity) => {
return htmlUnescapes[entity] || '\'';
});
}
return input || '';
}
};

/**
Expand Down