diff --git a/lib/parse.js b/lib/parse.js index 7d343ca..f44d15d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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 = ""; @@ -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({ @@ -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) ) ); diff --git a/test/parse.js b/test/parse.js index 22aa393..8e60598 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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)",