-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new lines in TTML cues with nested <br> tags #584
Add new lines in TTML cues with nested <br> tags #584
Conversation
@@ -217,6 +208,25 @@ shaka.media.TtmlTextParser.getLeafNodes_ = function(element) { | |||
|
|||
|
|||
/** | |||
* Insert \n where <br> tags are found |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rephrase to something like "Replaces
tags with /m characters"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking it's not actually replacing tags. How about "Insert '\n' characters into
tags"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current comment looks good to me. Seems accurate.
@@ -217,6 +208,25 @@ shaka.media.TtmlTextParser.getLeafNodes_ = function(element) { | |||
|
|||
|
|||
/** | |||
* Insert \n where <br> tags are found | |||
* | |||
* @param {!Node} element |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{!Element} for consistency (instead of Node)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had that but it failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, thanks, I appreciate the explanation. Will update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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'; | ||
} else if (childNodes[i].childNodes.length !== 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any advantages to "!==" operator over "childNodes.length > 0" here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I can see. Happy to change it if people prefer it that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, please change.
@@ -395,6 +395,15 @@ describe('TtmlTextParser', function() { | |||
'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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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