@@ -113,15 +113,15 @@ shaka.text.TtmlTextParser = class {
113113 tt . getElementsByTagName ( 'body' ) [ 0 ] ) ;
114114
115115 for ( let i = 0 ; i < textNodes . length ; i ++ ) {
116- let cue = TtmlTextParser . parseCue_ ( textNodes [ i ] ,
117- time . periodStart ,
118- rateInfo ,
119- metadataElements ,
120- styles ,
121- regionElements ,
122- cueRegions ,
123- whitespaceTrim ,
124- false ) ;
116+ const cue = TtmlTextParser . parseCue_ ( textNodes [ i ] ,
117+ time . periodStart ,
118+ rateInfo ,
119+ metadataElements ,
120+ styles ,
121+ regionElements ,
122+ cueRegions ,
123+ whitespaceTrim ,
124+ false ) ;
125125 if ( cue ) {
126126 ret . push ( cue ) ;
127127 }
@@ -155,11 +155,11 @@ shaka.text.TtmlTextParser = class {
155155 ) {
156156 // Get the leaves the child might contain.
157157 goog . asserts . assert ( childNodeItem instanceof Element ,
158- 'Node should be Element!' ) ;
158+ 'Node should be Element!' ) ;
159159 const leafChildren = shaka . text . TtmlTextParser . getLeafNodes_ (
160160 /** @type {Element } */ ( childNodeItem ) ) ;
161161 goog . asserts . assert ( leafChildren . length > 0 ,
162- 'Only a null Element should return no leaves!' ) ;
162+ 'Only a null Element should return no leaves!' ) ;
163163
164164 result = result . concat ( leafChildren ) ;
165165 }
@@ -193,16 +193,35 @@ shaka.text.TtmlTextParser = class {
193193 /**
194194 * Trims and removes multiple spaces from a string
195195 *
196- * @param {string } textContent
196+ * @param {Element } element
197+ * @param {boolean } whitespaceTrim
197198 * @return {string }
198199 * @private
199200 */
200- static sanitizeTextContent ( textContent ) {
201- return textContent
202- // Trim leading and trailing whitespace.
203- . trim ( )
204- // Collapse multiple spaces into one.
205- . replace ( / \s + / g, ' ' ) ;
201+ static sanitizeTextContent ( element , whitespaceTrim ) {
202+ let payload = '' ;
203+
204+ for ( let i = 0 ; i < element . childNodes . length ; i ++ ) {
205+ const childNode = element . childNodes [ i ] ;
206+
207+ if ( childNode . nodeName == 'br' && i > 0 ) {
208+ payload += '\n' ;
209+ } else if ( childNode . childNodes && childNode . childNodes . length > 0 ) {
210+ payload += shaka . text . TtmlTextParser . sanitizeTextContent (
211+ /** @type {!Element } */ ( childNode ) ,
212+ whitespaceTrim
213+ ) ;
214+ } else if ( whitespaceTrim ) {
215+ // Trim leading and trailing whitespace.
216+ let trimmed = childNode . textContent . trim ( ) ;
217+ // Collapse multiple spaces into one.
218+ trimmed = trimmed . replace ( / \s + / g, ' ' ) ;
219+
220+ payload += trimmed ;
221+ }
222+ }
223+
224+ return payload ;
206225 }
207226
208227 /**
@@ -254,31 +273,30 @@ shaka.text.TtmlTextParser = class {
254273 cueElement . getAttribute ( 'dur' ) , rateInfo ) ;
255274 let payload = '' ;
256275 const children = [ ] ;
257-
276+ // If one of the children is text node type
277+ // stop going down and write the payload
258278 if (
259- cueElement . childNodes . length === 1 &&
260- cueElement . childNodes [ 0 ] . nodeType === Node . TEXT_NODE
279+ Array . from ( cueElement . childNodes ) . find (
280+ ( childNode ) => childNode . nodeType === Node . TEXT_NODE
281+ )
261282 ) {
262- if ( whitespaceTrim ) {
263- payload = shaka . text . TtmlTextParser . sanitizeTextContent (
264- cueElement . textContent
265- ) ;
266- } else {
267- payload = cueElement . textContent ;
268- }
283+ payload = shaka . text . TtmlTextParser . sanitizeTextContent (
284+ cueElement ,
285+ whitespaceTrim ,
286+ ) ;
269287 } else {
270288 for ( let i = 0 ; i < cueElement . childNodes . length ; i ++ ) {
271289 const childNode = cueElement . childNodes [ i ] ;
272290 const childCue = shaka . text . TtmlTextParser . parseCue_ (
273- /** @type {!Element } */ ( childNode ) ,
274- offset ,
275- rateInfo ,
276- metadataElements ,
277- styles ,
278- regionElements ,
279- cueRegions ,
280- whitespaceTrim ,
281- true
291+ /** @type {!Element } */ ( childNode ) ,
292+ offset ,
293+ rateInfo ,
294+ metadataElements ,
295+ styles ,
296+ regionElements ,
297+ cueRegions ,
298+ whitespaceTrim ,
299+ true
282300 ) ;
283301
284302 if ( childCue ) {
@@ -309,10 +327,7 @@ shaka.text.TtmlTextParser = class {
309327 cueElement , 'region' , regionElements , /* prefix= */ '' ) [ 0 ] ;
310328 if ( regionElement && regionElement . getAttribute ( 'xml:id' ) ) {
311329 const regionId = regionElement . getAttribute ( 'xml:id' ) ;
312- const regionsWithId = cueRegions . filter ( function ( region ) {
313- return region . id == regionId ;
314- } ) ;
315- cue . region = regionsWithId [ 0 ] ;
330+ cue . region = cueRegions . filter ( ( region ) => region . id == regionId ) [ 0 ] ;
316331 }
317332 const imageElement = shaka . text . TtmlTextParser . getElementFromCollection_ (
318333 cueElement , 'smpte:backgroundImage' , metadataElements , '#' ) [ 0 ] ;
@@ -668,11 +683,12 @@ shaka.text.TtmlTextParser = class {
668683 const XmlUtils = shaka . util . XmlUtils ;
669684 const ttsNs = shaka . text . TtmlTextParser . styleNs_ ;
670685
671- // Styling on elements should take precedence over the main styling attributes
686+ // Styling on elements should take precedence
687+ // over the main styling attributes
672688 const elementAttribute = XmlUtils . getAttributeNS (
673- cueElement ,
674- ttsNs ,
675- attribute
689+ cueElement ,
690+ ttsNs ,
691+ attribute
676692 ) ;
677693
678694 if ( elementAttribute ) {
@@ -687,9 +703,9 @@ shaka.text.TtmlTextParser = class {
687703 // The last value in our styles stack takes the precedence over the others
688704 for ( let i = 0 ; i < inheritedStyles . length ; i ++ ) {
689705 const styleAttributeValue = XmlUtils . getAttributeNS (
690- inheritedStyles [ i ] ,
691- ttsNs ,
692- attribute
706+ inheritedStyles [ i ] ,
707+ ttsNs ,
708+ attribute
693709 ) ;
694710
695711 if ( styleAttributeValue ) {
@@ -728,18 +744,20 @@ shaka.text.TtmlTextParser = class {
728744 // <span style="style1 style2">A cue</span>
729745 const itemNames = attributeValue . split ( ' ' ) ;
730746
731- itemNames . forEach ( ( itemName ) => {
732- for ( let i = 0 ; i < collection . length ; i ++ ) {
733- if ( ( prefixName + collection [ i ] . getAttribute ( 'xml:id' ) ) == itemName ) {
734- items . push ( collection [ i ] ) ;
747+ for ( let i = 0 ; i < itemNames . length ; i ++ ) {
748+ for ( let k = 0 ; k < collection . length ; k ++ ) {
749+ if (
750+ ( prefixName + collection [ k ] . getAttribute ( 'xml:id' ) ) == itemNames [ i ]
751+ ) {
752+ items . push ( collection [ k ] ) ;
735753 break ;
736754 }
737755 }
738- } ) ;
756+ }
739757 }
740758
741759 return items ;
742- } ;
760+ }
743761
744762
745763 /**
0 commit comments