Skip to content

Commit

Permalink
fix(TTML): Fix trim surrounding spaces with xml:space="default" (#6395)
Browse files Browse the repository at this point in the history
Fixes #4974
  • Loading branch information
avelad authored Apr 4, 2024
1 parent 286126e commit bcedec3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
22 changes: 10 additions & 12 deletions lib/text/ttml_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ shaka.text.TtmlTextParser = class {
shaka.util.Error.Code.INVALID_XML,
'Invalid xml:space value: ' + spaceStyle);
}
const whitespaceTrim = spaceStyle == 'default';
const collapseMultipleSpaces = spaceStyle == 'default';

const rateInfo = new TtmlTextParser.RateInfo_(
frameRate, subFrameRate, frameRateMultiplier, tickRate);
Expand Down Expand Up @@ -146,7 +146,7 @@ shaka.text.TtmlTextParser = class {

const cue = TtmlTextParser.parseCue_(
body, time, rateInfo, metadataElements, styles,
regionElements, cueRegions, whitespaceTrim,
regionElements, cueRegions, collapseMultipleSpaces,
cellResolutionInfo, /* parentCueElement= */ null,
/* isContent= */ false, uri);
if (cue) {
Expand All @@ -172,7 +172,7 @@ shaka.text.TtmlTextParser = class {
* @param {!Array.<!shaka.extern.xml.Node>} styles
* @param {!Array.<!shaka.extern.xml.Node>} regionElements
* @param {!Array.<!shaka.text.CueRegion>} cueRegions
* @param {boolean} whitespaceTrim
* @param {boolean} collapseMultipleSpaces
* @param {?{columns: number, rows: number}} cellResolution
* @param {?shaka.extern.xml.Node} parentCueElement
* @param {boolean} isContent
Expand All @@ -182,8 +182,8 @@ shaka.text.TtmlTextParser = class {
*/
static parseCue_(
cueNode, timeContext, rateInfo, metadataElements, styles, regionElements,
cueRegions, whitespaceTrim, cellResolution, parentCueElement, isContent,
uri) {
cueRegions, collapseMultipleSpaces, cellResolution, parentCueElement,
isContent, uri) {
const TXml = shaka.util.TXml;
const StringUtils = shaka.util.StringUtils;
/** @type {shaka.extern.xml.Node} */
Expand Down Expand Up @@ -246,9 +246,9 @@ shaka.text.TtmlTextParser = class {
const parentIsContent = isContent;

const spaceStyle = cueElement.attributes['xml:space'] ||
(whitespaceTrim ? 'default' : 'preserve');
(collapseMultipleSpaces ? 'default' : 'preserve');

const localWhitespaceTrim = spaceStyle == 'default';
const localCollapseMultipleSpaces = spaceStyle == 'default';

// Parse any nested cues first.
const isLeafNode = cueElement.children.every(TXml.isText);
Expand All @@ -265,7 +265,7 @@ shaka.text.TtmlTextParser = class {
styles,
regionElements,
cueRegions,
localWhitespaceTrim,
localCollapseMultipleSpaces,
cellResolution,
cueElement,
isContent,
Expand Down Expand Up @@ -300,7 +300,7 @@ shaka.text.TtmlTextParser = class {
// as some information could be held by its attributes.
// <p /> won't, as it would not be displayed.
return null;
} else if (localWhitespaceTrim) {
} else if (localCollapseMultipleSpaces) {
// Disregards empty anonymous spans when (local) trim is true.
return null;
}
Expand Down Expand Up @@ -361,9 +361,7 @@ shaka.text.TtmlTextParser = class {
// If the childNodes are all text, this is a leaf node. Get the payload.
payload = StringUtils.htmlUnescape(
shaka.util.TXml.getTextContents(cueElement) || '');
if (localWhitespaceTrim) {
// Trim leading and trailing whitespace.
payload = payload.trim();
if (localCollapseMultipleSpaces) {
// Collapse multiple spaces into one.
payload = payload.replace(/\s+/g, ' ');
}
Expand Down
8 changes: 4 additions & 4 deletions test/text/ttml_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('TtmlTextParser', () => {
startTime: 62.03,
endTime: 62.05,
nestedCues: [{
payload: 'A B C',
payload: ' A B C ',
startTime: 62.03,
endTime: 62.05,
}],
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('TtmlTextParser', () => {
startTime: 62.03,
endTime: 62.05,
nestedCues: [{
payload: 'A B C',
payload: ' A B C ',
startTime: 62.03,
endTime: 62.05,
}],
Expand Down Expand Up @@ -2055,7 +2055,7 @@ describe('TtmlTextParser', () => {
{
startTime: 0,
endTime: 60,
payload: 'A',
payload: ' A',
fontSize: '16px',
},
{
Expand All @@ -2067,7 +2067,7 @@ describe('TtmlTextParser', () => {
{
startTime: 0,
endTime: 60,
payload: 'B',
payload: 'B ',
fontSize: '16px',
},
],
Expand Down

0 comments on commit bcedec3

Please sign in to comment.