From c7080b1e5269e99df006e79c2026f565f81ce3c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 10 Nov 2024 18:09:04 +0100 Subject: [PATCH] Implement DOMNode.getTextContent() according to API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix https://github.com/eclipse-lemminx/lemminx/issues/1695 Signed-off-by: Christoph Läubrich --- .../java/org/eclipse/lemminx/dom/DOMNode.java | 34 ++++++++++++++++++- .../eclipse/lemminx/dom/DOMParserTest.java | 8 +++++ 2 files changed, 41 insertions(+), 1 deletion(-) 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("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);