Compare commits
9 Commits
j2html-1.2
...
j2html-1.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb3f28a09f | ||
|
|
368241313d | ||
|
|
4288924f8f | ||
|
|
9948095c08 | ||
|
|
56090046b5 | ||
|
|
d45af42650 | ||
|
|
7d65f91f44 | ||
|
|
aecf5e71d6 | ||
|
|
cf47772c89 |
@@ -13,12 +13,12 @@ The project webpage is [j2html.com](http://j2html.com).
|
||||
<dependency>
|
||||
<groupId>com.j2html</groupId>
|
||||
<artifactId>j2html</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
```
|
||||
### OR the gradle dependency
|
||||
```
|
||||
compile 'com.j2html:j2html:1.1.0'
|
||||
compile 'com.j2html:j2html:1.2.0'
|
||||
```
|
||||
|
||||
### Import TagCreator and start building HTML
|
||||
|
||||
3
pom.xml
3
pom.xml
@@ -10,7 +10,7 @@
|
||||
|
||||
<groupId>com.j2html</groupId>
|
||||
<artifactId>j2html</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
|
||||
<name>j2html</name>
|
||||
<description>Java to HTML builder with a fluent API</description>
|
||||
@@ -70,6 +70,7 @@
|
||||
<version>2.5.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<?m2e ignore?>
|
||||
<goals>
|
||||
<goal>process-main</goal>
|
||||
<goal>process-test</goal>
|
||||
|
||||
@@ -11,6 +11,8 @@ import j2html.utils.TextEscaper;
|
||||
|
||||
public class Config {
|
||||
|
||||
private static String FOUR_SPACES = " ";
|
||||
|
||||
private Config() {
|
||||
}
|
||||
|
||||
@@ -36,7 +38,6 @@ public class Config {
|
||||
* Change this to configure indentation when rendering formatted html
|
||||
* The default is four spaces
|
||||
*/
|
||||
private static String FOUR_SPACES = " ";
|
||||
public static Indenter indenter = (level, text) -> String.join("", Collections.nCopies(level, FOUR_SPACES)) + text;
|
||||
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ public class TagCreator {
|
||||
*/
|
||||
public static String document(ContainerTag htmlTag) {
|
||||
if (htmlTag.getTagName().equals("html")) {
|
||||
return new EmptyTag("!DOCTYPE html").render() + htmlTag.render();
|
||||
return document().render() + htmlTag.render();
|
||||
}
|
||||
throw new IllegalArgumentException("Only HTML-tag can follow document declaration");
|
||||
}
|
||||
@@ -132,7 +132,7 @@ public class TagCreator {
|
||||
public static ContainerTag styleWithInlineFile_min(String path) { return InlineStaticResource.get(path, InlineStaticResource.TargetFormat.CSS_MIN); }
|
||||
public static ContainerTag scriptWithInlineFile_min(String path) { return InlineStaticResource.get(path, InlineStaticResource.TargetFormat.JS_MIN); }
|
||||
|
||||
public static EmptyTag document() { return new EmptyTag("!DOCTYPE html"); }
|
||||
public static DomContent document() { return rawHtml("<!DOCTYPE html>"); }
|
||||
|
||||
// EmptyTags, generated in class j2html.tags.TagCreatorCodeGenerator
|
||||
public static EmptyTag area() { return new EmptyTag("area"); }
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package j2html.tags;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
import j2html.Config;
|
||||
import j2html.utils.CSSMin;
|
||||
import j2html.utils.JSMin;
|
||||
|
||||
import static j2html.TagCreator.*;
|
||||
|
||||
@@ -27,27 +25,19 @@ public class InlineStaticResource {
|
||||
|
||||
public static String getFileAsString(String path) {
|
||||
try {
|
||||
return readFileAsString(InlineStaticResource.class.getResource(path).getPath());
|
||||
} catch (Exception e1) {
|
||||
return streamToString(InlineStaticResource.class.getResourceAsStream(path));
|
||||
} catch (Exception expected) { // we don't ask users to specify classpath or file-system
|
||||
try {
|
||||
return readFileAsString(path);
|
||||
} catch (Exception e2) {
|
||||
return streamToString(new FileInputStream(path));
|
||||
} catch (Exception exception) {
|
||||
throw new RuntimeException("Couldn't find file with path='" + path + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author kjheimark <3
|
||||
*/
|
||||
private static String readFileAsString(String path) throws IOException {
|
||||
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int c;
|
||||
while ((c = bufferedReader.read()) >= 0 && c >= 0) {
|
||||
sb.append((char) c);
|
||||
}
|
||||
return sb.toString();
|
||||
private static String streamToString(InputStream inputStream) {
|
||||
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
|
||||
return s.hasNext() ? s.next() : "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -124,9 +124,11 @@ public abstract class Tag<T extends Tag<T>> extends DomContent {
|
||||
public T withCharset(String charset) { return attr(Attr.CHARSET, charset); }
|
||||
public T withClass(String className) { return attr(Attr.CLASS, className); }
|
||||
public T withContent(String content) { return attr(Attr.CONTENT, content); }
|
||||
public T withDir(String dir) { return attr(Attr.DIR, dir); }
|
||||
public T withHref(String href) { return attr(Attr.HREF, href); }
|
||||
public T withId(String id) { return attr(Attr.ID, id); }
|
||||
public T withData(String dataAttr, String value) { return attr(Attr.DATA + "-" + dataAttr, value); }
|
||||
public T withLang(String lang) { return attr(Attr.LANG, lang); }
|
||||
public T withMethod(String method) { return attr(Attr.METHOD, method); }
|
||||
public T withName(String name) { return attr(Attr.NAME, name); }
|
||||
public T withPlaceholder(String placeholder) { return attr(Attr.PLACEHOLDER, placeholder); }
|
||||
@@ -148,9 +150,11 @@ public abstract class Tag<T extends Tag<T>> extends DomContent {
|
||||
public T withCharset(boolean condition, String charset) { return condAttr(condition, Attr.CHARSET, charset); }
|
||||
public T withCondClass(boolean condition, String className) { return condAttr(condition, Attr.CLASS, className); }
|
||||
public T withCondContent(boolean condition, String content) { return condAttr(condition, Attr.CONTENT, content); }
|
||||
public T withCondDir(boolean condition, String dir) { return condAttr(condition, Attr.DIR, dir); }
|
||||
public T withCondHref(boolean condition, String href) { return condAttr(condition, Attr.HREF, href); }
|
||||
public T withCondId(boolean condition, String id) { return condAttr(condition, Attr.ID, id); }
|
||||
public T withCondData(boolean condition, String dataAttr, String value) { return condAttr(condition, Attr.DATA + "-" + dataAttr, value); }
|
||||
public T withCondLang(boolean condition, String lang) { return condAttr(condition, Attr.LANG, lang); }
|
||||
public T withCondMethod(boolean condition, String method) { return condAttr(condition, Attr.METHOD, method); }
|
||||
public T withCondName(boolean condition, String name) { return condAttr(condition, Attr.NAME, name); }
|
||||
public T withCondPlaceholder(boolean condition, String placeholder) { return condAttr(condition, Attr.PLACEHOLDER, placeholder); }
|
||||
|
||||
@@ -224,7 +224,8 @@ class Selector {
|
||||
if (contents.length() == 1) {
|
||||
throw new EmptySelectorBodyException(selector);
|
||||
}
|
||||
contents = contents.substring(0, contents.length() - 2);
|
||||
int endIndex = contents.endsWith(";}") ? 2 : 1;
|
||||
contents = contents.substring(0, contents.length() - endIndex);
|
||||
|
||||
this.properties = new Property[0];
|
||||
this.properties = parseProperties(contents).toArray(this.properties);
|
||||
|
||||
@@ -24,6 +24,7 @@ public class InlineStaticResourceTest {
|
||||
|
||||
// classpath files
|
||||
assertThat(styleWithInlineFile_min("/test.css").render(), is(expectedCss));
|
||||
assertThat(styleWithInlineFile_min("/test-without-trailing-semis.css").render(), is(expectedCss));
|
||||
assertThat(scriptWithInlineFile_min("/test.js").render(), is(expectedJs));
|
||||
assertThat(fileAsString("/test.html").render(), is(expectedHtml));
|
||||
assertThat(fileAsEscapedString("/test.html").render(), is(expectedEscapedHtml));
|
||||
@@ -42,4 +43,4 @@ public class InlineStaticResourceTest {
|
||||
styleWithInlineFile_min("NOT A FILE");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import j2html.Config;
|
||||
|
||||
import static j2html.TagCreator.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TagCreatorTest {
|
||||
|
||||
@@ -16,6 +19,17 @@ public class TagCreatorTest {
|
||||
|
||||
List<Employee> employees = Arrays.asList(new Employee(1, "Name 1", "Title 1"), new Employee(2, "Name 2", "Title 2"), new Employee(3, "Name 3", "Title 3"));
|
||||
|
||||
|
||||
@Test
|
||||
public void testDocument() throws Exception {
|
||||
Config.closeEmptyTags = true;
|
||||
assertEquals("<!DOCTYPE html>", document().render());
|
||||
assertEquals("<!DOCTYPE html><html></html>", document(html()));
|
||||
Config.closeEmptyTags = false;
|
||||
assertEquals("<!DOCTYPE html>", document().render());
|
||||
assertEquals("<!DOCTYPE html><html></html>", document(html()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIff() throws Exception {
|
||||
String expected = "<div><p>Test</p><a href=\"#\">Test</a></div>";
|
||||
|
||||
7
src/test/resources/test-without-trailing-semis.css
Normal file
7
src/test/resources/test-without-trailing-semis.css
Normal file
@@ -0,0 +1,7 @@
|
||||
body {
|
||||
background: goldenrod;
|
||||
margin-top: 10px;
|
||||
margin-right: 10px;
|
||||
margin-bottom: 10px;
|
||||
margin-left: 10px
|
||||
}
|
||||
Reference in New Issue
Block a user