aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-14 11:40:44 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-14 11:40:44 +0000
commit2c4946f27b559245d070a6d85ccfbea3d3051134 (patch)
treed1ed8e8f8db444ccee20473b5f5cef49dfb70f6e /src
parent7fb58782e670a13ac7389e43ff0bacbce19f9d05 (diff)
Add attributes to HtmlElementNode
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/mammoth/html/Html.java8
-rw-r--r--src/main/java/org/zwobble/mammoth/html/HtmlElementNode.java31
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/html/HtmlWriteTests.java8
3 files changed, 41 insertions, 6 deletions
diff --git a/src/main/java/org/zwobble/mammoth/html/Html.java b/src/main/java/org/zwobble/mammoth/html/Html.java
index cef54b3..c89a41b 100644
--- a/src/main/java/org/zwobble/mammoth/html/Html.java
+++ b/src/main/java/org/zwobble/mammoth/html/Html.java
@@ -1,8 +1,10 @@
package org.zwobble.mammoth.html;
import java.util.List;
+import java.util.Map;
import static org.zwobble.mammoth.util.MammothLists.list;
+import static org.zwobble.mammoth.util.MammothMaps.map;
public class Html {
public static String write(List<HtmlNode> nodes) {
@@ -20,7 +22,11 @@ public class Html {
}
public static HtmlNode element(String tagName, List<HtmlNode> children) {
- return new HtmlElementNode(tagName, children);
+ return element(tagName, map(), children);
+ }
+
+ public static HtmlNode element(String tagName, Map<String, String> attributes, List<HtmlNode> children) {
+ return new HtmlElementNode(tagName, attributes, children);
}
public static HtmlNode selfClosingElement(String tagName) {
diff --git a/src/main/java/org/zwobble/mammoth/html/HtmlElementNode.java b/src/main/java/org/zwobble/mammoth/html/HtmlElementNode.java
index 34de63e..d237f6b 100644
--- a/src/main/java/org/zwobble/mammoth/html/HtmlElementNode.java
+++ b/src/main/java/org/zwobble/mammoth/html/HtmlElementNode.java
@@ -1,22 +1,39 @@
package org.zwobble.mammoth.html;
+import com.google.common.base.Function;
+import com.google.common.collect.Ordering;
+
import java.util.List;
+import java.util.Map;
public class HtmlElementNode implements HtmlNode {
private final String tagName;
+ private final Map<String, String> attributes;
private final List<HtmlNode> children;
- public HtmlElementNode(String tagName, List<HtmlNode> children) {
+ public HtmlElementNode(String tagName, Map<String, String> attributes, List<HtmlNode> children) {
this.tagName = tagName;
+ this.attributes = attributes;
this.children = children;
}
@Override
public void write(StringBuilder builder) {
- builder
- .append("<")
- .append(tagName)
- .append(">");
+ builder.append("<").append(tagName);
+
+ List<Map.Entry<String, String>> sortedAttributes = orderBy(Map.Entry<String, String>::getKey)
+ .sortedCopy(attributes.entrySet());
+
+ for (Map.Entry<String, String> attribute : sortedAttributes) {
+ builder
+ .append(" ")
+ .append(attribute.getKey())
+ .append("=\"")
+ .append(attribute.getValue())
+ .append("\"");
+ }
+
+ builder.append(">");
children.forEach(node -> node.write(builder));
@@ -25,4 +42,8 @@ public class HtmlElementNode implements HtmlNode {
.append(tagName)
.append(">");
}
+
+ private static <T, R extends Comparable<R>> Ordering<T> orderBy(Function<T, R> getKey) {
+ return Ordering.from((first, second) -> getKey.apply(first).compareTo(getKey.apply(second)));
+ }
}
diff --git a/src/test/java/org/zwobble/mammoth/tests/html/HtmlWriteTests.java b/src/test/java/org/zwobble/mammoth/tests/html/HtmlWriteTests.java
index 30c6649..4400959 100644
--- a/src/test/java/org/zwobble/mammoth/tests/html/HtmlWriteTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/html/HtmlWriteTests.java
@@ -6,6 +6,7 @@ import org.zwobble.mammoth.html.HtmlNode;
import static org.junit.Assert.assertEquals;
import static org.zwobble.mammoth.util.MammothLists.list;
+import static org.zwobble.mammoth.util.MammothMaps.map;
public class HtmlWriteTests {
@Test
@@ -45,6 +46,13 @@ public class HtmlWriteTests {
Html.element("ul")))));
}
+ @Test
+ public void canWriteElementWithAttributes() {
+ assertEquals(
+ "<a class=\"external\" href=\"http://example.com\"></a>",
+ write(Html.element("a", map("class", "external", "href", "http://example.com"), list())));
+ }
+
private String write(HtmlNode node) {
return Html.write(list(node));
}