Compare commits
8 Commits
j2html-1.0
...
j2html-1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc3bb29b6a | ||
|
|
ef5777e54a | ||
|
|
cfc1489399 | ||
|
|
8112dad1bb | ||
|
|
235dec78ed | ||
|
|
6316dd2262 | ||
|
|
5667d7c68d | ||
|
|
13911d5f57 |
@@ -13,12 +13,12 @@ The project webpage is [j2html.com](http://j2html.com).
|
||||
<dependency>
|
||||
<groupId>com.j2html</groupId>
|
||||
<artifactId>j2html</artifactId>
|
||||
<version>0.99</version>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
```
|
||||
### OR the gradle dependency
|
||||
```
|
||||
compile 'com.j2html:j2html:0.99'
|
||||
compile 'com.j2html:j2html:1.0.0'
|
||||
```
|
||||
|
||||
### Import TagCreator and start building HTML
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -10,7 +10,7 @@
|
||||
|
||||
<groupId>com.j2html</groupId>
|
||||
<artifactId>j2html</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
|
||||
<name>j2html</name>
|
||||
<description>Java to HTML builder with a fluent API</description>
|
||||
|
||||
13
src/main/java/j2html/Config.java
Normal file
13
src/main/java/j2html/Config.java
Normal 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();
|
||||
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -111,7 +111,13 @@ public class ContainerTag extends Tag<ContainerTag> {
|
||||
return with(new Text(text));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets number of child nodes this tag element contains
|
||||
*/
|
||||
public int getNumChildren() {
|
||||
return children.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the ContainerTag and its children
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
7
src/main/java/j2html/utils/TextEscaper.java
Normal file
7
src/main/java/j2html/utils/TextEscaper.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package j2html.utils;
|
||||
|
||||
public interface TextEscaper {
|
||||
|
||||
String escape(String text);
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
40
src/test/java/j2html/TextEscaperTest.java
Normal file
40
src/test/java/j2html/TextEscaperTest.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user