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
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.
0 commit comments