Bug 1419301 - Make HTML comments removal in prettifyCSS faster; r=tromey
MozReview-Commit-ID: LG8GKccRlXr
--- 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);