Make WordPress Core

Changeset 56790


Ignore:
Timestamp:
10/05/2023 10:40:48 PM (16 months ago)
Author:
SergeyBiryukov
Message:

HTML API: Rename WP_HTML_Processor::createFragment() to follow WPCS.

WP_HTML_Processor::create_fragment() is the correct method name as per the WordPress PHP coding standards.

Follow-up to [56274].

Props dmsnell, jrf, hellofromTonya, SergeyBiryukov.
Fixes #59547.

Location:
trunk
Files:
6 edited

Legend:

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

    r56702 r56790  
    4040 * Example:
    4141 *
    42  *     $processor = WP_HTML_Processor::createFragment( $html );
     42 *     $processor = WP_HTML_Processor::create_fragment( $html );
    4343 *     if ( $processor->next_tag( array( 'breadcrumbs' => array( 'DIV', 'FIGURE', 'IMG' ) ) ) ) {
    4444 *         $processor->add_class( 'responsive-image' );
     
    5959 * when parsed, the return value from `get_breadcrumbs()` will always
    6060 * contain any implicit outermost elements. For example, when parsing
    61  * with `createFragment()` in the `BODY` context (the default), any
     61 * with `create_fragment()` in the `BODY` context (the default), any
    6262 * tag in the given HTML document will contain `array( 'HTML', 'BODY', … )`
    6363 * in its breadcrumbs.
     
    240240     * @return WP_HTML_Processor|null The created processor if successful, otherwise null.
    241241     */
    242     public static function createFragment( $html, $context = '<body>', $encoding = 'UTF-8' ) {
     242    public static function create_fragment( $html, $context = '<body>', $encoding = 'UTF-8' ) {
    243243        if ( '<body>' !== $context || 'UTF-8' !== $encoding ) {
    244244            return null;
     
    281281     * @since 6.4.0
    282282     *
    283      * @see WP_HTML_Processor::createFragment()
     283     * @see WP_HTML_Processor::create_fragment()
    284284     *
    285285     * @param string      $html                                  HTML to process.
     
    293293                __METHOD__,
    294294                sprintf(
    295                     /* translators: %s: WP_HTML_Processor::createFragment. */
     295                    /* translators: %s: WP_HTML_Processor::create_fragment(). */
    296296                    __( 'Call %s to create an HTML Processor instead of calling the constructor directly.' ),
    297                     '<code>WP_HTML_Processor::createFragment</code>'
     297                    '<code>WP_HTML_Processor::create_fragment()</code>'
    298298                ),
    299299                '6.4.0'
     
    325325     * Example
    326326     *
    327      *     $processor = WP_HTML_Processor::createFragment( '<template><strong><button><em><p><em>' );
     327     *     $processor = WP_HTML_Processor::create_fragment( '<template><strong><button><em><p><em>' );
    328328     *     false === $processor->next_tag();
    329329     *     WP_HTML_Processor::ERROR_UNSUPPORTED === $processor->get_last_error();
     
    429429     * Example:
    430430     *
    431      *     $processor = WP_HTML_Processor::createFragment( '<div><span><figure><img></figure></span></div>' );
     431     *     $processor = WP_HTML_Processor::create_fragment( '<div><span><figure><img></figure></span></div>' );
    432432     *     $processor->next_tag( 'img' );
    433433     *     true  === $processor->matches_breadcrumbs( array( 'figure', 'img' ) );
     
    558558     * Example
    559559     *
    560      *     $processor = WP_HTML_Processor::createFragment( '<p><strong><em><img></em></strong></p>' );
     560     *     $processor = WP_HTML_Processor::create_fragment( '<p><strong><em><img></em></strong></p>' );
    561561     *     $processor->next_tag( 'IMG' );
    562562     *     $processor->get_breadcrumbs() === array( 'HTML', 'BODY', 'P', 'STRONG', 'EM', 'IMG' );
     
    14401440     * @access private
    14411441     */
    1442     const CONSTRUCTOR_UNLOCK_CODE = 'Use WP_HTML_Processor::createFragment instead of calling the class constructor directly.';
     1442    const CONSTRUCTOR_UNLOCK_CODE = 'Use WP_HTML_Processor::create_fragment() instead of calling the class constructor directly.';
    14431443}
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessor.php

    r56493 r56790  
    5353     */
    5454    public function test_get_tag_is_null_once_document_is_finished() {
    55         $p = WP_HTML_Processor::createFragment( '<div class="test">Test</div>' );
     55        $p = WP_HTML_Processor::create_fragment( '<div class="test">Test</div>' );
    5656        $p->next_tag();
    5757        $this->assertSame( 'DIV', $p->get_tag() );
     
    7070     */
    7171    public function test_stops_processing_after_unsupported_elements() {
    72         $p = WP_HTML_Processor::createFragment( '<p><x-not-supported></p><p></p>' );
     72        $p = WP_HTML_Processor::create_fragment( '<p><x-not-supported></p><p></p>' );
    7373        $p->next_tag( 'P' );
    7474        $this->assertFalse( $p->next_tag(), 'Stepped into a tag after encountering X-NOT-SUPPORTED element when it should have aborted.' );
     
    9696     */
    9797    public function test_clear_to_navigate_after_seeking() {
    98         $p = WP_HTML_Processor::createFragment( '<div one><strong></strong></div><p><strong two></strong></p>' );
     98        $p = WP_HTML_Processor::create_fragment( '<div one><strong></strong></div><p><strong two></strong></p>' );
    9999
    100100        while ( $p->next_tag() ) {
     
    145145     */
    146146    public function test_fails_to_reconstruct_formatting_elements() {
    147         $p = WP_HTML_Processor::createFragment( '<p><em>One<p><em>Two<p><em>Three<p><em>Four' );
     147        $p = WP_HTML_Processor::create_fragment( '<p><em>One<p><em>Two<p><em>Three<p><em>Four' );
    148148
    149149        $this->assertTrue( $p->next_tag( 'EM' ), 'Could not find first EM.' );
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php

    r56702 r56790  
    2424     */
    2525    public function test_navigates_into_normative_html_for_supported_elements( $html, $tag_name ) {
    26         $p = WP_HTML_Processor::createFragment( $html );
     26        $p = WP_HTML_Processor::create_fragment( $html );
    2727
    2828        $this->assertTrue( $p->step(), "Failed to step into supported {$tag_name} element." );
     
    8686     */
    8787    public function test_fails_when_encountering_unsupported_tag( $html ) {
    88         $p = WP_HTML_Processor::createFragment( $html );
     88        $p = WP_HTML_Processor::create_fragment( $html );
    8989
    9090        $this->assertFalse( $p->step(), "Should not have stepped into unsupported {$p->get_tag()} element." );
     
    237237     */
    238238    public function test_fails_when_encountering_unsupported_markup( $html, $description ) {
    239         $p = WP_HTML_Processor::createFragment( $html );
     239        $p = WP_HTML_Processor::create_fragment( $html );
    240240
    241241        while ( $p->step() && null === $p->get_attribute( 'supported' ) ) {
     
    278278     */
    279279    public function test_finds_correct_tag_given_breadcrumbs( $html, $breadcrumbs, $n ) {
    280         $p = WP_HTML_Processor::createFragment( $html );
     280        $p = WP_HTML_Processor::create_fragment( $html );
    281281
    282282        $p->next_tag(
     
    304304     */
    305305    public function test_reports_correct_breadcrumbs_for_html( $html, $breadcrumbs, $ignored_n ) {
    306         $p = WP_HTML_Processor::createFragment( $html );
     306        $p = WP_HTML_Processor::create_fragment( $html );
    307307
    308308        while ( $p->next_tag() && null === $p->get_attribute( 'target' ) ) {
     
    363363     */
    364364    public function test_reports_if_tag_matches_breadcrumbs_of_various_specificity( $html_with_target_node, $breadcrumbs, $should_match ) {
    365         $processor = WP_HTML_Processor::createFragment( $html_with_target_node );
     365        $processor = WP_HTML_Processor::create_fragment( $html_with_target_node );
    366366        while ( $processor->next_tag() && null === $processor->get_attribute( 'target' ) ) {
    367367            continue;
     
    421421     */
    422422    public function test_can_modify_attributes_after_finding_tag() {
    423         $p = WP_HTML_Processor::createFragment( '<div><figure><img><figcaption>test</figcaption></figure>' );
     423        $p = WP_HTML_Processor::create_fragment( '<div><figure><img><figcaption>test</figcaption></figure>' );
    424424
    425425        $this->assertTrue( $p->next_tag( array( 'breadcrumbs' => array( 'figcaption' ) ) ), 'Unable to find given tag.' );
     
    439439     */
    440440    public function test_can_query_an_element_by_tag_name() {
    441         $p = WP_HTML_Processor::createFragment( '<div><DIV><strong><img></strong></DIV>' );
     441        $p = WP_HTML_Processor::create_fragment( '<div><DIV><strong><img></strong></DIV>' );
    442442        $p->next_tag( 'IMG' );
    443443        $p->set_attribute( 'loading', 'lazy' );
     
    456456     */
    457457    public function test_can_seek_back_and_forth() {
    458         $p = WP_HTML_Processor::createFragment( '<div><p one><div><p><div two><p><div><p><div><p three>' );
     458        $p = WP_HTML_Processor::create_fragment( '<div><p one><div><p><div two><p><div><p><div><p three>' );
    459459
    460460        // Find first tag of interest.
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php

    r56380 r56790  
    2828     */
    2929    public function test_in_body_skips_unexpected_button_closer() {
    30         $p = WP_HTML_Processor::createFragment( '<div>Test</button></div>' );
     30        $p = WP_HTML_Processor::create_fragment( '<div>Test</button></div>' );
    3131
    3232        $p->step();
     
    5353     */
    5454    public function test_in_body_button_with_no_button_in_scope() {
    55         $p = WP_HTML_Processor::createFragment( '<div><p>Click the button <button one>here</button>!</p></div><button two>not here</button>' );
     55        $p = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>here</button>!</p></div><button two>not here</button>' );
    5656
    5757        $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
     
    8080     */
    8181    public function test_in_body_button_with_button_in_scope_as_parent() {
    82         $p = WP_HTML_Processor::createFragment( '<div><p>Click the button <button one>almost<button two>here</button>!</p></div><button three>not here</button>' );
     82        $p = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>almost<button two>here</button>!</p></div><button three>not here</button>' );
    8383
    8484        $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
     
    115115     */
    116116    public function test_in_body_button_with_button_in_scope_as_ancestor() {
    117         $p = WP_HTML_Processor::createFragment( '<div><button one><p>Click the button <span><button two>here</button>!</span></p></div><button three>not here</button>' );
     117        $p = WP_HTML_Processor::create_fragment( '<div><button one><p>Click the button <span><button two>here</button>!</span></p></div><button three>not here</button>' );
    118118
    119119        // This button finds itself normally nesting inside the DIV.
     
    150150     */
    151151    public function test_in_body_any_other_end_tag_with_unclosed_special_element() {
    152         $p = WP_HTML_Processor::createFragment( '<div><span><p></span><div>' );
     152        $p = WP_HTML_Processor::create_fragment( '<div><span><p></span><div>' );
    153153
    154154        $p->next_tag( 'P' );
     
    173173     */
    174174    public function test_in_body_any_other_end_tag_with_unclosed_non_special_element() {
    175         $p = WP_HTML_Processor::createFragment( '<div><span><code></span><div>' );
     175        $p = WP_HTML_Processor::create_fragment( '<div><span><code></span><div>' );
    176176
    177177        $p->next_tag( 'CODE' );
  • trunk/tests/phpunit/tests/html-api/wpHtmlSupportRequiredHtmlProcessor.php

    r56331 r56790  
    4343     */
    4444    private function ensure_support_is_added_everywhere( $tag_name ) {
    45         $p = WP_HTML_Processor::createFragment( "<$tag_name>" );
     45        $p = WP_HTML_Processor::create_fragment( "<$tag_name>" );
    4646
    4747        $this->assertFalse( $p->step(), "Must support terminating elements in specific scope check before adding support for the {$tag_name} element." );
  • trunk/tests/phpunit/tests/html-api/wpHtmlSupportRequiredOpenElements.php

    r56380 r56790  
    4545     */
    4646    private function ensure_support_is_added_everywhere( $tag_name ) {
    47         $p = WP_HTML_Processor::createFragment( "<$tag_name>" );
     47        $p = WP_HTML_Processor::create_fragment( "<$tag_name>" );
    4848
    4949        $this->assertFalse( $p->step(), "Must support terminating elements in specific scope check before adding support for the {$tag_name} element." );
Note: See TracChangeset for help on using the changeset viewer.