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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.