[wp-trac] [WordPress Trac] #65129: Improve maintainability of DOCTYPE quirks mode detection in `WP_HTML_Doctype_Info` class

WordPress Trac noreply at wordpress.org
Sat Apr 25 19:49:02 UTC 2026


#65129: Improve maintainability of DOCTYPE quirks mode detection in
`WP_HTML_Doctype_Info` class
------------------------------+--------------------------------------------
 Reporter:                    |       Owner:  (none)
  baikare.sandeep007          |
     Type:  enhancement       |      Status:  new
 Priority:  normal            |   Milestone:  Awaiting Review
Component:  HTML API          |     Version:  6.7
 Severity:  normal            |  Resolution:
 Keywords:  has-patch needs-  |     Focuses:  performance, coding-standards
  testing                     |
------------------------------+--------------------------------------------
Changes (by sabernhardt):

 * version:  trunk => 6.7


Old description:

> = Improve maintainability of DOCTYPE quirks mode detection in
> `WP_HTML_Doctype_Info` class =
>
> == Description
>
> This patch refactors the DOCTYPE quirks mode detection logic in
> `WP_HTML_Doctype_Info` by extracting the large list of legacy public
> identifier prefixes into a separate array and using a `foreach` loop
> instead of a monolithic `if` condition with multiple `str_starts_with()`
> calls chained with `||` operators.
>
> == Current Behavior
>
> The `determine_quirks_mode_from_public_identifier()` method currently
> contains a single `if` statement with over **70** conditions chained
> together using logical OR operators:
>
> {{{#!php
> <?php
> if (
>     str_starts_with( $public_identifier, '+//silmaril//dtd html pro v0r11
> 19970101//' ) ||
>     str_starts_with( $public_identifier, '-//as//dtd html 3.0 aswedit +
> extensions//' ) ||
>     // ... 70+ more conditions
> ) {
>     $this->indicated_compatibility_mode = 'quirks';
>     return;
> }
> }}}
>

> == Issues with Current Approach
>
> * Poor maintainability – Adding, removing, or modifying prefixes requires
> editing a long, sprawling conditional statement
> * Reduced readability – The logic flow is obscured by the sheer number of
> conditions
> * Difficult to test – Individual prefixes cannot be easily referenced or
> validated in isolation
> * Code duplication – The same pattern of str_starts_with() calls is
> repeated for each prefix
>
> == Proposed Changes
>
> This patch replaces the large conditional block with:
>
> * A single source of truth – All legacy quirks mode prefixes are now
> stored in a dedicated array $quirks_prefixes
> * Clean iteration – A foreach loop checks each prefix using
> str_starts_with()
> * Early return – When a match is found, the compatibility mode is set and
> the method exits immediately
>
> == Impact
> * No functional changes – The exact same set of public identifiers
> triggers quirks mode
> * Backward compatible – Behavior remains identical for all DOCTYPE
> declarations
> * Performance neutral – The loop iterates through the same number of
> checks as before
> * Improves code quality – Makes the HTML API more maintainable for future
> contributors

New description:

 This patch refactors the `DOCTYPE` quirks mode detection logic in
 `WP_HTML_Doctype_Info` by extracting the large list of legacy public
 identifier prefixes into a separate array and using a `foreach` loop
 instead of a monolithic `if` condition with multiple `str_starts_with()`
 calls chained with `||` operators.

 ==== Current Behavior

 The `determine_quirks_mode_from_public_identifier()` method currently
 contains a single `if` statement with over **70** conditions chained
 together using logical OR operators:

 {{{#!php
 <?php
 if (
     str_starts_with( $public_identifier, '+//silmaril//dtd html pro v0r11
 19970101//' ) ||
     str_starts_with( $public_identifier, '-//as//dtd html 3.0 aswedit +
 extensions//' ) ||
     // ... 70+ more conditions
 ) {
     $this->indicated_compatibility_mode = 'quirks';
     return;
 }
 }}}

 ==== Issues with Current Approach

 * Poor maintainability – Adding, removing, or modifying prefixes requires
 editing a long, sprawling conditional statement
 * Reduced readability – The logic flow is obscured by the sheer number of
 conditions
 * Difficult to test – Individual prefixes cannot be easily referenced or
 validated in isolation
 * Code duplication – The same pattern of `str_starts_with()` calls is
 repeated for each prefix

 ==== Proposed Changes

 This patch replaces the large conditional block with:

 * A single source of truth – All legacy quirks mode prefixes are now
 stored in a dedicated array `$quirks_prefixes`
 * Clean iteration – A `foreach` loop checks each prefix using
 `str_starts_with()`
 * Early return – When a match is found, the compatibility mode is set and
 the method exits immediately

 ==== Impact
 * No functional changes – The exact same set of public identifiers
 triggers quirks mode
 * Backward compatible – Behavior remains identical for all `DOCTYPE`
 declarations
 * Performance neutral – The loop iterates through the same number of
 checks as before
 * Improves code quality – Makes the HTML API more maintainable for future
 contributors

--

Comment:

 ([58925] added the `WP_HTML_Doctype_Info` class.)

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/65129#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list