Bug 1419301 - Make HTML comments removal in prettifyCSS faster; r=tromey
authorPatrick Brosset <pbrosset@mozilla.com>
Fri, 01 Dec 2017 16:58:48 +0100
changeset 394617 489e3c08c4574e78e26d6679c405c1e3fa8343dc
parent 394616 d0da33407d8f1651fe4d0fce9462a4179805ff03
child 394618 01e391e9a2d21a89c641de8c794f704a7de255b9
push id56395
push userpbrosset@mozilla.com
push dateFri, 01 Dec 2017 21:53:14 +0000
treeherderautoland@489e3c08c457 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstromey
bugs1419301
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1419301 - Make HTML comments removal in prettifyCSS faster; r=tromey MozReview-Commit-ID: LG8GKccRlXr
devtools/shared/inspector/css-logic.js
devtools/shared/tests/unit/test_prettifyCSS.js
--- a/devtools/shared/inspector/css-logic.js
+++ b/devtools/shared/inspector/css-logic.js
@@ -153,18 +153,23 @@ const SPACE_CHARS = " ";
  * @return string Prettified CSS source
  */
 function prettifyCSS(text, ruleCount) {
   if (prettifyCSS.LINE_SEPARATOR == null) {
     let os = Services.appinfo.OS;
     prettifyCSS.LINE_SEPARATOR = (os === "WINNT" ? "\r\n" : "\n");
   }
 
-  // remove initial and terminating HTML comments and surrounding whitespace
-  text = text.replace(/(?:^\s*<!--[\r\n]*)|(?:\s*-->\s*$)/g, "");
+  // Stylesheets may start and end with HTML comment tags (possibly with whitespaces
+  // before and after). Remove those first. Don't do anything there aren't any.
+  let trimmed = text.trim();
+  if (trimmed.startsWith("<!--")) {
+    text = trimmed.replace(/^<!--/, "").replace(/-->$/, "").trim();
+  }
+
   let originalText = text;
   text = text.trim();
 
   // don't attempt to prettify if there's more than one line per rule.
   let lineCount = text.split("\n").length - 1;
   if (ruleCount !== null && lineCount >= ruleCount) {
     return originalText;
   }
--- a/devtools/shared/tests/unit/test_prettifyCSS.js
+++ b/devtools/shared/tests/unit/test_prettifyCSS.js
@@ -117,16 +117,34 @@ const TESTS_SPACE_INDENT = [
       " margin:0",
       "}",
       "}",
       "div {",
       " color:red",
       "}",
     ]
   },
+
+  { name: "HTML comments with some whitespace padding",
+    input: "  \n\n\t  <!--\n\n\t body {color:red}  \n\n-->   \t\n",
+    expected: [
+      "body {",
+      " color:red",
+      "}"
+    ]
+  },
+
+  { name: "HTML comments without whitespace padding",
+    input: "<!--body {color:red}-->",
+    expected: [
+      "body {",
+      " color:red",
+      "}"
+    ]
+  },
 ];
 
 function run_test() {
   // Note that prettifyCSS.LINE_SEPARATOR is computed lazily, so we
   // ensure it is set.
   prettifyCSS("");
 
   Services.prefs.setBoolPref(EXPAND_TAB, true);