HTML Render with example UI
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package org.openautonomousconnection.htmlparser.dom;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
public final class Document {
|
||||
private final Element root;
|
||||
|
||||
public Document(Element root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
public Element getRoot() { return root; }
|
||||
|
||||
public Element createElement(String tag) { return new Element(tag); }
|
||||
public TextNode createTextNode(String text) { return new TextNode(text); }
|
||||
|
||||
public Element getElementById(String id) {
|
||||
if (id == null) return null;
|
||||
Deque<DomNode> stack = new ArrayDeque<>();
|
||||
stack.push(root);
|
||||
while (!stack.isEmpty()) {
|
||||
DomNode n = stack.pop();
|
||||
if (n instanceof Element e) {
|
||||
if (id.equals(e.id())) return e;
|
||||
}
|
||||
for (int i = n.getChildren().size() - 1; i >= 0; i--) stack.push(n.getChildren().get(i));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Element querySelector(String selector) {
|
||||
return DomQuery.querySelector(root, selector);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user