Skip to content

Conversation

@adamziel
Copy link
Collaborator

@adamziel adamziel commented Oct 30, 2025

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.

cc @dmsnell

@adamziel adamziel added the enhancement New feature or request label Oct 30, 2025
@adamziel adamziel merged commit 046f5ef into css-processor Oct 31, 2025
22 checks passed
@adamziel adamziel deleted the css-modifier branch October 31, 2025 00:04
@adamziel adamziel restored the css-modifier branch October 31, 2025 00:21
adamziel added a commit that referenced this pull request Oct 31, 2025
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.

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

cc @dmsnell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants