Bug 1225192 - remove leading whitespace before prettifying css; r=pbrosset
authorTom Tromey <tom@tromey.com>
Mon, 16 Nov 2015 10:27:01 -0700
changeset 309341 87204339949047f3c630cb57f37fb6e818d41f5f
parent 309340 5c706bfd11cc5a08fdb1bb693a0f2cf98958a27e
child 309342 3294bc6d07c44017e6b7a307f6f97052dda42d9c
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspbrosset
bugs1225192
milestone45.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 1225192 - remove leading whitespace before prettifying css; r=pbrosset
devtools/shared/styleinspector/css-logic.js
devtools/shared/tests/unit/test_prettifyCSS.js
--- a/devtools/shared/styleinspector/css-logic.js
+++ b/devtools/shared/styleinspector/css-logic.js
@@ -973,21 +973,23 @@ const TAB_CHARS = "\t";
 CssLogic.prettifyCSS = function(text, ruleCount) {
   if (CssLogic.LINE_SEPARATOR == null) {
     let os = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS;
     CssLogic.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, "");
+  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 text;
+    return originalText;
   }
 
   // We reformat the text using a simple state machine.  The
   // reformatting preserves most of the input text, changing only
   // whitespace.  The rules are:
   //
   // * After a "{" or ";" symbol, ensure there is a newline and
   //   indentation before the next non-comment, non-whitespace token.
@@ -1134,17 +1136,17 @@ CssLogic.prettifyCSS = function(text, ru
     // deal with any trailing comments.
     token = readUntilSignificantToken();
 
     // "Early" bail-out if the text does not appear to be minified.
     // Here we ignore the case where whitespace appears at the end of
     // the text.
     if (pushbackToken && token && token.tokenType === "whitespace" &&
         /\n/g.test(text.substring(token.startOffset, token.endOffset))) {
-      return text;
+      return originalText;
     }
 
     // Finally time for that newline.
     result = result + CssLogic.LINE_SEPARATOR;
 
     // Maybe we hit EOF.
     if (!pushbackToken) {
       break;
--- a/devtools/shared/tests/unit/test_prettifyCSS.js
+++ b/devtools/shared/tests/unit/test_prettifyCSS.js
@@ -2,17 +2,16 @@
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 // Test prettifyCSS.
 
 "use strict";
 
 const {CssLogic} = require("devtools/shared/styleinspector/css-logic");
 
-
 const TESTS = [
   { name: "simple test",
     input: "div { font-family:'Arial Black', Arial, sans-serif; }",
     expected: [
       "div {",
       "\tfont-family:'Arial Black', Arial, sans-serif;",
       "}"
     ]
@@ -24,30 +23,37 @@ const TESTS = [
       "div {",
       "}"
     ]
   },
 
   { name: "minified with trailing newline",
     input: "\nbody{background:white;}div{font-size:4em;color:red}span{color:green;}\n",
     expected: [
-      "",
       "body {",
       "\tbackground:white;",
       "}",
       "div {",
       "\tfont-size:4em;",
       "\tcolor:red",
       "}",
       "span {",
       "\tcolor:green;",
       "}"
     ]
   },
 
+  { name: "leading whitespace",
+    input: "\n    div{color: red;}",
+    expected: [
+      "div {",
+      "\tcolor: red;",
+      "}"
+    ]
+  },
 ];
 
 function run_test() {
   // Note that CssLogic.LINE_SEPARATOR is computed lazily, so we
   // ensure it is set.
   CssLogic.prettifyCSS("");
 
   for (let test of TESTS) {