From 466d4ed5374e3ada2d335bc04463a426be1327b8 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 21 Feb 2016 21:10:30 +0000 Subject: Allow HTML elements to have a choice of tag names --- src/main/java/org/zwobble/mammoth/html/Html.java | 20 ++++++++++++++------ .../java/org/zwobble/mammoth/html/HtmlElement.java | 12 ++++++++---- .../org/zwobble/mammoth/styles/HtmlPathElement.java | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/zwobble/mammoth/html/Html.java b/src/main/java/org/zwobble/mammoth/html/Html.java index c0b234b..e1bcef7 100644 --- a/src/main/java/org/zwobble/mammoth/html/Html.java +++ b/src/main/java/org/zwobble/mammoth/html/Html.java @@ -38,11 +38,15 @@ public class Html { } public static HtmlNode element(String tagName, Map attributes, List children) { - return new HtmlElement(tagName, attributes, children, false); + return new HtmlElement(list(tagName), attributes, children, false); } public static HtmlNode collapsibleElement(String tagName) { - return collapsibleElement(tagName, list()); + return collapsibleElement(list(tagName)); + } + + public static HtmlNode collapsibleElement(List tagNames) { + return collapsibleElement(tagNames, map(), list()); } public static HtmlNode collapsibleElement(String tagName, List children) { @@ -50,7 +54,11 @@ public class Html { } public static HtmlNode collapsibleElement(String tagName, Map attributes, List children) { - return new HtmlElement(tagName, attributes, children, true); + return collapsibleElement(list(tagName), attributes, children); + } + + public static HtmlNode collapsibleElement(List tagNames, Map attributes, List children) { + return new HtmlElement(tagNames, attributes, children, true); } public static HtmlNode selfClosingElement(String tagName) { @@ -74,7 +82,7 @@ public class Html { return list(); } else { return list(new HtmlElement( - element.getTagName(), + element.getTagNames(), element.getAttributes(), children, element.isCollapsible())); @@ -124,7 +132,7 @@ public class Html { @Override public HtmlNode visit(HtmlElement element) { return new HtmlElement( - element.getTagName(), + element.getTagNames(), element.getAttributes(), collapse(element.getChildren()), element.isCollapsible()); @@ -165,7 +173,7 @@ public class Html { } private static boolean isMatch(HtmlElement first, HtmlElement second) { - return first.getTagName().equals(second.getTagName()) && + return second.getTagNames().contains(first.getTagName()) && first.getAttributes().equals(second.getAttributes()); } } diff --git a/src/main/java/org/zwobble/mammoth/html/HtmlElement.java b/src/main/java/org/zwobble/mammoth/html/HtmlElement.java index cc81aa2..b6dab22 100644 --- a/src/main/java/org/zwobble/mammoth/html/HtmlElement.java +++ b/src/main/java/org/zwobble/mammoth/html/HtmlElement.java @@ -4,20 +4,24 @@ import java.util.List; import java.util.Map; public class HtmlElement implements HtmlNode { - private final String tagName; + private final List tagNames; private final Map attributes; private final List children; private final boolean collapsible; - public HtmlElement(String tagName, Map attributes, List children, boolean collapsible) { - this.tagName = tagName; + public HtmlElement(List tagNames, Map attributes, List children, boolean collapsible) { + this.tagNames = tagNames; this.attributes = attributes; this.children = children; this.collapsible = collapsible; } + public List getTagNames() { + return tagNames; + } + public String getTagName() { - return tagName; + return tagNames.get(0); } public Map getAttributes() { diff --git a/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java b/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java index b34fb50..cfd20a6 100644 --- a/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java +++ b/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java @@ -29,6 +29,6 @@ public class HtmlPathElement { } public List wrap(List nodes) { - return list(new HtmlElement(tagName, attributes, nodes, isCollapsible)); + return list(new HtmlElement(list(tagName), attributes, nodes, isCollapsible)); } } -- cgit v1.2.3