Author: hsaputra
Date: Sat Jul 10 00:24:47 2010
New Revision: 962730
URL: http://svn.apache.org/viewvc?rev=962730&view=rev
Log:
Patch from Jan Luehe | More efficient way of adding content in
RenderingGadgetRewriter#rewrite
Modified:
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java
Modified:
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java?rev=962730&r1=962729&r2=962730&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java
(original)
+++
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java
Sat Jul 10 00:24:47 2010
@@ -141,29 +141,21 @@ public class RenderingGadgetRewriter imp
Element head =
(Element)DomUtil.getFirstNamedChildNode(document.getDocumentElement(), "head");
- // Remove all the elements currently in head and add them back after we
inject content
- NodeList children = head.getChildNodes();
- List<Node> existingHeadContent =
Lists.newArrayListWithExpectedSize(children.getLength());
- for (int i = 0; i < children.getLength(); i++) {
- existingHeadContent.add(children.item(i));
- }
-
- for (Node n : existingHeadContent) {
- head.removeChild(n);
- }
+ // Insert new content before any of the existing children of the head
element
+ Node firstHeadChild = head.getFirstChild();
// Only inject default styles if no doctype was specified.
if (document.getDoctype() == null) {
Element defaultStyle = document.createElement("style");
defaultStyle.setAttribute("type", "text/css");
- head.appendChild(defaultStyle);
+ head.insertBefore(defaultStyle, firstHeadChild);
defaultStyle.appendChild(defaultStyle.getOwnerDocument().
createTextNode(DEFAULT_CSS));
}
injectBaseTag(gadget, head);
- injectGadgetBeacon(gadget, head);
- injectFeatureLibraries(gadget, head);
+ injectGadgetBeacon(gadget, head, firstHeadChild);
+ injectFeatureLibraries(gadget, head, firstHeadChild);
// This can be one script block.
Element mainScriptTag = document.createElement("script");
@@ -175,17 +167,12 @@ public class RenderingGadgetRewriter imp
injectPreloads(gadget, mainScriptTag);
// We need to inject our script before any developer scripts.
- head.appendChild(mainScriptTag);
+ head.insertBefore(mainScriptTag, firstHeadChild);
Element body =
(Element)DomUtil.getFirstNamedChildNode(document.getDocumentElement(), "body");
body.setAttribute("dir", bundle.getLanguageDirection());
- // re append head content
- for (Node node : existingHeadContent) {
- head.appendChild(node);
- }
-
injectOnLoadHandlers(body);
mutableContent.documentChanged();
@@ -215,16 +202,18 @@ public class RenderingGadgetRewriter imp
"gadgets.util.runOnLoadHandlers();"));
}
- protected void injectGadgetBeacon(Gadget gadget, Node headTag) throws
GadgetException {
+ protected void injectGadgetBeacon(Gadget gadget, Node headTag, Node
firstHeadChild)
+ throws GadgetException {
Element beaconNode = headTag.getOwnerDocument().createElement("script");
beaconNode.setTextContent(IS_GADGET_BEACON);
- headTag.appendChild(beaconNode);
+ headTag.insertBefore(beaconNode, firstHeadChild);
}
/**
* Injects javascript libraries needed to satisfy feature dependencies.
*/
- protected void injectFeatureLibraries(Gadget gadget, Node headTag) throws
GadgetException {
+ protected void injectFeatureLibraries(Gadget gadget, Node headTag, Node
firstHeadChild)
+ throws GadgetException {
// TODO: If there isn't any js in the document, we can skip this.
Unfortunately, that means
// both script tags (easy to detect) and event handlers (much more
complex).
GadgetContext context = gadget.getContext();
@@ -242,7 +231,7 @@ public class RenderingGadgetRewriter imp
String jsUrl = jsUriManager.makeExternJsUri(gadget,
externForcedLibs).toString();
Element libsTag = headTag.getOwnerDocument().createElement("script");
libsTag.setAttribute("src", StringUtils.replace(jsUrl, "&", "&"));
- headTag.appendChild(libsTag);
+ headTag.insertBefore(libsTag, firstHeadChild);
}
List<String> unsupported = Lists.newLinkedList();
@@ -284,7 +273,7 @@ public class RenderingGadgetRewriter imp
String jsUrl = jsUriManager.makeExternJsUri(gadget,
externGadgetLibs).toString();
Element libsTag = headTag.getOwnerDocument().createElement("script");
libsTag.setAttribute("src", StringUtils.replace(jsUrl, "&", "&"));
- headTag.appendChild(libsTag);
+ headTag.insertBefore(libsTag, firstHeadChild);
}
} else {
inlineResources.addAll(gadgetResources);
@@ -323,13 +312,13 @@ public class RenderingGadgetRewriter imp
if (resource.isExternal()) {
if (inlineJs.length() > 0) {
Element inlineTag =
headTag.getOwnerDocument().createElement("script");
- headTag.appendChild(inlineTag);
+ headTag.insertBefore(inlineTag, firstHeadChild);
inlineTag.appendChild(headTag.getOwnerDocument().createTextNode(inlineJs.toString()));
inlineJs.setLength(0);
}
Element referenceTag =
headTag.getOwnerDocument().createElement("script");
referenceTag.setAttribute("src", StringUtils.replace(theContent, "&",
"&"));
- headTag.appendChild(referenceTag);
+ headTag.insertBefore(referenceTag, firstHeadChild);
} else {
inlineJs.append(theContent).append(";\n");
}
@@ -339,7 +328,7 @@ public class RenderingGadgetRewriter imp
if (inlineJs.length() > 0) {
Element inlineTag = headTag.getOwnerDocument().createElement("script");
- headTag.appendChild(inlineTag);
+ headTag.insertBefore(inlineTag, firstHeadChild);
inlineTag.appendChild(headTag.getOwnerDocument().createTextNode(inlineJs.toString()));
}
}