Opened 3 years ago
Last modified 4 days ago
#60391 new defect (bug)
links_add_base_url will encode all attribut in null passed in
| Reported by: | pbearne | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Formatting | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
Change History (2)
This ticket was mentioned in PR #5982 on WordPress/wordpress-develop by @pbearne.
3 years ago
#1
- Keywords has-patch has-unit-tests added
This ticket was mentioned in PR #15843 on WordPress/wordpress-develop by @motylanogha.
4 days ago
#2
See also: https://core.trac.wordpress.org/ticket/65984
This PR refactors links_add_base_url() to use WP_HTML_Tag_Processor for standards-compliant, robust HTML parsing, resolving Trac tickets #60391 and contributing to the HTML API parsing improvements for 7.2 (#65984).
This complements the refactoring of its companion function links_add_target() in PR #14144. Both functions are invoked together in Core (e.g., in wp-admin/includes/plugin-install.php when preparing plugin readme sections).
Problems Solved
- Unwanted matches outside tags: The previous regex matched attribute syntax anywhere in plain text (e.g.,
<p>Check href="foo"</p>), modifying strings that were not inside HTML tags. - Special elements and comments: The regex incorrectly modified links inside HTML comments (
),<script>, and<style>tags.WP_HTML_Tag_Processorautomatically skips these contents safely. - Empty / null attributes bug (#60391): In the previous implementation, passing an empty array to
$attrscausedimplode( '|', (array) $attrs )to produce an empty string, resulting in a regex pattern of!(=(['"])(.+?)\2!i. This erroneously matched and transformed every attribute in the tag (for instance, convertingtarget="_blank"intotarget="https://localhost/_blank"). The Tag Processor implementation correctly defaults empty or null attributes toarray( 'src', 'href' ), preventing unintended attribute modifications. - Attribute quoting and case-insensitivity: Handles unquoted, single-quoted, and double-quoted attributes, as well as uppercase tag and attribute names.
- No global state: Eliminates dependency on the global variable
$_links_add_base. - Backward compatibility:
_links_add_base()is deprecated with_deprecated_function( __FUNCTION__, '7.2.0' )and retained for third-party compatibility.
Testing & Verification
- Added comprehensive tests in
tests/phpunit/tests/formatting/linksAddBaseUrl.phpcovering:- Default attributes (
src,href). - Custom attributes (
data-url). - Single string vs array attribute argument.
- Links inside HTML comments, script, and style blocks (ignored).
- Plain text matching attribute syntax outside tags (ignored).
- Empty array attributes (#60391).
- Allowed schemes (
mailto,tel,ftp,http,https). - Fragment identifiers (
#section) and relative query strings (?tab=faq). - Deprecation warning for
_links_add_base().
- Default attributes (
- All 2,278 formatting tests passed:
vendor/bin/phpunit --group formatting(1,118,623 assertions, 0 failures). - PHPCS passes with 0 errors:
vendor/bin/phpcs src/wp-includes/formatting.php tests/phpunit/tests/formatting/linksAddBaseUrl.php. - Full PHPStan passes with 0 errors:
composer phpstan.
The links_add_base_url function has been refactored to handle null attribute values without throwing errors. This change necessitated modifications within the test_links_add_base_url PHPUnit test, simplifying the test scenario to always include attribute values when calling the function.