Commit 1f9dac0
authored
Detect relative URLs in known block attributes (#188)
Gives `BlockMarkupUrlProcessor` knowledge about which block attributes
are designed to contain a relative URL.
Known URL attributes can be assumed to hold a URL and be parsed with the
base URL. For example, a `/about-us` value in a `wp:navigation-link`
block’s url attribute is a relative URL to the `/about-us` page.
Other attributes may or may not contain URLs, and we cannot assume they
do. A value like `/about-us` could be a relative URL or a class name. In
those cases we’ll ignore relative URLs and only detect absolute URLs to
avoid treating every string as a URL; this requires parsing without a
base URL.
Related to WordPress/wordpress-playground#1780
## Implementation details
This PR ships a list of all block attributes that are meant to hold a
URL. It's similar to how the HTML spec declares a list of all [HTML
attributes meant to hold a
URL](https://html.spec.whatwg.org/multipage/indices.html#attributes-1).
It also ships a filter the extenders can use to nudge the URL rewriter
to treat a block attribute as either:
```php
$is_relative_url_block_attribute = apply_filters(
'url_processor_is_relative_url_block_attribute',
$is_relative_url_block_attribute,
array(
'block_name' => $this->get_block_name(),
'attribute_key' => $this->get_block_attribute_key(),
)
)
```
A hypothetical plugin shipping a `wp:custom-image` block could nudge the
URL parser to treat its `src` attribute as a relative URL by registering
the following filter:
```php
<?php
function recognize_custom_image_block_attribute_as_relative_url ( $is_known, $context ) {
if (
'wp:custom-image' === $context['block_name'] &&
'src' === $context['attribute_key']
) {
return true;
}
return $is_known;
}
add_filter(
'url_processor_is_relative_url_block_attribute',
'recognize_custom_image_block_attribute_as_relative_url',
10,
2
);
```
### Other changes
This PR renames two constants for clarity:
* `URL_ATTRIBUTES` -> `HTML_ATTRIBUTES_TO_ACCEPT_RELATIVE_URLS_FROM`
* `URL_ATTRIBUTES_WITH_SUBSYNTAX` ->
`HTML_ATTRIBUTES_WITH_SUBSYNTAX_TO_ACCEPT_RELATIVE_URLS_FROM`
* `URL_CONTAINING_TAGS_WITH_SUBSYNTAX` ->
`HTML_TAGS_WITH_SUBSYNTAX_TO_ACCEPT_RELATIVE_URLS_FROM`
## Testing
CI – see the new tests shipped with this PR.1 parent 9196728 commit 1f9dac0
File tree
3 files changed
+140
-34
lines changed- components/DataLiberation
- BlockMarkup
- Tests
3 files changed
+140
-34
lines changedLines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
527 | 527 | | |
528 | 528 | | |
529 | 529 | | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
530 | 538 | | |
531 | 539 | | |
532 | 540 | | |
| |||
Lines changed: 90 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
135 | | - | |
| 135 | + | |
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
| |||
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
145 | | - | |
| 145 | + | |
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
| |||
184 | 184 | | |
185 | 185 | | |
186 | 186 | | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
193 | 230 | | |
194 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
195 | 246 | | |
196 | | - | |
197 | | - | |
198 | | - | |
| 247 | + | |
199 | 248 | | |
200 | | - | |
201 | | - | |
| 249 | + | |
| 250 | + | |
202 | 251 | | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
203 | 256 | | |
204 | 257 | | |
205 | 258 | | |
| |||
362 | 415 | | |
363 | 416 | | |
364 | 417 | | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
365 | 438 | | |
366 | 439 | | |
367 | 440 | | |
| |||
370 | 443 | | |
371 | 444 | | |
372 | 445 | | |
373 | | - | |
| 446 | + | |
374 | 447 | | |
375 | 448 | | |
376 | 449 | | |
| |||
405 | 478 | | |
406 | 479 | | |
407 | 480 | | |
408 | | - | |
| 481 | + | |
409 | 482 | | |
410 | 483 | | |
411 | 484 | | |
| |||
425 | 498 | | |
426 | 499 | | |
427 | 500 | | |
428 | | - | |
| 501 | + | |
429 | 502 | | |
430 | 503 | | |
431 | 504 | | |
| |||
Lines changed: 42 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | | - | |
32 | | - | |
33 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
34 | 37 | | |
35 | | - | |
| 38 | + | |
36 | 39 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | 40 | | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 41 | + | |
46 | 42 | | |
47 | 43 | | |
48 | 44 | | |
| 45 | + | |
49 | 46 | | |
50 | 47 | | |
51 | 48 | | |
52 | 49 | | |
| 50 | + | |
53 | 51 | | |
54 | 52 | | |
55 | 53 | | |
56 | 54 | | |
| 55 | + | |
57 | 56 | | |
58 | 57 | | |
59 | 58 | | |
60 | 59 | | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| 70 | + | |
69 | 71 | | |
70 | | - | |
| 72 | + | |
71 | 73 | | |
72 | 74 | | |
73 | 75 | | |
| 76 | + | |
74 | 77 | | |
75 | 78 | | |
76 | 79 | | |
77 | 80 | | |
78 | 81 | | |
| 82 | + | |
79 | 83 | | |
80 | 84 | | |
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
84 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
85 | 109 | | |
86 | 110 | | |
87 | 111 | | |
| |||
180 | 204 | | |
181 | 205 | | |
182 | 206 | | |
183 | | - | |
| 207 | + | |
184 | 208 | | |
185 | 209 | | |
186 | 210 | | |
| |||
204 | 228 | | |
205 | 229 | | |
206 | 230 | | |
| 231 | + | |
207 | 232 | | |
208 | 233 | | |
209 | | - | |
| 234 | + | |
210 | 235 | | |
211 | 236 | | |
212 | 237 | | |
| |||
0 commit comments