Make WordPress Core


Ignore:
Timestamp:
12/13/2023 05:51:42 PM (3 years ago)
Author:
Bernhard Reiter
Message:

HTML API: Add support for H1-H6 elements in the HTML Processor.

Previously these have been unsupported, but in this patch, support is added for the tags so that the HTML Processor can process documents containing them.

There was a design discussion about introducing a constant to communicate "any of the H1 - H6 elements" but this posed a number of challenges that don't need to be answered in this patch. For the time being, because the HTML specification treats H1 - H6 specially as a single kind of element, the HTML Processor uses an internal hard-coded string to indicate this. By using a hard-coded string it's possible to avoid introducing a class constant which cannot be made private due to PHP's class design. In the future, this will probably appear as a special constant in a new constant-containing class.

Props dmsnell, jonsurrell.
Fixes #60060.

File:
1 edited

Legend:

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

    r57115 r57186  
    103103 *  - Form elements: BUTTON, FIELDSET, SEARCH.
    104104 *  - Formatting elements: B, BIG, CODE, EM, FONT, I, SMALL, STRIKE, STRONG, TT, U.
    105  *  - Heading elements: HGROUP.
     105 *  - Heading elements: H1, H2, H3, H4, H5, H6, HGROUP.
    106106 *  - Links: A.
    107107 *  - Lists: DL.
     
    696696                                }
    697697                                $this->state->stack_of_open_elements->pop_until( $tag_name );
     698                                return true;
     699
     700                        /*
     701                         * > A start tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6"
     702                         */
     703                        case '+H1':
     704                        case '+H2':
     705                        case '+H3':
     706                        case '+H4':
     707                        case '+H5':
     708                        case '+H6':
     709                                if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) {
     710                                        $this->close_a_p_element();
     711                                }
     712
     713                                if (
     714                                        in_array(
     715                                                $this->state->stack_of_open_elements->current_node()->node_name,
     716                                                array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ),
     717                                                true
     718                                        )
     719                                ) {
     720                                        // @TODO: Indicate a parse error once it's possible.
     721                                        $this->state->stack_of_open_elements->pop();
     722                                }
     723
     724                                $this->insert_html_element( $this->state->current_token );
     725                                return true;
     726
     727                        /*
     728                         * > An end tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6"
     729                         */
     730                        case '-H1':
     731                        case '-H2':
     732                        case '-H3':
     733                        case '-H4':
     734                        case '-H5':
     735                        case '-H6':
     736                                if ( ! $this->state->stack_of_open_elements->has_element_in_scope( '(internal: H1 through H6 - do not use)' ) ) {
     737                                        /*
     738                                         * This is a parse error; ignore the token.
     739                                         *
     740                                         * @TODO: Indicate a parse error once it's possible.
     741                                         */
     742                                        return $this->step();
     743                                }
     744
     745                                $this->generate_implied_end_tags();
     746
     747                                if ( $this->state->stack_of_open_elements->current_node()->node_name !== $tag_name ) {
     748                                        // @TODO: Record parse error: this error doesn't impact parsing.
     749                                }
     750
     751                                $this->state->stack_of_open_elements->pop_until( '(internal: H1 through H6 - do not use)' );
    698752                                return true;
    699753
Note: See TracChangeset for help on using the changeset viewer.