Bug 1323713 - fix prettify CSS error when encountering extra closing brace;r=tromey
MozReview-Commit-ID: 5TAxEP5561K
--- 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) {