Skip to content

Commit 396c5f6

Browse files
Sanborn Hillandismena
authored andcommitted
Add new lines in TTML cues with nested <br> tags (#584)
This modifies the fix from #572. Streams that also have <br> tags nested in <span> tags were getting missed by #572.
1 parent 0e42d66 commit 396c5f6

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/media/ttml_text_parser.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,6 @@ shaka.media.TtmlTextParser.getLeafNodes_ = function(element) {
193193
return result;
194194

195195
var childNodes = element.childNodes;
196-
if (element.nodeName == 'p') {
197-
// Replace <br> inside a <p> paragraph with a newline character.
198-
// The <br> node is later on skipped
199-
for (var j = 0; j < childNodes.length; j++) {
200-
if (childNodes[j].nodeName == 'br' && j > 0) {
201-
childNodes[j - 1].textContent += '\n';
202-
}
203-
}
204-
}
205196
for (var i = 0; i < childNodes.length; i++) {
206197
// Currently we don't support styles applicable to span
207198
// elements, so they are ignored
@@ -228,6 +219,25 @@ shaka.media.TtmlTextParser.getLeafNodes_ = function(element) {
228219
};
229220

230221

222+
/**
223+
* Insert \n where <br> tags are found
224+
*
225+
* @param {!Node} element
226+
* @private
227+
*/
228+
shaka.media.TtmlTextParser.addNewLines_ = function(element) {
229+
var childNodes = element.childNodes;
230+
231+
for (var i = 0; i < childNodes.length; i++) {
232+
if (childNodes[i].nodeName == 'br' && i > 0) {
233+
childNodes[i - 1].textContent += '\n';
234+
} else if (childNodes[i].childNodes.length > 0) {
235+
shaka.media.TtmlTextParser.addNewLines_(childNodes[i]);
236+
}
237+
}
238+
};
239+
240+
231241
/**
232242
* Parses an Element into a TextTrackCue or VTTCue.
233243
*
@@ -251,6 +261,8 @@ shaka.media.TtmlTextParser.parseCue_ = function(
251261
cueElement.textContent == '')
252262
return null;
253263

264+
shaka.media.TtmlTextParser.addNewLines_(cueElement);
265+
254266
// Get time
255267
var start = shaka.media.TtmlTextParser.parseTime_(
256268
cueElement.getAttribute('begin'), rateInfo);

test/media/ttml_text_parser_unit.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,19 @@ describe('TtmlTextParser', function() {
386386
'</tt>');
387387
});
388388

389-
it('inserts a newline on br in a p block', function() {
389+
it('inserts newline characters into <br> tags', function() {
390390
verifyHelper(
391391
[
392392
{start: 62.05, end: 3723.2, text: 'Line1\nLine2'}
393393
],
394394
'<tt><body><p begin="01:02.05" ' +
395395
'end="01:02:03.200">Line1<br/>Line2</p></body></tt>');
396+
verifyHelper(
397+
[
398+
{start: 62.05, end: 3723.2, text: 'Line1\nLine2'}
399+
],
400+
'<tt><body><p begin="01:02.05" ' +
401+
'end="01:02:03.200"><span>Line1<br/>Line2</span></p></body></tt>');
396402
});
397403

398404
it('parses cue alignment from textAlign attribute', function() {

0 commit comments

Comments
 (0)