use of org.jsoup.nodes.Node in project jsoup by jhy.
the class TraversorTest method filterVisit.
// Note: NodeTraversor.traverse(new NodeVisitor) is tested in
// ElementsTest#traverse()
@Test
public void filterVisit() {
Document doc = Jsoup.parse("<div><p>Hello</p></div><div>There</div>");
final StringBuilder accum = new StringBuilder();
NodeTraversor.filter(new NodeFilter() {
@Override
public FilterResult head(Node node, int depth) {
accum.append("<").append(node.nodeName()).append(">");
return FilterResult.CONTINUE;
}
@Override
public FilterResult tail(Node node, int depth) {
accum.append("</").append(node.nodeName()).append(">");
return FilterResult.CONTINUE;
}
}, doc.select("div"));
assertEquals("<div><p><#text></#text></p></div><div><#text></#text></div>", accum.toString());
}
use of org.jsoup.nodes.Node in project jsoup by jhy.
the class TraversorTest method filterSkipChildren.
@Test
public void filterSkipChildren() {
Document doc = Jsoup.parse("<div><p>Hello</p></div><div>There</div>");
final StringBuilder accum = new StringBuilder();
NodeTraversor.filter(new NodeFilter() {
@Override
public FilterResult head(Node node, int depth) {
accum.append("<").append(node.nodeName()).append(">");
// OMIT contents of p:
return ("p".equals(node.nodeName())) ? FilterResult.SKIP_CHILDREN : FilterResult.CONTINUE;
}
@Override
public FilterResult tail(Node node, int depth) {
accum.append("</").append(node.nodeName()).append(">");
return FilterResult.CONTINUE;
}
}, doc.select("div"));
assertEquals("<div><p></p></div><div><#text></#text></div>", accum.toString());
}
use of org.jsoup.nodes.Node in project jsoup by jhy.
the class Cleaner method isValidBodyHtml.
public boolean isValidBodyHtml(String bodyHtml) {
Document clean = Document.createShell("");
Document dirty = Document.createShell("");
ParseErrorList errorList = ParseErrorList.tracking(1);
List<Node> nodes = Parser.parseFragment(bodyHtml, dirty.body(), "", errorList);
dirty.body().insertChildren(0, nodes);
int numDiscarded = copySafeNodes(dirty.body(), clean.body());
return numDiscarded == 0 && errorList.isEmpty();
}
use of org.jsoup.nodes.Node in project jsoup by jhy.
the class NodeTraversor method filter.
/**
* Start a depth-first filtering of the root and all of its descendants.
* @param filter Node visitor.
* @param root the root node point to traverse.
* @return The filter result of the root node, or {@link FilterResult#STOP}.
*/
public static FilterResult filter(NodeFilter filter, Node root) {
Node node = root;
int depth = 0;
while (node != null) {
FilterResult result = filter.head(node, depth);
if (result == FilterResult.STOP)
return result;
// Descend into child nodes:
if (result == FilterResult.CONTINUE && node.childNodeSize() > 0) {
node = node.childNode(0);
++depth;
continue;
}
// No siblings, move upwards:
while (true) {
// depth > 0, so has parent
assert node != null;
if (!(node.nextSibling() == null && depth > 0))
break;
// 'tail' current node:
if (result == FilterResult.CONTINUE || result == FilterResult.SKIP_CHILDREN) {
result = filter.tail(node, depth);
if (result == FilterResult.STOP)
return result;
}
// In case we need to remove it below.
Node prev = node;
node = node.parentNode();
depth--;
if (result == FilterResult.REMOVE)
// Remove AFTER finding parent.
prev.remove();
// Parent was not pruned.
result = FilterResult.CONTINUE;
}
// 'tail' current node, then proceed with siblings:
if (result == FilterResult.CONTINUE || result == FilterResult.SKIP_CHILDREN) {
result = filter.tail(node, depth);
if (result == FilterResult.STOP)
return result;
}
if (node == root)
return result;
// In case we need to remove it below.
Node prev = node;
node = node.nextSibling();
if (result == FilterResult.REMOVE)
// Remove AFTER finding sibling.
prev.remove();
}
// root == null?
return FilterResult.CONTINUE;
}
use of org.jsoup.nodes.Node in project jsoup by jhy.
the class HtmlTreeBuilder method insert.
void insert(Token.Character characterToken) {
final Node node;
// will be doc if no current element; allows for whitespace to be inserted into the doc root object (not on the stack)
Element el = currentElement();
final String tagName = el.normalName();
final String data = characterToken.getData();
if (characterToken.isCData())
node = new CDataNode(data);
else if (isContentForTagData(tagName))
node = new DataNode(data);
else
node = new TextNode(data);
// doesn't use insertNode, because we don't foster these; and will always have a stack.
el.appendChild(node);
}
Aggregations