Make WordPress Core


Ignore:
Timestamp:
08/07/2025 08:31:07 AM (15 months ago)
Author:
jonsurrell
Message:

HTML API: Reduce length checks in skip_script_data.

Apply an optimization to remove several repeated string length checks in WP_HTML_Tag_Processor::skip_script_data().

Developed in ​https://github.com/WordPress/wordpress-develop/pull/9230.

Props jonsurrell, dmsnell.
See #63738.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

    r59464 r60617  
    19791979                                '<script class="d-md-none"><!--<script>--><scRipt><span><!--<span><Script</script>--></scripT><div></div>-->',
    19801980                        ),
     1981                );
     1982        }
     1983
     1984        /**
     1985         * Test that script tags are parsed correctly.
     1986         *
     1987         * Script tag parsing is very complicated, see the following resources for more details:
     1988         *
     1989         * - https://html.spec.whatwg.org/multipage/parsing.html#script-data-state
     1990         * - https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
     1991         *
     1992         * @ticket 63738
     1993         *
     1994         * @dataProvider data_script_tag
     1995         */
     1996        public function test_script_tag_parsing( string $input, bool $closes ) {
     1997                $processor = new WP_HTML_Tag_Processor( $input );
     1998
     1999                if ( $closes ) {
     2000                        $this->assertTrue( $processor->next_token(), 'Expected to find complete script tag.' );
     2001                        $this->assertSame( 'SCRIPT', $processor->get_tag() );
     2002                        return;
     2003                }
     2004
     2005                $this->assertFalse( $processor->next_token(), 'Expected to fail next_token().' );
     2006                $this->assertTrue( $processor->paused_at_incomplete_token(), 'Expected an incomplete SCRIPT tag token.' );
     2007        }
     2008
     2009        /**
     2010         * Data provider.
     2011         */
     2012        public static function data_script_tag(): array {
     2013                return array(
     2014                        'Basic script tag'                          => array( '<script></script>', true ),
     2015                        'Script with type attribute'                => array( '<script type="text/javascript"></script>', true ),
     2016                        'Script data escaped'                       => array( '<script><!--</script>', true ),
     2017                        'Script data double-escaped exit (comment)' => array( '<script><!--<script>--></script>', true ),
     2018                        'Script data double-escaped exit (closed)'  => array( '<script><!--<script></script></script>', true ),
     2019                        'Script data double-escaped exit (closed/truncated)' => array( '<script><!--<script></script </script>', true ),
     2020                        'Script data no double-escape'              => array( '<script><!-- --><script></script>', true ),
     2021
     2022                        'Script tag with self-close flag (ignored)' => array( '<script />', false ),
     2023                        'Script data double-escaped'                => array( '<script><!--<script></script>', false ),
    19812024                );
    19822025        }
Note: See TracChangeset for help on using the changeset viewer.