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..b52523c44 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,65 @@ public void testNestedElement() { assertDocument("", html); } + @Test + public void testGetTextContentWithSimpleContent() { + DOMDocument document = DOMParser.getInstance().parse("Hello", "uri", null); + String textContent = document.getDocumentElement().getTextContent(); + assertNotNull(textContent); + assertEquals("Hello", textContent); + } + + @Test + public void testGetTextContentWithMixedContent() { + DOMDocument document = DOMParser.getInstance().parse("Hello", "uri", null); + String textContent = document.getDocumentElement().getTextContent(); + assertNotNull(textContent); + assertEquals("Hello", textContent); + } + + @Test + public void testGetTextContentWithComplexContent() { + DOMDocument document = DOMParser.getInstance().parse("Hello", "uri", + null); + String textContent = document.getDocumentElement().getTextContent(); + assertNotNull(textContent); + assertEquals("Hello", textContent); + } + + @Test + public void testGetTextContentWithCharContent() { + DOMDocument document = DOMParser.getInstance().parse("Hello", "uri", null); + String textContent = document.getDocumentElement().getChild(0).getTextContent(); + assertNotNull(textContent); + assertEquals("Hello", textContent); + } + + @Test + public void testGetTextContentWithCDATAContent() { + DOMDocument document = DOMParser.getInstance().parse("", "uri", null); + String textContent = document.getDocumentElement().getTextContent(); + assertNotNull(textContent); + assertEquals("Hello", textContent); + } + + @Test + public void testGetTextContentWithComment() { + DOMDocument document = DOMParser.getInstance() + .parse("Hello", "uri", null); + String textContent = document.getDocumentElement().getTextContent(); + assertNotNull(textContent); + assertEquals("Hello", textContent); + } + + @Test + public void testGetTextContentWithPI() { + DOMDocument document = DOMParser.getInstance().parse("Hello", + "uri", null); + String textContent = document.getDocumentElement().getTextContent(); + assertNotNull(textContent); + assertEquals("Hello", textContent); + } + @Test public void testNestedElements() { DOMNode head = createElement("head", 6, 12, 19, true); @@ -56,6 +115,7 @@ public void testNestedElements() { assertDocument("", html); } + @Test public void testNestedNestedElements() { DOMNode c = createElement("c", 6, 9, 13, true); @@ -95,7 +155,7 @@ public void testEmptyTagT() { @Test public void singleEndTag() { - DOMElement meta = (DOMElement) createElement("meta", 0, 0, 7, false); + DOMElement meta = createElement("meta", 0, 0, 7, false); assertDocument("", meta); assertFalse(meta.hasStartTag()); assertTrue(meta.hasEndTag()); @@ -104,8 +164,8 @@ public void singleEndTag() { @Test public void insideEndTag() { - DOMElement meta = (DOMElement) createElement("meta", 6, 6, 13, false); - DOMElement html = (DOMElement) createElement("html", 0, 13, 20, true); + DOMElement meta = createElement("meta", 6, 6, 13, false); + DOMElement html = createElement("html", 0, 13, 20, true); html.addChild(meta); assertDocument("", html);