myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jak...@apache.org
Subject svn commit: r937069 - in /myfaces/shared/trunk_4.0.x/core/src: main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java test/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImplTest.java
Date Thu, 22 Apr 2010 21:19:29 GMT
Author: jakobk
Date: Thu Apr 22 21:19:29 2010
New Revision: 937069

URL: http://svn.apache.org/viewvc?rev=937069&view=rev
Log:
MYFACES-2668 Rendering HTML elements inside a script section confuses HtmlResponseWriterImpl
(including test case)

Added:
    myfaces/shared/trunk_4.0.x/core/src/test/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImplTest.java
  (with props)
Modified:
    myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java

Modified: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java?rev=937069&r1=937068&r2=937069&view=diff
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
(original)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
Thu Apr 22 21:19:29 2010
@@ -56,7 +56,7 @@ public class HtmlResponseWriterImpl
     private String _contentType;
     private String _characterEncoding;
     private String _startElementName;
-    private Boolean _isScript;
+    private Boolean _isInsideScript;
     private Boolean _isStyle;
     private Boolean _isTextArea;
     private UIComponent _startElementUIComponent;
@@ -178,6 +178,14 @@ public class HtmlResponseWriterImpl
         _startElementName = name;
         _startElementUIComponent = uiComponent;
         _startTagOpen = true;
+        
+        // handle a <script> start
+        if(isScript(name))
+        {
+            _isInsideScript = Boolean.TRUE;
+            _isStyle = Boolean.FALSE;
+            _isTextArea = Boolean.FALSE;
+        }
     }
 
     private void closeStartTagIfNecessary() throws IOException
@@ -195,7 +203,7 @@ public class HtmlResponseWriterImpl
             {
                 _writer.write('>');
 
-                if(isScript())
+                if(isScript(_startElementName))
                 {
                     if(HtmlRendererUtils.isXHTMLContentType(_contentType))
                     {
@@ -218,7 +226,6 @@ public class HtmlResponseWriterImpl
     {
         _startElementName = null;
         _startElementUIComponent = null;
-        _isScript = null;
         _isStyle = null;
         _isTextArea = null;
     }
@@ -278,7 +285,7 @@ public class HtmlResponseWriterImpl
     private void writeEndTag(String name)
         throws IOException
     {
-        if(isScript())
+        if(isScript(name)) 
         {
             if(HtmlRendererUtils.isXHTMLContentType(_contentType))
             {
@@ -291,6 +298,9 @@ public class HtmlResponseWriterImpl
             {
                 _writer.write(COMMENT_COMMENT_END);
             }
+            
+            // reset _isInsideScript
+            _isInsideScript = Boolean.FALSE;
         }
 
         _writer.write("</");
@@ -459,14 +469,17 @@ public class HtmlResponseWriterImpl
         initializeStartedTagInfo();
 
         return (_isStyle != null && _isStyle.booleanValue()) ||
-                (_isScript != null && _isScript.booleanValue());
+                (_isInsideScript != null && _isInsideScript.booleanValue());
     }
-
-    private boolean isScript()
+    
+    /**
+     * Is the given element a script tag?
+     * @param element
+     * @return
+     */
+    private boolean isScript(String element)
     {
-        initializeStartedTagInfo();
-
-        return (_isScript != null && _isScript.booleanValue());
+        return (HTML.SCRIPT_ELEM.equalsIgnoreCase(element));
     }
 
     private boolean isTextarea()
@@ -480,19 +493,6 @@ public class HtmlResponseWriterImpl
     {
         if(_startElementName != null)
         {
-            if(_isScript==null)
-            {
-                if(_startElementName.equalsIgnoreCase(HTML.SCRIPT_ELEM))
-                {
-                    _isScript = Boolean.TRUE;
-                    _isStyle = Boolean.FALSE;
-                    _isTextArea = Boolean.FALSE;
-                }
-                else
-                {
-                    _isScript = Boolean.FALSE;
-                }
-            }
             if(_isStyle == null)
             {
                 if(_startElementName.equalsIgnoreCase(org.apache.myfaces.shared.renderkit.html.HTML.STYLE_ELEM))

Added: myfaces/shared/trunk_4.0.x/core/src/test/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImplTest.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/test/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImplTest.java?rev=937069&view=auto
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/test/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImplTest.java
(added)
+++ myfaces/shared/trunk_4.0.x/core/src/test/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImplTest.java
Thu Apr 22 21:19:29 2010
@@ -0,0 +1,146 @@
+/*
+ * 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.shared.renderkit.html;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.lang.reflect.Field;
+
+import org.apache.myfaces.test.base.AbstractJsfTestCase;
+
+/**
+ * Test class for HtmlResponseWriterImpl.
+ * 
+ * @author Jakob Korherr (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class HtmlResponseWriterImplTest extends AbstractJsfTestCase
+{
+    
+    private static final String COMMENT_START = "<!--";
+    private static final String COMMENT_END = "//-->";
+
+    private StringWriter _stringWriter;
+    private HtmlResponseWriterImpl _writer;
+    
+    public HtmlResponseWriterImplTest(String name)
+    {
+        super(name);
+    }
+
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        
+        _stringWriter = new StringWriter();
+        _writer = new HtmlResponseWriterImpl(_stringWriter, "text/html", "ISO-8859-1");
+    }
+
+    @Override
+    protected void tearDown() throws Exception
+    {
+        _writer = null;
+        _stringWriter = null;
+        
+        super.tearDown();
+    }
+    
+    /**
+     * This test tests if it is possible to render HTML elements inside
+     * a script section without confusing the HtmlResponseWriterImpl.
+     * The related issue to this test is MYFACES-2668.
+     * 
+     * @throws IOException
+     * @throws NoSuchFieldException 
+     * @throws SecurityException 
+     * @throws IllegalAccessException 
+     * @throws IllegalArgumentException 
+     */
+    public void testHtmlElementsInsideScript() throws IOException, SecurityException, 
+            NoSuchFieldException, IllegalArgumentException, IllegalAccessException
+    {
+        // use reflection to get the field _isInsideScript to verify
+        // the internal behavior of HtmlResponseWriterImpl
+        Field insideScriptField = _writer.getClass().getDeclaredField("_isInsideScript");
+        insideScriptField.setAccessible(true);
+        
+        _writer.startDocument();
+        _writer.startElement("head", null);
+        
+        assertFalse("We have not entered a script element yet, so _isInsideScript should
be " +
+                "false (or null).", getFieldBooleanValue(insideScriptField, _writer, false));
+        
+        _writer.startElement("script", null);
+        
+        assertTrue("We have now entered a script element, so _isInsideScript should be "
+
+                "true.", getFieldBooleanValue(insideScriptField, _writer, false));
+        
+        _writer.startElement("table", null);
+        _writer.startElement("tr", null);
+        _writer.startElement("td", null);
+        
+        assertTrue("We have now opened various elements inside a script element, "+
+                "but _isInsideScript should still be true.",
+                getFieldBooleanValue(insideScriptField, _writer, false));
+        
+        _writer.write("column value");
+        
+        assertTrue("We have now written some text inside a script element, "+
+                "but _isInsideScript should still be true.",
+                getFieldBooleanValue(insideScriptField, _writer, false));
+        
+        _writer.endElement("td");
+        _writer.endElement("tr");
+        _writer.endElement("table");
+        _writer.endElement("script");
+        
+        assertFalse("We have now closed the script element, so _isInsideScript should be
" +
+                "false (or null).", getFieldBooleanValue(insideScriptField, _writer, false));
+        
+        _writer.endElement("head");
+        _writer.endDocument();
+        
+        String output = _stringWriter.toString();
+        assertTrue("A script start was rendered, so the output has to " +
+                "contain " + COMMENT_START, output.contains(COMMENT_START));
+        assertTrue("A script end was rendered so the output has to " + 
+                "contain " + COMMENT_END, output.contains(COMMENT_END));
+    }
+    
+    /**
+     * Utility method to get the value of the given Field, which is of
+     * type java.lang.Boolean. If it is null, the given defaulValue will
+     * be returned.
+     * 
+     * @param field
+     * @param instance
+     * @param defaultValue
+     * @return
+     * @throws IllegalArgumentException
+     * @throws IllegalAccessException
+     */
+    private boolean getFieldBooleanValue(Field field, Object instance, boolean defaultValue)

+            throws IllegalArgumentException, IllegalAccessException
+    {
+        Boolean b = (Boolean) field.get(instance);
+        return b == null ? defaultValue : b;
+    }
+
+}

Propchange: myfaces/shared/trunk_4.0.x/core/src/test/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/shared/trunk_4.0.x/core/src/test/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImplTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: myfaces/shared/trunk_4.0.x/core/src/test/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImplTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message