diff --git a/js/lib/beautify-css.js b/js/lib/beautify-css.js index 210099123..761e5dee8 100644 --- a/js/lib/beautify-css.js +++ b/js/lib/beautify-css.js @@ -315,7 +315,12 @@ } } else if (ch === ']') { output.push(ch); - } else if (ch === '[' || ch === '=') { // no whitespace before or after + } else if (ch === '[') { + if (isAfterSpace) { + print.singleSpace(); + } + output.push(ch); + } else if (ch === '=') { // no whitespace before or after eatWhitespace(); output.push(ch); } else { diff --git a/js/test/beautify-tests.js b/js/test/beautify-tests.js index b6e11b16a..ac3681589 100755 --- a/js/test/beautify-tests.js +++ b/js/test/beautify-tests.js @@ -2001,6 +2001,9 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify, btc("a:not(\"foobar\\\";{}omg\"){\ncontent: 'example\\';{} text';\ncontent: \"example\\\";{} text\";}", "a:not(\"foobar\\\";{}omg\") {\n content: 'example\\';{} text';\n content: \"example\\\";{} text\";\n}\n"); + btc('html.js [data-custom="123"] {\n opacity: 1.00;\n}\n'); // may not eat the space before "[" + btc('html.js *[data-custom="123"] {\n opacity: 1.00;\n}\n'); + return sanitytest; } diff --git a/python/cssbeautifier/__init__.py b/python/cssbeautifier/__init__.py index eee078881..cf978a4ba 100644 --- a/python/cssbeautifier/__init__.py +++ b/python/cssbeautifier/__init__.py @@ -285,7 +285,11 @@ def beautify(self): printer.singleSpace() elif self.ch == ']': printer.push(self.ch) - elif self.ch == '[' or self.ch == '=': + elif self.ch == '[': + if isAfterSpace: + printer.singleSpace() + printer.push(self.ch) + elif self.ch == '=': # no whitespace before or after self.eatWhitespace() printer.push(self.ch) diff --git a/python/cssbeautifier/tests/test.py b/python/cssbeautifier/tests/test.py index 6e8c8b480..a270ec4f0 100644 --- a/python/cssbeautifier/tests/test.py +++ b/python/cssbeautifier/tests/test.py @@ -23,6 +23,10 @@ def testBasics(self): t("#bla, #foo{color:red}", "#bla,\n#foo {\n\tcolor: red\n}\n") t("@media print {.tab{}}", "@media print {\n\t.tab {}\n}\n") + # may not eat the space before "[" + t('html.js [data-custom="123"] {\n\topacity: 1.00;\n}\n'); + t('html.js *[data-custom="123"] {\n\topacity: 1.00;\n}\n'); + def testComments(self): self.resetOptions()