You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by hn...@apache.org on 2017/07/21 14:25:41 UTC
svn commit: r1802602 - in /myfaces/tobago/trunk:
tobago-core/src/main/java/org/apache/myfaces/tobago/internal/webapp/
tobago-core/src/main/java/org/apache/myfaces/tobago/util/
tobago-example/tobago-example-data/src/main/java/org/apache/myfaces/tobago/e...
Author: hnoeth
Date: Fri Jul 21 14:25:41 2017
New Revision: 1802602
URL: http://svn.apache.org/viewvc?rev=1802602&view=rev
Log:
TOBAGO-1769 Ajax doesn't work for text containing unicode characters
* encode method of HtmlResponseWriter copied to XmlResponseWriter
* test added
Added:
myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/AjaxUnicodeController.java
myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/
myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.test.js
myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.xhtml
Modified:
myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/webapp/XmlResponseWriter.java
myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/XmlUtils.java
myfaces/tobago/trunk/tobago-example/tobago-example-data/src/main/java/org/apache/myfaces/tobago/example/data/SolarObject.java
myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/080-sheet/sheet.xhtml
myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/QUnitTests.java
Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/webapp/XmlResponseWriter.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/webapp/XmlResponseWriter.java?rev=1802602&r1=1802601&r2=1802602&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/webapp/XmlResponseWriter.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/webapp/XmlResponseWriter.java Fri Jul 21 14:25:41 2017
@@ -42,7 +42,7 @@ public final class XmlResponseWriter ext
throws IOException {
closeOpenTag();
final String value = findValue(text, property);
- write(XmlUtils.escape(value));
+ write(XmlUtils.encode(value, false));
}
@Override
@@ -75,7 +75,7 @@ public final class XmlResponseWriter ext
@Override
protected void writerAttributeValue(final String value, final boolean escape) throws IOException {
- getWriter().write(XmlUtils.escape(value));
+ getWriter().write(XmlUtils.encode(value, true));
}
@Override
Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/XmlUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/XmlUtils.java?rev=1802602&r1=1802601&r2=1802602&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/XmlUtils.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/XmlUtils.java Fri Jul 21 14:25:41 2017
@@ -37,9 +37,91 @@ import java.util.Properties;
public final class XmlUtils {
+ private static final char[][] CHARS_TO_ESCAPE;
+ private static final char[] EMPTY = new char[0];
+
+ static {
+ // init lookup table
+ CHARS_TO_ESCAPE = new char[0xA0][];
+
+ for (int i = 0; i < 0x20; i++) {
+ CHARS_TO_ESCAPE[i] = EMPTY; // Control characters
+ }
+
+ CHARS_TO_ESCAPE['\t'] = "	".toCharArray(); // Horizontal tabulator
+ CHARS_TO_ESCAPE['\n'] = "
".toCharArray(); // Line feed
+ CHARS_TO_ESCAPE['\r'] = "
".toCharArray(); // Carriage return
+
+ CHARS_TO_ESCAPE['\''] = "'".toCharArray();
+ CHARS_TO_ESCAPE['\"'] = """.toCharArray();
+ CHARS_TO_ESCAPE['&'] = "&".toCharArray();
+ CHARS_TO_ESCAPE['<'] = "<".toCharArray();
+ CHARS_TO_ESCAPE['>'] = ">".toCharArray();
+ CHARS_TO_ESCAPE['/'] = "/".toCharArray();
+
+ CHARS_TO_ESCAPE[0x7F] = EMPTY; // Delete
+
+ for (int i = 0x80; i < 0xA0; i++) {
+ CHARS_TO_ESCAPE[i] = EMPTY; // Control characters
+ }
+
+ // all "normal" character positions contains null
+ }
+
private XmlUtils() {
}
+ public static String encode(final String s, final boolean isAttribute) {
+ StringBuilder stringBuilder = new StringBuilder();
+
+ final char[] text = s.toCharArray();
+ final int start = 0;
+ final int length = s.length();
+
+ int localIndex = -1;
+
+ final int end = start + length;
+ for (int i = start; i < end; i++) {
+ final char ch = text[i];
+ if (ch >= CHARS_TO_ESCAPE.length || CHARS_TO_ESCAPE[ch] != null) {
+ localIndex = i;
+ break;
+ }
+ }
+
+ if (localIndex == -1) {
+ // no need to escape
+ return s;
+ } else {
+ // write until localIndex and then encode the remainder
+ stringBuilder.append(text, start, localIndex);
+
+ for (int i = localIndex; i < end; i++) {
+ final char ch = text[i];
+
+ // Tilde or less...
+ if (ch < CHARS_TO_ESCAPE.length) {
+ if (isAttribute && ch == '&' && (i + 1 < end) && text[i + 1] == '{') {
+ // HTML 4.0, section B.7.1: ampersands followed by
+ // an open brace don't get escaped
+ stringBuilder.append('&');
+ } else if (CHARS_TO_ESCAPE[ch] != null) {
+ stringBuilder.append(CHARS_TO_ESCAPE[ch]);
+ } else {
+ stringBuilder.append(ch);
+ }
+ } else {
+ // Double-byte characters to encode.
+ // PENDING: when outputting to an encoding that
+ // supports double-byte characters (UTF-8, for example),
+ // we should not be encoding
+ stringBuilder.append(ch);
+ }
+ }
+ }
+ return stringBuilder.toString();
+ }
+
public static String escape(final String s) {
return escape(s, true);
}
Modified: myfaces/tobago/trunk/tobago-example/tobago-example-data/src/main/java/org/apache/myfaces/tobago/example/data/SolarObject.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-data/src/main/java/org/apache/myfaces/tobago/example/data/SolarObject.java?rev=1802602&r1=1802601&r2=1802602&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-data/src/main/java/org/apache/myfaces/tobago/example/data/SolarObject.java (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-data/src/main/java/org/apache/myfaces/tobago/example/data/SolarObject.java Fri Jul 21 14:25:41 2017
@@ -264,7 +264,7 @@ public class SolarObject {
new SolarObject("Pluto", "IX", "Sun", 5913520, 90800.0, 17.15, 0.25, "Tombaugh", 1930),
MOON,
new SolarObject("Phobos", "I", "Mars", 9, 0.32, 1.00, 0.02, "Hall", 1877),
- new SolarObject("Deimos", "II", "Mars", 23, 1.26, 1.80, 0.00, "Hall", 1877),
+ new SolarObject("Deimoyyy\u001Axxx", "II", "Mars", 23, 1.26, 1.80, 0.00, "Hall", 1877),
new SolarObject("Metis", "XVI", "Jupiter", 128, 0.29, 0.00, 0.00, "Synnott", 1979),
new SolarObject("Adrastea", "XV", "Jupiter", 129, 0.30, 0.00, 0.00, "Jewitt", 1979),
new SolarObject("Amalthea", "V", "Jupiter", 181, 0.50, 0.40, 0.00, "Barnard", 1892),
Added: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/AjaxUnicodeController.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/AjaxUnicodeController.java?rev=1802602&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/AjaxUnicodeController.java (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/AjaxUnicodeController.java Fri Jul 21 14:25:41 2017
@@ -0,0 +1,38 @@
+/*
+ * 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.example.demo;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Named;
+import java.io.Serializable;
+import java.util.Date;
+
+@RequestScoped
+@Named
+public class AjaxUnicodeController implements Serializable {
+
+ public long getTimestamp() {
+ return new Date().getTime();
+ }
+
+ public String getUnicodeString() {
+ return "SubstituteChar\u001AEND Comma\u002CEND Five\u0035END Micro\u00B5END EUR\u20ACEND";
+ }
+}
Modified: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/080-sheet/sheet.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/080-sheet/sheet.xhtml?rev=1802602&r1=1802601&r2=1802602&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/080-sheet/sheet.xhtml (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/080-sheet/sheet.xhtml Fri Jul 21 14:25:41 2017
@@ -49,7 +49,7 @@
<tc:column label="Name">
<tc:out value="\#{solarobject.name}" labelLayout="skip"/>
...</code></pre>
- <tc:sheet id="s1" value="#{sheetController.solarList}" var="object" rows="4">
+ <tc:sheet value="#{sheetController.solarList}" var="object" rows="4">
<tc:style maxHeight="500px"/>
<tc:column label="Name">
<tc:out value="#{object.name}" labelLayout="skip"/>
Added: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.test.js
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.test.js?rev=1802602&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.test.js (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.test.js Fri Jul 21 14:25:41 2017
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+QUnit.test("ajax excecute", function (assert) {
+ assert.expect(3);
+ var done = assert.async();
+
+ var $timestamp = jQueryFrame("#page\\:mainForm\\:timestamp span");
+ var $text = jQueryFrame("#page\\:mainForm\\:outText span");
+ var $tip = jQueryFrame("#page\\:mainForm\\:outTip span");
+ var $button = jQueryFrame("#page\\:mainForm\\:ajaxButton");
+
+ var timestampValue = $timestamp.text();
+ var textValue = $text.text();
+ var tipValue = $tip.attr('title');
+
+ $button.click();
+
+ waitForAjax(function () {
+ $timestamp = jQueryFrame($timestamp.selector);
+ $text = jQueryFrame($text.selector);
+ $tip = jQueryFrame($tip.selector);
+
+ return $timestamp.text() !== timestampValue
+ && $text.text() === textValue
+ && $tip.attr('title') === tipValue;
+ }, function () {
+ $timestamp = jQueryFrame($timestamp.selector);
+ $text = jQueryFrame($text.selector);
+ $tip = jQueryFrame($tip.selector);
+
+ assert.notEqual($timestamp.text(), timestampValue);
+ assert.equal($text.text(), textValue);
+ assert.equal($tip.attr('title'), tipValue);
+
+ done();
+ });
+});
Added: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.xhtml?rev=1802602&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.xhtml (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/content/40-test/50000-java/30-ajax-unicode/ajax-unicode.xhtml Fri Jul 21 14:25:41 2017
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ * 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.
+-->
+
+<ui:composition template="/main.xhtml"
+ xmlns:f="http://java.sun.com/jsf/core"
+ xmlns:tc="http://myfaces.apache.org/tobago/component"
+ xmlns:ui="http://java.sun.com/jsf/facelets">
+ <ui:param name="title" value="Ajax Unicode"/>
+
+ <tc:out id="timestamp" label="Timestamp" value="#{ajaxUnicodeController.timestamp}"/>
+ <tc:out id="outText" label="Text with unicode characters" value="#{ajaxUnicodeController.unicodeString}"/>
+ <tc:out id="outTip" label="Tip with unicode characters" value="Look at tooltip"
+ tip="#{ajaxUnicodeController.unicodeString}"/>
+ <tc:button id="ajaxButton" label="AJAX">
+ <f:ajax render="timestamp outText outTip"/>
+ </tc:button>
+</ui:composition>
Modified: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/QUnitTests.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/QUnitTests.java?rev=1802602&r1=1802601&r2=1802602&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/QUnitTests.java (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/QUnitTests.java Fri Jul 21 14:25:41 2017
@@ -493,4 +493,10 @@ public class QUnitTests {
String page = "content/40-test/50000-java/20-ajax-execute/ajax-execute.xhtml";
runStandardTest(page);
}
+
+ @Test
+ public void ajaxUnicode() throws UnsupportedEncodingException, InterruptedException {
+ String page = "content/40-test/50000-java/30-ajax-unicode/ajax-unicode.xhtml";
+ runStandardTest(page);
+ }
}