Skip to content

Commit df5df39

Browse files
committed
Document.evaluate() now throws a SyntaxError instead of an InternalError
1 parent e20c72e commit df5df39

4 files changed

Lines changed: 73 additions & 6 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
<body>
1010
<release version="4.10.0" date="February xx, 2025" description="Bugfixes">
11+
<action type="fix" dev="rbri">
12+
Document.evaluate() now throws a SyntaxError instead of an InternalError.
13+
</action>
1114
<action type="update" dev="rbri">
1215
Upgrade commons-codec to 1.18.0.
1316
</action>

src/main/java/org/htmlunit/javascript/JavaScriptEngine.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,16 @@ public static RuntimeException reportRuntimeError(final String message) {
11501150
throw Context.reportRuntimeError(message);
11511151
}
11521152

1153+
/**
1154+
* Report a runtime error using the error reporter for the current thread.
1155+
*
1156+
* @param message the error message to report
1157+
* @return EcmaError
1158+
*/
1159+
public static EcmaError syntaxError(final String message) {
1160+
return ScriptRuntime.syntaxError(message);
1161+
}
1162+
11531163
/**
11541164
* Report a runtime error using the error reporter for the current thread.
11551165
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ else if (resolver instanceof PrefixResolver) {
580580
return xPathResult;
581581
}
582582
catch (final Exception e) {
583-
throw JavaScriptEngine.reportRuntimeError("Failed to execute 'evaluate': " + e.getMessage());
583+
throw JavaScriptEngine.syntaxError("Failed to execute 'evaluate': " + e.getMessage());
584584
}
585585
}
586586

src/test/java/org/htmlunit/html/xpath/HtmlUnitXPath2Test.java

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,60 @@ protected boolean isWebClientCached() {
3838
return true;
3939
}
4040

41+
/**
42+
* @throws Exception if the test fails
43+
*/
44+
@Test
45+
@Alerts({"4", "null"})
46+
public void xPathNull() throws Exception {
47+
final String content = "<html><head>\n"
48+
+ "<script>\n"
49+
+ LOG_TITLE_FUNCTION
50+
+ "function test() {\n"
51+
+ " try {\n"
52+
+ " var node = '';"
53+
+ " var expr = null;\n"
54+
+ " var result = document.evaluate(expr, document.documentElement, null, XPathResult.ANY_TYPE, null);\n"
55+
+ " node = result.iterateNext();\n"
56+
+ " log(result.resultType);\n"
57+
+ " log(node);\n"
58+
+ " } catch (e) {log(e.name)}\n"
59+
+ "}\n"
60+
+ "</script></head>\n"
61+
+ "<body onload='test()'>\n"
62+
+ " <select name='test'><option value='1'>foo&nbsp;and&nbsp;foo</option></select>\n"
63+
+ "</body></html>";
64+
65+
loadPageVerifyTitle2(content);
66+
}
67+
68+
/**
69+
* @throws Exception if the test fails
70+
*/
71+
@Test
72+
@Alerts({"4", "null"})
73+
public void xPathUndefined() throws Exception {
74+
final String content = "<html><head>\n"
75+
+ "<script>\n"
76+
+ LOG_TITLE_FUNCTION
77+
+ "function test() {\n"
78+
+ " try {\n"
79+
+ " var node = '';"
80+
+ " var expr = undefined;\n"
81+
+ " var result = document.evaluate(expr, document.documentElement, null, XPathResult.ANY_TYPE, null);\n"
82+
+ " node = result.iterateNext();\n"
83+
+ " log(result.resultType);\n"
84+
+ " log(node);\n"
85+
+ " } catch (e) {log(e.name)}\n"
86+
+ "}\n"
87+
+ "</script></head>\n"
88+
+ "<body onload='test()'>\n"
89+
+ " <select name='test'><option value='1'>foo&nbsp;and&nbsp;foo</option></select>\n"
90+
+ "</body></html>";
91+
92+
loadPageVerifyTitle2(content);
93+
}
94+
4195
/**
4296
* @throws Exception if the test fails
4397
*/
@@ -55,7 +109,7 @@ public void optionText() throws Exception {
55109
+ " for (var i = 0; i < value.length; i++) {\n"
56110
+ " log(value.charCodeAt(i));\n"
57111
+ " }\n"
58-
+ " } catch (e) {log('error')}\n"
112+
+ " } catch (e) {log(e.name)}\n"
59113
+ "}\n"
60114
+ "</script></head>\n"
61115
+ "<body onload='test()'>\n"
@@ -84,7 +138,7 @@ public void pipe() throws Exception {
84138
+ " res += node;\n"
85139
+ " }\n"
86140
+ " log(res);\n"
87-
+ " } catch (e) {log('error')}\n"
141+
+ " } catch (e) {log(e.name)}\n"
88142
+ "}\n"
89143
+ "</script></head>\n"
90144
+ "<body onload='test()'>\n"
@@ -114,7 +168,7 @@ public void math() throws Exception {
114168
+ " res += node.id;\n"
115169
+ " }\n"
116170
+ " log(res);\n"
117-
+ " } catch (e) {log('error')}\n"
171+
+ " } catch (e) {log(e.name)}\n"
118172
+ "}\n"
119173
+ "</script></head>\n"
120174
+ "<body onload='test()'>\n"
@@ -433,7 +487,7 @@ private void compareStringValue(final String xpath) throws Exception {
433487
+ " var expr = '" + xpath + "';\n"
434488
+ " var result = document.evaluate(expr, document.documentElement, null, XPathResult.ANY_TYPE, null);\n"
435489
+ " log(\"'\" + result.stringValue + \"'\");\n"
436-
+ " } catch (e) {log('error')}\n"
490+
+ " } catch (e) {log(e.name)}\n"
437491
+ "}\n"
438492
+ "</script></head>\n"
439493
+ "<body onload='test()'>\n"
@@ -457,7 +511,7 @@ private void compareBooleanValue(final String xpath) throws Exception {
457511
+ " var expr = '" + xpath + "';\n"
458512
+ " var result = document.evaluate(expr, document.documentElement, null, XPathResult.ANY_TYPE, null);\n"
459513
+ " log(result.booleanValue);\n"
460-
+ " } catch (e) {log('error')}\n"
514+
+ " } catch (e) {log(e.name)}\n"
461515
+ "}\n"
462516
+ "</script></head>\n"
463517
+ "<body onload='test()'>\n"

0 commit comments

Comments
 (0)