You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[BlockMarkupURLProcessor] Support URLs in CSS (#195)
## Description
Adds support for migrating URLs within CSS syntax in the `style` HTML
attribute during WXR imports. For example, this markup:
```html
<div style="background-image:url(https://oldsite.com/image.png)">
```
Would be rewritten as:
```html
<div style="background-image:url(https://newsite.com/image.png)">
```
## Motivation
When importing WordPress sites via WXR, URLs embedded in CSS (like
`background: url("/old-site.com/image.jpg")`) need to be migrated to the
new site. Previously, these URLs were missed, leading to broken images
and assets after import.
[Cover blocks are a good
example](WordPress/wordpress-importer#223).
Without this PR, the background image in this cover block would not be
rewritten:
```html
<!-- wp:cover {"url":"http://localhost:8881/wp-content/image.jpg"}} -->
<div style="background-position:50% 50%;background-image:url(http://localhost:8881/wp-content/uploads/2025/09/image-2-766x1024.jpeg)">
```
### Implementation
The implementation introduces a new `CSSUrlProcessor` class that can
parse CSS `url()` functions, handle CSS escape sequences, and
efficiently skip over large data URIs. It uses the same design
principles as `WP_HTML_Tag_Processor`: simple state-machine API, no
regexps, minimal allocations. The `CSSUrlProcessor` is integrated with
`BlockMarkupURLProcessor` and can be used as follows:
```php
$markup = '<div style="background: url("/old.jpg")">Content</div>';
$processor = new BlockMarkupUrlProcessor( $markup, 'https://new-site.com' );
while ( $processor->next_url() ) {
// Finds "/old.jpg" in the style attribute
$processor->set_raw_url( '/new.jpg' );
}
echo $processor->get_updated_html();
// Output: <div style="background: url("/new.jpg")">Content</div>
```
## Testing instructions
* Review thoroughly
* Confirm the CI tests pass
0 commit comments