Add ability to change text escaper implementation (#70)

This commit is contained in:
Sergey Bezkostnyi
2017-08-14 18:10:02 +03:00
committed by David
parent cfc1489399
commit ef5777e54a
7 changed files with 68 additions and 8 deletions

View File

@@ -0,0 +1,13 @@
package j2html;
import j2html.utils.SimpleEscaper;
import j2html.utils.TextEscaper;
public class Config {
private Config() {
}
public static TextEscaper textEscaper = new SimpleEscaper();
}

View File

@@ -1,7 +1,7 @@
package j2html.attributes;
import j2html.utils.SimpleEscaper;
import j2html.Config;
public class Attribute {
private String name;
@@ -9,7 +9,7 @@ public class Attribute {
public Attribute(String name, String value) {
this.name = name;
this.value = SimpleEscaper.escape(value);
this.value = Config.textEscaper.escape(value);
}
public Attribute(String name) {

View File

@@ -1,6 +1,6 @@
package j2html.tags;
import j2html.utils.SimpleEscaper;
import j2html.Config;
public class Text extends DomContent {
@@ -12,7 +12,7 @@ public class Text extends DomContent {
@Override
public String render() {
return SimpleEscaper.escape(text);
return Config.textEscaper.escape(text);
}
}

View File

@@ -1,8 +1,8 @@
package j2html.utils;
public class SimpleEscaper {
public class SimpleEscaper implements TextEscaper {
public static String escape(String s) {
public String escape(String s) {
if (s == null) {
return null;
}

View File

@@ -0,0 +1,7 @@
package j2html.utils;
public interface TextEscaper {
String escape(String text);
}

View File

@@ -30,8 +30,8 @@ public class PerformanceTest {
@Test
public void test_escaper_performnce() throws Exception {
timeEscaper("SimpleEscaper#short", () -> SimpleEscaper.escape(shortTestString));
timeEscaper("SimpleEscaper#long", () -> SimpleEscaper.escape(longTestString));
timeEscaper("SimpleEscaper#short", () -> new SimpleEscaper().escape(shortTestString));
timeEscaper("SimpleEscaper#long", () -> new SimpleEscaper().escape(longTestString));
timeEscaper("ApacheEscaper#short", () -> StringEscapeUtils.escapeHtml4(shortTestString));
timeEscaper("ApacheEscaper#long", () -> StringEscapeUtils.escapeHtml4(longTestString));
}

View File

@@ -0,0 +1,40 @@
package j2html;
import j2html.utils.SimpleEscaper;
import j2html.utils.TextEscaper;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class TextEscaperTest {
@Test
public void testTextEscaper() throws Exception {
assertThat("default text escaper is SimpleEscaper",
Config.textEscaper, is(instanceOf(SimpleEscaper.class)));
String expected = "<div></div>";
assertThat("default text escaper works",
Config.textEscaper.escape("<div></div>"), is(expected));
Config.textEscaper = new NoOpEscaper();
assertThat("user can change text escaper implementation",
Config.textEscaper, is(instanceOf(NoOpEscaper.class)));
expected = "<div></div>";
assertThat("user provided text escaper actually works",
Config.textEscaper.escape("<div></div>"), is(expected));
}
private static class NoOpEscaper implements TextEscaper {
@Override
public String escape(String text) {
return text;
}
}
}