From: <asa...@us...> - 2013-11-12 10:56:34
|
Revision: 8769 http://sourceforge.net/p/htmlunit/code/8769 Author: asashour Date: 2013-11-12 10:56:31 +0000 (Tue, 12 Nov 2013) Log Message: ----------- JavaScript: element.setAttributeNode() applies to XML as well. Issue 1553 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/ElementTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-11-11 21:05:16 UTC (rev 8768) +++ trunk/htmlunit/src/changes/changes.xml 2013-11-12 10:56:31 UTC (rev 8769) @@ -8,6 +8,9 @@ <body> <release version="2.14" date="???" description="Bugfixes"> + <action type="fix" dev="asashour" issue="1553"> + JavaScript: element.setAttributeNode() applies to XML as well. + </action> <action type="fix" dev="rbri"> JavaScript: HTMLElement.insertAdjacentHTML() is available in FF since version 8. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java 2013-11-11 21:05:16 UTC (rev 8768) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java 2013-11-12 10:56:31 UTC (rev 8769) @@ -16,6 +16,7 @@ import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.GENERATED_37; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_GET_ATTRIBUTE_SUPPORTS_FLAGS_IN_QUIRKS_MODE; +import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_SET_ATTRIBUTE_CONSIDERS_ATTR_FOR_CLASS_AS_REAL; import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.CHROME; import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.FF; import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.IE; @@ -556,4 +557,33 @@ public String getDefaultStyleDisplay() { return "block"; } + + /** + * Sets the attribute node for the specified attribute. + * @param newAtt the attribute to set + * @return the replaced attribute node, if any + */ + @JsxFunction + public Attr setAttributeNode(final Attr newAtt) { + final String name = newAtt.getName(); + + final Attr replacedAtt; + final boolean undefForClass = getBrowserVersion(). + hasFeature(JS_SET_ATTRIBUTE_CONSIDERS_ATTR_FOR_CLASS_AS_REAL); + if (undefForClass) { + replacedAtt = (Attr) getAttributeNode(name); + } + else { + final NamedNodeMap nodes = (NamedNodeMap) getAttributes(); + replacedAtt = (Attr) nodes.getNamedItemWithoutSytheticClassAttr(name); + } + if (replacedAtt != null) { + replacedAtt.detachFromParent(); + } + + final DomAttr newDomAttr = newAtt.getDomNodeOrDie(); + getDomNodeOrDie().setAttributeNode(newDomAttr); + return replacedAtt; + } + } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java 2013-11-11 21:05:16 UTC (rev 8768) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java 2013-11-12 10:56:31 UTC (rev 8769) @@ -42,7 +42,6 @@ import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_OUTER_HTML_BODY_HEAD_READONLY; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_OUTER_THROW_EXCEPTION_WHEN_CLOSES; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_PREFIX_RETURNS_EMPTY_WHEN_UNDEFINED; -import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_SET_ATTRIBUTE_CONSIDERS_ATTR_FOR_CLASS_AS_REAL; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_SET_ATTRIBUTE_SUPPORTS_EVENT_HANDLERS; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WIDTH_HEIGHT_ACCEPTS_ARBITRARY_VALUES; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.QUERYSELECTORALL_NOT_IN_QUIRKS; @@ -774,34 +773,6 @@ } /** - * Sets the attribute node for the specified attribute. - * @param newAtt the attribute to set - * @return the replaced attribute node, if any - */ - @JsxFunction - public Attr setAttributeNode(final Attr newAtt) { - final String name = newAtt.getName(); - - final Attr replacedAtt; - final boolean undefForClass = getBrowserVersion(). - hasFeature(JS_SET_ATTRIBUTE_CONSIDERS_ATTR_FOR_CLASS_AS_REAL); - if (undefForClass) { - replacedAtt = (Attr) getAttributeNode(name); - } - else { - final NamedNodeMap nodes = (NamedNodeMap) getAttributes(); - replacedAtt = (Attr) nodes.getNamedItemWithoutSytheticClassAttr(name); - } - if (replacedAtt != null) { - replacedAtt.detachFromParent(); - } - - final DomAttr newDomAttr = newAtt.getDomNodeOrDie(); - getDomNodeOrDie().setAttributeNode(newDomAttr); - return replacedAtt; - } - - /** * Returns all the descendant elements with the specified class. * @param className the name to search for * @return all the descendant elements with the specified class name Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/ElementTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/ElementTest.java 2013-11-11 21:05:16 UTC (rev 8768) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/ElementTest.java 2013-11-12 10:56:31 UTC (rev 8769) @@ -1076,4 +1076,32 @@ getMockWebConnection().setDefaultResponse(xml, "text/xml"); loadPageWithAlerts2(html); } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("test") + public void setAttributeNode() throws Exception { + final String html = "<html>\n" + + " <head>\n" + + " <script>\n" + + " function test() {\n" + + " var doc = " + XMLDocumentTest.callCreateXMLDocument() + ";\n" + + " var element = doc.createElement('something');\n" + + " var attr = doc.createAttribute('name');\n" + + " attr.value = 'test';\n" + + " element.setAttributeNode(attr);\n" + + " alert(element.getAttributeNode('name').value)\n" + + " }\n" + + XMLDocumentTest.CREATE_XML_DOCUMENT_FUNCTION + + " </script>\n" + + " </head>\n" + + " <body onload='test()'>\n" + + " </body>\n" + + "</html>"; + + loadPageWithAlerts2(html); + } + } |