@Test public void testPrependNewHtml() { Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>"); Element div = doc.getElementById("1"); div.prepend("<p>there</p><p>now</p>"); assertEquals("<p>there</p><p>now</p><p>Hello</p>", TextUtil.stripNewlines(div.html())); // check sibling index (reindexChildren): Elements ps = doc.select("p"); for (int i = 0; i < ps.size(); i++) { assertEquals(i, ps.get(i).siblingIndex); } }
@Test public void testPrependRowToTable() { Document doc = Jsoup.parse("<table><tr><td>1</td></tr></table>"); Element table = doc.select("tbody").first(); table.prepend("<tr><td>2</td></tr>"); assertEquals( "<table><tbody><tr><td>2</td></tr><tr><td>1</td></tr></tbody></table>", TextUtil.stripNewlines(doc.body().html())); // check sibling index (reindexChildren): Elements ps = doc.select("tr"); for (int i = 0; i < ps.size(); i++) { assertEquals(i, ps.get(i).siblingIndex); } }
@Override protected String parseContent(Element prop) { org.jsoup.nodes.Document d = org.jsoup.Jsoup.parse(prop.text()); d.select("script").remove(); for (Element ytv : d.select(".youtube-player")) ytv.prepend( "<iframe src=\"https://www.youtube.com/embed/" + ytv.attr("data-id") + "\" frameborder=\"0\" allowfullscreen></iframe>"); for (Element s : d.select("[style]")) s.removeAttr("style"); String content = d.html(); int index = content.lastIndexOf("<hr>"); if (index != -1) content = content.substring(0, index); return content.replace("<pre ", "<code><p ").replace("/pre>", "/p></code>"); }