Skip to content

Commit a86f64b

Browse files
fix: empty url with whitespace characters (#44)
BREAKING CHANGE: empty urls with only spaces and newlines contain whitespaces characters in `before` property (before we duplicate their in `before` and `after`)
1 parent d579b64 commit a86f64b

File tree

3 files changed

+169
-22
lines changed

3 files changed

+169
-22
lines changed

lib/parse.js

+33-20
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ module.exports = function(input) {
1212
var tokens = [];
1313
var value = input;
1414

15-
var next, quote, prev, token, escape, escapePos, whitespacePos;
15+
var next,
16+
quote,
17+
prev,
18+
token,
19+
escape,
20+
escapePos,
21+
whitespacePos,
22+
parenthesesOpenPos;
1623
var pos = 0;
1724
var code = value.charCodeAt(pos);
1825
var max = value.length;
@@ -144,11 +151,12 @@ module.exports = function(input) {
144151
next += 1;
145152
code = value.charCodeAt(next);
146153
} while (code <= 32);
154+
parenthesesOpenPos = pos;
147155
token = {
148156
type: "function",
149157
sourceIndex: pos - name.length,
150158
value: name,
151-
before: value.slice(pos + 1, next)
159+
before: value.slice(parenthesesOpenPos + 1, next)
152160
};
153161
pos = next;
154162

@@ -175,26 +183,31 @@ module.exports = function(input) {
175183
whitespacePos -= 1;
176184
code = value.charCodeAt(whitespacePos);
177185
} while (code <= 32);
178-
if (pos !== whitespacePos + 1) {
179-
token.nodes = [
180-
{
181-
type: "word",
182-
sourceIndex: pos,
183-
value: value.slice(pos, whitespacePos + 1)
184-
}
185-
];
186+
if (parenthesesOpenPos < whitespacePos) {
187+
if (pos !== whitespacePos + 1) {
188+
token.nodes = [
189+
{
190+
type: "word",
191+
sourceIndex: pos,
192+
value: value.slice(pos, whitespacePos + 1)
193+
}
194+
];
195+
} else {
196+
token.nodes = [];
197+
}
198+
if (token.unclosed && whitespacePos + 1 !== next) {
199+
token.after = "";
200+
token.nodes.push({
201+
type: "space",
202+
sourceIndex: whitespacePos + 1,
203+
value: value.slice(whitespacePos + 1, next)
204+
});
205+
} else {
206+
token.after = value.slice(whitespacePos + 1, next);
207+
}
186208
} else {
187-
token.nodes = [];
188-
}
189-
if (token.unclosed && whitespacePos + 1 !== next) {
190209
token.after = "";
191-
token.nodes.push({
192-
type: "space",
193-
sourceIndex: whitespacePos + 1,
194-
value: value.slice(whitespacePos + 1, next)
195-
});
196-
} else {
197-
token.after = value.slice(whitespacePos + 1, next);
210+
token.nodes = [];
198211
}
199212
pos = next + 1;
200213
code = value.charCodeAt(pos);

test/parse.js

+98
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,104 @@ var tests = [
902902
}
903903
]
904904
},
905+
{
906+
message: "should parse empty url with space",
907+
fixture: "url( )",
908+
expected: [
909+
{
910+
type: "function",
911+
sourceIndex: 0,
912+
value: "url",
913+
before: " ",
914+
after: "",
915+
nodes: []
916+
}
917+
]
918+
},
919+
{
920+
message: "should parse empty url with multiple spaces",
921+
fixture: "url( )",
922+
expected: [
923+
{
924+
type: "function",
925+
sourceIndex: 0,
926+
value: "url",
927+
before: " ",
928+
after: "",
929+
nodes: []
930+
}
931+
]
932+
},
933+
{
934+
message: "should parse empty url with newline (LF)",
935+
fixture: "url(\n)",
936+
expected: [
937+
{
938+
type: "function",
939+
sourceIndex: 0,
940+
value: "url",
941+
before: "\n",
942+
after: "",
943+
nodes: []
944+
}
945+
]
946+
},
947+
{
948+
message: "should parse empty url with newline (CRLF)",
949+
fixture: "url(\r\n)",
950+
expected: [
951+
{
952+
type: "function",
953+
sourceIndex: 0,
954+
value: "url",
955+
before: "\r\n",
956+
after: "",
957+
nodes: []
958+
}
959+
]
960+
},
961+
{
962+
message: "should parse empty url with multiple newlines (LF)",
963+
fixture: "url(\n\n\n)",
964+
expected: [
965+
{
966+
type: "function",
967+
sourceIndex: 0,
968+
value: "url",
969+
before: "\n\n\n",
970+
after: "",
971+
nodes: []
972+
}
973+
]
974+
},
975+
{
976+
message: "should parse empty url with multiple newlines (CRLF)",
977+
fixture: "url(\r\n\r\n\r\n)",
978+
expected: [
979+
{
980+
type: "function",
981+
sourceIndex: 0,
982+
value: "url",
983+
before: "\r\n\r\n\r\n",
984+
after: "",
985+
nodes: []
986+
}
987+
]
988+
},
989+
{
990+
message: "should parse empty url with whitespace characters",
991+
fixture: "url( \n \t \r\n )",
992+
expected: [
993+
{
994+
type: "function",
995+
sourceIndex: 0,
996+
value: "url",
997+
before: " \n \t \r\n ",
998+
after: "",
999+
nodes: []
1000+
}
1001+
]
1002+
},
9051003
{
9061004
message: "should parse comments",
9071005
fixture: "/*before*/ 1px /*between*/ 1px /*after*/",

test/stringify.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,48 @@ var tests = [
2727
},
2828
{
2929
message: "Should correctly add comments",
30-
fixture: "/*comment*/ 1px /* unclosed "
30+
fixture: "/!*comment*!/ 1px /!* unclosed "
3131
},
3232
{
3333
message: "Should correctly process comments inside functions",
3434
fixture:
35-
"/*before*/ rgb( /*red component*/ 12, 54 /*green component*/, /* blue */ 65)/* after */ "
35+
"/!*before*!/ rgb( /!*red component*!/ 12, 54 /!*green component*!/, /!* blue *!/ 65)/!* after *!/ "
36+
},
37+
{
38+
message: "Should correctly process empty url",
39+
fixture: "url()"
40+
},
41+
{
42+
message: "Should correctly process empty url with space",
43+
fixture: "url( )"
44+
},
45+
{
46+
message: "Should correctly process empty url with spaces",
47+
fixture: "url( )"
48+
},
49+
{
50+
message: "Should correctly process empty url with tab",
51+
fixture: "url(\t)"
52+
},
53+
{
54+
message: "Should correctly process empty url with newline (LF)",
55+
fixture: "url(\n)"
56+
},
57+
{
58+
message: "Should correctly process empty url with newline (LF)",
59+
fixture: "url(\n\n\n)"
60+
},
61+
{
62+
message: "Should correctly process empty url with multiple newlines (CRLF)",
63+
fixture: "url(\r\n)"
64+
},
65+
{
66+
message: "Should correctly process empty url with multiple newlines (CRLF)",
67+
fixture: "url(\r\n\r\n\r\n)"
68+
},
69+
{
70+
message: "Should correctly process empty url whitespace characters",
71+
fixture: "url( \n \t \n )"
3672
}
3773
];
3874

0 commit comments

Comments
 (0)