|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit tests covering WP_HTML_Doctype_Info functionality. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage HTML-API |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @group html-api |
| 11 | + * |
| 12 | + * @coversDefaultClass WP_HTML_Doctype_Info |
| 13 | + */ |
| 14 | +class Tests_HtmlApi_WpHtmlDoctypeInfo extends WP_UnitTestCase { |
| 15 | + /** |
| 16 | + * Test DOCTYPE handling. |
| 17 | + * |
| 18 | + * @ticket 61576 |
| 19 | + * |
| 20 | + * @dataProvider data_parseable_raw_doctypes |
| 21 | + */ |
| 22 | + public function test_doctype_doc_info( |
| 23 | + string $html, |
| 24 | + string $expected_compat_mode, |
| 25 | + ?string $expected_name = null, |
| 26 | + ?string $expected_public_id = null, |
| 27 | + ?string $expected_system_id = null |
| 28 | + ) { |
| 29 | + $doctype = WP_HTML_Doctype_Info::from_doctype_token( $html ); |
| 30 | + $this->assertNotNull( |
| 31 | + $doctype, |
| 32 | + "Should have parsed the following doctype declaration: {$html}" |
| 33 | + ); |
| 34 | + |
| 35 | + $this->assertSame( |
| 36 | + $expected_compat_mode, |
| 37 | + $doctype->indicated_compatability_mode, |
| 38 | + 'Failed to infer the expected document compatability mode.' |
| 39 | + ); |
| 40 | + |
| 41 | + $this->assertSame( |
| 42 | + $expected_name, |
| 43 | + $doctype->name, |
| 44 | + 'Failed to parse the expected DOCTYPE name.' |
| 45 | + ); |
| 46 | + |
| 47 | + $this->assertSame( |
| 48 | + $expected_public_id, |
| 49 | + $doctype->public_identifier, |
| 50 | + 'Failed to parse the expected DOCTYPE public identifier.' |
| 51 | + ); |
| 52 | + |
| 53 | + $this->assertSame( |
| 54 | + $expected_system_id, |
| 55 | + $doctype->system_identifier, |
| 56 | + 'Failed to parse the expected DOCTYPE system identifier.' |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Data provider. |
| 62 | + * |
| 63 | + * @return array[] |
| 64 | + */ |
| 65 | + public static function data_parseable_raw_doctypes(): array { |
| 66 | + return array( |
| 67 | + 'Missing doctype name' => array( '<!DOCTYPE>', 'quirks' ), |
| 68 | + 'HTML5 doctype' => array( '<!DOCTYPE html>', 'no-quirks', 'html' ), |
| 69 | + 'HTML5 doctype no whitespace before name' => array( '<!DOCTYPEhtml>', 'no-quirks', 'html' ), |
| 70 | + 'XHTML doctype' => array( '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', 'no-quirks', 'html', '-//W3C//DTD HTML 4.01//EN', 'http://www.w3.org/TR/html4/strict.dtd' ), |
| 71 | + 'SVG doctype' => array( '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">', 'quirks', 'svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' ), |
| 72 | + 'MathML doctype' => array( '<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">', 'quirks', 'math', '-//W3C//DTD MathML 2.0//EN', 'http://www.w3.org/Math/DTD/mathml2/mathml2.dtd' ), |
| 73 | + 'Doctype with null byte replacement' => array( "<!DOCTYPE null-\0 PUBLIC '\0' '\0\0'>", 'quirks', "null-\u{FFFD}", "\u{FFFD}", "\u{FFFD}\u{FFFD}" ), |
| 74 | + 'Uppercase doctype' => array( '<!DOCTYPE UPPERCASE>', 'quirks', 'uppercase' ), |
| 75 | + 'Lowercase doctype' => array( '<!doctype lowercase>', 'quirks', 'lowercase' ), |
| 76 | + 'Doctype with whitespace' => array( "<!DOCTYPE\n\thtml\f\rPUBLIC\r\n''\t''>", 'no-quirks', 'html', '', '' ), |
| 77 | + 'Doctype trailing characters' => array( "<!DOCTYPE html PUBLIC '' '' Anything (except closing angle bracket) is just fine here !!!>", 'no-quirks', 'html', '', '' ), |
| 78 | + 'An ugly no-quirks doctype' => array( "<!dOcTyPehtml\tPublIC\"pub-id\"'sysid'>", 'no-quirks', 'html', 'pub-id', 'sysid' ), |
| 79 | + 'Missing public ID' => array( '<!DOCTYPE html PUBLIC>', 'quirks', 'html' ), |
| 80 | + 'Missing system ID' => array( '<!DOCTYPE html SYSTEM>', 'quirks', 'html' ), |
| 81 | + 'Missing close quote public ID' => array( "<!DOCTYPE html PUBLIC 'xyz>", 'quirks', 'html', 'xyz' ), |
| 82 | + 'Missing close quote system ID' => array( "<!DOCTYPE html SYSTEM 'xyz>", 'quirks', 'html', null, 'xyz' ), |
| 83 | + 'Missing close quote system ID with public' => array( "<!DOCTYPE html PUBLIC 'abc' 'xyz>", 'quirks', 'html', 'abc', 'xyz' ), |
| 84 | + 'Bogus characters instead of system/public' => array( '<!DOCTYPE html FOOBAR>', 'quirks', 'html' ), |
| 85 | + 'Bogus characters instead of PUBLIC quote' => array( "<!DOCTYPE html PUBLIC x ''''>", 'quirks', 'html' ), |
| 86 | + 'Bogus characters instead of SYSTEM quote ' => array( "<!DOCTYPE html SYSTEM x ''>", 'quirks', 'html' ), |
| 87 | + 'Emoji' => array( '<!DOCTYPE 🏴 PUBLIC "🔥" "😈">', 'quirks', "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", '🔥', '😈' ), |
| 88 | + 'Bogus characters instead of SYSTEM quote after public' => array( "<!DOCTYPE html PUBLIC ''x''>", 'quirks', 'html', '' ), |
| 89 | + 'Special quirks mode if system unset' => array( '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//">', 'quirks', 'html', '-//W3C//DTD HTML 4.01 Frameset//' ), |
| 90 | + 'Special limited-quirks mode if system set' => array( '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//" "">', 'limited-quirks', 'html', '-//W3C//DTD HTML 4.01 Frameset//', '' ), |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @dataProvider invalid_inputs |
| 96 | + * |
| 97 | + * @ticket 61576 |
| 98 | + */ |
| 99 | + public function test_invalid_inputs_return_null( string $html ) { |
| 100 | + $this->assertNull( WP_HTML_Doctype_Info::from_doctype_token( $html ) ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Data provider. |
| 105 | + * |
| 106 | + * @return array[] |
| 107 | + */ |
| 108 | + public static function invalid_inputs(): array { |
| 109 | + return array( |
| 110 | + 'Empty string' => array( '' ), |
| 111 | + 'Other HTML' => array( '<div>' ), |
| 112 | + 'DOCTYPE after HTML' => array( 'x<!DOCTYPE>' ), |
| 113 | + 'DOCTYPE before HTML' => array( '<!DOCTYPE>x' ), |
| 114 | + 'Incomplete DOCTYPE' => array( '<!DOCTYPE' ), |
| 115 | + 'Pseudo DOCTYPE containing ">"' => array( '<!DOCTYPE html PUBLIC ">">' ), |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments