*
* @param document to be updated
*/
public void updateDocument(Document document)
{
Element root = document.getRootElement();
// This can happen due to a catastrophic rendering error, such as a missing page template.
if (root == null) return;
// This only applies when the document is an HTML document. This may need to change in the
// future, perhaps configurable, to allow for html and xhtml and perhaps others. Does SVG
// use stylesheets?
if (!root.getName().equals("html")) return;
int stylesheets = includedStylesheets.size();
if (stylesheets > 0)
{
Element head = root.find("head");
if (head == null) head = root.elementAt(0, "head");
for (int i = 0; i < stylesheets; i++)
includedStylesheets.get(i).add(head, i);
}
Element body = root.find("body");
if (body == null) return;
// TAPESTRY-2364
for (String scriptURL : scripts)
{
body.element("script", "src", scriptURL, "type", "text/javascript");
}
boolean blockNeeded = (developmentMode && !scripts.isEmpty()) || scriptBlock.length() > 0;
if (blockNeeded)
{
Element e = body.element("script", "type", "text/javascript");
e.raw("\n<!--\n");
if (developmentMode)
e.raw("Tapestry.DEBUG_ENABLED = true;\n");
e.raw("Tapestry.onDOMLoaded(function() {\n");
e.raw(scriptBlock.toString());
e.raw("});\n");
e.raw("// -->\n");
}
}