From: <mgu...@us...> - 2013-07-11 08:59:55
|
Revision: 8388 http://sourceforge.net/p/htmlunit/code/8388 Author: mguillem Date: 2013-07-11 08:59:48 +0000 (Thu, 11 Jul 2013) Log Message: ----------- JavaScript: add support for XPathEvaluator (FF & Chrome). Issue 1516 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.java Added Paths: ----------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluator.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluatorTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-07-10 17:54:18 UTC (rev 8387) +++ trunk/htmlunit/src/changes/changes.xml 2013-07-11 08:59:48 UTC (rev 8388) @@ -8,6 +8,9 @@ <body> <release version="2.13" date="???" description="Bugfixes"> + <action type="update" dev="mguillem" issue="1516"> + JavaScript: add support for XPathEvaluator (FF & Chrome). + </action> <action type="fix" dev="rbri"> The display property now returns the correct default value for all html tags. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.java 2013-07-10 17:54:18 UTC (rev 8387) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.java 2013-07-11 08:59:48 UTC (rev 8388) @@ -82,6 +82,7 @@ import com.gargoylesoftware.htmlunit.javascript.host.UIEvent; import com.gargoylesoftware.htmlunit.javascript.host.WebSocket; import com.gargoylesoftware.htmlunit.javascript.host.Window; +import com.gargoylesoftware.htmlunit.javascript.host.XPathEvaluator; import com.gargoylesoftware.htmlunit.javascript.host.XPathNSResolver; import com.gargoylesoftware.htmlunit.javascript.host.XPathResult; import com.gargoylesoftware.htmlunit.javascript.host.XSLTProcessor; @@ -338,8 +339,8 @@ StaticNodeList.class, Storage.class, StyleSheetList.class, Text.class, TextRange.class, TreeWalker.class, UIEvent.class, Uint16Array.class, Uint32Array.class, Uint8Array.class, Uint8ClampedArray.class, WebSocket.class, Window.class, XMLAttr.class, XMLDocument.class, XMLDOMParseError.class, - XMLHttpRequest.class, XMLSerializer.class, XPathNSResolver.class, XPathResult.class, XSLTProcessor.class, - XSLTemplate.class}; + XMLHttpRequest.class, XMLSerializer.class, XPathEvaluator.class, XPathNSResolver.class, XPathResult.class, + XSLTProcessor.class, XSLTemplate.class}; /** Cache of browser versions and their corresponding JavaScript configurations. */ private static final Map<BrowserVersion, JavaScriptConfiguration> ConfigurationMap_ = Added: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluator.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluator.java (rev 0) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluator.java 2013-07-11 08:59:48 UTC (rev 8388) @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2002-2013 Gargoyle Software Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gargoylesoftware.htmlunit.javascript.host; + +import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.FF; + +import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; +import com.gargoylesoftware.htmlunit.javascript.configuration.WebBrowser; + +/** + * A JavaScript object for XPathEvaluator. + * + * @version $Revision$ + * @author Marc Guillemot + */ +@JsxClass(browsers = @WebBrowser(value = FF, minVersion = 17)) +public class XPathEvaluator extends SimpleScriptable { + + /** + * JavaScript constructor. + */ + @JsxConstructor + public void jsConstructor() { + // Empty + } + + /** + * Adapts any DOM node to resolve namespaces so that an XPath expression can be easily + * evaluated relative to the context of the node where it appeared within the document. + * @param nodeResolver the node to be used as a context for namespace resolution + * @return an XPathNSResolver which resolves namespaces with respect to the definitions + * in scope for a specified node + */ + @JsxFunction(@WebBrowser(FF)) + public XPathNSResolver createNSResolver(final Node nodeResolver) { + final XPathNSResolver resolver = new XPathNSResolver(); + resolver.setElement(nodeResolver); + resolver.setParentScope(getWindow()); + resolver.setPrototype(getPrototype(resolver.getClass())); + return resolver; + } + + /** + * Evaluates an XPath expression string and returns a result of the specified type if possible. + * @param expression the XPath expression string to be parsed and evaluated + * @param contextNode the context node for the evaluation of this XPath expression + * @param resolver the resolver permits translation of all prefixes, including the XML namespace prefix, + * within the XPath expression into appropriate namespace URIs. + * @param type If a specific type is specified, then the result will be returned as the corresponding type + * @param result the result object which may be reused and returned by this method + * @return the result of the evaluation of the XPath expression + */ + @JsxFunction(@WebBrowser(FF)) + public XPathResult evaluate(final String expression, final Node contextNode, + final Object resolver, final int type, final Object result) { + XPathResult xPathResult = (XPathResult) result; + if (xPathResult == null) { + xPathResult = new XPathResult(); + xPathResult.setParentScope(getParentScope()); + xPathResult.setPrototype(getPrototype(xPathResult.getClass())); + } + xPathResult.init(contextNode.getDomNodeOrDie().getByXPath(expression), type); + return xPathResult; + } +} Property changes on: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluator.java ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Author Date Id Revision \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluatorTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluatorTest.java (rev 0) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluatorTest.java 2013-07-11 08:59:48 UTC (rev 8388) @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2002-2013 Gargoyle Software Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gargoylesoftware.htmlunit.javascript.host; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.gargoylesoftware.htmlunit.BrowserRunner; +import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; +import com.gargoylesoftware.htmlunit.WebDriverTestCase; + +/** + * Tests for {@link XPathEvaluator}. + * + * @version $Revision$ + * @author Marc Guillemot + */ +@RunWith(BrowserRunner.class) +public class XPathEvaluatorTest extends WebDriverTestCase { + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = { "function", "[object XPathEvaluator]", "[object XPathNSResolver]", "first", "second" }, + IE = { "undefined", "exception" }, FF3_6 = { "undefined", "exception" }) + public void simple() throws Exception { + final String html = "<html><body>\n" + + "<span id='first'>hello</span>\n" + + "<div><span id='second'>world</span></div>\n" + + "<script>\n" + + "try {\n" + + " alert(typeof window.XPathEvaluator);\n" + + " var xpe = new XPathEvaluator();\n" + + " alert(xpe);\n" + + " var nsResolver = xpe.createNSResolver(document.documentElement);\n" + + " alert(nsResolver);\n" + + " var result = xpe.evaluate('//span', document.body, nsResolver, 0, null);\n" + + " var found = [];\n" + + " var res;\n" + + " while (res = result.iterateNext()) {\n" + + " alert(res.id);\n" + + " }\n" + + "} catch(e) { alert('exception'); }\n" + + "</script></body></html>"; + + loadPageWithAlerts2(html); + } +} Property changes on: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XPathEvaluatorTest.java ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Author Date Id Revision \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property |