From: SourceForge.net <no...@so...> - 2010-10-27 17:59:13
|
Bugs item #3071265, was opened at 2010-09-19 14:16 Message generated for change (Comment added) made by martinkurz You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=113153&aid=3071265&group_id=13153 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: Martin (martinkurz) Assigned to: Adrian Sandor (aditsu) Summary: NPE on appendChild with attribute node Initial Comment: for problem description see http://sourceforge.net/projects/jtidy/forums/forum/41437/topic/3787602 analysis: on appending a new child, the new child nodes type is checked on field adaptee.type, in attribute nodes, this is null (the field is called avAadaptee instead) solution: first check, if new childnode is an attribute node before checking adaptee.type, patch for java5 branch including testcase attached ---------------------------------------------------------------------- >Comment By: Martin (martinkurz) Date: 2010-10-27 17:59 Message: attached new patch to throw DOMException.HIERARCHY_REQUEST_ERR when newCh.adaptee is null ---------------------------------------------------------------------- Comment By: Martin (martinkurz) Date: 2010-10-27 16:48 Message: after looking at http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-184E7107, maybe it shouldn't be thrown a NPE but an HIERARCHY_REQUEST_ERR (Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to append is one of this node's ancestors)? ---------------------------------------------------------------------- Comment By: Martin (martinkurz) Date: 2010-10-27 16:45 Message: You're right, attributes are nodes too, but an attribute node isn't a node's childNode, the nodes childNodes doesn't contain the nodes attributes, so it would probably be a strange behaviour to a node, if it would accept an attribute childNode. I was just misleaded by the fact that attributes are nodes :o) ---------------------------------------------------------------------- Comment By: Adrian Sandor (aditsu) Date: 2010-10-27 03:52 Message: I don't think you're supposed to be able to use appendChild with an attribute node. See the program below for a demonstration (including how it should be done instead): import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; public class TestAppendAttr { public static void main(final String... args) throws Exception { final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); final Element html = doc.createElement("html"); doc.appendChild(html); final Element body = doc.createElement("body"); html.appendChild(body); final Element a = doc.createElement("a"); body.appendChild(a); a.setAttribute("href", "foo"); // correct final Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(doc), new StreamResult(System.out)); // just to see the result final Attr target = doc.createAttribute("target"); target.setValue("_blank"); a.appendChild(target); // DOMException } } Let me know if I missed anything. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=113153&aid=3071265&group_id=13153 |