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

From: Date: Sun, 08 Jul 2018 14:26:42 +0000
Subject: Bug #54429 [Sus->Nab]: domDocument::loadHTML adds extra CDATA tags causing script errors.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216223@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=54429&edit=1 ID: 54429 Updated by: cmb@php.net Reported by: ken at smallboxcms dot com Summary: domDocument::loadHTML adds extra CDATA tags causing script errors. -Status: Suspended +Status: Not a bug Type: Bug Package: DOM XML related Operating System: Centos 5.5 PHP Version: 5.3.6 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: > This is not a bug -- the problem is fn:saveXML() is being used > where fn:saveHTML must be used. That. See <https://3v4l.org/TfpK3>. Previous Comments: ------------------------------------------------------------------------ [2013-01-01 08:13:01] i_me_you2002 at yahoo dot com This is not a bug -- the problem is fn:saveXML() is being used where fn:saveHTML must be used. Also, fn:$DOMDocument->save is the XML version which will too result in the same problem. ------------------------------------------------------------------------ [2011-03-31 07:02:44] ken at smallboxcms dot com 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); } } ------------------------------------------------------------------------ [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 https://bugs.php.net/bug.php?id=54429&edit=1

« previous php.bugs (#216223) next »