Add calc()/min()/max()/clamp()#213
Merged
Merged
Conversation
calc(), min(), max() and clamp() (CSS Values 4 10) were rejected everywhere. Add a recursive-descent CalcParser, a CalcTypeChecker implementing the type-checking rules, and a CalcValueConverter composed onto the length, line-width, angle, number, length-or-percent and angle-or-number converters so support cascades to every property built on them. The authored text is preserved. Type-checking rejects dimensionally-inconsistent expressions (calc(1px + 1s), calc(10px * 20px)), division by a constant zero, and malformed input (missing whitespace around +/-, wrong clamp() arity, empty). An angle/number expression validates in the corresponding context. Includes the nested-parentheses lexer fix (also submitted standalone), needed so a parenthesized group inside the arguments - calc((100% - 20px) / 3) - isn't cut off at the inner ')'. If that merges first, this rebases cleanly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Feature
calc(),min(),max()andclamp()(CSS Values 4 §10) were rejected everywhere:Implementation
CalcParser— a recursive-descent parser for the<calc-sum>/<calc-product>/<calc-value>grammar, including nestedmin/max/clampand parenthesized groups. Binary+/-require whitespace on both sides;*//don't.CalcTypeChecker— the §10.10 type-checking rules: resolves each expression to aCalcCategory(number/length/percentage/angle/…) or rejects a dimensionally-inconsistent one. It folds pure-number subtrees so a constant divide-by-zero is caught here.CalcValueConverter— accepts a calc expression whose category fits the context, and preserves the authored text. Composed onto the length, line-width, angle, number, length-or-percent, and angle-or-number converters, so support cascades to every property built on them.Type-checking rejects
calc(1px + 1s),calc(10px * 20px),calc(100% / 0),calc(1px +1px)(missing space),calc(5)in a length context,clamp(1px, 2px)(wrong arity), and empty/malformed input.The evaluation of a calc() against layout context (
em/%→ pixels) is out of scope for a parser — only parsing, type-checking and serialization are provided.Dependency
A parenthesized group inside the arguments —
calc((100% - 20px) / 3)— needs the nested-parentheses lexer fix ("Fix function tokens terminating at a nested closing parenthesis", #190). This branch includes that one change so it's self-contained; if #190 merges first, it rebases cleanly.Tests
19 cases in
PropertyTests/CalcPropertyTests.cs: calc/min/max/clamp/nesting in a length context, angle-context calc, text preservation, and eight type-check/grammar rejections.The full suite (1263 existing tests) stays green — wiring calc into the core numeric converters disturbs nothing — and all seven target frameworks build with no new warnings.