aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/zwobble/mammoth/html/Html.java8
-rw-r--r--src/main/java/org/zwobble/mammoth/html/HtmlElementNode.java31
2 files changed, 33 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)));
+ }
}