You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2014/03/13 03:53:35 UTC

svn commit: r1577021 - in /myfaces/core/trunk/impl/src: main/java/org/apache/myfaces/view/facelets/component/ main/java/org/apache/myfaces/view/facelets/tag/jsf/ test/java/org/apache/myfaces/view/facelets/tag/jsf/html/ test/resources/org/apache/myfaces...

Author: lu4242
Date: Thu Mar 13 02:53:35 2014
New Revision: 1577021

URL: http://svn.apache.org/r1577021
Log:
MYFACES-3870 - Attribute <jsf:element elementName="..."> is not working as expected

Added:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ElementNameRule.java   (with props)
Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/JsfElementRenderer.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/_JsfElement.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/JsfElementHandler.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/PassthroughRuleImpl.java
    myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/jsf/html/DefaultHtmlDecoratorTestCase.java
    myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/jsf/html/testConvertTagAttributes1.xhtml

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/JsfElementRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/JsfElementRenderer.java?rev=1577021&r1=1577020&r2=1577021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/JsfElementRenderer.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/JsfElementRenderer.java Thu Mar 13 02:53:35 2014
@@ -19,17 +19,23 @@
 package org.apache.myfaces.view.facelets.component;
 
 import java.io.IOException;
+import java.util.List;
+import java.util.Map;
 import javax.faces.FacesException;
 import javax.faces.component.UIComponent;
+import javax.faces.component.behavior.ClientBehavior;
 import javax.faces.context.FacesContext;
 import javax.faces.context.ResponseWriter;
 import javax.faces.render.Renderer;
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
+import org.apache.myfaces.shared.renderkit.ClientBehaviorEvents;
 import org.apache.myfaces.shared.renderkit.RendererUtils;
+import org.apache.myfaces.shared.renderkit.html.CommonEventUtils;
 import org.apache.myfaces.shared.renderkit.html.CommonPropertyUtils;
 import org.apache.myfaces.shared.renderkit.html.HTML;
 import org.apache.myfaces.shared.renderkit.html.HtmlRenderer;
 import org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils;
+import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
 
 /**
  *
@@ -47,11 +53,6 @@ public class JsfElementRenderer extends 
         return true;
     }
 
-    protected boolean isCommonPropertiesOptimizationEnabled(FacesContext facesContext)
-    {
-        return false;
-    }
-
     public void encodeBegin(FacesContext facesContext, UIComponent component)
         throws IOException
     {
@@ -64,24 +65,46 @@ public class JsfElementRenderer extends 
             throw new FacesException("jsf:element with clientId"
                 + component.getClientId(facesContext) + " requires 'elementName' passthrough attribute");
         }
+        JsfElement jsfElement = (JsfElement) component;
+        Map<String, List<ClientBehavior>> behaviors = jsfElement.getClientBehaviors();
+        
+        if (behaviors != null && !behaviors.isEmpty())
+        {
+            ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
+        }
+        
         writer.startElement(elementName, component);
-        HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
+
+        if (!behaviors.isEmpty())
+        {
+            HtmlRendererUtils.writeIdAndName(writer, component, facesContext);
+        }
+        else
+        {
+            HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
+        }
         
-        if (isCommonPropertiesOptimizationEnabled(facesContext))
+        // Write in the optimized way, because this is a renderer for internal use only
+        long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(component);
+        if (behaviors.isEmpty())
         {
-            long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(component);
-            if (commonPropertiesMarked > 0)
-            {
-                CommonPropertyUtils.renderEventProperties(writer, commonPropertiesMarked, component);
-                CommonPropertyUtils.renderFocusBlurEventProperties(writer, commonPropertiesMarked, component);
-                CommonPropertyUtils.renderChangeSelectEventProperties(writer, commonPropertiesMarked, component);
-            }
+            CommonPropertyUtils.renderEventProperties(writer, commonPropertiesMarked, component);
+            CommonPropertyUtils.renderFocusBlurEventProperties(writer, commonPropertiesMarked, component);
+            CommonPropertyUtils.renderChangeSelectEventProperties(writer, commonPropertiesMarked, component);
         }
         else
         {
-            HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.EVENT_HANDLER_ATTRIBUTES);
-            HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.COMMON_FIELD_EVENT_ATTRIBUTES);
+            long commonEventsMarked = CommonEventUtils.getCommonEventsMarked(component);
+            CommonEventUtils.renderBehaviorizedEventHandlers(facesContext, writer, 
+                   commonPropertiesMarked, commonEventsMarked, component, behaviors);
+            CommonEventUtils.renderBehaviorizedFieldEventHandlers(facesContext, writer, 
+                   commonPropertiesMarked, commonEventsMarked, component, 
+                   component.getClientId(facesContext), behaviors);
         }
+        HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONLOAD_ATTR, component,
+                ClientBehaviorEvents.LOAD, behaviors, HTML.ONLOAD_ATTR);
+        HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONUNLOAD_ATTR, component,
+                ClientBehaviorEvents.UNLOAD, behaviors, HTML.ONUNLOAD_ATTR);
     }
 
     public void encodeChildren(FacesContext facesContext, UIComponent component)
@@ -98,4 +121,14 @@ public class JsfElementRenderer extends 
             Renderer.PASSTHROUGH_RENDERER_LOCALNAME_KEY);
         writer.endElement(elementName);
     }
+    
+    protected boolean isCommonPropertiesOptimizationEnabled(FacesContext facesContext)
+    {
+        return true;
+    }
+    
+    protected boolean isCommonEventsOptimizationEnabled(FacesContext facesContext)
+    {
+        return true;
+    }
 }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/_JsfElement.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/_JsfElement.java?rev=1577021&r1=1577020&r2=1577021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/_JsfElement.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/component/_JsfElement.java Thu Mar 13 02:53:35 2014
@@ -28,6 +28,8 @@ import org.apache.myfaces.buildtools.mav
  */
 @JSFComponent(name="jsf:element",
     clazz = "org.apache.myfaces.view.facelets.component.JsfElement",template=true,
+    implementz = "javax.faces.component.behavior.ClientBehaviorHolder",
+    defaultEventName = "click",
     defaultRendererType="javax.faces.passthrough.Element")
 abstract class _JsfElement extends UIPanel implements 
     _EventProperties, _FocusBlurProperties, _ChangeSelectProperties

Added: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ElementNameRule.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ElementNameRule.java?rev=1577021&view=auto
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ElementNameRule.java (added)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ElementNameRule.java Thu Mar 13 02:53:35 2014
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.myfaces.view.facelets.tag.jsf;
+
+import javax.faces.view.facelets.MetaRule;
+import javax.faces.view.facelets.Metadata;
+import javax.faces.view.facelets.MetadataTarget;
+import javax.faces.view.facelets.TagAttribute;
+
+/**
+ *
+ * @author lu4242
+ */
+final class ElementNameRule extends MetaRule
+{
+    
+    public final static ElementNameRule INSTANCE = new ElementNameRule();
+    
+    @Override
+    public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
+    {
+        if ("elementName".equals(name))
+        {
+            if (!attribute.isLiteral())
+            {
+                Class<?> type = meta.getPropertyType(name);
+                if (type == null)
+                {
+                    type = Object.class;
+                }
+                
+                return new PassthroughRuleImpl.ValueExpressionMetadata(name, type, attribute);
+            }
+            else
+            {
+                return new PassthroughRuleImpl.LiteralAttributeMetadata(name, attribute.getValue());
+            }
+        }
+        return null;
+    }
+    
+}

Propchange: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ElementNameRule.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/JsfElementHandler.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/JsfElementHandler.java?rev=1577021&r1=1577020&r2=1577021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/JsfElementHandler.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/JsfElementHandler.java Thu Mar 13 02:53:35 2014
@@ -19,6 +19,7 @@
 package org.apache.myfaces.view.facelets.tag.jsf;
 
 import javax.faces.view.facelets.ComponentConfig;
+import javax.faces.view.facelets.MetaRuleset;
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
 
 /**
@@ -36,4 +37,13 @@ public class JsfElementHandler extends j
         super(config);
     }
     
+    protected MetaRuleset createMetaRuleset(Class type)
+    {
+        MetaRuleset rules = super.createMetaRuleset(type);
+        
+        rules.addRule(ElementNameRule.INSTANCE);
+        
+        return rules;
+    }
+    
 }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/PassthroughRuleImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/PassthroughRuleImpl.java?rev=1577021&r1=1577020&r2=1577021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/PassthroughRuleImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/PassthroughRuleImpl.java Thu Mar 13 02:53:35 2014
@@ -33,7 +33,7 @@ import org.apache.myfaces.view.facelets.
 final class PassthroughRuleImpl extends MetaRule implements PassthroughRule
 {
 
-    final class LiteralAttributeMetadata extends Metadata
+    final static class LiteralAttributeMetadata extends Metadata
     {
         private final String _name;
         private final String _value;

Modified: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/jsf/html/DefaultHtmlDecoratorTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/jsf/html/DefaultHtmlDecoratorTestCase.java?rev=1577021&r1=1577020&r2=1577021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/jsf/html/DefaultHtmlDecoratorTestCase.java (original)
+++ myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/jsf/html/DefaultHtmlDecoratorTestCase.java Thu Mar 13 02:53:35 2014
@@ -370,6 +370,10 @@ public class DefaultHtmlDecoratorTestCas
         Assert.assertNull(input2.getPassThroughAttributes().get("value"));
         Assert.assertEquals(input2.getAttributes().get("value"), "value1");
         
+        UIComponent input3 = root.findComponent("myForm:box3");
+        Assert.assertFalse(input3 instanceof UIInput);
+        Assert.assertEquals(input3.getRendererType(), "javax.faces.passthrough.Element");        
+        
         //Assert.assertEquals(input2.getPassThroughAttributes().get("elementName"), "meter");
         
         StringWriter sw = new StringWriter();

Modified: myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/jsf/html/testConvertTagAttributes1.xhtml
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/jsf/html/testConvertTagAttributes1.xhtml?rev=1577021&r1=1577020&r2=1577021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/jsf/html/testConvertTagAttributes1.xhtml (original)
+++ myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/jsf/html/testConvertTagAttributes1.xhtml Thu Mar 13 02:53:35 2014
@@ -25,14 +25,24 @@
     <form jsf:id="myForm">
         <input jsf:id="box1" type="text"
                jsf:value="#{test.value}" jsf:customAttr="SomeValue"
+               onclick="alert('hello')"
                placeholder="Enter text" 
                pt:data_up="Going Up"/>
         
         <input jsf:id="box2" pt:elementName="meter"
                jsf:value="#{test.value}" jsf:customAttr="SomeValue"
+               onclick="alert('hello')"
                placeholder="Enter text" 
                pt:data_up="Going Up">Hello World!</input>
         
+        <jsf:element id="box3" elementName="meter" 
+               value="#{test.value}" customAttr="SomeValue"
+               onclick="alert('hello')"
+               placeholder="Enter text" 
+               pt:data_up="Going Up">
+               Hello Element!
+        </jsf:element>
+        
     </form>
 </body>
 </html>
\ No newline at end of file