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
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 35 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,39 @@ 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.
const htmlUnescapes = {
'&': '&',
'&lt;': '<',
'&gt;': '>',
'&quot;': '"',
'&#39;': '\'',
};

// Used to match HTML entities and HTML characters.
const reEscapedHtml = /&(?:amp|lt|gt|quot|#(0+)?39);/g;
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
const reHasEscapedHtml = RegExp(reEscapedHtml.source);
// This check is an optimization, since replace always makes a copy
if (input && reHasEscapedHtml.test(input)) {
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
return input.replace(reEscapedHtml, (entity) => {
// The only thing that might not match the dictionary above is the
// single quote, which can be matched by many strings in the regex, but
// only has a single entry in the dictionary.
return htmlUnescapes[entity] || '\'';
});
}
return input || '';
}
};

/**
Expand Down
11 changes: 11 additions & 0 deletions test/text/vtt_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,17 @@ describe('VttTextParser', () => {
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

it('support escaped html payload', () => {
verifyHelper(
[
{startTime: 20.1, endTime: 40.505, payload: '"Test & 1"'},
],
'WEBVTT\n\n' +
'00:00:20.100 --> 00:00:40.505\n' +
'&quot;Test &amp; 1&quot;',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

it('supports specific style blocks', () => {
verifyHelper(
[
Expand Down