Bug #54429 [Sus]: domDocument::loadHTML adds extra CDATA tags causing script errors.

From: Date: Thu, 31 Mar 2011 05:02:45 +0000
Subject: Bug #54429 [Sus]: domDocument::loadHTML adds extra CDATA tags causing script errors.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-159868@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=54429&edit=1 ID: 54429 User updated by: ken at smallboxcms dot com Reported by: ken at smallboxcms dot com Summary: domDocument::loadHTML adds extra CDATA tags causing script errors. Status: Suspended Type: Bug Package: DOM XML related Operating System: Centos 5.5 PHP Version: 5.3.6 Block user comment: N Private report: N New Comment: Here is a userland workaround: $list = $dom->getElementsByTagName('script'); foreach ($list as $script) { if ($script->childNodes->length && $script->firstChild->nodeType == 4) { $cdata = $script->removeChild($script->firstChild); $text = $dom->createTextNode($cdata->nodeValue); $script->appendChild($text); } } Previous Comments: ------------------------------------------------------------------------ [2011-03-31 06:42:14] ken at smallboxcms dot com This may be this bug which appears to have sat unconfirmed for 5 years: https://bugzilla.gnome.org/show_bug.cgi?id=357992 ------------------------------------------------------------------------ [2011-03-31 06:27:47] rasmus@php.net You would need to file this against the libxml2 library as this isn't something we can fix at the PHP level. See http://xmlsoft.org/bugs.html ------------------------------------------------------------------------ [2011-03-31 06:23:32] ken at smallboxcms dot com Description: ------------ This one has annoyed me for a long time. When you load HTML useing loadHTML you get extra CDATA tags on the script tags causing browser errors. This does not happen when loadXML is used. Test script: --------------- <?php $foo = ' <html> <head> <title></title> </head> <body> <script type="text/javascript"> //<![CDATA[ alert("foo bar"); //]]> </script> </body> </html> '; $dom = new domDocument('1,0', 'utf-8'); $dom->loadHTML($foo); echo $dom->saveXML(); ?> Expected result: ---------------- <?xml version="1.0" standalone="yes"?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><head><title/></head><body> <script type="text/javascript"> //<![CDATA[ alert("foo bar"); //]]> </script></body></html> Actual result: -------------- <?xml version="1.0" standalone="yes"?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><head><title/></head><body> <script type="text/javascript"><![CDATA[ //<![CDATA[ alert("foo bar"); //]]]]><![CDATA[> ]]></script></body></html> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=54429&edit=1

« previous php.bugs (#159868) next »