Bug 1338030 - parsecontent: refact the parseContent function input argument. r=rillian
Because we need the parseContent function return "document fragment" or "div" or "pseudo cue div" or "pseudo region div" ...
It's better to use enumerate instead boolean flag.
MozReview-Commit-ID: 5zmwXiYreqS
--- a/dom/media/webvtt/vtt.jsm
+++ b/dom/media/webvtt/vtt.jsm
@@ -300,18 +300,24 @@ const { XPCOMUtils } = require("resource
v: "title",
lang: "lang"
};
var NEEDS_PARENT = {
rt: "ruby"
};
+ const PARSE_CONTENT_MODE = {
+ NORMAL_CUE: "normal_cue",
+ PSUEDO_CUE: "pseudo_cue",
+ DOCUMENT_FRAGMENT: "document_fragment",
+ REGION_CUE: "region_cue",
+ }
// Parse content into a document fragment.
- function parseContent(window, input, bReturnFrag) {
+ function parseContent(window, input, mode) {
function nextToken() {
// Check for end-of-string.
if (!input) {
return null;
}
// Consume 'n' characters from the input.
function consume(result) {
@@ -379,26 +385,30 @@ const { XPCOMUtils } = require("resource
if (f[1]) {
f = f[1].slice(0, 3).padEnd(3, "0");
} else {
f = "000";
}
return hours + ':' + minutes + ':' + seconds + '.' + f;
}
- var isFirefoxSupportPseudo = (/firefox/i.test(window.navigator.userAgent))
- && Services.prefs.getBoolPref("media.webvtt.pseudo.enabled");
var root;
- if (bReturnFrag) {
- root = window.document.createDocumentFragment();
- } else if (isFirefoxSupportPseudo) {
- root = window.document.createElement("div", {pseudo: "::cue"});
- } else {
- root = window.document.createElement("div");
+ switch (mode) {
+ case PARSE_CONTENT_MODE.PSUEDO_CUE:
+ root = window.document.createElement("div", {pseudo: "::cue"});
+ break;
+ case PARSE_CONTENT_MODE.NORMAL_CUE:
+ case PARSE_CONTENT_MODE.REGION_CUE:
+ root = window.document.createElement("div");
+ break;
+ case PARSE_CONTENT_MODE.DOCUMENT_FRAGMENT:
+ root = window.document.createDocumentFragment();
+ break;
}
+
var current = root,
t,
tagStack = [];
while ((t = nextToken()) !== null) {
if (t[0] === '<') {
if (t[1] === "/") {
// If the closing tag matches, move back up to the parent node.
@@ -489,17 +499,21 @@ const { XPCOMUtils } = require("resource
backgroundColor = "rgb(0, 0, 0)";
}
StyleBox.call(this);
this.cue = cue;
// Parse our cue's text into a DOM tree rooted at 'cueDiv'. This div will
// have inline positioning and will function as the cue background box.
- this.cueDiv = parseContent(window, cue.text, false);
+ if (isFirefoxSupportPseudo) {
+ this.cueDiv = parseContent(window, cue.text, PARSE_CONTENT_MODE.PSUEDO_CUE);
+ } else {
+ this.cueDiv = parseContent(window, cue.text, PARSE_CONTENT_MODE.NORMAL_CUE);
+ }
var styles = {
color: color,
backgroundColor: backgroundColor,
display: "inline",
font: styleOptions.font,
whiteSpace: "pre-line",
};
if (isFirefoxSupportPseudo) {
@@ -891,17 +905,17 @@ const { XPCOMUtils } = require("resource
}
};
};
WebVTT.convertCueToDOMTree = function(window, cuetext) {
if (!window) {
return null;
}
- return parseContent(window, cuetext, true);
+ return parseContent(window, cuetext, PARSE_CONTENT_MODE.DOCUMENT_FRAGMENT);
};
var FONT_SIZE_PERCENT = 0.05;
var FONT_STYLE = "sans-serif";
// Runs the processing model over the cues and regions passed to it.
// @param overlay A block level element (usually a div) that the computed cues
// and regions will be placed into.