Detect relative URLs in known block attributes #188
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 awp: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.
It also ships a filter the extenders can use to nudge the URL rewriter to treat a block attribute as either:
A hypothetical plugin shipping a
wp:custom-image
block could nudge the URL parser to treat itssrc
attribute as a relative URL by registering the following filter: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.