-
Notifications
You must be signed in to change notification settings - Fork 13
CSSProcessor::set_token_value( $new_value ) #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…lly compose values
Follows up on #197 by adding a `set_token_value( $new_value )` method to allow rewriting CSS. At the moment, it only supports updating the URL token value. ```php $css = 'background: url(old.jpg);'; $processor = CSSProcessor::create( $css ); while ( $processor->next_token() ) { if ( CSSProcessor::TOKEN_URL === $processor->get_token_type() ) { // URL with safe characters: letters, digits, hyphens, underscores, dots, slashes. $processor->set_token_value( "image😀.jpg ("special")" ); } } echo $processor->get_updated_css(); // background: url("image😀.jpg (\22 special\22 )"); ``` ## Implementation details `set_token_value( $new_value )` always uses the quoted URL syntax to encode the new URL. It only escapes quotes newline characters, backslashes, and double quotes. All other bytes are preserved as-is. cc @dmsnell
dmsnell
reviewed
Oct 31, 2025
| $is_valid_utf8 = mb_check_encoding( $blueprint_string, 'UTF-8' ); | ||
| } else { | ||
| $is_valid_utf8 = utf8_is_valid_byte_stream( $blueprint_string ); | ||
| $is_valid_utf8 = ! _wp_has_noncharacters_fallback( $blueprint_string ); |
Member
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wp_has_noncharacters() from utf8.php is indeed much faster. unless there is a reason you need them, I would suggest sticking to the non-private methods if you can.
adamziel
added a commit
that referenced
this pull request
Nov 1, 2025
…der (#200) Replaces two instances of the old UTF-8 decoding utilities with the new utf-8.php toolkit by @dmsnell: * https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/compat-utf8.php * https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/utf8.php This PR only touches two tactical usages of the old tools: * Blueprint validation now uses `wp_is_valid_utf8` * CSSProcessor now uses `wp_scrub_utf8` instead of `_wp_scrub_utf8_fallback` More refactoring is coming once there's a faster alternative to `_wp_scan_utf8`, see https://core.trac.wordpress.org/ticket/63863#comment:51 Related to #196. Follows up on #199 and #197. ## Testing instructions If the CI passes, we're good. Unicode-related scenarios are covered by tests.
adamziel
added a commit
to WordPress/wordpress-importer
that referenced
this pull request
Nov 4, 2025
Adds support for rewriting URLs inside CSS syntax, e.g. here: ```html <div style="background-image:url(/wp-content/uploads/2025/09/image-2-766x1024.jpeg)"> ``` Before this PR, the `style` attributes in, e.g., the cover block were skipped by the URL rewriter and continued pointing to the old site. Fixes #223 ## Implementation details This PR backports `CSSProcessor`, `CSSURLProcessor`, and a few related PRs around Unicode handling from the WordPress/php-toolkit repo: * WordPress/php-toolkit#197 * WordPress/php-toolkit#195 * WordPress/php-toolkit#199 * WordPress/php-toolkit#200 * WordPress/php-toolkit#201 * WordPress/php-toolkit#202 Note the CSSProcessor and CSSURLProcessor are tested against 300 test cases containing various tricky inputs, quoted and unquoted URLs, strings, comments, unicode escape sequences, and more. ## Testing instructions This PR comes with a new test case specifically for various tricky CSS inputs. You're also welcome to try and import a WXR file that contains an inline background-image reference and confirm the URL is correctly rewritten.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Follows up on #197 by adding a
set_token_value( $new_value )method to allow rewriting CSS. At the moment, it only supports updating the URL token value.Implementation details
set_token_value( $new_value )always uses the quoted URL syntax to encode the new URL. It only escapes quotes newline characters, backslashes, and double quotes. All other bytes are preserved as-is.PR #198 was supposed to merge this into trunk, but I never updated the base.
cc @dmsnell