Skip to content

Commit 1e06dc4

Browse files
committed
running tests and renamed children into nestedCues
1 parent d0de86f commit 1e06dc4

File tree

8 files changed

+108
-40
lines changed

8 files changed

+108
-40
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ Tomas Tichy <mr.tichyt@gmail.com>
5151
Toshihiro Suzuki <t.suzuki326@gmail.com>
5252
uStudio Inc. <*@ustudio.com>
5353
Verizon Digital Media Services <*@verizondigitalmedia.com>
54+
Vincent Valot <valot.vince@gmail.com>

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@ Torbjörn Einarsson <torbjorn.einarsson@edgeware.tv>
8383
Toshihiro Suzuki <t.suzuki326@gmail.com>
8484
Vasanth Polipelli <vasanthap@google.com>
8585
Vignesh Venkatasubramanian <vigneshv@google.com>
86+
Vincent Valot <valot.vince@gmail.com>
8687
Yohann Connell <robinconnell@google.com>

externs/shaka/text.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ shaka.extern.Cue = class {
316316
* @exportDoc
317317
*/
318318
this.id;
319+
320+
/**
321+
* Nested cues
322+
* @type {Array.<!shaka.extern.Cue>}
323+
* @exportDoc
324+
*/
325+
this.nestedCues;
326+
327+
/**
328+
* Whether or not the cue only acts as a spacer between two cues
329+
* @type {boolean}
330+
* @exportDoc
331+
*/
332+
this.spacer;
319333
}
320334
};
321335

lib/text/cue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ shaka.text.Cue = class {
187187
* @override
188188
* @exportInterface
189189
*/
190-
this.children = [];
190+
this.nestedCues = [];
191191

192192
/**
193193
* @override

lib/text/simple_text_displayer.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ shaka.text.SimpleTextDisplayer = class {
214214
vttCue.position = shakaCue.position;
215215
}
216216

217-
if (shakaCue.color !== null) {
218-
vttCue.text = `<c.${shakaCue.color}>${shakaCue.payload}</c>`;
219-
}
220-
221217
return vttCue;
222218
}
223219

lib/text/ttml_text_parser.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ shaka.text.TtmlTextParser = class {
178178
* (begin and end attributes)
179179
*
180180
* @param {Element} element
181-
* @return {!Array.<!Element>}
181+
* @return {!NodeList.<!Element>|Array}
182182
* @private
183183
*/
184184
static getLeafCues_(element) {
185185
if (!element) {
186186
return [];
187187
}
188188

189-
return Array.from(element.querySelectorAll('[begin][end]'));
189+
return element.querySelectorAll('[begin][end]');
190190
}
191191

192192

@@ -254,7 +254,7 @@ shaka.text.TtmlTextParser = class {
254254
// If cueElement has neither time attributes, nor
255255
// non-whitespace text, don't try to make a cue out of it.
256256
if (
257-
/^\s*$/.test(cueElement.textContent) ||
257+
/^[\s\n]*$/.test(cueElement.textContent) ||
258258
(
259259
!isChild &&
260260
!cueElement.hasAttribute('begin') &&
@@ -272,12 +272,13 @@ shaka.text.TtmlTextParser = class {
272272
const duration = shaka.text.TtmlTextParser.parseTime_(
273273
cueElement.getAttribute('dur'), rateInfo);
274274
let payload = '';
275-
const children = [];
275+
const nestedCues = [];
276276
// If one of the children is text node type
277277
// stop going down and write the payload
278278
if (
279279
Array.from(cueElement.childNodes).find(
280-
(childNode) => childNode.nodeType === Node.TEXT_NODE
280+
(childNode) => childNode.nodeType === Node.TEXT_NODE &&
281+
/\w+/.test(childNode.textContent)
281282
)
282283
) {
283284
payload = shaka.text.TtmlTextParser.sanitizeTextContent(
@@ -300,7 +301,7 @@ shaka.text.TtmlTextParser = class {
300301
);
301302

302303
if (childCue) {
303-
children.push(childCue);
304+
nestedCues.push(childCue);
304305
}
305306
}
306307
}
@@ -320,7 +321,7 @@ shaka.text.TtmlTextParser = class {
320321
end += offset;
321322

322323
const cue = new shaka.text.Cue(start, end, payload);
323-
cue.children = children;
324+
cue.nestedCues = nestedCues;
324325

325326
// Get other properties if available.
326327
const regionElement = shaka.text.TtmlTextParser.getElementFromCollection_(

test/text/ttml_text_parser_unit.js

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,36 @@ describe('TtmlTextParser', () => {
4343
// When xml:space="default", ignore whitespace outside tags.
4444
verifyHelper(
4545
[
46-
{start: 62.03, end: 62.05, payload: 'A B C'},
46+
{
47+
start: 62.03,
48+
end: 62.05,
49+
nestedCues: [{payload: 'A B C'}],
50+
payload: '',
51+
},
4752
],
4853
'<tt xml:space="default">' + ttBody + '</tt>',
4954
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
5055
// When xml:space="preserve", take them into account.
5156
verifyHelper(
5257
[
53-
{start: 62.03, end: 62.05, payload: '\n A B C \n '},
58+
{
59+
start: 62.03,
60+
end: 62.05,
61+
nestedCues: [{payload: '\n A B C \n '}],
62+
payload: '',
63+
},
5464
],
5565
'<tt xml:space="preserve">' + ttBody + '</tt>',
5666
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
5767
// The default value for xml:space is "default".
5868
verifyHelper(
5969
[
60-
{start: 62.03, end: 62.05, payload: 'A B C'},
70+
{
71+
start: 62.03,
72+
end: 62.05,
73+
nestedCues: [{payload: 'A B C'}],
74+
payload: '',
75+
},
6176
],
6277
'<tt>' + ttBody + '</tt>',
6378
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
@@ -78,6 +93,25 @@ describe('TtmlTextParser', () => {
7893
'<tt><body><p begin="3.45" end="1a"></p></body></tt>');
7994
});
8095

96+
it('supports spans as nestedCues of paragraphs', () => {
97+
verifyHelper(
98+
[
99+
{
100+
start: 62.05,
101+
end: 3723.2,
102+
payload: '',
103+
nestedCues: [
104+
{payload: 'First cue'},
105+
{payload: '', spacer: true},
106+
{payload: 'Second cuee'},
107+
],
108+
},
109+
],
110+
'<tt><body><p begin="01:02.05" end="01:02:03.200">' +
111+
'<span>First cue</span><br /><span>Second cue</span></p></body></tt>',
112+
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
113+
});
114+
81115
it('supports colon formatted time', () => {
82116
verifyHelper(
83117
[
@@ -645,7 +679,12 @@ describe('TtmlTextParser', () => {
645679
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
646680
verifyHelper(
647681
[
648-
{start: 62.05, end: 3723.2, payload: 'Line1\nLine2'},
682+
{
683+
start: 62.05,
684+
end: 3723.2,
685+
nestedCues: [{payload: 'Line1\nLine2'}],
686+
payload: '',
687+
},
649688
],
650689
'<tt><body><p begin="01:02.05" ' +
651690
'end="01:02:03.200"><span>Line1<br/>Line2</span></p></body></tt>',
@@ -801,32 +840,48 @@ describe('TtmlTextParser', () => {
801840
const result = new shaka.text.TtmlTextParser().parseMedia(data, time);
802841
const properties = ['textAlign', 'lineAlign', 'positionAlign', 'size',
803842
'line', 'position', 'direction', 'color', 'writingMode',
804-
'backgroundColor', 'fontWeight', 'fontFamily',
843+
'backgroundColor', 'fontWeight', 'fontFamily', 'spacer',
805844
'wrapLine', 'lineHeight', 'fontStyle', 'fontSize'];
806845
expect(result).toBeTruthy();
807846
expect(result.length).toBe(cues.length);
847+
808848
for (let i = 0; i < cues.length; i++) {
809-
expect(result[i].startTime).toBeCloseTo(cues[i].start, 3);
810-
expect(result[i].endTime).toBeCloseTo(cues[i].end, 3);
811-
expect(result[i].payload).toBe(cues[i].payload);
849+
const cue = cues[i];
850+
const resultCue = result[i];
851+
852+
expect(resultCue.startTime).toBeCloseTo(cue.start, 3);
853+
expect(resultCue.endTime).toBeCloseTo(cue.end, 3);
854+
expect(resultCue.payload).toBe(cue.payload);
812855

813-
if (cues[i].region) {
814-
verifyRegion(cues[i].region, result[i].region);
856+
if (cue.region) {
857+
verifyRegion(cue.region, resultCue.region);
815858
}
816859

817-
const asObj = /** @type {!Object} */ (result[i]);
860+
const asObj = /** @type {!Object} */ (resultCue);
818861
for (const property of properties) {
819-
if (property in cues[i]) {
820-
expect(asObj[property]).toEqual(cues[i][property]);
862+
if (property in cue) {
863+
expect(asObj[property]).toEqual(cue[property]);
821864
}
822865
}
823866

824-
if (cues[i].textDecoration) {
825-
for (let j = 0; j < cues[i].textDecoration.length; j++) {
826-
expect(/** @type {?} */ (result[i]).textDecoration[j])
827-
.toBe(cues[i].textDecoration[j]);
867+
if (cue.textDecoration) {
868+
for (let j = 0; j < cue.textDecoration.length; j++) {
869+
expect(/** @type {?} */ (resultCue).textDecoration[j])
870+
.toBe(cue.textDecoration[j]);
828871
}
829872
}
873+
874+
const resultCueChildren = resultCue.nestedCues;
875+
const expectedCueChildren = cue.nestedCues;
876+
877+
expect(resultCueChildren.length).toBe(expectedCueChildren);
878+
for (let k = 0; k < expectedCueChildren.length; k++) {
879+
const expectedCueChild = expectedCueChildren[k];
880+
const resultCueChild = resultCueChildren[k];
881+
882+
expect(resultCueChild.payload).toBe(expectedCueChild.payload);
883+
expect(resultCueChild.spacer).toBe(expectedCueChild.spacer);
884+
}
830885
}
831886
}
832887

ui/text_displayer.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ shaka.ui.TextDisplayer = class {
186186
}
187187

188188
/**
189-
* Displays children cues of a cue
189+
* Displays a nested cue
190190
*
191191
* @param {Element} container
192192
* @param {!shaka.extern.Cue} cue
193193
* @returns {Element} the created captions container
194194
* @private
195195
*/
196-
displayChildrenCue_(container, cue) {
196+
displayNestedCue_(container, cue) {
197197
const captions = shaka.util.Dom.createHTMLElement('span');
198198

199199
if (cue.spacer) {
@@ -215,19 +215,19 @@ shaka.ui.TextDisplayer = class {
215215
* @private
216216
*/
217217
displayCue_(container, cue) {
218-
if (cue.children.length) {
219-
const childrenContainer = shaka.util.Dom.createHTMLElement('p');
220-
childrenContainer.style.width = '100%';
221-
this.setCaptionStyles_(childrenContainer, cue);
218+
if (cue.nestedCues.length) {
219+
const nestedCuesContainer = shaka.util.Dom.createHTMLElement('p');
220+
nestedCuesContainer.style.width = '100%';
221+
this.setCaptionStyles_(nestedCuesContainer, cue);
222222

223-
for (let i = 0; i < cue.children.length; i++) {
224-
this.displayChildrenCue_(childrenContainer, cue.children[i]);
223+
for (let i = 0; i < cue.nestedCues.length; i++) {
224+
this.displayNestedCue_(nestedCuesContainer, cue.nestedCues[i]);
225225
}
226226

227-
container.appendChild(childrenContainer);
228-
this.currentCuesMap_.set(cue, childrenContainer);
227+
container.appendChild(nestedCuesContainer);
228+
this.currentCuesMap_.set(cue, nestedCuesContainer);
229229
} else {
230-
this.currentCuesMap_.set(cue, this.displayChildrenCue_(container, cue));
230+
this.currentCuesMap_.set(cue, this.displayNestedCue_(container, cue));
231231
}
232232
}
233233

0 commit comments

Comments
 (0)