Skip to content

Conversation

@adamziel
Copy link
Collaborator

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.

$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.

PR #198 was supposed to merge this into trunk, but I never updated the base.

cc @dmsnell

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
@adamziel adamziel merged commit 42d4c2e into trunk Oct 31, 2025
22 checks passed
@adamziel adamziel deleted the css-processor branch October 31, 2025 00:23
$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 );
Copy link
Member

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants