9 Commits

Author SHA1 Message Date
David
eb3f28a09f [maven-release-plugin] prepare release j2html-1.2.1 2017-11-28 00:05:25 +01:00
David
368241313d Allow reading static resources from jars 2017-11-27 19:41:20 +01:00
David
4288924f8f Fix greedy semi-colon stripper bug in CSSMin
If CSSMin encounters a CSS-rule without a semi-colon at the end,
it will strip one char too many from that rule. This commit fixes that.
2017-11-27 12:54:07 +01:00
Janning Vygen
9948095c08 Fix for https://github.com/tipsy/j2html/issues/80 (#82)
* Fix for https://github.com/tipsy/j2html/issues/79

* Fix for https://github.com/tipsy/j2html/issues/80
2017-11-10 16:11:58 +01:00
Janning Vygen
56090046b5 Fix for https://github.com/tipsy/j2html/issues/79 (#81) 2017-11-10 16:07:20 +01:00
Thomas Tanon
d45af42650 Adds Tag.withLang and Tag.withDir (#76)
* Adds Tag.withLang and Tag.withDir

Allows easiest addition of mixed languages

* Add withCond(Lang|Dir)
2017-10-23 14:21:29 +02:00
David
7d65f91f44 Fix javadoc bug 2017-09-03 11:30:05 +02:00
David
aecf5e71d6 Update README.md 2017-09-03 11:26:10 +02:00
David
cf47772c89 [maven-release-plugin] prepare for next development iteration 2017-09-03 11:16:05 +02:00
10 changed files with 47 additions and 28 deletions

View File

@@ -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

View File

@@ -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>

View File

@@ -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;

View File

@@ -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"); }

View File

@@ -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() : "";
}
}

View File

@@ -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); }

View File

@@ -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);

View File

@@ -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");
}
}
}

View 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>";

View File

@@ -0,0 +1,7 @@
body {
background: goldenrod;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
margin-left: 10px
}