Make WordPress Core


Ignore:
Timestamp:
05/24/2024 01:19:10 AM (11 months ago)
Author:
dmsnell
Message:

HTML API: Add expects_closer() method to HTML Processor

This patch adds a new method, WP_HTML_Processor->expects_closer() to indicate
if the currently-matched node expects to find a closing token. For example, a
DIV element expects a closing </div> tag, but an <img> expects none, because
it's a void element. Similarly, #text nodes and HTML comments only appear as
unitary nodes on the stack of open elements. Once proceeding further in the
document they are immediately removed without any closing tag.

This new method serves as a helper to indicate whether or not to expect the
closer, as this can be more complicated than it seems, and calling code
shouldn't have to build custom interpretations and implementations. Instead,
the HTML Processor ought to export its internal knowledge to make it easy for
consuming code and projects.

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

Fixes #61257.
Props dmsnell, jonsurrell.

File:
1 edited

Legend:

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

    r58191 r58192  
    507507
    508508        return false;
     509    }
     510
     511    /**
     512     * Indicates if the currently-matched node expects a closing
     513     * token, or if it will self-close on the next step.
     514     *
     515     * Most HTML elements expect a closer, such as a P element or
     516     * a DIV element. Others, like an IMG element are void and don't
     517     * have a closing tag. Special elements, such as SCRIPT and STYLE,
     518     * are treated just like void tags. Text nodes and self-closing
     519     * foreign content will also act just like a void tag, immediately
     520     * closing as soon as the processor advances to the next token.
     521     *
     522     * @since 6.6.0
     523     *
     524     * @todo When adding support for foreign content, ensure that
     525     *       this returns false for self-closing elements in the
     526     *       SVG and MathML namespace.
     527     *
     528     * @return bool Whether to expect a closer for the currently-matched node,
     529     *              or `null` if not matched on any token.
     530     */
     531    public function expects_closer() {
     532        $token_name = $this->get_token_name();
     533        if ( ! isset( $token_name ) ) {
     534            return null;
     535        }
     536
     537        return ! (
     538            // Comments, text nodes, and other atomic tokens.
     539            '#' === $token_name[0] ||
     540            // Doctype declarations.
     541            'html' === $token_name ||
     542            // Void elements.
     543            self::is_void( $token_name ) ||
     544            // Special atomic elements.
     545            in_array( $token_name, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true )
     546        );
    509547    }
    510548
Note: See TracChangeset for help on using the changeset viewer.