/** * Perform a depth-first traversal on each of the selected elements. * @param nodeVisitor the visitor callbacks to perform on each node * @return this, for chaining */ public Elements traverse(NodeVisitor nodeVisitor) { NodeTraversor.traverse(nodeVisitor, this); return this; }
/** * Start a depth-first traverse of the root and all of its descendants. * @param root the root node point to traverse. * @deprecated Just use the static {@link NodeTraversor#filter(NodeFilter, Node)} method. */ public void traverse(Node root) { traverse(visitor, root); }
/** * Get the (unencoded) text of all children of this element, including any newlines and spaces present in the * original. * * @return unencoded, un-normalized text * @see #text() */ public String wholeText() { final StringBuilder accum = new StringBuilder(); NodeTraversor.traverse(new NodeVisitor() { public void head(Node node, int depth) { if (node instanceof TextNode) { TextNode textNode = (TextNode) node; accum.append(textNode.getWholeText()); } } public void tail(Node node, int depth) { } }, this); return accum.toString(); }
/** * Perform a depth-first traversal through this node and its descendants. * @param nodeVisitor the visitor callbacks to perform on each node * @return this node, for chaining */ public Node traverse(NodeVisitor nodeVisitor) { Validate.notNull(nodeVisitor); NodeTraversor.traverse(nodeVisitor, this); return this; }
private int copySafeNodes(Element source, Element dest) { CleaningVisitor cleaningVisitor = new CleaningVisitor(source, dest); NodeTraversor.traverse(cleaningVisitor, source); return cleaningVisitor.numDiscarded; }
NodeTraversor.traverse(new NodeVisitor() { public void head(Node node, int depth) { if (node instanceof TextNode) {
private void copySafeNodes(Element source, Element destination) { CleaningVisitor cleaningVisitor = new CleaningVisitor(source, destination); NodeTraversor traversor = new NodeTraversor(cleaningVisitor); traversor.traverse(source); }
/** Build a list of elements, by visiting root and every descendant of root, and testing it against the evaluator. @param eval Evaluator to test elements against @param root root of tree to descend @return list of matches; empty if none */ public static Elements collect (Evaluator eval, Element root) { Elements elements = new Elements(); NodeTraversor.traverse(new Accumulator(root, elements, eval), root); return elements; }
/** * Start a depth-first traverse of all elements. * @param visitor Node visitor. * @param elements Elements to filter. */ public static void traverse(NodeVisitor visitor, Elements elements) { Validate.notNull(visitor); Validate.notNull(elements); for (Element el : elements) traverse(visitor, el); }
protected void outerHtml(Appendable accum) { NodeTraversor.traverse(new OuterHtmlVisitor(accum, getOutputSettings()), this); }
/** * Converts a jsoup document into the provided W3C Document. If required, you can set options on the output document * before converting. * @param in jsoup doc * @param out w3c doc * @see org.jsoup.helper.W3CDom#fromJsoup(org.jsoup.nodes.Document) */ public void convert(org.jsoup.nodes.Document in, Document out) { if (!StringUtil.isBlank(in.location())) out.setDocumentURI(in.location()); org.jsoup.nodes.Element rootEl = in.child(0); // skip the #root node NodeTraversor.traverse(new W3CBuilder(out), rootEl); }
private void copySafeNodes(Element source, Element dest) { CleaningVisitor cleaningVisitor = new CleaningVisitor(dest); NodeTraversor traversor = new NodeTraversor(cleaningVisitor); traversor.traverse(source); }
/** * Format an Element to plain-text * @param element the root element to format * @return formatted text */ public String getPlainText(Element element) { FormattingVisitor formatter = new FormattingVisitor(); NodeTraversor traversor = new NodeTraversor(formatter); traversor.traverse(element); // walk the DOM, and call .head() and .tail() for each node return formatter.toString(); }
@Override public Iterator<URL> getEmbeddedResourceURLs(String userAgent, byte[] html, URL baseUrl, URLCollection coll, String encoding) throws HTMLParseException { try { // TODO Handle conditional comments for IE String contents = new String(html,encoding); Document doc = Jsoup.parse(contents); JMeterNodeVisitor nodeVisitor = new JMeterNodeVisitor(new URLPointer(baseUrl), coll); new NodeTraversor(nodeVisitor).traverse(doc); return coll.iterator(); } catch (Exception e) { throw new HTMLParseException(e); } } }
private static Document convert(org.jsoup.nodes.Document document) { Document w3cDoc = new org.apache.html.dom.HTMLDocumentImpl(); org.jsoup.nodes.Element rootEl = document.children().first(); if (rootEl != null) { NodeTraversor.traverse(new DocumentConverter(w3cDoc), rootEl); } return w3cDoc; }
@Override public String convert(@Nonnull final String html) throws IOException { final DocumentUtilities.DocumentElement document = DocumentUtilities.parseHtml(html); final DocumentUtilities.BodyElement body = DocumentUtilities.getBody(document); FormattingVisitor formatter = new FormattingVisitor(); NodeTraversor traversor = new NodeTraversor(formatter); traversor.traverse(body.getBody()); // walk the DOM, and call .head() and .tail() for each node return formatter.toString(); }
tranversor.traverse(rendered);
public static DocumentFragment fromJsoup( org.jsoup.nodes.Document jsoupDocument) { HTMLDocumentImpl htmlDoc = new HTMLDocumentImpl(); htmlDoc.setErrorChecking(false); DocumentFragment fragment = htmlDoc.createDocumentFragment(); org.jsoup.nodes.Element rootEl = jsoupDocument.child(0); // skip the // #root node NodeTraversor.traverse(new W3CBuilder(htmlDoc, fragment), rootEl); return fragment; }
Document doc = Jsoup.parse(input); NodeTraversor traversor = new NodeTraversor(new NodeVisitor() { @Override public void tail(Node node, int depth) { if (node instanceof Element) { Element e = (Element) node; e.removeAttr("class"); e.removeAttr("style"); } } @Override public void head(Node node, int depth) { } }); traversor.traverse(doc.body()); String modifiedHtml = doc.toString();
Document doc = Jsoup.parse(input); NodeTraversor traversor = new NodeTraversor(new NodeVisitor() { @Override public void tail(Node node, int depth) { if (node instanceof Element) { Element e = (Element) node; e.removeAttr("width"); e.removeAttr("border"); e.removeAttr("bgcolor"); } } @Override public void head(Node node, int depth) { } }); traversor.traverse(doc.body()); String modifiedHtml = doc.toString();