Skip to content

Commit 928e022

Browse files
committed
parse nested items correctly
1 parent f5ea690 commit 928e022

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

lib/parseValues.js

+38-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,40 @@ function itemMatch(match) {
5656
});
5757
}
5858

59+
function nestedItemMatch(match, name, spacing) {
60+
this.stack.push(this.root);
61+
this.root = {
62+
type: "nested-item",
63+
name: name,
64+
nodes: [
65+
{ type: "value", nodes: [] }
66+
]
67+
};
68+
if(spacing) {
69+
this.root.nodes[0].before = spacing;
70+
}
71+
this.value.nodes.push(this.root);
72+
this.value = this.root.nodes[0];
73+
}
74+
75+
function nestedItemEndMatch(match, spacing, remaining) {
76+
if(this.stack.length === 0) {
77+
if(spacing) {
78+
var item = this.value.nodes[this.value.nodes.length - 1];
79+
item.after = (item.after || "") + spacing;
80+
}
81+
this.value.nodes.push({
82+
type: "invalid",
83+
value: remaining
84+
});
85+
} else {
86+
if(spacing) {
87+
this.value.after = spacing;
88+
}
89+
this.root = this.stack.pop();
90+
}
91+
}
92+
5993
function urlMatch(match, innerSpacingBefore, content, innerSpacingAfter) {
6094
var item = {
6195
type: "url"
@@ -91,10 +125,12 @@ var parser = new Parser({
91125
"url\\((\\s*)(\"(?:[^\\\\\"]|\\\\.)*\")(\\s*)\\)": urlMatch,
92126
"url\\((\\s*)('(?:[^\\\\']|\\\\.)*')(\\s*)\\)": urlMatch,
93127
"url\\((\\s*)((?:[^\\\\)'\"]|\\\\.)*)(\\s*)\\)": urlMatch,
128+
"(\\w+)\\((\\s*)": nestedItemMatch,
129+
"(\\s*)(\\))": nestedItemEndMatch,
94130
",(\\s*)": commaMatch,
95131
"\\s+$": endSpacingMatch,
96132
"\\s+": spacingMatch,
97-
"[^\\s,]+": itemMatch
133+
"[^\\s,\)]+": itemMatch
98134
}
99135
});
100136

@@ -110,6 +146,7 @@ function parseValues(str) {
110146
]
111147
};
112148
parser.parse("decl", str, {
149+
stack: [],
113150
root: rootNode,
114151
value: valueNode
115152
});

lib/stringifyValues.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ function stringifyWithoutBeforeAfter(tree) {
1010
return tree.nodes.map(stringify).join("");
1111
case "item":
1212
return tree.name;
13+
case "nested-item":
14+
return tree.name + "(" + tree.nodes.map(stringify).join(",") + ")";
15+
case "invalid":
16+
return tree.value;
1317
case "comment":
1418
return "/*" + tree.content + "*/";
1519
case "string":

test/test-cases-values.js

+44
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,50 @@ module.exports = {
6868
{ type: "url", url: "ghi)j\"k", innerSpacingBefore: " " }
6969
])
7070
],
71+
"nested-item": [
72+
"format('woff')",
73+
singleValue([
74+
{ type: "nested-item", name: "format", nodes: [
75+
{ type: "value", nodes: [
76+
{ type: "string", stringType: "'", value: "woff"}
77+
]}
78+
] }
79+
])
80+
],
81+
"nested-item-difficult": [
82+
"format('woff'), format( \"a b, c\" )",
83+
{
84+
type: "values",
85+
nodes: [
86+
{ type: "value", nodes: [
87+
{ type: "nested-item", name: "format", nodes: [
88+
{ type: "value", nodes: [
89+
{ type: "string", stringType: "'", value: "woff"}
90+
]}
91+
] }
92+
] },
93+
{ type: "value", nodes: [
94+
{ type: "nested-item", name: "format", nodes: [
95+
{ type: "value", nodes: [
96+
{ type: "string", stringType: "\"", value: "a b, c"}
97+
], before: " ", after: " " }
98+
] }
99+
], before: " " }
100+
]
101+
}
102+
],
103+
"invalid": [
104+
" ) ) ",
105+
{
106+
type: "values",
107+
nodes: [
108+
{ type: "value", nodes: [
109+
{ type: "invalid", value: ")", after: " " },
110+
{ type: "invalid", value: ")" }
111+
], before: " ", after: " " }
112+
]
113+
}
114+
],
71115
"spacing": [
72116
" hello\n\t world\t",
73117
{

0 commit comments

Comments
 (0)