url: clamp the truncated file name slice in filterURLForDisplay - #81529
Conversation
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Thanks for contributing, @Kgupta62! Same question as in #81527 (comment). Does this solve a real use case or consumer bug? |
|
The missing clamp looks like an actual bug. String gets sliced unexpectedly with a negative value. @im3dabasia, do you have any other concerns? |
|
Thanks @Mamaduka! Agreed, the negative slice is a real bug and the clamp is the right fix. No further concerns from me. |
|
@Kgupta62, do you mind rebasing the branch and resolving merge conflicts? Then I think we can merge. |
f201086 to
70b473c
Compare
|
@Mamaduka rebased onto trunk and the conflict in |
What?
filterURLForDisplay()no longer returns a string far longer than themaxLengthit was given.Why?
In the branch that truncates a long file name, the head slice is:
truncatedFileis alwaysfileName.slice( -3 ) + '.' + extension— seven characters for a three-character extension. As soon asmaxLengthis at or below that, the second argument goes negative, andString.slicereads 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:So the smaller the
maxLength, the longer the output — the opposite of the function's contract.maxLength: 8already returns'…ame.png', so that is the shortest form this branch can produce.Callers pass
maxLengthto 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 existingmaxLength: 20test still passes unchanged.Adds a unit test for the tiny-
maxLengthcase, which fails on trunk with the 26-character output above.Testing Instructions
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.