note 81235 modified in domdocument.savehtml by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 May 2009 13:40:14 +0000 |
| Subject: | note 81235 modified in domdocument.savehtml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154555@lists.php.net to get a copy of this message | ||
DOMDocument->saveXML() doesn't generate a proper XHTML format either.
There is a problem with "script" empty elements. For example:
This will be the code generated by saveXML, with an empty script tag.
<html>
<head>
<script type="text/JavaScript" src="myScript.js"/>
</head>
<body>
<p>I will not appear</p>
<script type="text/JavaScript">
alert("Not working");
</script>
</body>
</html>
I don't know if this is valid XHTML (W3C Validator doesn't complain), but both FF 2.0 and
IE 6 will not render it properly. Both will use </script> as the closing tag for the first
script causing js errors and ignoring in between elements.
You can post-process saveXML string in order to close empty tags with the following function:
<?php
function cerrarTag($tag, $xml){
$indice = 0;
while ($indice< strlen($xml)){
$pos = strpos($xml, "<$tag ", $indice);
if ($pos){
$posCierre = strpos($xml, ">", $pos);
if ($xml[$posCierre-1] == "/"){
$xml = substr_replace($xml, "></$tag>", $posCierre-1, 2);
}
$indice = $posCierre;
}
else break;
}
return $xml;
}
?>
At least script and select empty elements should be closed. This example shows how it can be used:
<?php
define("CABECERA_XHTML", '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
$xhtml = $docXML->saveXML($docXML->documentElement);
$xhtml = cerrarTag("script", $xhtml);
$xhtml = cerrarTag("select", $xhtml);
$xhtml = CABECERA_XHTML."\n".$xhtml;
echo $xhtml;
?>
--was--
DOMDocument->saveXML() doesn't generate a proper XHTML format either.
There is a problem with "script" empty elements. For example:
This will be the code generated by saveXML, with an empty script tag.
<html>
<head>
<script type="text/JavaScript" src="myScript.js"/>
</head>
<body>
<p>I will not appear</p>
<script type="text/JavaScript">
alert("Not working");
</script>
</body>
</html>
I don't know if this is valid XHTML (W3C Validator doesn't complain), but both FF 2.0 and
IE 6 will not render it properly. Both will use </script> as the closing tag for the first
script causing js errors and ignoring in between elements.
http://php.net/manual/en/domdocument.savehtml.php