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(WebVTT): Remove un-needed VTT payload normalisation #6145

Merged
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
62 changes: 1 addition & 61 deletions lib/text/vtt_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ shaka.text.VttTextParser = class {
if (styles.size === 0) {
VttTextParser.addDefaultTextColor_(styles);
}
payload = VttTextParser.replaceColorPayload_(payload);

payload = VttTextParser.replaceKaraokeStylePayload_(payload);
payload = VttTextParser.replaceVoiceStylePayload_(payload);
const xmlPayload = '<span>' + payload + '</span>';
Expand Down Expand Up @@ -601,66 +601,6 @@ shaka.text.VttTextParser = class {
return newPayload;
}

/**
* Converts color end tag to be valid for xml parsing
* For example,
* input: <c.yellow.bg_blue>Yellow text on blue bg</c>
* output: <c.yellow.bg_blue>Yellow text on blue bg</c.yellow.bg_blue>
*
* Returns original payload if invalid tag is found.
* Invalid tag example: <c.yellow><b>Example</c></b>
*
* @param {string} payload
* @return {string} processed payload
* @private
*/
static replaceColorPayload_(payload) {
const names = [];
let nameStart = -1;
let newPayload = '';
for (let i = 0; i < payload.length; i++) {
if (payload[i] === '/' && i > 0 && payload[i - 1] === '<') {
const end = payload.indexOf('>', i);
if (end <= i) {
return payload;
}
const tagEnd = payload.substring(i + 1, end);
if (!tagEnd || tagEnd !== 'c') {
newPayload += payload[i];
continue;
}
const tagStart = names.pop();
if (!tagStart) {
newPayload += payload[i];
} else if (tagStart === tagEnd) {
newPayload += '/' + tagEnd + '>';
i += tagEnd.length + 1;
} else {
if (!tagStart.startsWith('c.')) {
newPayload += payload[i];
continue;
}
i += tagEnd.length + 1;
newPayload += '/' + tagStart + '>';
}
} else {
if (payload[i] === '<') {
nameStart = i + 1;
if (payload[nameStart] != 'c') {
nameStart = -1;
}
} else if (payload[i] === '>') {
if (nameStart > 0) {
names.push(payload.substr(nameStart, i - nameStart));
nameStart = -1;
}
}
newPayload += payload[i];
}
}
return newPayload;
}

/**
* @param {string} value
* @param {string} defaultValue
Expand Down
6 changes: 3 additions & 3 deletions test/text/vtt_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1075,13 +1075,13 @@ describe('VttTextParser', () => {
{
startTime: 80,
endTime: 90,
payload: '<b><c.lime>Parse fail 1</b></c.lime>',
payload: '<b><c.lime>Parse fail 1</b></c>',
nestedCues: [],
},
{
startTime: 90,
endTime: 100,
payload: '<c.lime><b>Parse fail 2</c.lime></b>',
payload: '<c.lime><b>Parse fail 2</c></b>',
nestedCues: [],
},
{
Expand All @@ -1100,7 +1100,7 @@ describe('VttTextParser', () => {
{
startTime: 110,
endTime: 120,
payload: '<c.lime>less or more < > in text</c.lime>',
payload: '<c.lime>less or more < > in text</c>',
nestedCues: [],
},
],
Expand Down
Loading