added naive html escaper
This commit is contained in:
20
src/HtmlEscaper/HtmlEscaper.java
Normal file
20
src/HtmlEscaper/HtmlEscaper.java
Normal file
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -12,7 +12,7 @@ public abstract class Tag {
|
||||
|
||||
protected Tag(String tagType) {
|
||||
this.tag = tagType;
|
||||
this.attributes = new ArrayList<Attribute>();
|
||||
this.attributes = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void setParent(Tag parent) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
44
test/testPerformance/RenderSpeed.java
Normal file
44
test/testPerformance/RenderSpeed.java
Normal file
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user