Skip to content
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

Merged

Conversation

sanbornhilland
Copy link
Contributor

This modifies the fix from #572. We have streams that also have <br> tags nested in <span> tags that were getting missed by #572

@@ -217,6 +208,25 @@ shaka.media.TtmlTextParser.getLeafNodes_ = function(element) {


/**
* Insert \n where <br> tags are found
Copy link
Contributor

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"

Copy link
Contributor Author

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"

Copy link
Member

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
Copy link
Contributor

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)

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Member

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?

Copy link
Contributor Author

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]);

Copy link
Member

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]));

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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() {
Copy link
Contributor

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).

@joeyparrish joeyparrish added the status: waiting on response Waiting on a response from the reporter(s) of the issue label Nov 29, 2016
@joeyparrish joeyparrish removed the status: waiting on response Waiting on a response from the reporter(s) of the issue label Dec 6, 2016
@joeyparrish
Copy link
Member

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.

@shaka-bot
Copy link
Collaborator

Testing in progress...

@shaka-bot
Copy link
Collaborator

Failure:

Chrome_55_0_2883_(Linux_0_0_0).StreamingEngine StreamingEngine VOD plays FAILED
  Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  
Chrome_55_0_2883_(Linux_0_0_0).StreamingEngine StreamingEngine VOD plays at high playback rates FAILED
  Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  
Chrome_55_0_2883_(Linux_0_0_0).StreamingEngine StreamingEngine VOD can handle buffered seeks FAILED
  Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  
Chrome_55_0_2883_(Linux_0_0_0).StreamingEngine StreamingEngine VOD can handle unbuffered seeks FAILED
  Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  
Chrome_55_0_2883_(Linux_0_0_0).StreamingEngine StreamingEngine Live plays through Period transition FAILED
  Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  
Chrome_55_0_2883_(Linux_0_0_0).StreamingEngine StreamingEngine Live can handle seeks ahead of availability window FAILED
  Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  
Chrome_55_0_2883_(Linux_0_0_0).StreamingEngine StreamingEngine Live can handle seeks behind availability window FAILED
  Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  

Failed 7 tests, passed 902, skipped 37

@joeyparrish
Copy link
Member

I think those errors have already been fixed in master (#623). I will cherry-pick this and test it rebased on top of master.

@shaka-bot
Copy link
Collaborator

Whoops! Trying again with a rebase...

@shaka-bot
Copy link
Collaborator

Sorry about that. Looks good! All tests passed after rebasing.

@joeyparrish joeyparrish merged commit 71ce370 into shaka-project:master Dec 6, 2016
ismena pushed a commit that referenced this pull request Dec 15, 2016
This modifies the fix from #572.
Streams that also have <br> tags nested in <span> tags were getting missed by #572.
@github-actions github-actions bot added the status: archived Archived and locked; will not be updated label Jul 25, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: archived Archived and locked; will not be updated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants