Skip to content

Reject negative lengths for border-spacing - #321

Closed
afonsojanu wants to merge 1 commit into
jsdom:mainfrom
afonsojanu:fix/border-spacing-negative-length
Closed

Reject negative lengths for border-spacing#321
afonsojanu wants to merge 1 commit into
jsdom:mainfrom
afonsojanu:fix/border-spacing-negative-length

Conversation

@afonsojanu

Copy link
Copy Markdown

Summary

border-spacing currently accepts and stores negative lengths, e.g.:

style.borderSpacing = '-10px';
style.borderSpacing; // '-10px'

That's not spec-compliant. The CSS Tables spec defines the property as <length [0,∞]>{1,2} and its prose says the values "must be non-negative" (this carries over from CSS 2.1, which put it even more plainly: "Negative values for <length> are illegal").

Looking through the other length properties in this codebase that carry the same non-negative constraint, they all pass min: 0 into resolveNumericValue/serializeLength: border-*-width, padding*, width, height, line-height, flex-grow/flex-shrink. border-spacing is the one property with that same constraint that was missing it, in both the single-value and two-value branches of its parser.

Fix

Adds min: 0 to the resolveNumericValue calls in lib/properties/borderSpacing.js, for both the one-length and two-length forms. With this in place, a negative length now falls through to undefined the same way it already does for the sibling properties, and the whole property is left unset (matching how e.g. border-top-width: -1px already behaves here).

Test plan

  • Added three cases to test/properties.test.js: a single negative length, a negative length as the first of two values, and as the second of two values. All assert the property does not get set.
  • Verified these three tests fail on the old code ('-10px' !== '', etc.) and pass with the fix.
  • node --test passes (664/664).
  • eslint is clean on both changed files.

Disclosure

I used an AI coding assistant (Claude) to help investigate and write this fix, per this project's contribution norms.

The CSS Tables spec defines border-spacing as <length [0,∞]>{1,2} and
says the values "must be non-negative". Every sibling length property
in this codebase that has the same constraint (border-width, padding,
width, height, line-height, flex-grow/shrink) passes min: 0 into
resolveNumericValue, but borderSpacing.js never did, so a negative
length was happily accepted and stored:

  style.borderSpacing = '-10px';
  style.borderSpacing; // '-10px', should be rejected

This adds the missing min: 0 to both the single-value and two-value
branches of the parser, matching how the rest of the codebase handles
non-negative lengths, and adds three tests covering a single negative
value and a negative value in either position of a two-value pair.

Co-authored-by: Claude <noreply@anthropic.com>
@domenic

domenic commented Sep 10, 2026

Copy link
Copy Markdown
Member

This project is no longer maintained; it has been rolled into jsdom. Good reminder to archive it though.

@domenic domenic closed this Sep 10, 2026
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.

2 participants