Skip to content

Commit

Permalink
feat(WebVTT): Remove un-needed VTT payload normalisation (#6145)
Browse files Browse the repository at this point in the history
The tXml parser gracefully handles this now, so it is no longer needed.
  • Loading branch information
dave-nicholas authored Jan 23, 2024
1 parent df8dbb9 commit ee600c4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 64 deletions.
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

0 comments on commit ee600c4

Please sign in to comment.