Skip to content

Commit 8216672

Browse files
committed
scope handling fix (wip)
1 parent 592c4bd commit 8216672

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

src/main/java/org/htmlunit/javascript/host/dom/XPathEvaluator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.htmlunit.corejs.javascript.Function;
1919
import org.htmlunit.corejs.javascript.NativeFunction;
2020
import org.htmlunit.corejs.javascript.Scriptable;
21-
import org.htmlunit.corejs.javascript.TopLevel;
21+
import org.htmlunit.corejs.javascript.ScriptableObject;
2222
import org.htmlunit.html.DomNode;
2323
import org.htmlunit.javascript.HtmlUnitScriptable;
2424
import org.htmlunit.javascript.JavaScriptEngine;
@@ -141,7 +141,7 @@ else if (resolver instanceof NativeFunction nativeFunction) {
141141

142142
try {
143143
final String xpath = JavaScriptEngine.toString(args[0]);
144-
final Window window = (Window) ((TopLevel) scope).getGlobalThis();
144+
final Window window = (Window) (ScriptableObject.getTopLevelScope(scope)).getGlobalThis();
145145
final DomNode doc = window.getDocument().getDocumentElement().getDomNodeOrDie();
146146
final XPathExpression xPathExpression = new XPathExpression(xpath, prefixResolver, doc);
147147
xPathExpression.setParentScope(evaluator.getParentScope());

src/test/java/org/htmlunit/javascript/host/dom/XPathEvaluatorTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@
2727
*/
2828
public class XPathEvaluatorTest extends WebDriverTestCase {
2929

30+
/**
31+
* @throws Exception if the test fails
32+
*/
33+
@Test
34+
@Alerts({"function", "[object XPathEvaluator]", "[object HTMLHtmlElement]", "first", "second", ""})
35+
public void ctor() throws Exception {
36+
final String html = DOCTYPE_HTML
37+
+ "<html><body>\n"
38+
+ "<span id='first'>hello</span>\n"
39+
+ "<div><span id='second'>world</span></div>\n"
40+
+ "<script>\n"
41+
+ LOG_TITLE_FUNCTION
42+
+ "var res = '';\n"
43+
+ "try {\n"
44+
+ " res += typeof window.XPathEvaluator + '§';\n"
45+
+ " var xpe = new XPathEvaluator();\n"
46+
+ " res += xpe + '§';\n"
47+
+ " var nsResolver = xpe.createNSResolver(document.documentElement);\n"
48+
+ " res += nsResolver + '§';\n"
49+
+ " var result = xpe.evaluate('//span', document.body, nsResolver, 0, null);\n"
50+
+ " var found = [];\n"
51+
+ " var next;\n"
52+
+ " while (next = result.iterateNext()) {\n"
53+
+ " res += next.id + '§';\n"
54+
+ " }\n"
55+
+ "} catch(e) { res += 'exception' + '§'; }\n"
56+
+ "log(res);\n"
57+
+ "</script></body></html>";
58+
59+
loadPageVerifyTitle2(html);
60+
}
61+
3062
/**
3163
* @throws Exception if the test fails
3264
*/
@@ -236,6 +268,27 @@ public void namespacesWithCustomNSResolver() throws Exception {
236268
loadPageVerifyTitle2(html);
237269
}
238270

271+
/**
272+
* @throws Exception if the test fails
273+
*/
274+
@Test
275+
@Alerts("[object XPathExpression]")
276+
public void createExpression() throws Exception {
277+
final String html = DOCTYPE_HTML
278+
+ "<html><body>\n"
279+
+ "<script>\n"
280+
+ LOG_TITLE_FUNCTION
281+
+ "var res = '';\n"
282+
+ "try {\n"
283+
+ " var expression = new XPathEvaluator().createExpression('//span');\n"
284+
+ " res += expression;\n"
285+
+ "} catch(e) { res += e.name; }\n"
286+
+ "log(res);\n"
287+
+ "</script></body></html>";
288+
289+
loadPageVerifyTitle2(html);
290+
}
291+
239292
/**
240293
* @throws Exception if the test fails
241294
*/

0 commit comments

Comments
 (0)