WordPress.org

Make WordPress Core

Ticket #35022: 35022.3.patch

File 35022.3.patch, 3.6 KB (added by gitlost, 4 years ago)

Use single-byte mode. Unit tests included.

  • src/wp-includes/formatting.php

     
    216216
    217217        // Look for shortcodes and HTML elements.
    218218
    219         preg_match_all( '@\[/?([^<>&/\[\]\x00-\x20=]++)@', $text, $matches );
     219        preg_match_all( '@\[/?(' . WP_SHORTCODE_NAME_NON_TERMINATORS . '++)@', $text, $matches );
    220220        $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
    221221        $found_shortcodes = ! empty( $tagnames );
    222222        $shortcode_regex = $found_shortcodes ? _get_wptexturize_shortcode_regex( $tagnames ) : '';
  • src/wp-includes/shortcodes.php

     
    4141 */
    4242$shortcode_tags = array();
    4343
     44if ( in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
     45        define( 'WP_SHORTCODE_NAME_TERMINATORS',         '(?:[<>&\/\[\]\x00-\x20=]|\xC2\xA0)' );
     46        define( 'WP_SHORTCODE_NAME_NON_TERMINATORS', '(?:[^<>&\/\[\]\x00-\x20=\xC2]|\xC2[^\xA0])' );
     47} else {
     48        define( 'WP_SHORTCODE_NAME_TERMINATORS',         '[<>&\/\[\]\x00-\x20=]' );
     49        define( 'WP_SHORTCODE_NAME_NON_TERMINATORS', '[^<>&\/\[\]\x00-\x20=]' );
     50}
     51
    4452/**
    4553 * Add hook for shortcode tag.
    4654 *
     
    95103                return;
    96104        }
    97105
    98         if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) {
     106        if ( 0 !== preg_match( '@' . WP_SHORTCODE_NAME_TERMINATORS . '@', $tag ) ) {
    99107                /* translators: 1: shortcode name, 2: space separated list of reserved characters */
    100108                $message = sprintf( __( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ), $tag, '& / < > [ ] =' );
    101109                _doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
     
    210218                return $content;
    211219
    212220        // Find all registered tag names in $content.
    213         preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
     221        preg_match_all( '@\[(' . WP_SHORTCODE_NAME_NON_TERMINATORS . '++)@', $content, $matches );
    214222        $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
    215223
    216224        if ( empty( $tagnames ) ) {
     
    481489function shortcode_parse_atts($text) {
    482490        $atts = array();
    483491        $pattern = get_shortcode_atts_regex();
     492
    484493        $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
    485494        if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
    486495                foreach ($match as $m) {
     
    578587                return $content;
    579588
    580589        // Find all registered tag names in $content.
    581         preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
     590        preg_match_all( '@\[(' . WP_SHORTCODE_NAME_NON_TERMINATORS . '++)@', $content, $matches );
    582591        $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
    583592
    584593        if ( empty( $tagnames ) ) {
  • tests/phpunit/tests/shortcode.php

     
    312312        }
    313313
    314314        /**
     315         * @ticket 35022
     316         */
     317        public function test_non_breaking_space_following_shortcode_tag() {
     318                do_shortcode( "[test-shortcode-tag\xC2\xA0foo=\"bar\"]" );
     319                $this->assertSame( 'test-shortcode-tag', $this->tagname );
     320                $this->assertSame( array( 'foo' => 'bar' ), $this->atts );
     321        }
     322
     323        /**
     324         * @ticket 35022
     325         */
     326        public function test_non_non_breaking_space_following_shortcode_tag() {
     327                do_shortcode( "[test-shortcode-tag\xC2\xA1foo=\"bar\"]" );
     328                $this->assertNull( $this->tagname );
     329                $this->assertNull( $this->atts );
     330        }
     331
     332        /**
    315333         * @ticket 6562
    316334         */
    317335        function test_utf8_whitespace_2() {