Skip to content

Commit c7a66e0

Browse files
authored
Merge pull request #117 from Masterminds/downsider-bugfix-outputrules-notice
Fix PHP Notice in OutputRules
2 parents 9da7ec0 + 24257c8 commit c7a66e0

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/HTML5/Serializer/OutputRules.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ public function element($ele)
221221
$this->openTag($ele);
222222
if (Elements::isA($name, Elements::TEXT_RAW)) {
223223
foreach ($ele->childNodes as $child) {
224-
$this->wr($child->data);
224+
if ($child instanceof \DOMCharacterData) {
225+
$this->wr($child->data);
226+
} elseif ($child instanceof \DOMElement) {
227+
$this->element($child);
228+
}
225229
}
226230
} else {
227231
// Handle children.

test/HTML5/Serializer/OutputRulesTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Masterminds\HTML5\Serializer\OutputRules;
55
use Masterminds\HTML5\Serializer\Traverser;
6+
use Masterminds\HTML5;
67

78
class 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,32 @@ 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+
<script id="template" type="x-tmpl-mustache">
633+
<h1>Hello!</h1>
634+
</script>
635+
</body>
636+
</html>');
637+
638+
$badNode = $dom->createElement("p", "Bar");
639+
640+
// modify the content of the TEXT_RAW element: <script id="template"> appending dom nodes
641+
$styleElement = $dom->getElementById("template");
642+
$styleElement->appendChild($badNode);
643+
644+
$contents = $this->html5->saveHTML($dom);
645+
646+
$this->assertTrue(strpos($contents, '<script id="template" type="x-tmpl-mustache">
647+
<h1>Hello!</h1>
648+
<p>Bar</p></script>')!==false);
649+
}
616650
}

0 commit comments

Comments
 (0)