[xml] Fix impl and tests

This commit is contained in:
Andreas Dangel 2024-01-05 17:23:42 +01:00
parent adfd3963a0
commit 120d24dae4
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
2 changed files with 12 additions and 10 deletions

View File

@ -20,6 +20,7 @@ import org.w3c.dom.Text;
import net.sourceforge.pmd.lang.document.TextDocument;
import net.sourceforge.pmd.lang.document.TextRegion;
import net.sourceforge.pmd.lang.rule.xpath.Attribute;
import net.sourceforge.pmd.lang.rule.xpath.NoAttribute;
import net.sourceforge.pmd.lang.rule.xpath.internal.CoordinateXPathFunction;
import net.sourceforge.pmd.lang.xml.ast.XmlNode;
import net.sourceforge.pmd.util.DataMap;
@ -98,6 +99,7 @@ class XmlNodeWrapper implements XmlNode {
/**
* Returns the text, if the underlying node is a text node.
*/
@NoAttribute // see getXPathAttributesIterator() for how this is exposed as XPath attribute
public String getText() {
return node instanceof Text ? ((Text) node).getData() : null;
}
@ -131,9 +133,9 @@ class XmlNodeWrapper implements XmlNode {
@Override
public Iterator<Attribute> getXPathAttributesIterator() {
// Expose Text/CDATA nodes to have an 'Image' attribute like AST Nodes
// Expose Text/CDATA nodes to have an 'Text' attribute like AST Nodes
if (node instanceof Text) {
return Collections.singletonList(new Attribute(this, "Image", ((Text) node).getData())).iterator();
return Collections.singletonList(new Attribute(this, "Text", getText())).iterator();
}
// Expose DOM Attributes

View File

@ -2,14 +2,14 @@
+- comment[]
+- rootElement[]
+- rootElement[]
+- text[@Image = "\n "]
+- text[@Text = "\n "]
+- comment[]
+- text[@Image = "\n "]
+- text[@Text = "\n "]
+- child1[@test = "1"]
| +- text[@Image = "entity: Copyright: PMD\n "]
+- text[@Image = "\n "]
| +- text[@Text = "entity: Copyright: PMD\n "]
+- text[@Text = "\n "]
+- child2[]
| +- text[@Image = "\n "]
| +- cdata-section[@Image = " cdata section "]
| +- text[@Image = "\n "]
+- text[@Image = "\n"]
| +- text[@Text = "\n "]
| +- cdata-section[@Text = " cdata section "]
| +- text[@Text = "\n "]
+- text[@Text = "\n"]