@@ -25,4 +25,56 @@ public function testAddHtml5()
25
25
$ crawler ->add ($ this ->getDoctype ().'<html><body><h1><p>Foo</p></h1></body></html> ' );
26
26
$ this ->assertEquals ('Foo ' , $ crawler ->filterXPath ('//h1 ' )->text (), '->add() adds nodes from a string ' );
27
27
}
28
+
29
+ /** @dataProvider validHtml5Provider */
30
+ public function testHtml5ParserParseContentStartingWithValidHeading (string $ content ): void
31
+ {
32
+ $ this ->skipTestIfHTML5LibraryNotAvailable ();
33
+
34
+ $ crawler = $ this ->createCrawler ();
35
+ $ crawler ->addHtmlContent ($ content );
36
+ self ::assertEquals (
37
+ 'Foo ' ,
38
+ $ crawler ->filterXPath ('//h1 ' )->text (),
39
+ '->addHtmlContent() parses valid HTML with comment before doctype '
40
+ );
41
+ }
42
+
43
+ /** @dataProvider invalidHtml5Provider */
44
+ public function testHtml5ParserWithInvalidHeadedContent (string $ content ): void
45
+ {
46
+ $ this ->skipTestIfHTML5LibraryNotAvailable ();
47
+
48
+ $ crawler = $ this ->createCrawler ();
49
+ $ crawler ->addHtmlContent ($ content );
50
+ self ::assertEmpty ($ crawler ->filterXPath ('//h1 ' )->text (), '->addHtmlContent failed as expected ' );
51
+ }
52
+
53
+ public function validHtml5Provider (): iterable
54
+ {
55
+ $ html = $ this ->getDoctype ().'<html><body><h1><p>Foo</p></h1></body></html> ' ;
56
+ $ BOM = \chr (0xEF ).\chr (0xBB ).\chr (0xBF );
57
+
58
+ yield 'BOM first ' => [$ BOM .$ html ];
59
+ yield 'Single comment ' => ['<!-- comment --> ' .$ html ];
60
+ yield 'Multiline comment ' => ["<!-- \n multiline comment \n --> " .$ html ];
61
+ yield 'Several comments ' => ['<!--c--> <!--cc--> ' .$ html ];
62
+ yield 'Whitespaces ' => [' ' .$ html ];
63
+ yield 'All together ' => [$ BOM .' ' .'<!--c--> ' .$ html ];
64
+ }
65
+
66
+ public function invalidHtml5Provider (): iterable
67
+ {
68
+ $ html = $ this ->getDoctype ().'<html><body><h1><p>Foo</p></h1></body></html> ' ;
69
+
70
+ yield 'Text ' => ['hello world ' .$ html ];
71
+ yield 'Text between comments ' => ['<!--c--> test <!--cc--> ' .$ html ];
72
+ }
73
+
74
+ private function skipTestIfHTML5LibraryNotAvailable (): void
75
+ {
76
+ if (!class_exists (\Masterminds \HTML5 ::class)) {
77
+ self ::markTestSkipped ('HTML5 library is not available ' );
78
+ }
79
+ }
28
80
}
0 commit comments