Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ module.exports = function (input) {
var balanced = 0;
var parent;

function inCalc() {
for (var i = stack.length - 1; i > 0; i -= 1) {
if (stack[i].value === "calc") return true;
}
return false;
}

var name = "";
var before = "";
var after = "";
Expand Down Expand Up @@ -128,7 +135,8 @@ module.exports = function (input) {
(code === slash || code === star) &&
parent &&
parent.type === "function" &&
parent.value === "calc"
(parent.value === "calc" ||
(code === star && parent.value === "" && inCalc()))
) {
token = value[pos];
tokens.push({
Expand Down Expand Up @@ -277,10 +285,7 @@ module.exports = function (input) {
(code === star &&
parent &&
parent.type === "function" &&
parent.value === "calc") ||
(code === slash &&
parent.type === "function" &&
parent.value === "calc") ||
(parent.value === "calc" || (parent.value === "" && inCalc()))) ||
(code === closeParentheses && balanced)
)
);
Expand Down
106 changes: 106 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,112 @@ const tests = [
},
],
},
{
message: "should parse multiplication inside calc parentheses",
fixture: "calc((1*2))",
expected: [
{
type: "function",
sourceIndex: 0,
sourceEndIndex: 11,
value: "calc",
before: "",
after: "",
nodes: [
{
type: "function",
sourceIndex: 5,
sourceEndIndex: 10,
value: "",
before: "",
after: "",
nodes: [
{ type: "word", sourceIndex: 6, sourceEndIndex: 7, value: "1" },
{ type: "word", sourceIndex: 7, sourceEndIndex: 8, value: "*" },
{ type: "word", sourceIndex: 8, sourceEndIndex: 9, value: "2" },
],
},
],
},
],
},
{
message: "should parse multiplication before a nested function",
fixture: "calc((-1*var(--a)))",
expected: [
{
type: "function",
sourceIndex: 0,
sourceEndIndex: 19,
value: "calc",
before: "",
after: "",
nodes: [
{
type: "function",
sourceIndex: 5,
sourceEndIndex: 18,
value: "",
before: "",
after: "",
nodes: [
{ type: "word", sourceIndex: 6, sourceEndIndex: 8, value: "-1" },
{ type: "word", sourceIndex: 8, sourceEndIndex: 9, value: "*" },
{
type: "function",
sourceIndex: 9,
sourceEndIndex: 17,
value: "var",
before: "",
after: "",
nodes: [
{
type: "word",
sourceIndex: 13,
sourceEndIndex: 16,
value: "--a",
},
],
},
],
},
],
},
],
},
{
message:
"should keep multiplication inside a nested non-arithmetic function",
fixture: "calc(var(--a*b))",
expected: [
{
type: "function",
sourceIndex: 0,
sourceEndIndex: 16,
value: "calc",
before: "",
after: "",
nodes: [
{
type: "function",
sourceIndex: 5,
sourceEndIndex: 15,
value: "var",
before: "",
after: "",
nodes: [
{
type: "word",
sourceIndex: 9,
sourceEndIndex: 14,
value: "--a*b",
},
],
},
],
},
],
},
{
message: "should correctly parse division without spaces",
fixture: "calc(1/2)",
Expand Down