Can't render page, or DOMDocumentFragment breaks on <script> tags, or something
After some trial and error, I've come to notice that my Template engine fails on template files that contain <script>
tags. This is clearly a huge problem, but I can't seem to fig开发者_开发问答ure out why it's failing.
It's very peculiar too, the manner with which it fails. With the <script>
tags commented out, the page loads normally, however with the <script>
tags uncommented, the page loads, and performing a View Source shows that the entire page has loaded, though nothing is rendered in the browser. The Firebug DOM inspector, shows a head tag, and an empty body.
The stranger part (I think at least) is that these script tags are targeting external files. If there was in-page scripting outside of a CDATA section, I could understand some unexpected results.
I'd post the code used, but it's extremely long and I wouldn't know where to start, ie; where the issue is, if in fact there even is one.
In short, my template engine creates a single DOMDocument
object, and then creates DOMDocumentFragment
objects to manage parts of the template. The first parsing iteration typically loads the entire template file into a fragment, and then it works down the tree based on certain parameters, looking for certain tags.
TL;DR: Does anyone know why the PHP DOM classes may be failing to build the page properly due to script tags? If I enclose them in <![CDATA[ ... ]]>
tags, it does nothing. Only when I remove or comment out the tags does a browser load and render the page.
Is the content of the script tag wrapped in a CDATA block? If it isn't, the DOM will attempt to parse its contents and fail. Make sure all script code is wrapped in CDATA blocks, and that the DOM parser is configured to output CDATA blocks (otherwise the output will contain empty script tags).
I would also advise putting as much of the script code off-page as possible and placing it in its own files that you then link to with <script src="/path/to/script.js"></script>
markup. This avoids the DOM parsing problem, and has the additional benefit that the browser only needs to load the code once and can keep it cached in subsequent page views.
精彩评论