Skip to content
This repository was archived by the owner on Aug 5, 2026. It is now read-only.

Fix layout validators to reject negative percentages in spacing props - #4979

Merged
Faye (Faye-Xiao) merged 5 commits into
mainfrom
fix/layout-negative-percentage-spacing
Jul 29, 2026
Merged

Faye (Faye-Xiao) merged 5 commits into
mainfrom
fix/layout-negative-percentage-spacing

Conversation

@Faye-Xiao

Copy link
Copy Markdown
Contributor

Summary

  • isPercentage in tokens.ts used -? in its regex, unintentionally allowing negative percentages to pass validation for padding, margin, and gap props
  • Negative percentages are not meaningful CSS spacing values and should be rejected
  • isValidPositionValue and isValidSizeValue intentionally support negative values (e.g. top: '-50%' for translate offsets), so they now inline their own /^-?\d+(\.\d+)?%$/ regex instead of delegating to isPercentage

Files changed:

  • tokens.ts — remove -? from isPercentage; inline negative-% regex in isValidPositionValue and isValidSizeValue
  • tokenUtils-test.ts — add tests asserting negative % is rejected for padding, margin, gap and accepted for position props

Test plan

  • pnpm run jest --testPathPattern="tokenUtils-test" — 41 tests pass
  • eslint on changed files — no errors
  • Negative % for padding/margin/gap is dropped with a console warning
  • Negative % for top/left/right/bottom continues to pass through unchanged

🤖 Generated with Claude Code

…s in spacing props

`isPercentage` previously allowed negative percentages (`-?` in the regex),
which leaked into `isValidSpacingValue` and `isValidMarginValue`. Negative
percentages are not valid spacing values and were being accepted silently.

- Remove `-?` from `isPercentage` so spacing/margin/gap props reject negative %
- Inline `/^-?\d+(\.\d+)?%$/` in `isValidPositionValue` and `isValidSizeValue`
  so those continue to accept negative percentages intentionally
- Add unit tests covering rejection of negative % for padding, margin, and gap,
  and the existing passing-through of negative % for position props

Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings July 29, 2026 09:47
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@Faye-Xiao Faye (Faye-Xiao) added the patch Patch production bug label Jul 29, 2026
@skyscanner-backpack-bot

skyscanner-backpack-bot Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor
Warnings
⚠️

Package source files (e.g. packages/package-name/src/Component.js) were updated, but snapshots weren't. Have you checked that the tests still pass?

Browser support

If this is a visual change, make sure you've tested it in multiple browsers.

Generated by 🚫 dangerJS against 457cd97

- Spacing (padding/margin/gap): clarify only non-negative percentages are accepted
- Position offsets: explicitly document negative rem and negative % are supported

Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Updates layout prop validators so spacing props reject negative percentage values, while preserving negative percentage support for offset-style props.

Changes:

  • Tightens isPercentage to only accept non-negative percentage strings for spacing validation.
  • Inlines signed-percentage regex in position/size validators to keep negative percentage support where intended.
  • Adds Jest coverage for rejecting negative % in padding/margin/gap and allowing it for position props; updates README wording accordingly.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
packages/backpack-web/src/bpk-component-layout/src/tokens.ts Makes spacing % validation non-negative; inlines signed % regex for position/size validators.
packages/backpack-web/src/bpk-component-layout/src/tokenUtils-test.ts Adds tests ensuring negative % is dropped for spacing props and preserved for position props.
packages/backpack-web/src/bpk-component-layout/README.md Documents that spacing percentages must be non-negative; clarifies negative values for offset props.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 203 to 209
export function isValidSizeValue(value: string): boolean {
return (
/^-?\d+(\.\d+)?rem$/.test(value) || // rem values
isPercentage(value) || // percentage values
/^-?\d+(\.\d+)?rem$/.test(value) || // rem values (negative allowed for transforms)
/^-?\d+(\.\d+)?%$/.test(value) || // percentage values (negative allowed)
value === 'auto' ||
value === 'full' ||
value === 'fit-content'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the follow-up commit — isValidSizeValue now uses /^\d+(\.\d+)?rem$/ (no -?) and delegates to isPercentage (non-negative) instead of inlining the signed regex.

return (
/^-?\d+(\.\d+)?rem$/.test(value) || // rem values
isPercentage(value) || // percentage values
/^-?\d+(\.\d+)?rem$/.test(value) || // rem values (negative allowed for transforms)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — the misleading comment and the signed regex in isValidSizeValue were both removed in the follow-up commit.

Comment on lines +435 to +447
it('rejects negative percentage for padding and drops the prop', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});

const result = processBpkProps({
padding: '-10%' as any,
paddingTop: '-5%' as any,
});

expect(result.padding).toBeUndefined();
expect(result.paddingTop).toBeUndefined();
expect(warnSpy).toHaveBeenCalled();
warnSpy.mockRestore();
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — added afterEach(() => jest.restoreAllMocks()) to the processBpkProps describe block so spy cleanup is guaranteed even if an assertion throws.

Comment on lines 202 to 204
*/
export function isValidSizeValue(value: string): boolean {
return (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isValidSizeValue comment is misleading (likely a pre-existing bug surfaced here)

/^-?\d+(\.\d+)?rem$/.test(value) || // rem values (negative allowed for transforms)
/^-?\d+(\.\d+)?%$/.test(value) ||   // percentage values (negative allowed)

isValidSizeValue governs width, height, minWidth, maxWidth, etc. Negative values for these properties are invalid CSS — a negative width or minWidth is meaningless. The comment "negative allowed for transforms" doesn't make sense in this context (transform uses isValidTransformValue, not this function). This looks like a copy-paste from the position validator. Worth either:

  • Dropping the -? from both lines here (if negative size was never intentional), or
  • Adding a note confirming this is intentional with a concrete use-case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — isValidSizeValue now uses /^\d+(\.\d+)?rem$/ (no -?) and isPercentage (non-negative). Negative size values were never intentional; this was a copy-paste from the position validator.

Comment on lines +205 to +206
/^-?\d+(\.\d+)?rem$/.test(value) || // rem values (negative allowed for transforms)
/^-?\d+(\.\d+)?%$/.test(value) || // percentage values (negative allowed)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex /^-?\d+(\.\d+)?%$/ now appears in two places. A named export like:

const NEGATIVE_PERCENTAGE_RE = /^-?\d+(\.\d+)?%$/;
would remove the duplication and make the intent self-documenting. Low priority, but worth doing while the change is open.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — extracted as NEGATIVE_PERCENTAGE_RE and used in isValidPositionValue to eliminate the duplication.

Comment on lines +457 to +470
expect(result.margin).toBeUndefined();
expect(result.marginTop).toBeUndefined();
expect(warnSpy).toHaveBeenCalled();
warnSpy.mockRestore();
});

it('rejects negative percentage for gap and drops the prop', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});

const result = processBpkProps({
gap: '-10%' as any,
});

expect(result.gap).toBeUndefined();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BpkFlex exposes rowGap and columnGap as separate gap props. The new "rejects negative percentage for gap" test only covers gap. If those props go through a different validator path, the fix might not cover them. Worth adding:

const result = processBpkProps({ gap: '-10%', rowGap: '-5%', columnGap: '-5%' } as any);
expect(result.rowGap).toBeUndefined();
expect(result.columnGap).toBeUndefined();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — expanded the gap test to cover rowGap and columnGap as well.

});

expect(result.gap).toBeUndefined();
expect(warnSpy).toHaveBeenCalled();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three new tests assert only expect(warnSpy).toHaveBeenCalled(). If the warning message changes to something uninformative (or is removed), this passes silently. Prefer:

expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('-10%'));
or at minimum toHaveBeenCalledTimes(1) per rejected prop so the count is explicit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — assertions now use toHaveBeenCalledWith(expect.stringContaining(value)) so they verify the invalid value appears in the warning message.

});

it('passes through negative percentages for position props (e.g. top: -50% for vertical centering)', () => {
it('passes through negative percentages for position props (e.g. top: -50% for translate offsets)', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no test confirming that padding: '50%' still passes after the change. Given this is the exact behavior the fix changes, one positive assertion is cheap insurance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — added a dedicated test: padding: '50%' passes through as '50%' after the change.

Faye (Faye-Xiao) and others added 2 commits July 29, 2026 18:14
- isValidSizeValue: revert to non-negative rem and percentage (negative size
  values are not meaningful CSS; the signed regex was introduced unintentionally
  in the previous commit)
- tokenUtils-test: add afterEach(jest.restoreAllMocks) to processBpkProps
  describe block so warnSpy cannot leak if an assertion throws

Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
- Extract NEGATIVE_PERCENTAGE_RE constant to avoid duplicating the signed
  percentage regex in isValidPositionValue
- Add rowGap/columnGap coverage to the negative gap test
- Tighten warn spy assertions to check the invalid value appears in the message
- Add positive padding: '50%' regression guard

Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor


// Matches percentage strings that include a leading minus sign, e.g. '-50%', '-0.5%'.
// Used only by position validators where negative offsets are intentional.
const NEGATIVE_PERCENTAGE_RE = /^-?\d+(\.\d+)?%$/;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about rename to SIGNED_PERCENTAGE_RE as -? let this regex match both positive and negative percentages simultaneously (both 50% and -50% can pass)

Also comment should update

// Matches both positive and negative percentage strings, e.g. '50%', '-50%', '-0.5%'.
// Used by position validators where negative offsets are intentional.
const SIGNED_PERCENTAGE_RE = /^-?\d+(\.\d+)?%$/;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — renamed to SIGNED_PERCENTAGE_RE and updated the comment accordingly.

The regex uses -? so it matches both positive and negative percentages.
SIGNED_PERCENTAGE_RE is more accurate. Also update the JSDoc reference.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@Faye-Xiao
Faye (Faye-Xiao) merged commit 6fa0065 into main Jul 29, 2026
15 checks passed
@Faye-Xiao
Faye (Faye-Xiao) deleted the fix/layout-negative-percentage-spacing branch July 29, 2026 10:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

patch Patch production bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants