Bug 1323713 - fix prettify CSS error when encountering extra closing brace;r=tromey draft
authorJulian Descottes <jdescottes@mozilla.com>
Tue, 10 Jan 2017 22:19:47 +0100
changeset 458689 41fdc014c018d2f2611e698a2292d15978e49df8
parent 458610 e68cbc3b5b3d3fba4fe3e17e234713020f44e4a0
child 541708 c571258309fa28deeec91729d4c4295fe5864bc9
push id41018
push userjdescottes@mozilla.com
push dateTue, 10 Jan 2017 21:20:23 +0000
reviewerstromey
bugs1323713
milestone53.0a1
Bug 1323713 - fix prettify CSS error when encountering extra closing brace;r=tromey MozReview-Commit-ID: 5TAxEP5561K
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
@@ -279,17 +279,21 @@ function prettifyCSS(text, ruleCount) {
         result = result + indent + text.substring(startIndex, endIndex);
         if (isCloseBrace) {
           result += prettifyCSS.LINE_SEPARATOR;
         }
       }
     }
 
     if (isCloseBrace) {
-      indent = TAB_CHARS.repeat(--indentLevel);
+      // Even if the stylesheet contains extra closing braces, the indent level should
+      // remain > 0.
+      indentLevel = Math.max(0, indentLevel - 1);
+
+      indent = TAB_CHARS.repeat(indentLevel);
       result = result + indent + "}";
     }
 
     if (!token) {
       break;
     }
 
     if (token.tokenType === "symbol" && token.text === "{") {
--- a/devtools/shared/tests/unit/test_prettifyCSS.js
+++ b/devtools/shared/tests/unit/test_prettifyCSS.js
@@ -44,16 +44,29 @@ const TESTS = [
   { name: "leading whitespace",
     input: "\n    div{color: red;}",
     expected: [
       "div {",
       "\tcolor: red;",
       "}"
     ]
   },
+
+  { name: "CSS with extra closing brace",
+    input: "body{margin:0}} div{color:red}",
+    expected: [
+      "body {",
+      "\tmargin:0",
+      "}",
+      "}",
+      "div {",
+      "\tcolor:red",
+      "}",
+    ]
+  },
 ];
 
 function run_test() {
   // Note that prettifyCSS.LINE_SEPARATOR is computed lazily, so we
   // ensure it is set.
   prettifyCSS("");
 
   for (let test of TESTS) {