/** * Replace each element in the set of matched elements with the provided * new content and return the set of elements that was removed. */ public Jerry replaceWith(String html) { if (html == null) { html = StringPool.EMPTY; } final Document doc = builder.parse(html); if (nodes.length == 0) { return this; } for (Node node : nodes) { Node parent = node.getParentNode(); // if a node already is the root element, don't unwrap if (parent == null) { continue; } // replace, if possible Document workingDoc = doc.clone(); int index = node.getSiblingIndex(); parent.insertChild(workingDoc.getFirstChild(), index); node.detachFromParent(); } return this; }
@Test void testDoubleDecode() { LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder(); Document document = lagartoDOMBuilder.parse("<div title=\"&lt;root /&gt;\">"); Element div = (Element) document.getFirstChild(); assertEquals("<root />", div.getAttribute("title")); } }
/** * Wraps an HTML structure around each element in the set of matched elements. * Returns the original set of elements for chaining purposes. */ public Jerry wrap(String html) { if (html == null) { html = StringPool.EMPTY; } final Document doc = builder.parse(html); if (nodes.length == 0) { return this; } for (Node node : nodes) { Document workingDoc = doc.clone(); Node inmostNode = workingDoc; while (inmostNode.hasChildNodes()) { inmostNode = inmostNode.getFirstChild(); } // replace Node parent = node.getParentNode(); int index = node.getSiblingIndex(); inmostNode.addChild(node); parent.insertChild(workingDoc.getFirstChild(), index); } return this; }
@Test void testAppendable() { LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder(); Document document = lagartoDOMBuilder.parse("<div foo=\"123\">some <b>nice</b> text</div>"); Element div = (Element) document.getFirstChild(); String textContent = div.getTextContent(); StringBuilder stringBuilder = new StringBuilder(); div.appendTextContent(stringBuilder); assertEquals(textContent, stringBuilder.toString()); FastCharArrayWriter charBuffer = new FastCharArrayWriter(); div.appendTextContent(charBuffer); assertEquals(textContent, charBuffer.toString()); }
@Test void testEncode() { LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder(); Document document = lagartoDOMBuilder.parse("<div foo=\"q w\">a<b</div>"); Element div = (Element) document.getFirstChild(); String foo = div.getAttribute("foo"); assertEquals("q\u00A0w", foo); div.setAttribute("foo", "q\u00A0w\u00A0e"); assertEquals("<div foo=\"q w e\">a<b</div>", document.getHtml()); Text text = (Text) document.getFirstChild().getFirstChild(); assertEquals("a<b", text.getNodeValue()); assertEquals("a<b", text.getTextContent()); assertEquals("a<b", text.getTextValue()); assertTrue(document.check()); }
@Test void testXmlDec() { LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder(); lagartoDOMBuilder.enableXmlMode(); Document document = lagartoDOMBuilder.parse("<?xml version=\"1.0\"?><div?></div>"); XmlDeclaration xml = (XmlDeclaration) document.getFirstChild(); assertEquals(0, xml.getAttributesCount()); assertEquals("xml", xml.getNodeName()); Element div = (Element) xml.getNextSibling(); assertEquals(0, div.getAttributesCount()); assertEquals("div?", div.getNodeName()); assertTrue(document.check()); }
@Test void testOrphanAttribute() { LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder(); Document document = lagartoDOMBuilder.parse("<div qwe '8989' foo zoo='123'/>"); Element div = (Element) document.getFirstChild(); assertEquals("div", div.getNodeName()); assertEquals(4, div.getAttributesCount()); assertTrue(div.hasAttribute("qwe")); assertTrue(div.hasAttribute("foo")); assertTrue(div.hasAttribute("zoo")); assertTrue(div.hasAttribute("'8989'")); assertTrue(document.check()); }
@Test void testPeopleXml() throws IOException { File file = new File(testDataRoot, "people.xml"); String xmlContent = FileUtil.readString(file); LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder(); lagartoDOMBuilder.enableXmlMode(); Document doc = lagartoDOMBuilder.parse(xmlContent); assertEquals(2, doc.getChildNodesCount()); // not 3! XmlDeclaration xml = (XmlDeclaration) doc.getFirstChild(); assertEquals(0, xml.getAttributesCount()); Element peopleList = (Element) doc.getChild(1); assertEquals(1, peopleList.getChildNodesCount()); Element person = peopleList.getFirstChildElement(); assertEquals(3, person.getChildNodesCount()); Element name = (Element) person.getChild(0); assertEquals("Fred Bloggs", name.getTextContent()); assertEquals("Male", person.getChild(2).getTextContent()); xmlContent = StringUtil.removeChars(xmlContent, "\n\r\t"); assertEquals(xmlContent, doc.getHtml()); assertTrue(doc.check()); }
@Test void testNamesAndChilds() { Document document = new LagartoDOMBuilder().parse("<div id='top'><p id='id1'>one</p><span id='t1'>text1</span><p id='id2'>two</p><span id='t2'>text2</span><p id='id3'>three</p><span id='t3'>text3</span></div>"); Element topDiv = (Element) document.getFirstChild(); Element p1 = (Element) topDiv.getFirstChild(); assertEquals("id1", p1.getAttribute("id")); Element s1 = (Element) p1.getNextSibling(); assertEquals("t1", s1.getAttribute("id")); Element p2 = (Element) s1.getNextSiblingElement(); assertEquals("id2", p2.getAttribute("id")); Element p3 = (Element) p2.getNextSiblingElement().getNextSiblingElement(); assertEquals("id3", p3.getAttribute("id")); Element s3 = (Element) p3.getNextSibling(); assertEquals("t3", s3.getAttribute("id")); assertEquals(p2, p1.getNextSiblingName()); assertEquals(s3, s1.getNextSiblingName().getNextSiblingName()); assertEquals(p2, p3.getPreviousSiblingName()); assertEquals(s1, s3.getPreviousSiblingName().getPreviousSiblingName()); assertEquals(p1, topDiv.getFirstChild()); assertEquals(p1, topDiv.getFirstChildElement()); assertEquals(p1, topDiv.getFirstChildElement("p")); assertEquals(s1, topDiv.getFirstChildElement("span")); assertEquals(s3, topDiv.getLastChild()); assertEquals(s3, topDiv.getLastChildElement()); assertEquals(p3, topDiv.getLastChildElement("p")); assertEquals(s3, topDiv.getLastChildElement("span")); assertTrue(document.check()); }
@Test void testSiblingsAndNames() { Document document = new LagartoDOMBuilder().parse("<div id='top'><p id='id1'>one</p>text1<p id='id2'>two</p>text2<p id='id3'>three</p>text3</div>"); Element topDiv = (Element) document.getFirstChild(); Element p1 = (Element) topDiv.getFirstChild(); assertEquals("id1", p1.getAttribute("id"));
assertEquals(1, root.getChildNodesCount()); Element html = (Element) root.getFirstChild(); assertEquals(Node.NodeType.ELEMENT, html.getNodeType()); assertNotNull(html);
/** * Replace each element in the set of matched elements with the provided * new content and return the set of elements that was removed. */ public Jerry replaceWith(String html) { if (html == null) { html = StringPool.EMPTY; } final Document doc = builder.parse(html); if (nodes.length == 0) { return this; } for (Node node : nodes) { Node parent = node.getParentNode(); // if a node already is the root element, don't unwrap if (parent == null) { continue; } // replace, if possible Document workingDoc = doc.clone(); int index = node.getSiblingIndex(); parent.insertChild(workingDoc.getFirstChild(), index); node.detachFromParent(); } return this; }
/** * Wraps an HTML structure around each element in the set of matched elements. * Returns the original set of elements for chaining purposes. */ public Jerry wrap(String html) { final Document doc = builder.parse(html); for (Node node : nodes) { Document workingDoc = doc.clone(); Node inmostNode = workingDoc; while (inmostNode.hasChildNodes()) { inmostNode = inmostNode.getFirstChild(); } // replace Node parent = node.getParentNode(); int index = node.getSiblingIndex(); inmostNode.addChild(node); parent.insertChild(workingDoc.getFirstChild(), index); } return this; }
/** * Wraps an HTML structure around each element in the set of matched elements. * Returns the original set of elements for chaining purposes. */ public Jerry wrap(String html) { final Document doc = builder.parse(html); for (Node node : nodes) { Document workingDoc = doc.clone(); Node inmostNode = workingDoc; while (inmostNode.hasChildNodes()) { inmostNode = inmostNode.getFirstChild(); } // replace Node parent = node.getParentNode(); int index = node.getSiblingIndex(); inmostNode.addChild(node); parent.insertChild(workingDoc.getFirstChild(), index); } return this; }
/** * Wraps an HTML structure around each element in the set of matched elements. * Returns the original set of elements for chaining purposes. */ public Jerry wrap(String html) { if (html == null) { html = StringPool.EMPTY; } final Document doc = builder.parse(html); if (nodes.length == 0) { return this; } for (Node node : nodes) { Document workingDoc = doc.clone(); Node inmostNode = workingDoc; while (inmostNode.hasChildNodes()) { inmostNode = inmostNode.getFirstChild(); } // replace Node parent = node.getParentNode(); int index = node.getSiblingIndex(); inmostNode.addChild(node); parent.insertChild(workingDoc.getFirstChild(), index); } return this; }