@Test public void testNormalizeTextNodes() { Document document = new Document(""); Element element = document.appendElement("root"); element.appendText("first "); element.appendText("second,"); element.appendText(" third"); element.appendElement("break"); element.appendText("fourth"); assertEquals(5, element.childNodes().size()); TestDocumentProcessor.normalizeTextNodes(element); assertEquals(3, element.childNodes().size()); assertTrue(element.childNode(0) instanceof TextNode); assertEquals("first second, third", ((TextNode) element.childNode(0)).text()); assertTrue(element.childNode(2) instanceof TextNode); assertEquals("fourth", ((TextNode) element.childNode(2)).text()); }
protected static void normalizeTextNodes(Element parentElement) { List<Node> children = parentElement.childNodes(); if (!children.isEmpty()) { children = new ArrayList<Node>(children); for (Node child : children) { if (child instanceof TextNode) { TextNode childTextNode = (TextNode) child; Node previousSibling = child.previousSibling(); if (previousSibling instanceof TextNode) { TextNode previousSiblingTextNode = (TextNode) previousSibling; childTextNode.text(previousSiblingTextNode.text() + childTextNode.text()); previousSibling.remove(); } String wholeText = childTextNode.getWholeText(); if (!Html.isWhitespacePreserve(parentElement)) { wholeText = StringUtil.normaliseWhitespace(wholeText); } childTextNode.text(wholeText); } } } }
DocumentProcessor { protected static void normalizeTextNodes(Element parentElement) { List<Node> children = parentElement.childNodes(); if (!children.isEmpty()) { children = new ArrayList<Node>(children); for (Node child : children) { if (child instanceof TextNode) { TextNode childTextNode = (TextNode) child; Node previousSibling = child.previousSibling(); if (previousSibling instanceof TextNode) { TextNode previousSiblingTextNode = (TextNode) previousSibling; childTextNode.text(previousSiblingTextNode.text() + childTextNode.text()); previousSibling.remove(); } String wholeText = childTextNode.getWholeText(); if (!Html.isWhitespacePreserve(parentElement)) { wholeText = StringUtil.normaliseWhitespace(wholeText); } childTextNode.text(wholeText); } } } } }
DocumentProcessor { protected static void normalizeTextNodes(Element parentElement) { List<Node> children = parentElement.childNodes(); if (!children.isEmpty()) { children = new ArrayList<Node>(children); for (Node child : children) { if (child instanceof TextNode) { TextNode childTextNode = (TextNode) child; Node previousSibling = child.previousSibling(); if (previousSibling instanceof TextNode) { TextNode previousSiblingTextNode = (TextNode) previousSibling; childTextNode.text(previousSiblingTextNode.text() + childTextNode.text()); previousSibling.remove(); } String wholeText = childTextNode.getWholeText(); if (!Html.isWhitespacePreserve(parentElement)) { wholeText = StringUtil.normaliseWhitespace(wholeText); } childTextNode.text(wholeText); } } } }  }
DocumentProcessor { protected static void normalizeTextNodes(Element parentElement) { List<Node> children = parentElement.childNodes(); if (!children.isEmpty()) { children = new ArrayList<Node>(children); for (Node child : children) { if (child instanceof TextNode) { TextNode childTextNode = (TextNode) child; Node previousSibling = child.previousSibling(); if (previousSibling instanceof TextNode) { TextNode previousSiblingTextNode = (TextNode) previousSibling; childTextNode.text(previousSiblingTextNode.text() + childTextNode.text()); previousSibling.remove(); } String wholeText = childTextNode.getWholeText(); if (!Html.isWhitespacePreserve(parentElement)) { wholeText = StringUtil.normaliseWhitespace(wholeText); } childTextNode.text(wholeText); } } } }  abstract void process(Document document); }
DocumentProcessor { protected static void normalizeTextNodes(Element parentElement) { List<Node> children = parentElement.childNodes(); if (!children.isEmpty()) { children = new ArrayList<Node>(children); for (Node child : children) { if (child instanceof TextNode) { TextNode childTextNode = (TextNode) child; Node previousSibling = child.previousSibling(); if (previousSibling instanceof TextNode) { TextNode previousSiblingTextNode = (TextNode) previousSibling; childTextNode.text(previousSiblingTextNode.text() + childTextNode.text()); previousSibling.remove(); } String wholeText = childTextNode.getWholeText(); if (!Html.isWhitespacePreserve(parentElement)) { wholeText = StringUtil.normaliseWhitespace(wholeText); } childTextNode.text(wholeText); } } } }  abstract void process(Document document);  }