Skip to content

Commit 0c06f6e

Browse files
committed
Extract normalize input method
1 parent ccb7761 commit 0c06f6e

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

src/wp-includes/html-api/class-wp-css-complex-selector-list.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,12 @@ class WP_CSS_Complex_Selector_List extends WP_CSS_Compound_Selector_List impleme
4343
* @return static|null
4444
*/
4545
public static function from_selectors( string $input ) {
46-
// > A selector string is a list of one or more complex selectors ([SELECTORS4], section 3.1) that may be surrounded by whitespace…
47-
$input = trim( $input, " \t\r\n\r" );
46+
$input = self::normalize_selector_input( $input );
4847

4948
if ( '' === $input ) {
5049
return null;
5150
}
5251

53-
/*
54-
* > The input stream consists of the filtered code points pushed into it as the input byte stream is decoded.
55-
* >
56-
* > To filter code points from a stream of (unfiltered) code points input:
57-
* > Replace any U+000D CARRIAGE RETURN (CR) code points, U+000C FORM FEED (FF) code points, or pairs of U+000D CARRIAGE RETURN (CR) followed by U+000A LINE FEED (LF) in input by a single U+000A LINE FEED (LF) code point.
58-
* > Replace any U+0000 NULL or surrogate code points in input with U+FFFD REPLACEMENT CHARACTER (�).
59-
*
60-
* https://www.w3.org/TR/css-syntax-3/#input-preprocessing
61-
*/
62-
$input = str_replace( array( "\r\n" ), "\n", $input );
63-
$input = str_replace( array( "\r", "\f" ), "\n", $input );
64-
$input = str_replace( "\0", "\u{FFFD}", $input );
65-
6652
$offset = 0;
6753

6854
$selector = self::parse_complex_selector( $input, $offset );

src/wp-includes/html-api/class-wp-css-compound-selector-list.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,12 @@ protected function __construct( array $selectors ) {
120120
* @return static|null
121121
*/
122122
public static function from_selectors( string $input ) {
123-
$input = trim( $input, " \t\r\n\r" );
123+
$input = self::normalize_selector_input( $input );
124124

125125
if ( '' === $input ) {
126126
return null;
127127
}
128128

129-
/*
130-
* > The input stream consists of the filtered code points pushed into it as the input byte stream is decoded.
131-
* >
132-
* > To filter code points from a stream of (unfiltered) code points input:
133-
* > Replace any U+000D CARRIAGE RETURN (CR) code points, U+000C FORM FEED (FF) code points, or pairs of U+000D CARRIAGE RETURN (CR) followed by U+000A LINE FEED (LF) in input by a single U+000A LINE FEED (LF) code point.
134-
* > Replace any U+0000 NULL or surrogate code points in input with U+FFFD REPLACEMENT CHARACTER (�).
135-
*
136-
* https://www.w3.org/TR/css-syntax-3/#input-preprocessing
137-
*/
138-
$input = str_replace( array( "\r\n" ), "\n", $input );
139-
$input = str_replace( array( "\r", "\f" ), "\n", $input );
140-
$input = str_replace( "\0", "\u{FFFD}", $input );
141-
142129
$offset = 0;
143130

144131
$selector = self::parse_compound_selector( $input, $offset );
@@ -842,4 +829,32 @@ final protected static function check_if_three_code_points_would_start_an_ident_
842829
// > Return false.
843830
return self::is_ident_start_codepoint( $input, $offset );
844831
}
832+
833+
/**
834+
* @todo doc…
835+
*/
836+
final protected static function normalize_selector_input( string $input ): string {
837+
/*
838+
* > A selector string is a list of one or more complex selectors ([SELECTORS4], section 3.1) that may be surrounded by whitespace…
839+
*
840+
* This list includes \f.
841+
* A later step would normalize it to a known whitespace character, but it can be trimmed here as well.
842+
*/
843+
$input = trim( $input, " \t\r\n\r\f" );
844+
845+
/*
846+
* > The input stream consists of the filtered code points pushed into it as the input byte stream is decoded.
847+
* >
848+
* > To filter code points from a stream of (unfiltered) code points input:
849+
* > Replace any U+000D CARRIAGE RETURN (CR) code points, U+000C FORM FEED (FF) code points, or pairs of U+000D CARRIAGE RETURN (CR) followed by U+000A LINE FEED (LF) in input by a single U+000A LINE FEED (LF) code point.
850+
* > Replace any U+0000 NULL or surrogate code points in input with U+FFFD REPLACEMENT CHARACTER (�).
851+
*
852+
* https://www.w3.org/TR/css-syntax-3/#input-preprocessing
853+
*/
854+
$input = str_replace( array( "\r\n" ), "\n", $input );
855+
$input = str_replace( array( "\r", "\f" ), "\n", $input );
856+
$input = str_replace( "\0", "\u{FFFD}", $input );
857+
858+
return $input;
859+
}
845860
}

0 commit comments

Comments
 (0)