Skip to content

Support scientific notation in CSS numeric values - #582

Open
Pradeep Ramola (pradeep-ramola) wants to merge 1 commit into
microsoft:mainfrom
pradeep-ramola:fix/scientific-notation-numbers
Open

Pradeep Ramola (pradeep-ramola) wants to merge 1 commit into
microsoft:mainfrom
pradeep-ramola:fix/scientific-notation-numbers

Conversation

@pradeep-ramola

Copy link
Copy Markdown
Contributor

Problem

Scientific notation is part of CSS number syntax, but the scanner currently treats exponent text as a dimension unit or splits a signed exponent into multiple tokens. For example:

  • 1e2 is treated as an unknown dimension instead of a number.
  • 1E+2px is split instead of remaining a single length token.
  • NumericValue.getValue() mistakes exponent text for the unit.
  • Valid colors such as rgb(1e2 0 0) and hsl(5e-1turn 100% 50%) are not reported by findDocumentColors.

Spec: https://www.w3.org/TR/css-values-4/#numbers

Change

  • Consume an optional e/E exponent only when its optional sign is followed by a digit, before scanning a percentage/unit suffix.
  • Preserve the leading sign and exponent when separating a numeric node's value from its unit.
  • Accept exponent syntax in the existing numeric color-component and hue-angle readers.

Incomplete exponents and ordinary em/ex units retain their existing token boundaries. The existing mantissa scanning and color-channel range handling are unchanged. This PR is based directly on main and does not include or require #579 or #581.

Tests

Added 60 cases across CSS, LESS, and SCSS:

  • Numeric, percentage, length, em, angle and time tokens, including uppercase E and signed exponents.
  • Exact token text/ranges and numeric-node value/unit separation.
  • Incomplete exponents and em/ex controls.
  • Color extraction and exact source ranges compared with equivalent decimal values.
  • Invalid exponents/units are not accepted as colors.
  • zeroUnits lint recognizes 0e0px.

The initial 57-case regression run on unchanged upstream had 54 failures and 3 passing controls. Three additional invalid-color control tests were added afterward.

Verification on Windows after a clean npm ci:

  • npm run compile: TypeScript compilation and 7 lint checks passed.
  • Explicitly enumerated all 31 compiled test files and ran node --enable-source-maps --test: 484 passed, no failures/skips.
  • git diff --check: passed.
  • Supplemental headless Chromium 148 check: 8 color pairs and 4 dimension-expression pairs have the same computed values as their decimal spellings.

Test files were enumerated explicitly to avoid the single-quoted npm glob selecting no tests in this Windows shell. Full VS Code editor UI integration and Sass/Less compiler execution were not run; coverage uses this repository's language-service implementations.

AI assistance was used to investigate, implement, test, and draft this contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant