Bug 1419301 - Make HTML comments removal in prettifyCSS faster; r=tromey draft
authorPatrick Brosset <pbrosset@mozilla.com>
Fri, 01 Dec 2017 16:58:48 +0100
changeset 706260 86eb5612f8264bbe4f765ab5baaea4db8c7a84a3
parent 706205 781485c695e1f07b8782427d556f6570e4a8072f
child 742623 9db9ee19e44bfddbe58a19ae4afbfd5978d0367a
push id91758
push userbmo:pbrosset@mozilla.com
push dateFri, 01 Dec 2017 15:59:25 +0000
reviewerstromey
bugs1419301
milestone59.0a1
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);