Commit 5303dfb
authored
[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 pass1 parent 9cae1f9 commit 5303dfb
File tree
6 files changed
+1161
-38
lines changed- components/DataLiberation
- BlockMarkup
- CSS
- Tests
- URL
6 files changed
+1161
-38
lines changedLines changed: 98 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
| |||
23 | 21 | | |
24 | 22 | | |
25 | 23 | | |
| 24 | + | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
55 | 63 | | |
56 | 64 | | |
57 | 65 | | |
| |||
70 | 78 | | |
71 | 79 | | |
72 | 80 | | |
73 | | - | |
74 | | - | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
75 | 86 | | |
76 | 87 | | |
77 | 88 | | |
| |||
111 | 122 | | |
112 | 123 | | |
113 | 124 | | |
114 | | - | |
| 125 | + | |
115 | 126 | | |
116 | 127 | | |
117 | 128 | | |
| |||
130 | 141 | | |
131 | 142 | | |
132 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
133 | 183 | | |
134 | 184 | | |
135 | 185 | | |
136 | | - | |
137 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
138 | 194 | | |
139 | 195 | | |
140 | 196 | | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
147 | 213 | | |
148 | 214 | | |
149 | 215 | | |
| |||
160 | 226 | | |
161 | 227 | | |
162 | 228 | | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
163 | 241 | | |
164 | 242 | | |
165 | 243 | | |
| |||
277 | 355 | | |
278 | 356 | | |
279 | 357 | | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
280 | 364 | | |
281 | 365 | | |
282 | 366 | | |
| |||
Lines changed: 46 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
742 | 742 | | |
743 | 743 | | |
744 | 744 | | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
745 | 771 | | |
746 | 772 | | |
747 | 773 | | |
| |||
812 | 838 | | |
813 | 839 | | |
814 | 840 | | |
815 | | - | |
816 | | - | |
817 | | - | |
818 | | - | |
819 | | - | |
820 | | - | |
821 | | - | |
822 | | - | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
823 | 860 | | |
824 | | - | |
825 | | - | |
826 | | - | |
827 | | - | |
828 | | - | |
829 | | - | |
830 | | - | |
831 | | - | |
832 | | - | |
833 | | - | |
834 | | - | |
835 | | - | |
836 | 861 | | |
837 | 862 | | |
838 | 863 | | |
| |||
0 commit comments