Skip to content

Commit 721c42d

Browse files
authored
Fix nested json parsing errors (#40)
1 parent 9201ba4 commit 721c42d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/safe-parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SafeParser extends Parser {
6565
for (nextStart = colon - 1; nextStart >= 0; nextStart--) {
6666
if (tokens[nextStart][0] === 'word') break
6767
}
68-
if (nextStart === 0) return
68+
if (nextStart === 0 || nextStart < 0) return
6969

7070
for (prevEnd = nextStart - 1; prevEnd >= 0; prevEnd--) {
7171
if (tokens[prevEnd][0] !== 'space') {

test/parse.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,38 @@ test('fixes colon instead of semicolon', () => {
8888
equal(root.toString(), 'a { one: 1: } b { one: 1 : }')
8989
})
9090

91+
test('fixes {} error', () => {
92+
let root = parse(`:root { --example-var: { "Version": { "Build": "10.30.7.350828.20230224122352", "Source": "10.30.7.350828.1", "Required": "10.30.7.307232"; }}}; @font-face { font-family: 'Calibri'; }`)
93+
equal(root.toString(), `:root { --example-var: { "Version": { "Build": "10.30.7.350828.20230224122352", "Source": "10.30.7.350828.1", "Required": "10.30.7.307232"; }}}; @font-face { font-family: 'Calibri'; }`)
94+
})
95+
96+
test('Rule at the start of tokens', () => {
97+
let root = parse(`.start { font-size: 16px; }`);
98+
equal(root.toString(), `.start { font-size: 16px; }`);
99+
});
100+
101+
test('Complex nested structures with JSON-like properties', () => {
102+
let root = parse(`:root { --complex: {"nested": {"key": "value"}, "array": [1, {"sub": "value"}]}; } @font-face { font-family: 'Calibri'; }`);
103+
equal(root.toString(), `:root { --complex: {"nested": {"key": "value"}, "array": [1, {"sub": "value"}]}; } @font-face { font-family: 'Calibri'; }`);
104+
});
105+
106+
test('Multiple rules with one JSON-like custom property', () => {
107+
let root = parse(`
108+
.class1 { margin: 10px; }
109+
:root { --jsonProp: {"a": 1, "b": {"c": 3}}; }
110+
.class2 { padding: 20px; }
111+
`);
112+
equal(root.toString(), `
113+
.class1 { margin: 10px; }
114+
:root { --jsonProp: {"a": 1, "b": {"c": 3}}; }
115+
.class2 { padding: 20px; }
116+
`);
117+
});
118+
119+
test('Custom property at start without modifications', () => {
120+
let root = parse(`--example: {"key": "value"}; .class { color: black; }`);
121+
equal(root.toString(), `--example: {"key": "value"}; .class { color: black; }`);
122+
});
123+
91124
test.run()
92125

0 commit comments

Comments
 (0)