From: <mgu...@us...> - 2013-02-01 14:00:31
|
Revision: 8073 http://sourceforge.net/p/htmlunit/code/8073 Author: mguillem Date: 2013-02-01 14:00:27 +0000 (Fri, 01 Feb 2013) Log Message: ----------- JavaScript: move method contains to Node, throw runtime exception on invalid argument. Issue 1480 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Node.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElementTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-02-01 13:36:15 UTC (rev 8072) +++ trunk/htmlunit/src/changes/changes.xml 2013-02-01 14:00:27 UTC (rev 8073) @@ -8,6 +8,9 @@ <body> <release version="2.12" date="???" description="Bugfixes, CSS3 Selectors"> + <action type="fix" dev="mguillem" issue="1480"> + JavaScript: move method contains to Node, throw runtime exception on invalid argument. + </action> <action type="fix" dev="rbri"> Method innerHTML does not encode entities for style tags. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Node.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Node.java 2013-02-01 13:36:15 UTC (rev 8072) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Node.java 2013-02-01 14:00:27 UTC (rev 8073) @@ -1023,4 +1023,21 @@ return null; } + /** + * Checks whether the given element is contained within this object. + * @param element element object that specifies the element to check + * @return true if the element is contained within this object + */ + @JsxFunction({ @WebBrowser(IE), @WebBrowser(value = FF, minVersion = 10), @WebBrowser(CHROME) }) + public boolean contains(final Object element) { + if (!(element instanceof Node)) { + throw Context.reportRuntimeError("Could not convert JavaScript argument arg 0"); + } + for (Node parent = (Node) element; parent != null; parent = parent.getParentElement()) { + if (this == parent) { + return true; + } + } + return false; + } } 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-02-01 13:36:15 UTC (rev 8072) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java 2013-02-01 14:00:27 UTC (rev 8073) @@ -853,7 +853,7 @@ final StringBuilder buf = new StringBuilder(); final String tagName = getTagName(); - boolean isPlain = "SCRIPT".equals(tagName) || "STYLE".equals(tagName); + final boolean isPlain = "SCRIPT".equals(tagName) || "STYLE".equals(tagName); // we can't rely on DomNode.asXml because it adds indentation and new lines printChildren(buf, getDomNodeOrDie(), !isPlain); @@ -1964,21 +1964,6 @@ } /** - * Checks whether the given element is contained within this object. - * @param element element object that specifies the element to check - * @return true if the element is contained within this object - */ - @JsxFunction({ @WebBrowser(IE), @WebBrowser(value = FF, minVersion = 10), @WebBrowser(CHROME) }) - public boolean contains(final HTMLElement element) { - for (HTMLElement parent = element; parent != null; parent = (HTMLElement) parent.getParentElement()) { - if (this == parent) { - return true; - } - } - return false; - } - - /** * Sets the focus to this element. */ @JsxFunction Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElementTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElementTest.java 2013-02-01 13:36:15 UTC (rev 8072) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElementTest.java 2013-02-01 14:00:27 UTC (rev 8073) @@ -2177,7 +2177,7 @@ */ @Test @Alerts(FF3_6 = "exception", - DEFAULT = { "true", "true", "true", "false", "false", "false", "false", "true" }) + DEFAULT = { "true", "true", "true", "false", "false", "false", "false", "true", "true", "false", "false" }) public void contains() throws Exception { final String html = "<html><head>\n" @@ -2186,6 +2186,7 @@ + "try {\n" + " var div1 = document.getElementById('div1');\n" + " var div2 = document.getElementById('div2');\n" + + " var text = div2.firstChild;\n" + " var div3 = document.getElementById('div3');\n" + " alert(div1.contains(div1));\n" + " alert(div1.contains(div2));\n" @@ -2195,11 +2196,14 @@ + " alert(div3.contains(div1));\n" + " alert(div4.contains(div1));\n" + " alert(div2.contains(div3));\n" + + " alert(div2.contains(text));\n" + + " alert(div3.contains(text));\n" + + " alert(text.contains(div3));\n" + "} catch(e) { alert('exception'); }\n" + "}\n" + "</script></head><body onload='test()'>\n" + "<div id='div1'>\n" - + " <div id='div2'>\n" + + " <div id='div2'>hello\n" + " <div id='div3'>\n" + " </div>\n" + " </div>\n" @@ -2215,6 +2219,21 @@ * @throws Exception if the test fails */ @Test + @Alerts("exception") + public void contains_invalid_argument() throws Exception { + final String html = "<html><body><script>\n" + + "try {\n" + + " alert(document.body.contains([]));\n" + + "} catch(e) { alert('exception'); }\n" + + "</script></body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test @Alerts(DEFAULT = "undefined", IE = "defined") public void filters() throws Exception { final String html |