You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2017/09/13 14:41:04 UTC

[myfaces-tobago] 01/02: TOBAGO-1786: Selector to address UIStyle * fix of id writing in selector * UnitTest

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git

commit d67a725e1b0220dd1ca63e7914398b9daa63b6cb
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Wed Sep 13 16:37:46 2017 +0200

    TOBAGO-1786: Selector to address UIStyle
    * fix of id writing in selector
    * UnitTest
---
 .../tobago/internal/util/StyleRenderUtils.java     | 16 +++-
 .../internal/config/AbstractTobagoTestBase.java    | 17 ++++
 .../internal/util/StyleRenderUtilsUnitTest.java    | 91 ++++++++++++++++++++++
 3 files changed, 120 insertions(+), 4 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/StyleRenderUtils.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/StyleRenderUtils.java
index f9043ec..aac421b 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/StyleRenderUtils.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/StyleRenderUtils.java
@@ -20,6 +20,8 @@
 package org.apache.myfaces.tobago.internal.util;
 
 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 
@@ -29,11 +31,12 @@ import java.io.IOException;
  */
 public class StyleRenderUtils {
 
+  private static final Logger LOG = LoggerFactory.getLogger(StyleRenderUtils.class);
+
   private StyleRenderUtils() {
     // to prevent instantiation
   }
 
-
   public static void writeIdSelector(TobagoResponseWriter writer, String id) throws IOException {
 
     writer.writeText("#");
@@ -43,15 +46,20 @@ public class StyleRenderUtils {
     for (int i = 0; i < chars.length; i++) {
       char c = chars[i];
       if (c == ':') {
-        writer.writeText(chars, last, i);
-        writer.writeText("\\\\:");
-        last = i;
+        writer.writeText(chars, last, i - last);
+        writer.writeText("\\:");
+        last = i + 1;
       }
     }
+    writer.writeText(chars, last, chars.length - last);
   }
 
   // not using writeText, because > must not be encoded!
   public static void writeSelector(TobagoResponseWriter writer, String selector) throws IOException {
+    if (selector.contains("<")) {
+      LOG.warn("Found invalid char < inside of style!");
+      selector = selector.replaceAll("<", "&lt;");
+    }
     writer.write(selector);
   }
 
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/AbstractTobagoTestBase.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/AbstractTobagoTestBase.java
index 218bba0..beff44e 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/AbstractTobagoTestBase.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/AbstractTobagoTestBase.java
@@ -34,11 +34,14 @@ import org.apache.myfaces.tobago.context.ThemeImpl;
 import org.apache.myfaces.tobago.context.TobagoContext;
 import org.apache.myfaces.tobago.internal.mock.faces.MockTheme;
 import org.apache.myfaces.tobago.internal.util.MimeTypeUtils;
+import org.apache.myfaces.tobago.internal.webapp.HtmlResponseWriter;
 import org.junit.After;
 import org.junit.Before;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
+import java.io.StringWriter;
 import java.util.Collections;
 import java.util.Locale;
 
@@ -56,6 +59,9 @@ public abstract class AbstractTobagoTestBase extends AbstractJsfTestCase {
 
   private static final Logger LOG = LoggerFactory.getLogger(AbstractTobagoTestBase.class);
 
+  private StringWriter stringWriter;
+  private int last = 0;
+
   /**
    * <p>Set up instance variables required by Tobago test cases.</p>
    */
@@ -65,6 +71,9 @@ public abstract class AbstractTobagoTestBase extends AbstractJsfTestCase {
 
     super.setUp();
 
+    stringWriter = new StringWriter();
+    getFacesContext().setResponseWriter(new HtmlResponseWriter(stringWriter, "", "UTF-8"));
+
     // Tobago specific extensions
 
     final TobagoConfigImpl tobagoConfig = TobagoConfigMergingUnitTest.loadAndMerge("tobago-config-for-unit-tests.xml");
@@ -112,4 +121,12 @@ public abstract class AbstractTobagoTestBase extends AbstractJsfTestCase {
   public MockHttpServletRequest getRequest() {
     return request;
   }
+
+  public String getLastWritten() throws IOException {
+    getFacesContext().getResponseWriter().flush(); // is this needed
+    final String full = stringWriter.toString();
+    final String result = full.substring(last);
+    last = full.length();
+    return result;
+  }
 }
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/StyleRenderUtilsUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/StyleRenderUtilsUnitTest.java
new file mode 100644
index 0000000..7a0518e
--- /dev/null
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/StyleRenderUtilsUnitTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.tobago.internal.util;
+
+import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
+import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.faces.context.FacesContext;
+import java.io.IOException;
+
+public class StyleRenderUtilsUnitTest extends AbstractTobagoTestBase {
+
+  @Test
+  public void testEncodeSelector() {
+    Assert.assertEquals("", StyleRenderUtils.encodeSelector());
+
+    Assert.assertEquals("", StyleRenderUtils.encodeSelector(""));
+
+    Assert.assertEquals("tag", StyleRenderUtils.encodeSelector("tag"));
+
+    Assert.assertEquals(".class", StyleRenderUtils.encodeSelector(".class"));
+
+    Assert.assertEquals("parent>child", StyleRenderUtils.encodeSelector("parent>child"));
+
+    Assert.assertEquals("#id\\:sub", StyleRenderUtils.encodeSelector("#id:sub"));
+
+    Assert.assertEquals("#id\\:sub\\:sub2", StyleRenderUtils.encodeSelector("#id:sub:sub2"));
+
+    Assert.assertEquals("#id\\:sub\\:sub2\\:sub3", StyleRenderUtils.encodeSelector("#id:sub:sub2:sub3"));
+  }
+
+  @Test
+  public void writeIdSelector() throws IOException {
+
+    final FacesContext facesContext = FacesContext.getCurrentInstance();
+    final TobagoResponseWriter writer = (TobagoResponseWriter) facesContext.getResponseWriter();
+
+    StyleRenderUtils.writeIdSelector(writer, "id");
+    Assert.assertEquals("#id", getLastWritten());
+
+    StyleRenderUtils.writeIdSelector(writer, "id:sub");
+    Assert.assertEquals("#id\\:sub", getLastWritten());
+
+    StyleRenderUtils.writeIdSelector(writer, "id:sub:sub2");
+    Assert.assertEquals("#id\\:sub\\:sub2", getLastWritten());
+
+    StyleRenderUtils.writeIdSelector(writer, "id:sub:sub2:sub3");
+    Assert.assertEquals("#id\\:sub\\:sub2\\:sub3", getLastWritten());
+
+    StyleRenderUtils.writeIdSelector(writer, "id::sub");
+    Assert.assertEquals("#id\\:\\:sub", getLastWritten());
+  }
+
+  @Test
+  public void writeSelector() throws IOException {
+
+    final FacesContext facesContext = FacesContext.getCurrentInstance();
+    final TobagoResponseWriter writer = (TobagoResponseWriter) facesContext.getResponseWriter();
+
+    StyleRenderUtils.writeSelector(writer, "parent>child");
+    Assert.assertEquals("parent>child", getLastWritten());
+
+    StyleRenderUtils.writeSelector(writer, "parent<child");
+    Assert.assertEquals("parent&lt;child", getLastWritten());
+
+    StyleRenderUtils.writeSelector(writer, "#id");
+    Assert.assertEquals("#id", getLastWritten());
+
+    StyleRenderUtils.writeSelector(writer, "#id\\:sub");
+    Assert.assertEquals("#id\\:sub", getLastWritten());
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@myfaces.apache.org" <co...@myfaces.apache.org>.