33
44use Masterminds \HTML5 \Serializer \OutputRules ;
55use Masterminds \HTML5 \Serializer \Traverser ;
6+ use Masterminds \HTML5 ;
67
78class OutputRulesTest extends \Masterminds \HTML5 \Tests \TestCase
89{
@@ -18,6 +19,11 @@ class OutputRulesTest extends \Masterminds\HTML5\Tests\TestCase
1819 </body>
1920 </html> ' ;
2021
22+ /**
23+ * @var HTML5
24+ */
25+ protected $ html5 ;
26+
2127 public function setUp ()
2228 {
2329 $ this ->html5 = $ this ->getInstance ();
@@ -29,7 +35,7 @@ public function setUp()
2935 * @param string $name
3036 * The name of the method on the Traverser class to test.
3137 *
32- * @return \ReflectionMethod \ReflectionMethod for the specified method
38+ * @return \ReflectionMethod for the specified method
3339 */
3440 public function getProtectedMethod ($ name )
3541 {
@@ -613,4 +619,42 @@ public function testAddressTag()
613619 $ this ->assertRegExp ('|contact persons for the <a href="Activity">W3C HTML Activity</a>| ' , $ contents );
614620 $ this ->assertRegExp ('|</address>| ' , $ contents );
615621 }
622+
623+ /**
624+ * Ensure direct DOM manipulation doesn't break TEXT_RAW elements (iframe, script, etc...)
625+ */
626+ public function testHandlingInvalidRawContent ()
627+ {
628+ $ dom = $ this ->html5 ->loadHTML (
629+ '<!doctype html>
630+ <html lang="en" id="base">
631+ <body>
632+ <p>Foo</p>
633+ <style id="test">
634+ blah
635+ </style>
636+ <p>Baz</p>
637+ </body>
638+ </html> ' );
639+
640+ // modify the content of the TEXT_RAW element
641+ $ badNode = $ dom ->createElement ("p " );
642+ $ badNode ->appendChild ($ dom ->createTextNode ("Bar " ));
643+
644+ $ styleElement = $ dom ->getElementById ("test " );
645+ $ styleElement ->appendChild ($ badNode );
646+
647+ // create the OutputRules instance and run the tests
648+ $ stream = fopen ('php://temp ' , 'w ' );
649+ $ r = new OutputRules ($ stream , $ this ->html5 ->getOptions ());
650+ $ t = new Traverser ($ dom , $ stream , $ r , $ this ->html5 ->getOptions ());
651+
652+ $ r ->element ($ dom ->getElementById ('base ' ));
653+ $ contents = stream_get_contents ($ stream , - 1 , 0 );
654+
655+ $ this ->assertRegExp ('|<p>Foo</p>| ' , $ contents );
656+ $ this ->assertNotRegExp ('|<p>Bar</p>| ' , $ contents );
657+ $ this ->assertRegExp ('|<p>Baz</p>| ' , $ contents );
658+
659+ }
616660}
0 commit comments