Jump to content

DOMDocument wrecking inline javascript


fivestringsurf

Recommended Posts

I built a custom templating class that uses DOMDocument to manipulate some of the nodes/tags within my html.  I run into all kinds of problems when I use inline js (js that lives on page)  Most notably with < and & used in the js itself.  This code exibits some of the problems:

$domStr = '
<!DOCTYPE html>
	<head>
		<meta charset="utf-8"/>
		<title>my page</title>
		<script>
			var elem = "<div>some content</div>";
		</script>
	</head>
	<body>
		<div>
			MY PAGE
		</div>
	</body>
</html>
';
	$doc = new DOMDocument();
	libxml_use_internal_errors(true);//prevents tags in js from throwing errors; see php.net manual
	$doc->formatOutput = true;
	$doc->strictErrorChecking = false;
	$doc->preserveWhiteSpace  = true;
	
	$doc->loadHTML($domStr);
	echo $doc->saveHTML();
exit;

and the html output is:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>my page</title>
<script>
			var elem = "<div>some content";
		</script>
</head>
<body>
		<div>
			MY PAGE
		</div>
	</body>
</html>

You'll notice that the closing </div> tag is removed.  ???  I have very dissapointed  with domdocument overall as it doesn't seem to always do what it promises to do...even with preserving space/formatting etc.  I'm to the point at which I might just abandon domdocument altogether and parst the html myself with regex (yuck)

 

any expert advice in this matter would be greatly appretiated.

Link to comment
https://forums.phpfreaks.com/topic/282535-domdocument-wrecking-inline-javascript/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.