diff --git a/lib/safe-parser.js b/lib/safe-parser.js index e30519b..c9a471c 100644 --- a/lib/safe-parser.js +++ b/lib/safe-parser.js @@ -35,9 +35,11 @@ class SafeParser extends Parser { this.tokenizer = tokenizer(this.input, { ignoreErrors: true }) } - decl(tokens) { + decl(tokens, customProperty) { if (tokens.length > 1 && tokens.some(i => i[0] === 'word')) { - super.decl(tokens) + this.customProperty = customProperty + super.decl(tokens, customProperty) + this.customProperty = false // Words before colon are already in raws.between, // so remove their copy added by unknownWord() this.spaces = '' @@ -62,6 +64,7 @@ class SafeParser extends Parser { } precheckMissedSemicolon(tokens) { + if (this.customProperty) return let colon = this.colon(tokens) if (colon === false) return diff --git a/test/parse.test.js b/test/parse.test.js index d5fe65f..5d7bbc2 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -5,12 +5,10 @@ let { eachTest, jsonify } = require('postcss-parser-tests') let parse = require('../lib/safe-parse') eachTest((name, css, json) => { - if (name !== 'apply.css' && name !== 'custom-properties.css') { - test('parses ' + name, () => { - let parsed = jsonify(parse(css, { from: name })) - deepStrictEqual(parsed, json) - }) - } + test('parses ' + name, () => { + let parsed = jsonify(parse(css, { from: name })) + deepStrictEqual(parsed, json) + }) }) test('fixes unclosed blocks in safe mode', () => { @@ -155,3 +153,16 @@ test('Custom property at start without modifications', () => { `--example: {"key": "value"}; .class { color: black; }` ) }) + +test('keeps {} blocks with colons in custom property values', () => { + let root = parse('a{--y:{a:b; c:d}; color:red}') + equal(root.toString(), 'a{--y:{a:b; c:d}; color:red}') + equal(root.first.first.prop, '--y') + equal(root.first.first.value, '{a:b; c:d}') + equal(root.first.nodes.length, 2) + equal(parse('a{--y: {a:b}}').toString(), 'a{--y: {a:b}}') +}) + +test('still fixes missed semicolon in regular declarations', () => { + equal(parse('a{color:red b:c}').toString(), 'a{color:red; b:c}') +})