diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/DOMNode.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/DOMNode.java index 40aeca47b..4bec436c1 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/DOMNode.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/DOMNode.java @@ -827,7 +827,39 @@ public DOMElement getOrphanEndElement(int offset, String tagName, boolean anyOrp */ @Override public String getTextContent() throws DOMException { - return getNodeValue(); + + switch (getNodeType()) { + // Text like nodes simply return their node value + case Node.TEXT_NODE: + case Node.CDATA_SECTION_NODE: + case Node.COMMENT_NODE: + case Node.PROCESSING_INSTRUCTION_NODE: + return getNodeValue(); + // These special types has to return null + case Node.DOCUMENT_NODE: + case Node.DOCUMENT_TYPE_NODE: + case Node.NOTATION_NODE: + return null; + // concatenation of the textContent attribute value of every child node + default: + if (this.children != null && children.size() > 0) { + final StringBuilder builder = new StringBuilder(); + for (DOMNode child : children) { + short nodeType = child.getNodeType(); + if (nodeType == Node.COMMENT_NODE || nodeType == Node.PROCESSING_INSTRUCTION_NODE) { + // excluding COMMENT_NODE and PROCESSING_INSTRUCTION_NODE nodes. + continue; + } + String text = child.getTextContent(); + if (text != null && !text.isEmpty()) { + builder.append(text); + } + } + return builder.toString(); + } + // empty string if the node has no children + return ""; + } } @Override diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/dom/DOMParserTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/dom/DOMParserTest.java index 497fbc220..efdb4d1e3 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/dom/DOMParserTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/dom/DOMParserTest.java @@ -45,6 +45,14 @@ public void testNestedElement() { assertDocument("
", html); } + @Test + public void testGetText() { + DOMDocument document = DOMParser.getInstance().parse("