Skip to content

url: clamp the truncated file name slice in filterURLForDisplay - #81529

Merged
Mamaduka merged 1 commit into
WordPress:trunkfrom
Kgupta62:fix/url-filter-max-length
Aug 14, 2026
Merged

url: clamp the truncated file name slice in filterURLForDisplay#81529
Mamaduka merged 1 commit into
WordPress:trunkfrom
Kgupta62:fix/url-filter-max-length

Conversation

@Kgupta62

Copy link
Copy Markdown
Contributor

What?

filterURLForDisplay() no longer returns a string far longer than the maxLength it was given.

Why?

In the branch that truncates a long file name, the head slice is:

file.slice( 0, maxLength - truncatedFile.length - 1 )

truncatedFile is always fileName.slice( -3 ) + '.' + extension — seven characters for a three-character extension. As soon as maxLength is at or below that, the second argument goes negative, and String.slice reads a negative end as an offset from the end of the string. Instead of clipping the head to nothing, it keeps nearly the whole file name:

filterURLForDisplay( 'https://example.com/averylongfilename.png', 5 );
// actual:   'averylongfilename.…ame.png'   (26 characters, for maxLength 5)
// expected: '…ame.png'

So the smaller the maxLength, the longer the output — the opposite of the function's contract. maxLength: 8 already returns '…ame.png', so that is the shortest form this branch can produce.

Callers pass maxLength to fit a URL into a fixed-width UI (LinkControl's preview, for example), so an over-long return overflows the very layout the argument exists to protect.

How?

Clamps the head slice at zero with Math.max( 0, … ). Anything that was already producing a non-negative end is unaffected — the existing maxLength: 20 test still passes unchanged.

Adds a unit test for the tiny-maxLength case, which fails on trunk with the 26-character output above.

Testing Instructions

npm run test:unit -- packages/url

Testing Instructions for Keyboard

n/a — no interaction changes.

Use of AI Tools

AI tooling (Claude Code) was used to help find this defect and draft the fix and test. I confirmed the wrong output on trunk myself, verified the test fails before the change and passes after, and take responsibility for the code in this PR.

@github-actions github-actions Bot added the [Package] Url /packages/url label Aug 12, 2026
@github-actions

github-actions Bot commented Aug 12, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Kgupta62 <kuber8821@git.wordpress.org>
Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka Mamaduka added the [Type] Bug An existing feature does not function as intended label Aug 12, 2026
@im3dabasia

Copy link
Copy Markdown
Contributor

Thanks for contributing, @Kgupta62!

Same question as in #81527 (comment). Does this solve a real use case or consumer bug?

@Mamaduka

Copy link
Copy Markdown
Member

The missing clamp looks like an actual bug. String gets sliced unexpectedly with a negative value.

@im3dabasia, do you have any other concerns?

@im3dabasia

Copy link
Copy Markdown
Contributor

Thanks @Mamaduka!

Agreed, the negative slice is a real bug and the clamp is the right fix. No further concerns from me.

@Mamaduka

Copy link
Copy Markdown
Member

@Kgupta62, do you mind rebasing the branch and resolving merge conflicts? Then I think we can merge.

@Kgupta62
Kgupta62 force-pushed the fix/url-filter-max-length branch from f201086 to 70b473c Compare August 14, 2026 03:58
@Kgupta62

Copy link
Copy Markdown
Contributor Author

@Mamaduka rebased onto trunk and the conflict in packages/url/CHANGELOG.md is resolved — the entry now sits alongside the getQueryArgs one under Unreleased. packages/url unit tests pass locally (502/502). Ready when you are.

@Mamaduka
Mamaduka merged commit 9aa7406 into WordPress:trunk Aug 14, 2026
43 checks passed
@github-actions github-actions Bot added this to the Gutenberg 23.9 milestone Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Url /packages/url [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants