Author: hlship
Date: Fri Jul 15 00:12:50 2011
New Revision: 1146927
URL: http://svn.apache.org/viewvc?rev=1146927&view=rev
Log:
TAP-1366: Insert <script> elements before first <script> element (if present),
or at bottom of <head> element
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/added_scripts_go_before_existing_script.txt
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java?rev=1146927&r1=1146926&r2=1146927&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
Fri Jul 15 00:12:50 2011
@@ -16,7 +16,6 @@ package org.apache.tapestry5.internal.se
import org.apache.tapestry5.dom.Document;
import org.apache.tapestry5.dom.Element;
-import org.apache.tapestry5.dom.Node;
import org.apache.tapestry5.func.F;
import org.apache.tapestry5.func.Worker;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
@@ -137,11 +136,11 @@ public class DocumentLinkerImpl implemen
if (!rootElementName.equals("html"))
throw new
RuntimeException(ServicesMessages.documentMissingHTMLRoot(rootElementName));
- Element container = findOrCreateElement(root, "head", true);
+ Element head = findOrCreateElement(root, "head", true);
// TAPESTRY-2364
- addScriptLinksForIncludedScripts(container, scripts);
+ addScriptLinksForIncludedScripts(head, scripts);
if (hasDynamicScript)
addDynamicScriptBlock(findOrCreateElement(root, "body", false));
@@ -227,28 +226,34 @@ public class DocumentLinkerImpl implemen
}
/**
- * Adds a script link for each included script to the top of the container
(the <head>).
- * and just after css
+ * Adds a script link for each included script to the top of the the
{@code <head>} element.
+ * The new elements are inserted just before the first {@code <script>}
tag, or appended at
+ * the end.
*
- * @param container element to add the script links to
- * @param scripts scripts to add
+ * @param headElement element to add the script links to
+ * @param scripts scripts URLs to add as {@code <script>} elements
*/
- protected void addScriptLinksForIncludedScripts(Element container,
List<String> scripts)
+ protected void addScriptLinksForIncludedScripts(final Element headElement,
List<String> scripts)
{
// TAP5-1486
- final Element scriptContainer =
container.elementAt(includedStylesheets.size(), "script-container");
+
+ // Find the first existing <script> tag if it exists.
+
+ final Element insertionPoint = headElement.find("script");
Worker<String> addScript = new Worker<String>()
{
public void work(String scriptURL)
{
- scriptContainer.element("script", "type", "text/javascript",
"src", scriptURL);
+ Element e =
+ insertionPoint == null ? headElement.element("script")
:
+ insertionPoint.elementBefore("script");
+
+ e.attributes("type", "text/javascript", "src", scriptURL);
}
};
F.flow(scripts).each(addScript);
-
- scriptContainer.pop();
}
/**
@@ -285,20 +290,4 @@ public class DocumentLinkerImpl implemen
container.pop();
}
-
- Element findExistingElement(Element container, String elementName)
- {
- for (Node n : container.getChildren())
- {
- if (n instanceof Element)
- {
- Element e = (Element) n;
-
- if (e.getName().equalsIgnoreCase(elementName))
- return e;
- }
- }
-
- return null;
- }
}
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/added_scripts_go_before_existing_script.txt
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/added_scripts_go_before_existing_script.txt?rev=1146927&r1=1146926&r2=1146927&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/added_scripts_go_before_existing_script.txt
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/added_scripts_go_before_existing_script.txt
Fri Jul 15 00:12:50 2011
@@ -1 +1 @@
-<html><head><script src="/foo.js"
type="text/javascript"></script><meta></meta><script></script></head></html>
+<html><head><meta></meta><script src="/foo.js"
type="text/javascript"></script><script></script></head></html>
\ No newline at end of file