tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andy...@apache.org
Subject svn commit: r818538 - in /tapestry/tapestry4/trunk/tapestry-framework/src: java/org/apache/tapestry/html/ java/org/apache/tapestry/markup/ test/org/apache/tapestry/html/
Date Thu, 24 Sep 2009 16:31:05 GMT
Author: andyhot
Date: Thu Sep 24 16:31:04 2009
New Revision: 818538

URL: http://svn.apache.org/viewvc?rev=818538&view=rev
Log:
TAPESTRY-2750: Enhance Style component to specifically target IE browsers

Added:
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/markup/MarkupUtils.java
Modified:
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/RelationBean.java
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Shell.java
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Style.jwc
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/html/TestRelation.java

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java?rev=818538&r1=818537&r2=818538&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java
(original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java
Thu Sep 24 16:31:04 2009
@@ -1,4 +1,4 @@
-// Copyright Aug 2, 2006 The Apache Software Foundation
+// Copyright 2006 - 2009 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.
@@ -19,6 +19,7 @@
 import org.apache.tapestry.IMarkupWriter;
 import org.apache.tapestry.IRequestCycle;
 import org.apache.tapestry.markup.MarkupWriterSource;
+import org.apache.tapestry.markup.MarkupUtils;
 import org.apache.tapestry.util.ContentType;
 
 import java.io.PrintWriter;
@@ -83,6 +84,10 @@
         bean.setRev(getRev());
         bean.setTitle(getTitle());
         bean.setType(getType());
+
+        if (isParameterBound("ieCondition"))
+            bean.setIeCondition(getIeCondition());
+
         shell.addRelation(bean);
     }
 
@@ -97,16 +102,24 @@
         StringWriter sWriter = new StringWriter();
         IMarkupWriter nested = getMarkupWriterSource().newMarkupWriter(new PrintWriter(sWriter),
                                                                        new ContentType(writer.getContentType()));
+        String ieCondition = getIeCondition();
+        if (ieCondition != null)
+            MarkupUtils.beginConditionalComment(nested, ieCondition);
 
         nested.begin("style");
         nested.attribute("type", "text/css");
-
         if (getMedia()!=null)
             nested.attribute("media", getMedia());
         if (getTitle()!=null)
             nested.attribute("title", getTitle());
 
         renderBody(nested, cycle);
+
+        nested.end();
+
+        if (ieCondition != null)
+            MarkupUtils.endConditionalComment(nested);
+
         nested.close();
 
         shell.includeAdditionalContent(sWriter.toString());
@@ -126,6 +139,11 @@
 
     public abstract String getMedia();
 
+    /**
+     * @since 4.1.7
+     */
+    public abstract String getIeCondition();
+
     /* injected */
     public abstract MarkupWriterSource getMarkupWriterSource();
 

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/RelationBean.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/RelationBean.java?rev=818538&r1=818537&r2=818538&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/RelationBean.java
(original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/RelationBean.java
Thu Sep 24 16:31:04 2009
@@ -33,6 +33,8 @@
     private String _type;
     /** Title of the relation. */
     private String _title;
+    /** Condition to allow targeting this relation to IE browsers. */
+    private String _ieCondition;
     
     public RelationBean()
     {        
@@ -98,17 +100,21 @@
         _title = title;
     }
 
+    public String getIeCondition()
+    {
+        return _ieCondition;
+    }
+
+    public void setIeCondition(String ieCondition)
+    {
+        _ieCondition = ieCondition;
+    }
+
     public int hashCode()
     {
         final int prime = 31;
-        int result = 1;
-        result = prime * result + ((_href == null) ? 0 : _href.hashCode());
-        result = prime * result + ((_media == null) ? 0 : _media.hashCode());
-        result = prime * result + ((_rel == null) ? 0 : _rel.hashCode());
-        result = prime * result + ((_rev == null) ? 0 : _rev.hashCode());
-        result = prime * result + ((_title == null) ? 0 : _title.hashCode());
-        result = prime * result + ((_type == null) ? 0 : _type.hashCode());
-        return result;
+        // href will most probably be the difference between two relations
+        return prime + ((_href == null) ? 0 : _href.hashCode());
     }
 
     public boolean equals(Object obj)
@@ -135,6 +141,9 @@
         if (_type == null) {
             if (other._type != null) return false;
         } else if (!_type.equals(other._type)) return false;
+        if (_ieCondition == null) {
+            if (other._ieCondition != null) return false;
+        } else if (!_ieCondition.equals(other._ieCondition)) return false;        
         return true;
     }  
     

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Shell.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Shell.java?rev=818538&r1=818537&r2=818538&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Shell.java
(original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Shell.java
Thu Sep 24 16:31:04 2009
@@ -1,4 +1,4 @@
-// Copyright 2004, 2005 The Apache Software Foundation
+// Copyright 2004 - 2009 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,6 +17,7 @@
 import org.apache.commons.lang.StringUtils;
 import org.apache.hivemind.HiveMind;
 import org.apache.tapestry.*;
+import org.apache.tapestry.markup.MarkupUtils;
 import org.apache.tapestry.coerce.ValueConverter;
 import org.apache.tapestry.engine.IEngineService;
 import org.apache.tapestry.engine.ILink;
@@ -227,6 +228,11 @@
 
     private void writeRelation(IMarkupWriter writer, RelationBean relationBean)
     {
+        String ieCondition = relationBean.getIeCondition();
+
+        if (ieCondition!=null)
+            MarkupUtils.beginConditionalComment(writer, ieCondition);
+
         writer.beginEmpty("link");
 
         writeAttributeIfNotNull(writer, "rel", relationBean.getRel());
@@ -235,8 +241,13 @@
         writeAttributeIfNotNull(writer, "media", relationBean.getMedia());
         writeAttributeIfNotNull(writer, "title", relationBean.getTitle());
         writeAttributeIfNotNull(writer, "href", relationBean.getHref());
-        
-        writer.println();
+
+        writer.closeTag();
+
+        if (ieCondition!=null)
+            MarkupUtils.endConditionalComment(writer);
+        else
+            writer.println();
     }
 
     private void writeAttributeIfNotNull(IMarkupWriter writer, String name, String value)

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Style.jwc
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Style.jwc?rev=818538&r1=818537&r2=818538&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Style.jwc
(original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Style.jwc
Thu Sep 24 16:31:04 2009
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!-- 
-   Copyright 2004, 2005 The Apache Software Foundation
+   Copyright 2004 - 2009 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.
@@ -51,6 +51,14 @@
     Specifies on which device this stylesheet will apply.
     </description>
   </parameter>
+
+  <parameter name="ieCondition">
+    <description>
+    If specified, the style (or the css file) will only target IE browsers by surrounding
the normal output
+    with a condition comment. That comment starts with &lt;!--[if IE 6]&gt; and ends
with &lt;![endif]--&gt;
+    Possible values for this parameter include IE, IE 6, lt IE 6, gte IE 5 e.t.c.
+    </description>
+  </parameter>
   
   <property name="type" initial-value="literal:text/css"/>
   <property name="useBody" initial-value="ognl:true"/>

Added: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/markup/MarkupUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/markup/MarkupUtils.java?rev=818538&view=auto
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/markup/MarkupUtils.java
(added)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/markup/MarkupUtils.java
Thu Sep 24 16:31:04 2009
@@ -0,0 +1,41 @@
+// Copyright 2009 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.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.markup;
+
+import org.apache.tapestry.IMarkupWriter;
+
+/**
+ * MarkupUtils includes some useful functions related to {@link IMarkupWriter}.
+ *
+ * @author Andreas Andreou
+ */
+public class MarkupUtils {
+    /* defeat instantiation */
+    private MarkupUtils() { }
+
+    public static void beginConditionalComment(IMarkupWriter writer, String condition)
+    {
+        writer.println();
+        writer.printRaw("<!--[if " + condition + "]>");
+        writer.println();
+    }
+
+    public static void endConditionalComment(IMarkupWriter writer)
+    {
+        writer.println();
+        writer.printRaw("<![endif]-->");
+        writer.println();
+    }
+}

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/html/TestRelation.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/html/TestRelation.java?rev=818538&r1=818537&r2=818538&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/html/TestRelation.java
(original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/html/TestRelation.java
Thu Sep 24 16:31:04 2009
@@ -1,4 +1,4 @@
-// Copyright 2005 The Apache Software Foundation
+// Copyright 2005-2009 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,17 +14,25 @@
 
 package org.apache.tapestry.html;
 
-import static org.easymock.EasyMock.expect;
+import java.io.PrintWriter;
 
 import org.apache.hivemind.ApplicationRuntimeException;
 import org.apache.hivemind.Location;
 import org.apache.tapestry.BaseComponentTestCase;
 import org.apache.tapestry.IMarkupWriter;
 import org.apache.tapestry.IRequestCycle;
+import org.apache.tapestry.IRender;
+import org.apache.tapestry.util.ContentType;
+import org.apache.tapestry.markup.MarkupWriterSource;
+import org.apache.tapestry.markup.MarkupWriterImpl;
+import org.apache.tapestry.markup.AsciiMarkupFilter;
 import org.testng.annotations.Test;
 
+import static org.easymock.EasyMock.*;
+import org.easymock.IAnswer;
+
 /**
- * Tests for the {@link org.apache.tapestry.html.Relation}&nbsp; component.
+ * Tests for the {@link org.apache.tapestry.html.Relation} component.
  * 
  * @author Andreas Andreou
  * @since 4.1.1
@@ -32,7 +40,8 @@
 @Test(sequential=true)
 public class TestRelation extends BaseComponentTestCase
 {
-    
+    private static final String SYSTEM_NEWLINE = (String)java.security.AccessController.doPrivileged(
+            new sun.security.action.GetPropertyAction("line.separator"));
     /**
      * Test that Relation does nothing when the entire page is rewinding
      */
@@ -121,7 +130,64 @@
         }
 
         verify();         
-    }    
+    }
+
+    public void testIeStyle()
+    {
+        IMarkupWriter writer = newBufferWriter();
+        IRequestCycle cycle = newCycle(false);
+        Location componentLocation = newMock(Location.class);
+        StringBuffer shellOutput = new StringBuffer();
+
+        MarkupWriterSource source = newMock(MarkupWriterSource.class);
+        expect(source.newMarkupWriter(isA(PrintWriter.class), isA(ContentType.class))).andAnswer(
+                new IAnswer<IMarkupWriter>() {
+                    public IMarkupWriter answer() throws Throwable {
+                        return new MarkupWriterImpl("text/html",
+                                (PrintWriter) getCurrentArguments()[0],
+                                new AsciiMarkupFilter());
+                    }
+                }
+        );
+
+        Relation relation = newInstance(Relation.class,
+                "location", componentLocation,
+                "markupWriterSource", source,
+                "useBody", true,
+                "ieCondition", "IE"
+        );
+
+        relation.addBody(new IRender()
+        {
+            public void render(IMarkupWriter writer, IRequestCycle cycle)
+            {
+                writer.print("Some css rules");
+            }
+        });
+
+        trainResponseBuilder(cycle, writer);
+
+        Shell shell = newInstance(Shell.class, "contentBuffer", shellOutput);
+
+        expect(cycle.renderStackPush(relation)).andReturn(relation);
+
+        trainGetShellFromCycle(cycle, shell);
+
+        expect(cycle.renderStackPop()).andReturn(relation);
+
+        replay();
+
+        relation.render(writer, cycle);
+
+        // nothing should be output - just pushed to Shell
+        assertBuffer("");
+
+        // now check what Shell has gathered
+        assertEquals(shellOutput.toString(), SYSTEM_NEWLINE + "<!--[if IE]>" + SYSTEM_NEWLINE
+
+                "<style type=\"text/css\">Some css rules</style>" + SYSTEM_NEWLINE
+
+                "<![endif]-->" + SYSTEM_NEWLINE);
+        verify();
+    }
     
     protected void trainGetShellFromCycle(IRequestCycle cycle, Shell shell)
     {



Mime
View raw message