From 13192d385f0f9f261ddf1ef50f43121e163a964b Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 3 Nov 2023 15:31:51 +0000 Subject: [PATCH] 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: [https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php 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. git-svn-id: https://develop.svn.wordpress.org/trunk@57056 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/kses.php | 47 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/kses.php b/src/wp-includes/kses.php index 27da1679e8..7449d8fb4a 100644 --- a/src/wp-includes/kses.php +++ b/src/wp-includes/kses.php @@ -1536,36 +1536,37 @@ function wp_kses_hair_parse( $attr ) { return array(); } - // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation $regex = - '(?:' - . '[_a-zA-Z][-_a-zA-Z0-9:.]*' // Attribute name. - . '|' - . '\[\[?[^\[\]]+\]\]?' // Shortcode in the name position implies unfiltered_html. - . ')' - . '(?:' // Attribute value. - . '\s*=\s*' // All values begin with '='. - . '(?:' - . '"[^"]*"' // Double-quoted. - . '|' - . "'[^']*'" // Single-quoted. - . '|' - . '[^\s"\']+' // Non-quoted. - . '(?:\s|$)' // Must have a space. - . ')' - . '|' - . '(?:\s|$)' // If attribute has no value, space is required. - . ')' - . '\s*'; // Trailing space is optional except as mentioned above. - // phpcs:enable + '(?: + [_a-zA-Z][-_a-zA-Z0-9:.]* # Attribute name. + | + \[\[?[^\[\]]+\]\]? # Shortcode in the name position implies unfiltered_html. + ) + (?: # Attribute value. + \s*=\s* # All values begin with "=". + (?: + "[^"]*" # Double-quoted. + | + \'[^\']*\' # Single-quoted. + | + [^\s"\']+ # Non-quoted. + (?:\s|$) # Must have a space. + ) + | + (?:\s|$) # If attribute has no value, space is required. + ) + \s* # Trailing space is optional except as mentioned above. + '; /* * Although it is possible to reduce this procedure to a single regexp, * we must run that regexp twice to get exactly the expected result. + * + * Note: do NOT remove the `x` modifiers as they are essential for the above regex! */ - $validation = "%^($regex)+$%"; - $extraction = "%$regex%"; + $validation = "%^($regex)+$%x"; + $extraction = "%$regex%x"; if ( 1 === preg_match( $validation, $attr ) ) { preg_match_all( $extraction, $attr, $attrarr );