Add new lines in TTML cues with nested <br> tags#584
Add new lines in TTML cues with nested <br> tags#584joeyparrish merged 3 commits intoshaka-project:masterfrom
Conversation
| }; | ||
|
|
||
|
|
||
| /** |
There was a problem hiding this comment.
Let's rephrase to something like "Replaces
tags with /m characters"
There was a problem hiding this comment.
Strictly speaking it's not actually replacing tags. How about "Insert '\n' characters into
tags"
There was a problem hiding this comment.
Current comment looks good to me. Seems accurate.
|
|
||
| /** | ||
| * Insert \n where <br> tags are found | ||
| * |
There was a problem hiding this comment.
{!Element} for consistency (instead of Node)
There was a problem hiding this comment.
I had that but it failed.
There was a problem hiding this comment.
The build failed? Ok, please leave a TODO above the param declaration (line 212) that having param Element here gives build problems and I'll look at it when we merge the PR.
There was a problem hiding this comment.
At the call site, the type of cueElement is !Element, so using the same here should work. What was the error?
There was a problem hiding this comment.
Just re ran the build and looked more closely. The issue is that because this function calls itself to drill down and find nested tags, the nested nodes are not guaranteed to be elements. Initial call is always !Element but subsequent calls are not guaranteed.
/Users/sanbornh/Documents/shaka-player/lib/media/ttml_text_parser.js:223: ERROR - actual parameter 1 of shaka.media.TtmlTextParser.addNewLines_ does not match formal parameter
found : Node
required: Element
shaka.media.TtmlTextParser.addNewLines_(childNodes[i]);
There was a problem hiding this comment.
We solved this elsewhere in with an assertion and a cast:
if (childNodes[i].nodeType == Node.ELEMENT_NODE && ...) {
goog.asserts.assert(childNodes[i] instanceof Element,
'Node should be Element!');
var leafChildren = shaka.media.TtmlTextParser.getLeafNodes_(
/** @type {Element} */(childNodes[i]));
There was a problem hiding this comment.
I'm not familiar with the problem we are attempting to solve here. Can you describe why it is important to be using elements instead of nodes?
There was a problem hiding this comment.
There's no runtime difference. It's just a matter of describing the types accurately and asserting to the compiler that it is that type.
Element is a subclass of Node, but childNodes contains all Nodes, even those that are not Elements. If we only want to walk through the Elements, we could use children, but some older browsers won't support that. Instead we use childNodes, and we have to both filter out the Elements by nodeType, and we have to tell the compiler that this is a safe conversion.
There was a problem hiding this comment.
Okay, thanks, I appreciate the explanation. Will update.
There was a problem hiding this comment.
No problem. I'm sorry that the compiler is a bit of a burden in this instance. It's not perfect by any means, but I think on balance, it has helped to catch and prevent a lot of bugs.
|
|
||
| for (var i = 0; i < childNodes.length; i++) { | ||
| if (childNodes[i].nodeName == 'br' && i > 0) { | ||
| childNodes[i - 1].textContent += '\n'; |
There was a problem hiding this comment.
Any advantages to "!==" operator over "childNodes.length > 0" here?
There was a problem hiding this comment.
Not that I can see. Happy to change it if people prefer it that way.
test/media/ttml_text_parser_unit.js
Outdated
| 'end="01:02:03.200">Line1<br/>Line2</p></body></tt>'); | ||
| }); | ||
|
|
||
| it('inserts a newline on br in elements nested in cue', function() { |
There was a problem hiding this comment.
Let's merge this test and a previous one into a single "it('replaces
tags with newline characters')" with 2 calls to verifyHelper in it.
(Like in 'disregards empty divs and ps' above which has 3 calls).
|
The only part of this that I still wanted changed was the use of Element vs Node, but it's not that important. I'm going to run this PR through the build bot and merge it if everything checks out. |
|
Testing in progress... |
|
Failure: |
|
I think those errors have already been fixed in |
|
Whoops! Trying again with a rebase... |
|
Sorry about that. Looks good! All tests passed after rebasing. |
This modifies the fix from #572. We have streams that also have
<br>tags nested in<span>tags that were getting missed by #572