Make WordPress Core

Changeset 57056 for trunk


Ignore:
Timestamp:
11/03/2023 03:31:51 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Remove unnecessary ignore annotation in wp_kses_hair_parse().

It is perfectly possible to write a commented regex with layout for readability by using the x modifier.

As per the manual:

x (PCRE_EXTENDED)

If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include commentary inside complicated patterns.

Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.

Reference: ​PHP Manual: Pattern Modifiers.

This commit rewrites these two regexes to use the x modifier and gets rid of the unnecessary phpcs:disable comments.

The tests in the tests/phpunit/tests/db/dbDelta.php file cover this change.

Follow-up to [42249].

Props jrf.
See #59650.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/kses.php

    r56617 r57056  
    15371537        }
    15381538
    1539         // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
    15401539        $regex =
    1541                 '(?:'
    1542                 .     '[_a-zA-Z][-_a-zA-Z0-9:.]*' // Attribute name.
    1543                 . '|'
    1544                 .     '\[\[?[^\[\]]+\]\]?'        // Shortcode in the name position implies unfiltered_html.
    1545                 . ')'
    1546                 . '(?:'               // Attribute value.
    1547                 .     '\s*=\s*'       // All values begin with '='.
    1548                 .     '(?:'
    1549                 .         '"[^"]*"'   // Double-quoted.
    1550                 .     '|'
    1551                 .         "'[^']*'"   // Single-quoted.
    1552                 .     '|'
    1553                 .         '[^\s"\']+' // Non-quoted.
    1554                 .         '(?:\s|$)'  // Must have a space.
    1555                 .     ')'
    1556                 . '|'
    1557                 .     '(?:\s|$)'      // If attribute has no value, space is required.
    1558                 . ')'
    1559                 . '\s*';              // Trailing space is optional except as mentioned above.
    1560         // phpcs:enable
     1540                '(?:
     1541                                [_a-zA-Z][-_a-zA-Z0-9:.]* # Attribute name.
     1542                        |
     1543                                \[\[?[^\[\]]+\]\]?        # Shortcode in the name position implies unfiltered_html.
     1544                )
     1545                (?:                               # Attribute value.
     1546                        \s*=\s*                       # All values begin with "=".
     1547                        (?:
     1548                                "[^"]*"                   # Double-quoted.
     1549                        |
     1550                                \'[^\']*\'                # Single-quoted.
     1551                        |
     1552                                [^\s"\']+                 # Non-quoted.
     1553                                (?:\s|$)                  # Must have a space.
     1554                        )
     1555                |
     1556                        (?:\s|$)                      # If attribute has no value, space is required.
     1557                )
     1558                \s*                               # Trailing space is optional except as mentioned above.
     1559                ';
    15611560
    15621561        /*
    15631562         * Although it is possible to reduce this procedure to a single regexp,
    15641563         * we must run that regexp twice to get exactly the expected result.
     1564         *
     1565         * Note: do NOT remove the `x` modifiers as they are essential for the above regex!
    15651566         */
    15661567
    1567         $validation = "%^($regex)+$%";
    1568         $extraction = "%$regex%";
     1568        $validation = "%^($regex)+$%x";
     1569        $extraction = "%$regex%x";
    15691570
    15701571        if ( 1 === preg_match( $validation, $attr ) ) {
Note: See TracChangeset for help on using the changeset viewer.