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
53 changes: 53 additions & 0 deletions src/ExCSS.Tests/Tokenization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,59 @@ public void CssParserUnicodeRangeSelectedRangeIsExpandedOnDemand()
Assert.Equal(new[] {"A", "B", "C"}, range.SelectedRange);
}

[Fact]
public void CssParserFunctionWithNestedParentheses()
{
// A bare parenthesized group inside a function's arguments must not terminate the function:
// only the ')' matching the function's own '(' does. Previously "calc((1px + 2px) * 3)" ended
// at the inner ')', leaving "* 3)" behind as stray top-level tokens.
var teststring = "calc((1px + 2px) * 3)";
var tokenizer = new Lexer(new TextSource(teststring));
var token = tokenizer.Get();

Assert.Equal(TokenType.Function, token.Type);
Assert.Equal("calc", token.Data);
Assert.Equal(teststring, token.ToValue());
Assert.Equal(TokenType.EndOfFile, tokenizer.Get().Type);
}

[Fact]
public void CssParserFunctionWithDeeplyNestedParentheses()
{
var teststring = "calc(((1px + 2px) * (3 - 1)) / 2)";
var tokenizer = new Lexer(new TextSource(teststring));
var token = tokenizer.Get();

Assert.Equal(TokenType.Function, token.Type);
Assert.Equal(teststring, token.ToValue());
Assert.Equal(TokenType.EndOfFile, tokenizer.Get().Type);
}

[Fact]
public void CssParserFunctionWithNestedFunctionAndParentheses()
{
// A nested function's own parens are consumed by its own recursive call, so they never surface
// as bare bracket tokens here - this asserts the depth counter doesn't double-count them.
var teststring = "calc(min(10px, (2px + 3px)) * 2)";
var tokenizer = new Lexer(new TextSource(teststring));
var token = tokenizer.Get();

Assert.Equal(TokenType.Function, token.Type);
Assert.Equal(teststring, token.ToValue());
Assert.Equal(TokenType.EndOfFile, tokenizer.Get().Type);
}

[Fact]
public void CssParserFunctionWithUnbalancedParenthesesStopsAtEof()
{
var teststring = "calc((1px + 2px";
var tokenizer = new Lexer(new TextSource(teststring));
var token = tokenizer.Get();

Assert.Equal(TokenType.Function, token.Type);
Assert.Equal(TokenType.EndOfFile, tokenizer.Get().Type);
}

[Fact]
public void LexerOnlyCarriageReturn()
{
Expand Down
10 changes: 6 additions & 4 deletions src/ExCSS/Parser/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,10 +1061,12 @@ private Token NewFunction(string value)
{
var function = new FunctionToken(value, _position);

// Track paren depth so a bare parenthesized group nested in the arguments - e.g.
// calc((100% - 20px) / 3) - isn't mistaken for the end of the function; only the ')' matching
// this function's own '(' terminates it. (Also submitted standalone as the nested-parentheses
// lexer fix; if that merges first, this rebases cleanly.)
// Tracks paren depth so a bare (non-function) parenthesized group nested inside the
// function's arguments - e.g. calc((1px + 2px) * 3) - isn't mistaken for the end of the
// function: only the closing paren matching this function's own opening paren (depth
// returning to 0) terminates it. A nested function call's own parens are already fully
// consumed by its own (recursive) call to this method before it's added as a single token
// here, so they never surface as bare RoundBracketOpen/Close tokens at this level.
var depth = 1;
var token = Get();
while (token.Type != TokenType.EndOfFile)
Expand Down