Skip to content

CSSProcessor::set_token_value( $new_value ) - #198

Merged
adamziel merged 5 commits into
css-processorfrom
css-modifier
Oct 31, 2025
Merged

CSSProcessor::set_token_value( $new_value )#198
adamziel merged 5 commits into
css-processorfrom
css-modifier

Conversation

@adamziel

@adamziel adamziel commented Oct 30, 2025

Copy link
Copy Markdown
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.

cc @dmsnell

@adamziel adamziel added the enhancement New feature or request label Oct 30, 2025
Comment thread components/DataLiberation/URL/class-cssprocessor.php Outdated
@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
@sirreal
sirreal deleted the css-modifier branch April 8, 2026 08:28
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.

1 participant