Subtree merge in gwtexpui as gerrit-gwtexpui
Nobody uses the standalone gwtexpui project that we know of. Its release process is frustrating, as even getting a simple bugfix into Gerrit requires releasing gwtexpui (incrementing the version, tagging, deploying, setting the next snapshot version). Change-Id: I958ed659b8de8d855f7f9a7f4df66f6b4aad882f
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// Licensed 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 com.google.gwtexpui.safehtml.client;
|
||||
|
||||
import static com.google.gwtexpui.safehtml.client.LinkFindReplace.hasValidScheme;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class LinkFindReplaceTest extends TestCase {
|
||||
public void testNoEscaping() {
|
||||
String find = "find";
|
||||
String link = "link";
|
||||
LinkFindReplace a = new LinkFindReplace(find, link);
|
||||
assertEquals(find, a.pattern().getSource());
|
||||
assertEquals("<a href=\"link\">find</a>", a.replace(find));
|
||||
assertEquals("find = " + find + ", link = " + link, a.toString());
|
||||
}
|
||||
|
||||
public void testBackreference() {
|
||||
assertEquals("<a href=\"/bug?id=123\">issue 123</a>",
|
||||
new LinkFindReplace("(bug|issue)\\s*([0-9]+)", "/bug?id=$2")
|
||||
.replace("issue 123"));
|
||||
}
|
||||
|
||||
public void testHasValidScheme() {
|
||||
assertTrue(hasValidScheme("/absolute/path"));
|
||||
assertTrue(hasValidScheme("relative/path"));
|
||||
assertTrue(hasValidScheme("http://url/"));
|
||||
assertTrue(hasValidScheme("HTTP://url/"));
|
||||
assertTrue(hasValidScheme("https://url/"));
|
||||
assertTrue(hasValidScheme("mailto://url/"));
|
||||
assertFalse(hasValidScheme("ftp://url/"));
|
||||
assertFalse(hasValidScheme("data:evil"));
|
||||
assertFalse(hasValidScheme("javascript:alert(1)"));
|
||||
}
|
||||
|
||||
public void testInvalidSchemeInReplace() {
|
||||
try {
|
||||
new LinkFindReplace("find", "javascript:alert(1)").replace("find");
|
||||
fail("Expected IllegalStateException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvalidSchemeWithBackreference() {
|
||||
try {
|
||||
new LinkFindReplace(".*(script:[^;]*)", "java$1")
|
||||
.replace("Look at this script: alert(1);");
|
||||
fail("Expected IllegalStateException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testReplaceEscaping() {
|
||||
assertEquals("<a href=\"a"&'<>b\">find</a>",
|
||||
new LinkFindReplace("find", "a\"&'<>b").replace("find"));
|
||||
}
|
||||
|
||||
public void testHtmlInFind() {
|
||||
String rawFind = "<b>"bold"</b>";
|
||||
LinkFindReplace a = new LinkFindReplace(rawFind, "/bold");
|
||||
assertEquals(rawFind, a.pattern().getSource());
|
||||
assertEquals("<a href=\"/bold\">" + rawFind + "</a>", a.replace(rawFind));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed 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 com.google.gwtexpui.safehtml.client;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class RawFindReplaceTest extends TestCase {
|
||||
public void testFindReplace() {
|
||||
final String find = "find";
|
||||
final String replace = "replace";
|
||||
final RawFindReplace a = new RawFindReplace(find, replace);
|
||||
assertEquals(find, a.pattern().getSource());
|
||||
assertEquals(replace, a.replace(find));
|
||||
assertEquals("find = " + find + ", replace = " + replace, a.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed 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 com.google.gwtexpui.safehtml.client;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SafeHtmlBuilderTest extends TestCase {
|
||||
public void testEmpty() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertTrue(b.isEmpty());
|
||||
assertFalse(b.hasContent());
|
||||
assertEquals("", b.asString());
|
||||
|
||||
b.append("a");
|
||||
assertTrue(b.hasContent());
|
||||
assertEquals("a", b.asString());
|
||||
}
|
||||
|
||||
public void testToSafeHtml() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
b.append(1);
|
||||
|
||||
final SafeHtml h = b.toSafeHtml();
|
||||
assertNotNull(h);
|
||||
assertNotSame(h, b);
|
||||
assertFalse(h instanceof SafeHtmlBuilder);
|
||||
assertEquals("1", h.asString());
|
||||
}
|
||||
|
||||
public void testAppend_boolean() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append(true));
|
||||
assertSame(b, b.append(false));
|
||||
assertEquals("truefalse", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_char() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append('a'));
|
||||
assertSame(b, b.append('b'));
|
||||
assertEquals("ab", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_int() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append(4));
|
||||
assertSame(b, b.append(2));
|
||||
assertSame(b, b.append(-100));
|
||||
assertEquals("42-100", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_long() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append(4L));
|
||||
assertSame(b, b.append(2L));
|
||||
assertEquals("42", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_float() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append(0.0f));
|
||||
assertEquals("0.0", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_double() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append(0.0));
|
||||
assertEquals("0.0", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_String() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append((String) null));
|
||||
assertEquals("", b.asString());
|
||||
assertSame(b, b.append("foo"));
|
||||
assertSame(b, b.append("bar"));
|
||||
assertEquals("foobar", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_StringBuilder() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append((StringBuilder) null));
|
||||
assertEquals("", b.asString());
|
||||
assertSame(b, b.append(new StringBuilder("foo")));
|
||||
assertSame(b, b.append(new StringBuilder("bar")));
|
||||
assertEquals("foobar", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_StringBuffer() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append((StringBuffer) null));
|
||||
assertEquals("", b.asString());
|
||||
assertSame(b, b.append(new StringBuffer("foo")));
|
||||
assertSame(b, b.append(new StringBuffer("bar")));
|
||||
assertEquals("foobar", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_Object() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append((Object) null));
|
||||
assertEquals("", b.asString());
|
||||
assertSame(b, b.append(new Object() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "foobar";
|
||||
}
|
||||
}));
|
||||
assertEquals("foobar", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_CharSequence() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append((CharSequence) null));
|
||||
assertEquals("", b.asString());
|
||||
assertSame(b, b.append((CharSequence) "foo"));
|
||||
assertSame(b, b.append((CharSequence) "bar"));
|
||||
assertEquals("foobar", b.asString());
|
||||
}
|
||||
|
||||
public void testAppend_SafeHtml() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.append((SafeHtml) null));
|
||||
assertEquals("", b.asString());
|
||||
assertSame(b, b.append(new SafeHtmlString("foo")));
|
||||
assertSame(b, b.append(new SafeHtmlBuilder().append("bar")));
|
||||
assertEquals("foobar", b.asString());
|
||||
}
|
||||
|
||||
public void testHtmlSpecialCharacters() {
|
||||
assertEquals("&", escape("&"));
|
||||
assertEquals("<", escape("<"));
|
||||
assertEquals(">", escape(">"));
|
||||
assertEquals(""", escape("\""));
|
||||
assertEquals("'", escape("'"));
|
||||
|
||||
assertEquals("&", escape('&'));
|
||||
assertEquals("<", escape('<'));
|
||||
assertEquals(">", escape('>'));
|
||||
assertEquals(""", escape('"'));
|
||||
assertEquals("'", escape('\''));
|
||||
|
||||
assertEquals("<b>", escape("<b>"));
|
||||
assertEquals("&lt;b&gt;", escape("<b>"));
|
||||
}
|
||||
|
||||
public void testEntityNbsp() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.nbsp());
|
||||
assertEquals(" ", b.asString());
|
||||
}
|
||||
|
||||
public void testTagBr() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.br());
|
||||
assertEquals("<br />", b.asString());
|
||||
}
|
||||
|
||||
public void testTagTableTrTd() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.openElement("table"));
|
||||
assertSame(b, b.openTr());
|
||||
assertSame(b, b.openTd());
|
||||
assertSame(b, b.append("d<a>ta"));
|
||||
assertSame(b, b.closeTd());
|
||||
assertSame(b, b.closeTr());
|
||||
assertSame(b, b.closeElement("table"));
|
||||
assertEquals("<table><tr><td>d<a>ta</td></tr></table>", b.asString());
|
||||
}
|
||||
|
||||
public void testTagDiv() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.openDiv());
|
||||
assertSame(b, b.append("d<a>ta"));
|
||||
assertSame(b, b.closeDiv());
|
||||
assertEquals("<div>d<a>ta</div>", b.asString());
|
||||
}
|
||||
|
||||
public void testTagAnchor() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.openAnchor());
|
||||
|
||||
assertEquals("", b.getAttribute("href"));
|
||||
assertSame(b, b.setAttribute("href", "http://here"));
|
||||
assertEquals("http://here", b.getAttribute("href"));
|
||||
assertSame(b, b.setAttribute("href", "d<a>ta"));
|
||||
assertEquals("d<a>ta", b.getAttribute("href"));
|
||||
|
||||
assertEquals("", b.getAttribute("target"));
|
||||
assertSame(b, b.setAttribute("target", null));
|
||||
assertEquals("", b.getAttribute("target"));
|
||||
|
||||
assertSame(b, b.append("go"));
|
||||
assertSame(b, b.closeAnchor());
|
||||
assertEquals("<a href=\"d<a>ta\">go</a>", b.asString());
|
||||
}
|
||||
|
||||
public void testTagHeightWidth() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.openElement("img"));
|
||||
assertSame(b, b.setHeight(100));
|
||||
assertSame(b, b.setWidth(42));
|
||||
assertSame(b, b.closeSelf());
|
||||
assertEquals("<img height=\"100\" width=\"42\" />", b.asString());
|
||||
}
|
||||
|
||||
public void testStyleName() {
|
||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||
assertSame(b, b.openSpan());
|
||||
assertSame(b, b.setStyleName("foo"));
|
||||
assertSame(b, b.addStyleName("bar"));
|
||||
assertSame(b, b.append("d<a>ta"));
|
||||
assertSame(b, b.closeSpan());
|
||||
assertEquals("<span class=\"foo bar\">d<a>ta</span>", b.asString());
|
||||
}
|
||||
|
||||
public void testRejectJavaScript_AnchorHref() {
|
||||
final String href = "javascript:window.close();";
|
||||
try {
|
||||
new SafeHtmlBuilder().openAnchor().setAttribute("href", href);
|
||||
fail("accepted javascript in a href");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("javascript unsafe in href: " + href, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testRejectJavaScript_ImgSrc() {
|
||||
final String href = "javascript:window.close();";
|
||||
try {
|
||||
new SafeHtmlBuilder().openElement("img").setAttribute("src", href);
|
||||
fail("accepted javascript in img src");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("javascript unsafe in href: " + href, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testRejectJavaScript_FormAction() {
|
||||
final String href = "javascript:window.close();";
|
||||
try {
|
||||
new SafeHtmlBuilder().openElement("form").setAttribute("action", href);
|
||||
fail("accepted javascript in form action");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("javascript unsafe in href: " + href, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static String escape(final char c) {
|
||||
return new SafeHtmlBuilder().append(c).asString();
|
||||
}
|
||||
|
||||
private static String escape(final String c) {
|
||||
return new SafeHtmlBuilder().append(c).asString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed 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 com.google.gwtexpui.safehtml.client;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SafeHtml_LinkifyTest extends TestCase {
|
||||
public void testLinkify_SimpleHttp1() {
|
||||
final SafeHtml o = html("A http://go.here/ B");
|
||||
final SafeHtml n = o.linkify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("A <a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a> B", n.asString());
|
||||
}
|
||||
|
||||
public void testLinkify_SimpleHttps2() {
|
||||
final SafeHtml o = html("A https://go.here/ B");
|
||||
final SafeHtml n = o.linkify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("A <a href=\"https://go.here/\" target=\"_blank\">https://go.here/</a> B", n.asString());
|
||||
}
|
||||
|
||||
public void testLinkify_Parens1() {
|
||||
final SafeHtml o = html("A (http://go.here/) B");
|
||||
final SafeHtml n = o.linkify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("A (<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>) B", n.asString());
|
||||
}
|
||||
|
||||
public void testLinkify_Parens() {
|
||||
final SafeHtml o = html("A http://go.here/#m() B");
|
||||
final SafeHtml n = o.linkify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("A <a href=\"http://go.here/#m()\" target=\"_blank\">http://go.here/#m()</a> B", n.asString());
|
||||
}
|
||||
|
||||
public void testLinkify_AngleBrackets1() {
|
||||
final SafeHtml o = html("A <http://go.here/> B");
|
||||
final SafeHtml n = o.linkify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("A <<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>> B", n.asString());
|
||||
}
|
||||
|
||||
private static SafeHtml html(String text) {
|
||||
return new SafeHtmlBuilder().append(text).toSafeHtml();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed 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 com.google.gwtexpui.safehtml.client;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SafeHtml_ReplaceTest extends TestCase {
|
||||
public void testReplaceEmpty() {
|
||||
SafeHtml o = html("A\nissue42\nB");
|
||||
assertSame(o, o.replaceAll(null));
|
||||
assertSame(o, o.replaceAll(Collections.<FindReplace> emptyList()));
|
||||
}
|
||||
|
||||
public void testReplaceOneLink() {
|
||||
SafeHtml o = html("A\nissue 42\nB");
|
||||
SafeHtml n = o.replaceAll(repls(
|
||||
new RawFindReplace("(issue\\s(\\d+))", "<a href=\"?$2\">$1</a>")));
|
||||
assertNotSame(o, n);
|
||||
assertEquals("A\n<a href=\"?42\">issue 42</a>\nB", n.asString());
|
||||
}
|
||||
|
||||
public void testReplaceNoLeadingOrTrailingText() {
|
||||
SafeHtml o = html("issue 42");
|
||||
SafeHtml n = o.replaceAll(repls(
|
||||
new RawFindReplace("(issue\\s(\\d+))", "<a href=\"?$2\">$1</a>")));
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<a href=\"?42\">issue 42</a>", n.asString());
|
||||
}
|
||||
|
||||
public void testReplaceTwoLinks() {
|
||||
SafeHtml o = html("A\nissue 42\nissue 9918\nB");
|
||||
SafeHtml n = o.replaceAll(repls(
|
||||
new RawFindReplace("(issue\\s(\\d+))", "<a href=\"?$2\">$1</a>")));
|
||||
assertNotSame(o, n);
|
||||
assertEquals("A\n"
|
||||
+ "<a href=\"?42\">issue 42</a>\n"
|
||||
+ "<a href=\"?9918\">issue 9918</a>\n"
|
||||
+ "B"
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testReplaceInOrder() {
|
||||
SafeHtml o = html("A\nissue 42\nReally GWTEXPUI-9918 is better\nB");
|
||||
SafeHtml n = o.replaceAll(repls(
|
||||
new RawFindReplace("(GWTEXPUI-(\\d+))",
|
||||
"<a href=\"gwtexpui-bug?$2\">$1</a>"),
|
||||
new RawFindReplace("(issue\\s+(\\d+))",
|
||||
"<a href=\"generic-bug?$2\">$1</a>")));
|
||||
assertNotSame(o, n);
|
||||
assertEquals("A\n"
|
||||
+ "<a href=\"generic-bug?42\">issue 42</a>\n"
|
||||
+ "Really <a href=\"gwtexpui-bug?9918\">GWTEXPUI-9918</a> is better\n"
|
||||
+ "B"
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testReplaceOverlappingAfterFirstChar() {
|
||||
SafeHtml o = html("abcd");
|
||||
RawFindReplace ab = new RawFindReplace("ab", "AB");
|
||||
RawFindReplace bc = new RawFindReplace("bc", "23");
|
||||
RawFindReplace cd = new RawFindReplace("cd", "YZ");
|
||||
|
||||
assertEquals("ABcd", o.replaceAll(repls(ab, bc)).asString());
|
||||
assertEquals("ABcd", o.replaceAll(repls(bc, ab)).asString());
|
||||
assertEquals("ABYZ", o.replaceAll(repls(ab, bc, cd)).asString());
|
||||
}
|
||||
|
||||
public void testReplaceOverlappingAtFirstCharLongestMatch() {
|
||||
SafeHtml o = html("abcd");
|
||||
RawFindReplace ab = new RawFindReplace("ab", "AB");
|
||||
RawFindReplace abc = new RawFindReplace("[^d][^d][^d]", "234");
|
||||
|
||||
assertEquals("ABcd", o.replaceAll(repls(ab, abc)).asString());
|
||||
assertEquals("234d", o.replaceAll(repls(abc, ab)).asString());
|
||||
}
|
||||
|
||||
public void testReplaceOverlappingAtFirstCharFirstMatch() {
|
||||
SafeHtml o = html("abcd");
|
||||
RawFindReplace ab1 = new RawFindReplace("ab", "AB");
|
||||
RawFindReplace ab2 = new RawFindReplace("[^cd][^cd]", "12");
|
||||
|
||||
assertEquals("ABcd", o.replaceAll(repls(ab1, ab2)).asString());
|
||||
assertEquals("12cd", o.replaceAll(repls(ab2, ab1)).asString());
|
||||
}
|
||||
|
||||
public void testFailedSanitization() {
|
||||
SafeHtml o = html("abcd");
|
||||
LinkFindReplace evil = new LinkFindReplace("(b)", "javascript:alert('$1')");
|
||||
LinkFindReplace ok = new LinkFindReplace("(b)", "/$1");
|
||||
assertEquals("abcd", o.replaceAll(repls(evil)).asString());
|
||||
String linked = "a<a href=\"/b\">b</a>cd";
|
||||
assertEquals(linked, o.replaceAll(repls(ok)).asString());
|
||||
assertEquals(linked, o.replaceAll(repls(evil, ok)).asString());
|
||||
}
|
||||
|
||||
private static SafeHtml html(String text) {
|
||||
return new SafeHtmlBuilder().append(text).toSafeHtml();
|
||||
}
|
||||
|
||||
private static List<FindReplace> repls(FindReplace... repls) {
|
||||
return Arrays.asList(repls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed 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 "<p>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 com.google.gwtexpui.safehtml.client;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SafeHtml_WikifyListTest extends TestCase {
|
||||
private static final String BEGIN_LIST = "<ul class=\"wikiList\">";
|
||||
private static final String END_LIST = "</ul>";
|
||||
|
||||
private static String item(String raw) {
|
||||
return "<li>" + raw + "</li>";
|
||||
}
|
||||
|
||||
public void testBulletList1() {
|
||||
final SafeHtml o = html("A\n\n* line 1\n* 2nd line");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A</p>"//
|
||||
+ BEGIN_LIST //
|
||||
+ item("line 1") //
|
||||
+ item("2nd line") //
|
||||
+ END_LIST //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testBulletList2() {
|
||||
final SafeHtml o = html("A\n\n* line 1\n* 2nd line\n\nB");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A</p>"//
|
||||
+ BEGIN_LIST //
|
||||
+ item("line 1") //
|
||||
+ item("2nd line") //
|
||||
+ END_LIST //
|
||||
+ "<p>B</p>" //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testBulletList3() {
|
||||
final SafeHtml o = html("* line 1\n* 2nd line\n\nB");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals(BEGIN_LIST //
|
||||
+ item("line 1") //
|
||||
+ item("2nd line") //
|
||||
+ END_LIST //
|
||||
+ "<p>B</p>" //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testBulletList4() {
|
||||
final SafeHtml o = html("To see this bug, you have to:\n" //
|
||||
+ "* Be on IMAP or EAS (not on POP)\n"//
|
||||
+ "* Be very unlucky\n");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>To see this bug, you have to:</p>" //
|
||||
+ BEGIN_LIST //
|
||||
+ item("Be on IMAP or EAS (not on POP)") //
|
||||
+ item("Be very unlucky") //
|
||||
+ END_LIST //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testBulletList5() {
|
||||
final SafeHtml o = html("To see this bug,\n" //
|
||||
+ "you have to:\n" //
|
||||
+ "* Be on IMAP or EAS (not on POP)\n"//
|
||||
+ "* Be very unlucky\n");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>To see this bug, you have to:</p>" //
|
||||
+ BEGIN_LIST //
|
||||
+ item("Be on IMAP or EAS (not on POP)") //
|
||||
+ item("Be very unlucky") //
|
||||
+ END_LIST //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testDashList1() {
|
||||
final SafeHtml o = html("A\n\n- line 1\n- 2nd line");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A</p>"//
|
||||
+ BEGIN_LIST //
|
||||
+ item("line 1") //
|
||||
+ item("2nd line") //
|
||||
+ END_LIST //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testDashList2() {
|
||||
final SafeHtml o = html("A\n\n- line 1\n- 2nd line\n\nB");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A</p>"//
|
||||
+ BEGIN_LIST //
|
||||
+ item("line 1") //
|
||||
+ item("2nd line") //
|
||||
+ END_LIST //
|
||||
+ "<p>B</p>" //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testDashList3() {
|
||||
final SafeHtml o = html("- line 1\n- 2nd line\n\nB");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals(BEGIN_LIST //
|
||||
+ item("line 1") //
|
||||
+ item("2nd line") //
|
||||
+ END_LIST //
|
||||
+ "<p>B</p>" //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
private static SafeHtml html(String text) {
|
||||
return new SafeHtmlBuilder().append(text).toSafeHtml();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed 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 "<p>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 com.google.gwtexpui.safehtml.client;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SafeHtml_WikifyPreformatTest extends TestCase {
|
||||
private static final String B = "<span class=\"wikiPreFormat\">";
|
||||
private static final String E = "</span><br />";
|
||||
|
||||
private static String pre(String raw) {
|
||||
return B + raw + E;
|
||||
}
|
||||
|
||||
public void testPreformat1() {
|
||||
final SafeHtml o = html("A\n\n This is pre\n formatted");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A</p>"//
|
||||
+ "<p>" //
|
||||
+ pre(" This is pre") //
|
||||
+ pre(" formatted") //
|
||||
+ "</p>" //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testPreformat2() {
|
||||
final SafeHtml o = html("A\n\n This is pre\n formatted\n\nbut this is not");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A</p>" //
|
||||
+ "<p>" //
|
||||
+ pre(" This is pre") //
|
||||
+ pre(" formatted") //
|
||||
+ "</p>" //
|
||||
+ "<p>but this is not</p>" //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testPreformat3() {
|
||||
final SafeHtml o = html("A\n\n Q\n <R>\n S\n\nB");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A</p>" //
|
||||
+ "<p>" //
|
||||
+ pre(" Q") //
|
||||
+ pre(" <R>") //
|
||||
+ pre(" S") //
|
||||
+ "</p>" //
|
||||
+ "<p>B</p>" //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
public void testPreformat4() {
|
||||
final SafeHtml o = html(" Q\n <R>\n S\n\nB");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>" //
|
||||
+ pre(" Q") //
|
||||
+ pre(" <R>") //
|
||||
+ pre(" S") //
|
||||
+ "</p>" //
|
||||
+ "<p>B</p>" //
|
||||
, n.asString());
|
||||
}
|
||||
|
||||
private static SafeHtml html(String text) {
|
||||
return new SafeHtmlBuilder().append(text).toSafeHtml();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed 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 "<p>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 com.google.gwtexpui.safehtml.client;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SafeHtml_WikifyTest extends TestCase {
|
||||
public void testWikify_OneLine1() {
|
||||
final SafeHtml o = html("A B");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A B</p>", n.asString());
|
||||
}
|
||||
|
||||
public void testWikify_OneLine2() {
|
||||
final SafeHtml o = html("A B\n");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A B\n</p>", n.asString());
|
||||
}
|
||||
|
||||
public void testWikify_OneParagraph1() {
|
||||
final SafeHtml o = html("A\nB");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A\nB</p>", n.asString());
|
||||
}
|
||||
|
||||
public void testWikify_OneParagraph2() {
|
||||
final SafeHtml o = html("A\nB\n");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A\nB\n</p>", n.asString());
|
||||
}
|
||||
|
||||
public void testWikify_TwoParagraphs() {
|
||||
final SafeHtml o = html("A\nB\n\nC\nD");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A\nB</p><p>C\nD</p>", n.asString());
|
||||
}
|
||||
|
||||
public void testLinkify_SimpleHttp1() {
|
||||
final SafeHtml o = html("A http://go.here/ B");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A <a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a> B</p>", n.asString());
|
||||
}
|
||||
|
||||
public void testLinkify_SimpleHttps2() {
|
||||
final SafeHtml o = html("A https://go.here/ B");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A <a href=\"https://go.here/\" target=\"_blank\">https://go.here/</a> B</p>", n.asString());
|
||||
}
|
||||
|
||||
public void testLinkify_Parens1() {
|
||||
final SafeHtml o = html("A (http://go.here/) B");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A (<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>) B</p>", n.asString());
|
||||
}
|
||||
|
||||
public void testLinkify_Parens() {
|
||||
final SafeHtml o = html("A http://go.here/#m() B");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A <a href=\"http://go.here/#m()\" target=\"_blank\">http://go.here/#m()</a> B</p>", n.asString());
|
||||
}
|
||||
|
||||
public void testLinkify_AngleBrackets1() {
|
||||
final SafeHtml o = html("A <http://go.here/> B");
|
||||
final SafeHtml n = o.wikify();
|
||||
assertNotSame(o, n);
|
||||
assertEquals("<p>A <<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>> B</p>", n.asString());
|
||||
}
|
||||
|
||||
private static SafeHtml html(String text) {
|
||||
return new SafeHtmlBuilder().append(text).toSafeHtml();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user