Bug 1307710 - part3: fix ProcessingInstruction node for testcase. r=rillian draft
authorbechen <bechen@mozilla.com>
Fri, 03 Mar 2017 11:34:33 +0800
changeset 492717 b3389f2ea24e0b4a366ef3c78be15f3d96edadc5
parent 490737 041252e2153fedb856e1c37cf6c1c1e75bbb09a7
child 492718 56fee1d0bb19712a16dd9dfca534cd297e2a0b6d
push id47615
push userbechen@mozilla.com
push dateFri, 03 Mar 2017 03:37:41 +0000
reviewersrillian
bugs1307710
milestone54.0a1
Bug 1307710 - part3: fix ProcessingInstruction node for testcase. r=rillian MozReview-Commit-ID: 9Wc98cOVjHD
dom/media/webvtt/vtt.jsm
--- a/dom/media/webvtt/vtt.jsm
+++ b/dom/media/webvtt/vtt.jsm
@@ -348,16 +348,41 @@ Cu.import('resource://gre/modules/Servic
       element.localName = tagName;
       var name = TAG_ANNOTATION[type];
       if (name) {
         element[name] = annotation ? annotation.trim() : "";
       }
       return element;
     }
 
+    // https://w3c.github.io/webvtt/#webvtt-timestamp-object
+    // Return hhhhh:mm:ss.fff
+    function normalizedTimeStamp(secondsWithFrag) {
+      var totalsec = parseInt(secondsWithFrag, 10);
+      var hours = Math.floor(totalsec / 3600);
+      var minutes = Math.floor(totalsec % 3600 / 60);
+      var seconds = Math.floor(totalsec % 60);
+      if (hours < 10) {
+        hours = "0" + hours;
+      }
+      if (minutes < 10) {
+        minutes = "0" + minutes;
+      }
+      if (seconds < 10) {
+        seconds = "0" + seconds;
+      }
+      var f = secondsWithFrag.toString().split(".");
+      if (f[1]) {
+        f = f[1].slice(0, 3).padEnd(3, "0");
+      } else {
+        f = "000";
+      }
+      return hours + ':' + minutes + ':' + seconds + '.' + f;
+    }
+
     var root;
     if (bReturnFrag) {
       root = window.document.createDocumentFragment();
     } else {
       root = window.document.createElement("div");
     }
     var current = root,
         t,
@@ -374,17 +399,17 @@ Cu.import('resource://gre/modules/Servic
           }
           // Otherwise just ignore the end tag.
           continue;
         }
         var ts = collectTimeStamp(t.substr(1, t.length - 2));
         var node;
         if (ts) {
           // Timestamps are lead nodes as well.
-          node = window.document.createProcessingInstruction("timestamp", ts);
+          node = window.document.createProcessingInstruction("timestamp", normalizedTimeStamp(ts));
           current.appendChild(node);
           continue;
         }
         var m = t.match(/^<([^.\s/0-9>]+)(\.[^\s\\>]+)?([^>\\]+)?(\\?)>?$/);
         // If we can't parse the tag, skip to the next tag.
         if (!m) {
           continue;
         }