Author: hlship Date: Wed Oct 19 23:23:19 2011 New Revision: 1186562 URL: http://svn.apache.org/viewvc?rev=1186562&view=rev Log: TAP5-1708: Add support for marking a stylesheet link as the Ajax insertion point
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/stylesheet_insertion_point.txt Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PartialMarkupDocumentLinker.java tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetLink.java tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetOptions.java tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PartialMarkupDocumentLinker.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PartialMarkupDocumentLinker.java?rev=1186562&r1=1186561&r2=1186562&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PartialMarkupDocumentLinker.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/PartialMarkupDocumentLinker.java Wed Oct 19 23:23:19 2011 @@ -1,4 +1,4 @@ -// Copyright 2008, 2010 The Apache Software Foundation +// Copyright 2008, 2010, 2011 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,14 +14,14 @@ package org.apache.tapestry5.internal.services; -import java.util.Map; - import org.apache.tapestry5.ioc.internal.util.CollectionFactory; import org.apache.tapestry5.json.JSONArray; import org.apache.tapestry5.json.JSONObject; import org.apache.tapestry5.services.javascript.InitializationPriority; import org.apache.tapestry5.services.javascript.StylesheetLink; +import java.util.Map; + public class PartialMarkupDocumentLinker implements DocumentLinker { private final JSONArray scripts = new JSONArray(); @@ -39,9 +39,9 @@ public class PartialMarkupDocumentLinker { JSONObject object = new JSONObject( - "href", stylesheet.getURL(), + "href", stylesheet.getURL(), - "media", stylesheet.getOptions().getMedia()); + "media", stylesheet.getOptions().media); stylesheets.put(object); } @@ -59,9 +59,8 @@ public class PartialMarkupDocumentLinker /** * Commits changes, adding one or more keys to the reply. - * - * @param reply - * JSON Object to be sent to client + * + * @param reply JSON Object to be sent to client */ public void commit(JSONObject reply) { Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetLink.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetLink.java?rev=1186562&r1=1186561&r2=1186562&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetLink.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetLink.java Wed Oct 19 23:23:19 2011 @@ -1,4 +1,4 @@ -// Copyright 2008, 2009, 2010 The Apache Software Foundation +// Copyright 2008, 2009, 2010, 2011 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -24,7 +24,7 @@ import org.apache.tapestry5.ioc.internal /** * Captures the information needed to create a stylesheet link in the final {@link Document}, or * as part of a JSON partial page render response. - * + * * @see DocumentLinker * @see JavaScriptStack * @since 5.2.0 @@ -75,9 +75,8 @@ public final class StylesheetLink /** * Invoked to add the stylesheet link to a container element. - * - * @param container - * to add the new element to + * + * @param container to add the new element to */ public void add(Element container) { @@ -85,12 +84,18 @@ public final class StylesheetLink boolean hasCondition = InternalUtils.isNonBlank(condition); if (hasCondition) + { container.raw(String.format("\n<!--[if %s]>\n", condition)); + } - container.element("link", "href", url, "rel", "stylesheet", "type", "text/css", "media", options.getMedia()); + String rel = options.ajaxInsertionPoint ? "stylesheet t-ajax-insertion-point" : "stylesheet"; + + container.element("link", "href", url, "rel", rel, "type", "text/css", "media", options.media); if (hasCondition) + { container.raw("\n<![endif]-->\n"); + } } @Override Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetOptions.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetOptions.java?rev=1186562&r1=1186561&r2=1186562&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetOptions.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/StylesheetOptions.java Wed Oct 19 23:23:19 2011 @@ -1,4 +1,4 @@ -// Copyright 2010, 29011 The Apache Software Foundation +// Copyright 2010, 2011 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,28 +17,84 @@ package org.apache.tapestry5.services.ja import org.apache.tapestry5.internal.TapestryInternalUtils; /** - * Provides options to describe options associated with importing a stylesheet onto a page. + * Provides options to describe options associated with importing a stylesheet onto a page. Stylesheet options + * are immutable. * * @since 5.2.0 */ -public class StylesheetOptions +public final class StylesheetOptions { - private final String media, condition; + /** + * The media associated with this stylesheet, i.e., "print". Becomes the media attribute + * of the <link> tag. May be null. + */ + public final String media; + + /** + * The Internet Explorer condition associated with the link. When non-blank, the + * <link> element will be written inside a specially formatted comment interpreted + * by Internet Explorer. Usually null, and only used for full page renders (not partial page renders). + * + * @see <a href="http://en.wikipedia.org/wiki/Conditional_comment">http://en.wikipedia.org/wiki/Conditional_comment</a> + */ + public final String condition; + + /** + * If true, then this stylesheet is the insertion point for Ajax operations; any added CSS links will be inserted before this link. Only at most + * one CSS link should be the insertion point. Only used for full page renders (not partial page renders). When this is true, the {@code <link>} element's + * ref attribute is wrtten out as "stylesheet t-ajax-insertion-point". + */ + public boolean ajaxInsertionPoint; + + /** + * Returns a new options object with media as null (that is, unspecified), no condition, and not the Ajax insertion point. + */ + public StylesheetOptions() + { + this(null); + } public StylesheetOptions(String media) { this(media, null); } + /** + * @deprecated In 5.3, may be removed in a later release. Use {@link #StylesheetOptions(String)} and {@link #withCondition(String)}} instead. + */ public StylesheetOptions(String media, String condition) { + this(media, condition, false); + } + + private StylesheetOptions(String media, String condition, boolean ajaxInsertionPoint) + { this.media = media; this.condition = condition; + this.ajaxInsertionPoint = ajaxInsertionPoint; + } + + /** + * Returns a new options object with the indicated {@linkplain #condition Internet Explorer condition}. @since 5.3 + */ + public StylesheetOptions withCondition(String condition) + { + return new StylesheetOptions(media, condition, ajaxInsertionPoint); + } + + /** + * Returns a new options object with the {@link #ajaxInsertionPoint} flag set to true. + */ + public StylesheetOptions asAjaxInsertionPoint() + { + return new StylesheetOptions(media, condition, true); } /** * The media associated with this stylesheet, i.e., "print". Becomes the media attribute * of the <link> tag. May be null. + * + * @deprecated in 5.3, may be removed in a later release, use the {@link #media} field instead */ public String getMedia() { @@ -51,6 +107,7 @@ public class StylesheetOptions * by Internet Explorer. Usually null. * * @see <a href="http://en.wikipedia.org/wiki/Conditional_comment">http://en.wikipedia.org/wiki/Conditional_comment</a> + * @deprecated in 5.3, may be removed in a later release, use the {@link #condition} field instead */ public String getCondition() { @@ -60,7 +117,29 @@ public class StylesheetOptions @Override public String toString() { - return String.format("StylesheetOptions[media=%s condition=%s]", media, condition); + StringBuilder builder = new StringBuilder("StyleSheetOptions["); + + String sep = ""; + + if (media != null) + { + builder.append("media=").append(media); + sep = " "; + } + + if (condition != null) + { + builder.append(sep).append("condition=").append(condition); + sep = " "; + } + + if (ajaxInsertionPoint) + { + builder.append(sep).append("ajaxInsertionPoint=true"); + sep = " "; + } + + return builder.append("]").toString(); } @Override @@ -74,7 +153,7 @@ public class StylesheetOptions StylesheetOptions sso = (StylesheetOptions) obj; - return TapestryInternalUtils.isEqual(media, sso.media) + return ajaxInsertionPoint == sso.ajaxInsertionPoint && TapestryInternalUtils.isEqual(media, sso.media) && TapestryInternalUtils.isEqual(condition, sso.condition); } } Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java?rev=1186562&r1=1186561&r2=1186562&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java Wed Oct 19 23:23:19 2011 @@ -50,8 +50,7 @@ public class DocumentLinkerImplTest exte { linker.updateDocument(document); unreachable(); - } - catch (RuntimeException ex) + } catch (RuntimeException ex) { assertEquals( ex.getMessage(), @@ -380,11 +379,28 @@ public class DocumentLinkerImplTest exte DocumentLinkerImpl linker = new DocumentLinkerImpl(true, "1.2.3", true); linker.addStylesheetLink(new StylesheetLink("everybody.css")); - linker.addStylesheetLink(new StylesheetLink("just_ie.css", new StylesheetOptions(null, "IE"))); + linker.addStylesheetLink(new StylesheetLink("just_ie.css", new StylesheetOptions().withCondition("IE"))); linker.updateDocument(document); assertEquals(document.toString(), readFile("ie_conditional_stylesheet.txt")); + } + + @Test + public void stylesheet_insertion_point() throws Exception + { + Document document = new Document(); + + document.newRootElement("html"); + + DocumentLinkerImpl linker = new DocumentLinkerImpl(true, "1.2.3", true); + + linker.addStylesheetLink(new StylesheetLink("whatever.css")); + linker.addStylesheetLink(new StylesheetLink("insertion-point.css", new StylesheetOptions().asAjaxInsertionPoint())); + + linker.updateDocument(document); + + assertEquals(document.toString(), readFile("stylesheet_insertion_point.txt")); } } Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/stylesheet_insertion_point.txt URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/stylesheet_insertion_point.txt?rev=1186562&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/stylesheet_insertion_point.txt (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/stylesheet_insertion_point.txt Wed Oct 19 23:23:19 2011 @@ -0,0 +1 @@ +<html><head><link type="text/css" rel="stylesheet" href="whatever.css"/><link type="text/css" rel="stylesheet t-ajax-insertion-point" href="insertion-point.css"/></head></html> \ No newline at end of file