Make WordPress Core


Ignore:
Timestamp:
06/08/2024 10:55:55 AM (7 months ago)
Author:
dmsnell
Message:

HTML API: Return subclass from ::create_fragment

When the WP_HTML_Processor was introduced with its ::create_fragment()
static creator method, that method has been returning a new self(...).
Unfortunately, this means that subclasses cannot use that method since it
will return the WP_HTML_Processor instead of the subclass.

With this patch, the static creator method returns new static(...) to preserve
the intended behavior. A new test asserts this behavior for future changes.

Developed in https://github.com/WordPress/wordpress-develop/pull/6729
Discussed in https://core.trac.wordpress.org/ticket/61374

Props dmsnell, jonsurrell.
Follow-up to [56274].
Fixes #61374.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-processor.php

    r58304 r58363  
    277277     *
    278278     * @since 6.4.0
     279     * @since 6.6.0 Returns `static` instead of `self` so it can create subclass instances.
    279280     *
    280281     * @param string $html     Input HTML fragment to process.
    281282     * @param string $context  Context element for the fragment, must be default of `<body>`.
    282283     * @param string $encoding Text encoding of the document; must be default of 'UTF-8'.
    283      * @return WP_HTML_Processor|null The created processor if successful, otherwise null.
     284     * @return static|null The created processor if successful, otherwise null.
    284285     */
    285286    public static function create_fragment( $html, $context = '<body>', $encoding = 'UTF-8' ) {
     
    288289        }
    289290
    290         $processor                        = new self( $html, self::CONSTRUCTOR_UNLOCK_CODE );
     291        $processor                        = new static( $html, self::CONSTRUCTOR_UNLOCK_CODE );
    291292        $processor->state->context_node   = array( 'BODY', array() );
    292293        $processor->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY;
Note: See TracChangeset for help on using the changeset viewer.