Skip to content

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented Sep 17, 2025

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.

It also ships a filter the extenders can use to nudge the URL rewriter to treat a block attribute as either:

$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
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.

@adamziel adamziel marked this pull request as ready for review September 18, 2025 09:27
@adamziel adamziel added the enhancement New feature or request label Sep 18, 2025
@adamziel adamziel changed the title Replace relative URLs in known block attributes Detect relative URLs in known block attributes Sep 18, 2025
@adamziel adamziel merged commit 1f9dac0 into trunk Sep 18, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant