Skip to content

Commit

Permalink
Implement DOMNode.getTextContent() according to API
Browse files Browse the repository at this point in the history
Fix #1695

Signed-off-by: Christoph Läubrich <[email protected]>
  • Loading branch information
laeubi committed Nov 12, 2024
1 parent 0aadd16 commit 7ebf6a1
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,65 @@ public void testNestedElement() {
assertDocument("<html><body></body></html>", html);
}

@Test
public void testGetTextContentWithSimpleContent() {
DOMDocument document = DOMParser.getInstance().parse("<a><b><c>Hello</c></b></a>", "uri", null);
String textContent = document.getDocumentElement().getTextContent();
assertNotNull(textContent);
assertEquals("Hello", textContent);
}

@Test
public void testGetTextContentWithMixedContent() {
DOMDocument document = DOMParser.getInstance().parse("<a>H<b>e<c>ll</c></b>o</a>", "uri", null);
String textContent = document.getDocumentElement().getTextContent();
assertNotNull(textContent);
assertEquals("Hello", textContent);
}

@Test
public void testGetTextContentWithComplexContent() {
DOMDocument document = DOMParser.getInstance().parse("<a><b>H</b><c>e</c><b>ll</b><x>o</x></b></a>", "uri",
null);
String textContent = document.getDocumentElement().getTextContent();
assertNotNull(textContent);
assertEquals("Hello", textContent);
}

@Test
public void testGetTextContentWithCharContent() {
DOMDocument document = DOMParser.getInstance().parse("<text>Hello</text>", "uri", null);
String textContent = document.getDocumentElement().getChild(0).getTextContent();
assertNotNull(textContent);
assertEquals("Hello", textContent);
}

@Test
public void testGetTextContentWithCDATAContent() {
DOMDocument document = DOMParser.getInstance().parse("<a><b><c><![CDATA[Hello]]></c></b></a>", "uri", null);
String textContent = document.getDocumentElement().getTextContent();
assertNotNull(textContent);
assertEquals("Hello", textContent);
}

@Test
public void testGetTextContentWithComment() {
DOMDocument document = DOMParser.getInstance()
.parse("<a><b><c>Hello</c><!-- comments must not be included --></b></a>", "uri", null);
String textContent = document.getDocumentElement().getTextContent();
assertNotNull(textContent);
assertEquals("Hello", textContent);
}

@Test
public void testGetTextContentWithPI() {
DOMDocument document = DOMParser.getInstance().parse("<a><b><c>Hello</c><?PI must not be included ?></b></a>",
"uri", null);
String textContent = document.getDocumentElement().getTextContent();
assertNotNull(textContent);
assertEquals("Hello", textContent);
}

@Test
public void testNestedElements() {
DOMNode head = createElement("head", 6, 12, 19, true);
Expand All @@ -56,6 +115,7 @@ public void testNestedElements() {
assertDocument("<html><head></head><body></body></html>", html);
}


@Test
public void testNestedNestedElements() {
DOMNode c = createElement("c", 6, 9, 13, true);
Expand Down Expand Up @@ -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>", meta);
assertFalse(meta.hasStartTag());
assertTrue(meta.hasEndTag());
Expand All @@ -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></meta></html>", html);
Expand Down

0 comments on commit 7ebf6a1

Please sign in to comment.