diff --git a/src/HtmlEscaper/HtmlEscaper.java b/src/HtmlEscaper/HtmlEscaper.java
new file mode 100644
index 0000000..2336e1d
--- /dev/null
+++ b/src/HtmlEscaper/HtmlEscaper.java
@@ -0,0 +1,20 @@
+package j2html.src.HtmlEscaper;
+
+public class HtmlEscaper {
+
+ public static String escape(String s) {
+ StringBuilder out = new StringBuilder(Math.max(16, s.length()));
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+ if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') {
+ out.append("");
+ out.append((int) c);
+ out.append(';');
+ } else {
+ out.append(c);
+ }
+ }
+ return out.toString();
+ }
+
+}
diff --git a/src/attributes/Attribute.java b/src/attributes/Attribute.java
index ba868df..89d6870 100644
--- a/src/attributes/Attribute.java
+++ b/src/attributes/Attribute.java
@@ -1,12 +1,14 @@
package j2html.src.attributes;
+import static j2html.src.HtmlEscaper.HtmlEscaper.escape;
+
public class Attribute {
private String name;
private String value;
public Attribute(String name, String value) {
this.name = name;
- this.value = value;
+ this.value = escape(value);
}
public Attribute(String name) {
diff --git a/src/tags/Tag.java b/src/tags/Tag.java
index 7e0ef02..936df32 100644
--- a/src/tags/Tag.java
+++ b/src/tags/Tag.java
@@ -12,7 +12,7 @@ public abstract class Tag {
protected Tag(String tagType) {
this.tag = tagType;
- this.attributes = new ArrayList();
+ this.attributes = new ArrayList<>();
}
public void setParent(Tag parent) {
diff --git a/src/tags/Text.java b/src/tags/Text.java
index ad02b5b..788cce0 100644
--- a/src/tags/Text.java
+++ b/src/tags/Text.java
@@ -1,5 +1,7 @@
package j2html.src.tags;
+import static j2html.src.HtmlEscaper.HtmlEscaper.escape;
+
public class Text extends Tag {
public Text(String text) {
@@ -8,12 +10,14 @@ public class Text extends Tag {
@Override
public String render(){
- return tag;
+ return escape(tag);
}
@Override
public String toString(){
return this.render();
}
+
+
}
diff --git a/test/testPerformance/RenderSpeed.java b/test/testPerformance/RenderSpeed.java
new file mode 100644
index 0000000..a0c53e1
--- /dev/null
+++ b/test/testPerformance/RenderSpeed.java
@@ -0,0 +1,44 @@
+package j2html.test.testPerformance;
+
+import org.junit.Test;
+
+import static j2html.src.tags.TagCreator.*;
+
+public class RenderSpeed {
+
+ private String renderTest(int i) {
+ return html().with(
+ head().with(
+ title().withText("Test " + i)
+ ),
+ body().with(
+ header().with(
+ h1().withText("Test Header")
+ ),
+ main().with(
+ h2().withText("Test Form"),
+ div().with(
+ input().withType("email").withName("email").withPlaceholder("Email"),
+ input().withType("password").withName("password").withPlaceholder("password"),
+ button().withType("submit").withText("Login")
+ )
+ ),
+ footer().withText("Test Footer"),
+ script().withSrc("/testScript.js")
+ )
+ ).render();
+ }
+
+ @Test
+ public void testRenderSpeed() {
+ long startTime = System.nanoTime();
+ int pages = 100000;
+ for(int i = 0; i < pages; i++){
+ String testString = renderTest(i);
+ }
+ long endTime = System.nanoTime();
+ long duration = (endTime - startTime)/1000000;
+ System.out.print("Rendered 100000 pages in " + duration + "ms");
+ }
+
+}