Skip to content

Commit

Permalink
Check element type in TTML inheritance
Browse files Browse the repository at this point in the history
Also adds support for style attributes on the cue element itself.

Closes #473

Change-Id: I886ec2d0c07c9192f7599b565422692a165b43e5
  • Loading branch information
joeyparrish committed Aug 12, 2016
1 parent 0a9cae8 commit 2ee7eec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
45 changes: 29 additions & 16 deletions lib/media/ttml_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ shaka.media.TtmlTextParser.parseCue_ = function(
// Get other properties if available
var region = shaka.media.TtmlTextParser.getElementFromCollection_(
cueElement, 'region', regions);
shaka.media.TtmlTextParser.addStyle_(cue, region, styles);
shaka.media.TtmlTextParser.addStyle_(cue, cueElement, region, styles);
} else {
cue = new TextTrackCue(start, end, payload);
}
Expand All @@ -241,20 +241,23 @@ shaka.media.TtmlTextParser.parseCue_ = function(
* Adds applicable style properties to a cue.
*
* @param {!VTTCue} cue
* @param {!Element} cueElement
* @param {Element} region
* @param {!Array.<!Element>} styles
* @private
*/
shaka.media.TtmlTextParser.addStyle_ = function(cue, region, styles) {
shaka.media.TtmlTextParser.addStyle_ = function(
cue, cueElement, region, styles) {
var TtmlTextParser = shaka.media.TtmlTextParser;
var results = null;

var align = TtmlTextParser.getStyleAttribute_(
region, styles, 'tts:textAlign');
cueElement, region, styles, 'tts:textAlign');
if (align)
cue.lineAlign = align;

var extent = TtmlTextParser.getStyleAttribute_(region, styles, 'tts:extent');
var extent = TtmlTextParser.getStyleAttribute_(
cueElement, region, styles, 'tts:extent');
if (extent) {
results = TtmlTextParser.percentValues_.exec(extent);
if (results != null) {
Expand All @@ -265,7 +268,7 @@ shaka.media.TtmlTextParser.addStyle_ = function(cue, region, styles) {
}

var writingMode = TtmlTextParser.getStyleAttribute_(
region, styles, 'tts:writingMode');
cueElement, region, styles, 'tts:writingMode');
var isVerticalText = true;
if (writingMode == 'tb' || writingMode == 'tblr')
cue.vertical = 'lr';
Expand All @@ -274,7 +277,8 @@ shaka.media.TtmlTextParser.addStyle_ = function(cue, region, styles) {
else
isVerticalText = false;

var origin = TtmlTextParser.getStyleAttribute_(region, styles, 'tts:origin');
var origin = TtmlTextParser.getStyleAttribute_(
cueElement, region, styles, 'tts:origin');
if (origin) {
results = TtmlTextParser.percentValues_.exec(origin);
if (results != null) {
Expand All @@ -294,29 +298,32 @@ shaka.media.TtmlTextParser.addStyle_ = function(cue, region, styles) {


/**
* Finds a specified attribute in ttml and returns its value if the
* attribute was found.
* Finds a specified attribute on either the original cue element or its
* associated region and returns the value if the attribute was found.
*
* @param {!Element} cueElement
* @param {Element} region
* @param {!Array.<!Element>} styles
* @param {string} attribute
* @return {?string}
* @private
*/
shaka.media.TtmlTextParser.getStyleAttribute_ = function(
region, styles, attribute) {
cueElement, region, styles, attribute) {

// an attribute can be specified on region level or in a styling block
// associated with the region
// An attribute can be specified on region level or in a styling block
// associated with the region or original element.
var regionChildren = shaka.media.TtmlTextParser.getLeafNodes_(region);
for (var i = 0; i < regionChildren.length; i++) {
var attr = regionChildren[i].getAttribute(attribute);
if (attr)
return attr;
}

var style = shaka.media.TtmlTextParser.getElementFromCollection_(
region, 'style', styles);
var getElementFromCollection_ =
shaka.media.TtmlTextParser.getElementFromCollection_;
var style = getElementFromCollection_(region, 'style', styles) ||
getElementFromCollection_(cueElement, 'style', styles);
if (style)
return style.getAttribute(attribute);
return null;
Expand Down Expand Up @@ -370,10 +377,16 @@ shaka.media.TtmlTextParser.getInheritedAttribute_ = function(
if (ret) {
break;
}
// TODO: Temporary cast, Node must be handled explicitly.
element = /** @type {!Element} */(element.parentNode);
}

// Element.parentNode can lead to XMLDocument, which is not an Element and
// has no getAttribute().
var parentNode = element.parentNode;
if (parentNode instanceof Element) {
element = parentNode;
} else {
break;
}
}
return ret;
};

Expand Down
20 changes: 19 additions & 1 deletion test/media/ttml_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('TtmlTextParser', function() {
'</tt>');
});

it('parses alignment from <style> block', function() {
it('parses alignment from <style> block with id on region', function() {
verifyHelper(
[
{start: 62.05, end: 3723.2, text: 'Test', lineAlign: 'end'}
Expand All @@ -183,6 +183,24 @@ describe('TtmlTextParser', function() {
'</tt>');
});

it('parses alignment from <style> block with id on p', function() {
verifyHelper(
[
{start: 62.05, end: 3723.2, text: 'Test', lineAlign: 'end'}
],
'<tt xmlns:tts="ttml#styling">' +
'<styling>' +
'<style xml:id="s1" tts:textAlign="end"/>' +
'</styling>' +
'<layout xmlns:tts="ttml#styling">' +
'<region xml:id="subtitleArea" />' +
'</layout>' +
'<body region="subtitleArea">' +
'<p begin="01:02.05" end="01:02:03.200" style="s1">Test</p>' +
'</body>' +
'</tt>');
});

it('supports size setting', function() {
verifyHelper(
[
Expand Down

0 comments on commit 2ee7eec

Please sign in to comment.