Skip to content

Commit d579b64

Browse files
diervoevilebottnawi
authored andcommitted
feat: allow certain calc() operations to be parsed according to the spec (#42)
1 parent c1f40a5 commit d579b64

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

lib/parse.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ module.exports = function(input) {
104104
pos = next + 2;
105105
code = value.charCodeAt(pos);
106106

107+
// Operation within calc
108+
} else if (
109+
(code === slash || code === star) &&
110+
parent &&
111+
parent.type === "function" &&
112+
parent.value === "calc"
113+
) {
114+
token = value[pos];
115+
tokens.push({
116+
type: "word",
117+
sourceIndex: pos - before.length,
118+
value: token
119+
});
120+
pos += 1;
121+
code = value.charCodeAt(pos);
122+
107123
// Dividers
108124
} else if (code === slash || code === comma || code === colon) {
109125
token = value[pos];
@@ -224,6 +240,13 @@ module.exports = function(input) {
224240
code === colon ||
225241
code === slash ||
226242
code === openParentheses ||
243+
(code === star &&
244+
parent &&
245+
parent.type === "function" &&
246+
parent.value === "calc") ||
247+
(code === slash &&
248+
parent.type === "function" &&
249+
parent.value === "calc") ||
227250
(code === closeParentheses && balanced)
228251
)
229252
);

test/parse.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,116 @@ var tests = [
452452
}
453453
]
454454
},
455+
{
456+
message: "should correctly parse spaces",
457+
fixture: "calc(1 + 2)",
458+
expected: [
459+
{
460+
type: "function",
461+
sourceIndex: 0,
462+
value: "calc",
463+
before: "",
464+
after: "",
465+
nodes: [
466+
{
467+
type: "word",
468+
sourceIndex: 5,
469+
value: "1"
470+
},
471+
{
472+
type: "space",
473+
sourceIndex: 6,
474+
value: " "
475+
},
476+
{
477+
type: "word",
478+
sourceIndex: 7,
479+
value: "+"
480+
},
481+
{
482+
type: "space",
483+
sourceIndex: 8,
484+
value: " "
485+
},
486+
{
487+
type: "word",
488+
sourceIndex: 9,
489+
value: "2"
490+
}
491+
]
492+
}
493+
]
494+
},
495+
{
496+
message: "should correctly parse multiplication with spaces",
497+
fixture: "calc(1 * 2)",
498+
expected: [
499+
{
500+
type: "function",
501+
sourceIndex: 0,
502+
value: "calc",
503+
before: "",
504+
after: "",
505+
nodes: [
506+
{
507+
type: "word",
508+
sourceIndex: 5,
509+
value: "1"
510+
},
511+
{
512+
type: "space",
513+
sourceIndex: 6,
514+
value: " "
515+
},
516+
{
517+
type: "word",
518+
sourceIndex: 7,
519+
value: "*"
520+
},
521+
{
522+
type: "space",
523+
sourceIndex: 8,
524+
value: " "
525+
},
526+
{
527+
type: "word",
528+
sourceIndex: 9,
529+
value: "2"
530+
}
531+
]
532+
}
533+
]
534+
},
535+
{
536+
message: "should correctly parse multiplication without spaces",
537+
fixture: "calc(1*2)",
538+
expected: [
539+
{
540+
type: "function",
541+
sourceIndex: 0,
542+
value: "calc",
543+
before: "",
544+
after: "",
545+
nodes: [
546+
{
547+
type: "word",
548+
sourceIndex: 5,
549+
value: "1"
550+
},
551+
{
552+
type: "word",
553+
sourceIndex: 6,
554+
value: "*"
555+
},
556+
{
557+
type: "word",
558+
sourceIndex: 7,
559+
value: "2"
560+
}
561+
]
562+
}
563+
]
564+
},
455565
{
456566
message: "should correctly process nested calc functions",
457567
fixture: "calc(((768px - 100vw) / 2) - 15px)",

0 commit comments

Comments
 (0)