#64574 closed defect (bug) (fixed)
Regression in `wp.sanitize.stripTags()` in WordPress 7.0-alpha
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | normal | Version: | trunk |
| Component: | General | Keywords: | has-patch has-unit-tests |
| Focuses: | javascript | Cc: |
Description
wp.sanitize.stripTags() doesn't return the same value when given null.
With WordPress 6.9:
console.log( 'testStripTagsWithNull', wp.sanitize.stripTags( null ) ); // "" console.log( 'typeofResult', typeof wp.sanitize.stripTags( null ) ); // string
With WordPress 7.0-alpha:
console.log( 'testStripTagsWithNull', wp.sanitize.stripTags( null ) ); // "null" console.log( 'typeofResult', typeof wp.sanitize.stripTags( null ) ); // string
Leading to unexpected behavior in wp.sanitize.stripTagsAndEncodeText() as well...
I think the regression has been introduced in https://github.com/WordPress/wordpress-develop/commit/3865859fd8982575204bc04e77197ea4c743ccf5.
Change History (16)
#1
@
4 months ago
- Milestone changed from Awaiting Review to 7.0
- Owner set to westonruter
- Status changed from new to accepted
This ticket was mentioned in PR #10833 on WordPress/wordpress-develop by @westonruter.
4 months ago
#2
- Keywords has-patch has-unit-tests added
#3
@
4 months ago
- Version changed from trunk to 6.9
@hugod Thanks for reporting this.
This indeed was introduced in [60907] to fix #48054.
I have an initial PR to fix the issue here available for testing and review: https://github.com/WordPress/wordpress-develop/pull/10833
@mukesh27 commented on PR #10833:
3 months ago
#5
@westonruter Can you share testing steps please?
@westonruter commented on PR #10833:
3 months ago
#6
@mukeshpanchal27 the ticket description has the reproduction info. I guess go to a screen where that script is enqueued, or enqueued the script yourself, and then run the function in the console.
This ticket was mentioned in PR #10856 on WordPress/wordpress-develop by @westonruter.
3 months ago
#8
Follow-up to:
- @sirreal: https://github.com/WordPress/wordpress-develop/pull/10833#discussion_r2758059447
- @Hug0-Drelon: https://github.com/WordPress/wordpress-develop/pull/10833#discussion_r2757723749
Trac ticket: https://core.trac.wordpress.org/ticket/64574
This ticket was mentioned in PR #10994 on WordPress/wordpress-develop by @westonruter.
3 months ago
#11
See https://github.com/WordPress/wordpress-develop/pull/10833#discussion_r2833992103:
I’m late to the game, but do we expect any other type errors? I suppose we expect people to send things that are strings nominally, but may be missing.
However, if we make a _positive assertion_ we wouldn’t have to chase this later.
if ( 'string' !== typeof $text ) { return ''; }
It turns out that passing a non-string in 6.9 would cause an error due to the replace() method not being available on it (probably). So the previous change in r61578 which added support for sanitizing numbers actually would have previously caused an error. So we can indeed just short-circuit when a non-string is passed.
Trac ticket: https://core.trac.wordpress.org/ticket/64574
## Use of AI Tools
None
@westonruter commented on PR #10994:
3 months ago
#12
@Hug0-Drelon How does this work for you?
@hugod commented on PR #10994:
3 months ago
#13
@westonruter LGTM 👍
@jonsurrell commented on PR #10994:
3 months ago
#14
let _text = text || '';
As such, numbers (aside from 0) would error because of the missing .replace() method and don't need to be handled here.
@westonruter commented on PR #10994:
3 months ago
#16
Committed in r61783 (7e46834)
…
Ensure that
wp.sanitize.stripTags()returns an empty string when the input isnullorundefined, preventing it from being converted to the string "null" byDOMParser.Added regression tests.
Trac ticket: https://core.trac.wordpress.org/ticket/64574