Skip to content

Commit 1c04ed8

Browse files
fix: ast for division in calc
1 parent 9ee9aaa commit 1c04ed8

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

lib/parse.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ module.exports = function(input) {
5353
} else if (
5454
code === comma ||
5555
code === colon ||
56-
(code === slash && value.charCodeAt(next + 1) !== star)
56+
(code === slash &&
57+
value.charCodeAt(next + 1) !== star &&
58+
(!parent ||
59+
(parent && parent.type === "function" && parent.value !== "calc")))
5760
) {
5861
before = token;
5962
} else {

test/parse.js

+110
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,46 @@ var tests = [
492492
}
493493
]
494494
},
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+
},
495535
{
496536
message: "should correctly parse multiplication with spaces",
497537
fixture: "calc(1 * 2)",
@@ -532,6 +572,46 @@ var tests = [
532572
}
533573
]
534574
},
575+
{
576+
message: "should correctly parse multiplication with spaces",
577+
fixture: "calc(1 / 2)",
578+
expected: [
579+
{
580+
type: "function",
581+
sourceIndex: 0,
582+
value: "calc",
583+
before: "",
584+
after: "",
585+
nodes: [
586+
{
587+
type: "word",
588+
sourceIndex: 5,
589+
value: "1"
590+
},
591+
{
592+
type: "space",
593+
sourceIndex: 6,
594+
value: " "
595+
},
596+
{
597+
type: "word",
598+
sourceIndex: 7,
599+
value: "/"
600+
},
601+
{
602+
type: "space",
603+
sourceIndex: 8,
604+
value: " "
605+
},
606+
{
607+
type: "word",
608+
sourceIndex: 9,
609+
value: "2"
610+
}
611+
]
612+
}
613+
]
614+
},
535615
{
536616
message: "should correctly parse multiplication without spaces",
537617
fixture: "calc(1*2)",
@@ -562,6 +642,36 @@ var tests = [
562642
}
563643
]
564644
},
645+
{
646+
message: "should correctly parse multiplication without spaces",
647+
fixture: "calc(1/2)",
648+
expected: [
649+
{
650+
type: "function",
651+
sourceIndex: 0,
652+
value: "calc",
653+
before: "",
654+
after: "",
655+
nodes: [
656+
{
657+
type: "word",
658+
sourceIndex: 5,
659+
value: "1"
660+
},
661+
{
662+
type: "word",
663+
sourceIndex: 6,
664+
value: "/"
665+
},
666+
{
667+
type: "word",
668+
sourceIndex: 7,
669+
value: "2"
670+
}
671+
]
672+
}
673+
]
674+
},
565675
{
566676
message: "should correctly process nested calc functions",
567677
fixture: "calc(((768px - 100vw) / 2) - 15px)",

0 commit comments

Comments
 (0)