|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the WP_CSS_Builder class. |
| 5 | + * |
| 6 | + * @group css-api |
| 7 | + * |
| 8 | + * @coversDefaultClass WP_CSS_Builder |
| 9 | + */ |
| 10 | +class Tests_CssApi_WpCssBuilder extends WP_UnitTestCase { |
| 11 | + /** |
| 12 | + * @dataProvider data_string_escaping |
| 13 | + * |
| 14 | + * @covers ::string |
| 15 | + */ |
| 16 | + public function test_string_escaping( string $input, string $expected ): void { |
| 17 | + $this->assertSame( $expected, WP_CSS_Builder::string( $input ) ); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Data provider for individual escape mappings and basic cases. |
| 22 | + * |
| 23 | + * @return array |
| 24 | + */ |
| 25 | + public static function data_string_escaping(): array { |
| 26 | + return array( |
| 27 | + // Basic behavior. |
| 28 | + 'empty string' => array( '', '""' ), |
| 29 | + 'simple ASCII' => array( 'hello', '"hello"' ), |
| 30 | + 'spaces preserved' => array( 'hello world', '"hello world"' ), |
| 31 | + 'numbers pass through' => array( '12345', '"12345"' ), |
| 32 | + 'non-ASCII passthrough' => array( 'café', '"café"' ), |
| 33 | + |
| 34 | + // Backslash escaping. |
| 35 | + 'single backslash' => array( '\\', '"\5C "' ), |
| 36 | + 'double backslash' => array( '\\\\', '"\5C \5C "' ), |
| 37 | + |
| 38 | + // NULL byte. |
| 39 | + 'null byte' => array( "\0", "\"\u{FFFD}\"" ), |
| 40 | + |
| 41 | + // Newline variants. |
| 42 | + 'LF' => array( "\n", '"\A "' ), |
| 43 | + 'CR' => array( "\r", '"\A "' ), |
| 44 | + 'CRLF' => array( "\r\n", '"\A "' ), |
| 45 | + 'form feed' => array( "\f", '"\A "' ), |
| 46 | + |
| 47 | + // HTML-problematic characters. |
| 48 | + 'less than' => array( '<', '"\3C "' ), |
| 49 | + 'greater than' => array( '>', '"\3E "' ), |
| 50 | + 'ampersand' => array( '&', '"\26 "' ), |
| 51 | + |
| 52 | + // CSS-problematic characters. |
| 53 | + 'comma' => array( ',', '"\2C "' ), |
| 54 | + 'semicolon' => array( ';', '"\3B "' ), |
| 55 | + 'open brace' => array( '{', '"\7B "' ), |
| 56 | + 'close brace' => array( '}', '"\7D "' ), |
| 57 | + 'double quote' => array( '"', '"\22 "' ), |
| 58 | + 'single quote' => array( "'", '"\27 "' ), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Tests the example from the class docblock. |
| 64 | + * |
| 65 | + * @covers ::string |
| 66 | + */ |
| 67 | + public function test_docblock_example(): void { |
| 68 | + $value = 'CSS & a "<style>" tag\'s strings'; |
| 69 | + $expected = '"CSS \26 a \22 \3C style\3E \22 tag\27 s strings"'; |
| 70 | + |
| 71 | + $this->assertSame( $expected, WP_CSS_Builder::string( $value ) ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Tests a string containing mixed newline types. |
| 76 | + * |
| 77 | + * @covers ::string |
| 78 | + */ |
| 79 | + public function test_mixed_newlines(): void { |
| 80 | + $input = "line1\nline2\rline3\r\nline4\fline5"; |
| 81 | + $expected = '"line1\A line2\A line3\A line4\A line5"'; |
| 82 | + |
| 83 | + $this->assertSame( $expected, WP_CSS_Builder::string( $input ) ); |
| 84 | + } |
| 85 | +} |
0 commit comments