Bug 1338030 - parsecontent: refact the parseContent function input argument. r=rillian draft
authorbechen@mozilla.com <bechen@mozilla.com>
Fri, 10 Nov 2017 11:40:08 +0800
changeset 695984 57706a7fbf36b2c65fa93362dbfbf9a30d2d7418
parent 695983 8f001f1256ffc930da657ff968265ed85013c6d7
child 695985 7781f716438c6bf7e2b4550f0b77f245f674fd15
push id88606
push userbmo:bechen@mozilla.com
push dateFri, 10 Nov 2017 03:41:56 +0000
reviewersrillian
bugs1338030
milestone58.0a1
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
dom/media/webvtt/vtt.jsm
--- 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.