dev@javaserverfaces.java.net

[REVIEW] Update inlined style handling when content type is XHTML

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Fri, 13 Oct 2006 11:52:02 -0700


Fix for an issue Jason Lee found with inlined styles
in XHTML.


SECTION: Modified Files
----------------------------
M src/com/sun/faces/renderkit/html_basic/HtmlResponseWriter.java
 - break out script and style element handling.
   In the xhmtl case, inlined styles will be escaped
   using CDATA without the leading //.
M test/com/sun/faces/renderkit/html_basic/TestHtmlResponseWriter.java
 - test to validate style behavior


SECTION: Diffs
----------------------------
Index: src/com/sun/faces/renderkit/html_basic/HtmlResponseWriter.java
===================================================================
RCS file: /cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/renderkit/html_basic/HtmlResponseWriter.java,v
retrieving revision 1.35
diff -u -r1.35 HtmlResponseWriter.java
--- src/com/sun/faces/renderkit/html_basic/HtmlResponseWriter.java 12 Oct 2006 20:19:08 -0000 1.35
+++ src/com/sun/faces/renderkit/html_basic/HtmlResponseWriter.java 13 Oct 2006 18:47:27 -0000
@@ -75,9 +75,12 @@
     private boolean writingCdata;
 
     // flag to indicate that we're writing a 'script' or 'style' element
- private boolean scriptOrStyle;
+ private boolean isScript;
 
- // flag to indicate that we're writing a 'src' attribute as part of
+ // flag to indicate that we're writing a 'style' element
+ private boolean isStyle;
+
+ // flag to indicate that we're writing a 'src' attribute as part of
     // 'script' or 'style' element
     private boolean scriptOrStyleSrc;
 
@@ -220,13 +223,17 @@
             RIConstants.XHTML_CONTENT_TYPE);
         if (isScriptOrStyle(name) && !scriptOrStyleSrc) {
             if (isXhtml) {
- writer.write("\n//]]>\n");
+ if (isScript) {
+ writer.write("\n//]]>\n");
+ } else {
+ writer.write("\n]]>\n");
+ }
             } else {
                 writer.write("\n//-->\n");
             }
         }
- scriptOrStyle = false;
- scriptOrStyle = false;
+ isScript = false;
+ isStyle = false;
         if ("cdata".equalsIgnoreCase(name)) {
             writer.write("]]>");
             writingCdata = false;
@@ -391,7 +398,7 @@
             return;
         }
 
- if (name.equalsIgnoreCase("src") && scriptOrStyle) {
+ if (name.equalsIgnoreCase("src") && isScriptOrStyle()) {
             scriptOrStyleSrc = true;
         }
 
@@ -654,11 +661,16 @@
             closeStart = false;
         }
         if (isScriptOrStyle() && !scriptOrStyleSrc) {
- scriptOrStyle = false;
+ isScript = false;
+ isStyle = false;
             isXhtml = getContentType().equals(
                 RIConstants.XHTML_CONTENT_TYPE);
             if (isXhtml) {
- writer.write("\n//<![CDATA[");
+ if (isScript) {
+ writer.write("\n//<![CDATA[");
+ } else {
+ writer.write("\n<![CDATA[");
+ }
             } else {
                 writer.write("\n<!--");
             }
@@ -667,20 +679,23 @@
 
 
     private boolean isScriptOrStyle(String name) {
- if ("script".equalsIgnoreCase(name) ||
- "style".equalsIgnoreCase(name)) {
- scriptOrStyle = true;
+ if ("script".equalsIgnoreCase(name)) {
+ isScript = true;
+ dontEscape = true;
+ } else if ("style".equalsIgnoreCase(name)) {
+ isStyle = true;
             dontEscape = true;
         } else {
- scriptOrStyle = false;
+ isScript = false;
+ isStyle = false;
             dontEscape = false;
         }
 
- return scriptOrStyle;
+ return (isScript || isStyle);
     }
 
     private boolean isScriptOrStyle() {
- return scriptOrStyle;
+ return (isScript || isStyle);
     }
 
 }
Index: test/com/sun/faces/renderkit/html_basic/TestHtmlResponseWriter.java
===================================================================
RCS file: /cvs/javaserverfaces-sources/jsf-ri/test/com/sun/faces/renderkit/html_basic/TestHtmlResponseWriter.java,v
retrieving revision 1.22
diff -u -r1.22 TestHtmlResponseWriter.java
--- test/com/sun/faces/renderkit/html_basic/TestHtmlResponseWriter.java 6 Oct 2006 20:36:06 -0000 1.22
+++ test/com/sun/faces/renderkit/html_basic/TestHtmlResponseWriter.java 13 Oct 2006 18:47:29 -0000
@@ -291,6 +291,49 @@
         assertTrue((!result.contains("<[CDATA[") && !result.contains("]]>")));
     }
 
+ public void testWriteStyleElement() throws Exception {
+ StringWriter sw = new StringWriter();
+ StringWriter swx = new StringWriter();
+ writer = renderKit.createResponseWriter(sw, "text/html", "ISO-8859-1");
+ ResponseWriter xmlWriter = renderKit.createResponseWriter(swx, "application/xhtml+xml", "UTF-8");
+ UIOutput output = new UIOutput();
+ writer.startElement("style", output);
+ writer.writeAttribute("src", "http://foo.net/some.css", "src");
+ writer.writeAttribute("type", "text/css", "type");
+ writer.endElement("style");
+ String result = sw.toString();
+ System.out.println(result);
+ assertTrue((!result.contains("<!--") && !result.contains("-->")));
+
+ xmlWriter.startElement("style", output);
+ xmlWriter.writeAttribute("src", "http://foo.net/some.css", "src");
+ xmlWriter.writeAttribute("type", "text/css", "type");
+ xmlWriter.endElement("style");
+ result = swx.toString();
+ System.out.println(result);
+ assertTrue((!result.contains("<![CDATA[") && !result.contains("]]>")));
+
+ sw = new StringWriter();
+ swx = new StringWriter();
+ writer = renderKit.createResponseWriter(sw, "text/html", "ISO-8859-1");
+ xmlWriter = renderKit.createResponseWriter(swx, "application/xhtml+xml", "UTF-8");
+ writer.startElement("style", output);
+ writer.writeAttribute("type", "text/css", "type");
+ writer.write(".h1 { color: red }");
+ writer.endElement("style");
+ result = sw.toString();
+ System.out.println(result);
+ assertTrue((result.contains("<!--") && result.contains("-->")));
+
+ xmlWriter.startElement("style", output);
+ xmlWriter.writeAttribute("type", "text/css", "type");
+ xmlWriter.write(".h1 { color: red }");
+ xmlWriter.endElement("style");
+ result = swx.toString();
+ System.out.println(result);
+ assertTrue((result.contains("<![CDATA[") && result.contains("]]>")));
+ }
+
 
     //
     // Test variations of the writeText method..