Fix DimensionToken serialization for scientific-notation-like units#74
Open
gaoflow wants to merge 1 commit into
Open
Fix DimensionToken serialization for scientific-notation-like units#74gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
DimensionToken._serialize_to escaped a unit's leading 'e'/'E' to stop it merging with the number into a scientific-notation number token on re-parse, but the guard was both incomplete and case-corrupting: - It hard-coded the escape '\65 ' (lowercase 'e'), so a unit 'E' round-tripped to 'e' (case flipped). - It only triggered for 'e'/'E' and 'e-'/'E-', missing 'e'/'E' followed by a digit (e3, E3, e05, ...). Those units merged into the exponent, so the dimension silently re-parsed as a number (1 + unit 'e3' -> '1e3' -> 1000.0). Escape the leading letter using its own code point (preserving case) and widen the trigger to any unit whose first char is 'e'/'E' followed by end-of-unit, a sign, or a digit. Common units (em, ex, EX, Q, ...) are unaffected.
Member
|
Hi! Thanks for the pull request. Out of curiosity: did this problem happen in one of your projects? |
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.
Problem
DimensionToken._serialize_toescapes a unit's leadinge/Eso the unit doesnot merge with the number into a scientific-notation number token when the
output is re-parsed. The guard is both incomplete and case-corrupting, so
serialize(parse(css)) == cssis violated for some valid dimension tokens:Two failure modes:
\65(lowercasee), so a unitEround-tripsto
e(case flip).e/Eande-/E-, missinge/Efollowed by adigit (
e3,E3,e05, ...). Those units merge into the exponent and thedimension silently becomes a number with a different value.
Common units (
em,ex,EX,Q, ...) are unaffected.Fix
Escape the leading letter using its own code point (
\\%X) so its case ispreserved, and widen the trigger to any unit whose first character is
e/Efollowed by end-of-unit, a sign, or a digit.
Tests
test_serialize_dimension_scientific_notationround-trips the problem units(
e,E,e3,E3,e05,e-3,E-3), asserting they stay dimensions withthe same value/int_value/unit, and checks that common units are not over-escaped.
Full suite: 16976 passed.
ruff checkclean.