Make WordPress Core

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

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.

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

  1. 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.
  2. Special elements and comments: The regex incorrectly modified links inside HTML comments (), <script>, and <style> tags. WP_HTML_Tag_Processor automatically skips these contents safely.
  3. Empty / null attributes bug (#60391): In the previous implementation, passing an empty array to $attrs caused implode( '|', (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, converting target="_blank" into target="https://localhost/_blank"). The Tag Processor implementation correctly defaults empty or null attributes to array( 'src', 'href' ), preventing unintended attribute modifications.
  4. Attribute quoting and case-insensitivity: Handles unquoted, single-quoted, and double-quoted attributes, as well as uppercase tag and attribute names.
  5. No global state: Eliminates dependency on the global variable $_links_add_base.
  6. 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.php covering:
    • 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().
  • 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.
Note: See TracTickets for help on using tickets.