Make WordPress Core

Opened 2 weeks ago

Last modified 2 weeks ago

#65129 new enhancement

Improve maintainability of DOCTYPE quirks mode detection in `WP_HTML_Doctype_Info` class

Reported by: baikaresandeep007's profile baikare.sandeep007 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.7
Component: HTML API Keywords: has-patch needs-testing close
Focuses: coding-standards Cc:

Description (last modified by sabernhardt)

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

Attachments (1)

65129.patch (8.4 KB) - added by baikare.sandeep007 2 weeks ago.

Download all attachments as: .zip

Change History (5)

This ticket was mentioned in PR #11645 on WordPress/wordpress-develop by @baikare.sandeep007.


2 weeks ago
#1

Trac ticket: https://core.trac.wordpress.org/ticket/65129

## Use of AI Tools

#2 @sabernhardt
2 weeks ago

  • Description modified (diff)
  • Version changed from trunk to 6.7

([58925] added the WP_HTML_Doctype_Info class.)

#3 @westonruter
2 weeks ago

  • Focuses performance removed

#4 @jonsurrell
2 weeks ago

  • Keywords close added

Hi @baikaresandeep007, thanks for your interest in the HTML API and this contribution!

These seems to be a refactor with no intentional behavior changes. The arguments in favor of this refactor are fairly subjective and I don't think change meets the guidelines around refactoring. The performance impact is unclear and the PR notes that this is unlikely to be a hotspot. The impact on maintainability of this block also seems highly subjective.

I'll recommend this for closure but leave it open for feedback or other opinions.

Note: See TracTickets for help on using tickets.