TextNode extends Node { public TextNode splitText(int offset) { Validate.isTrue(offset >= 0, "Split offset must be not be negative"); Validate.isTrue(offset < text.length(), "Split offset must not be greater than current text length"); String head = getWholeText().substring(0, offset); String tail = getWholeText().substring(offset); text(head); TextNode tailNode = new TextNode(tail, this.baseUri()); if (parent() != null) parent().addChildren(siblingIndex()+1, tailNode); return tailNode; } TextNode(String text, String baseUri); String nodeName(); String text(); TextNode text(String text); String getWholeText(); boolean isBlank(); TextNode splitText(int offset); @Override String toString(); static TextNode createFromEncoded(String encodedText, String baseUri); @Override String attr(String attributeKey); @Override Attributes attributes(); @Override Node attr(String attributeKey, String attributeValue); @Override boolean hasAttr(String attributeKey); @Override Node removeAttr(String attributeKey); @Override String absUrl(String attributeKey);  }
@Test public void testSplitText() { Document doc = Jsoup.parse("<div>Hello there</div>"); Element div = doc.select("div").first(); TextNode tn = (TextNode) div.childNode(0); TextNode tail = tn.splitText(6); assertEquals("Hello ", tn.getWholeText()); assertEquals("there", tail.getWholeText()); tail.text("there!"); assertEquals("Hello there!", div.text()); assertTrue(tn.parent() == tail.parent()); }
@Test public void testSplitAnEmbolden() { Document doc = Jsoup.parse("<div>Hello there</div>"); Element div = doc.select("div").first(); TextNode tn = (TextNode) div.childNode(0); TextNode tail = tn.splitText(6); tail.wrap("<b></b>"); assertEquals("Hello <b>there</b>", TextUtil.stripNewlines(div.html())); }