Skip to content

Commit 7001435

Browse files
committed
CanvasRenderingContext2D.measureText() throws a TypeError.
1 parent 8f25586 commit 7001435

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

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+
CanvasRenderingContext2D.measureText() throws a TypeError.
13+
</action>
1114
<action type="fix" dev="rbri">
1215
HTMLTableElement/HTMLTableRowElement several fixes to throw the correct error.
1316
</action>

src/main/java/org/htmlunit/javascript/host/canvas/CanvasRenderingContext2D.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,7 @@ public void lineTo(final double x, final double y) {
563563
@JsxFunction
564564
public TextMetrics measureText(final Object text) {
565565
if (text == null || JavaScriptEngine.isUndefined(text)) {
566-
throw JavaScriptEngine.throwAsScriptRuntimeEx(
567-
new RuntimeException("Missing argument for CanvasRenderingContext2D.measureText()."));
566+
throw JavaScriptEngine.typeError("Missing argument for CanvasRenderingContext2D.measureText().");
568567
}
569568

570569
final String textValue = JavaScriptEngine.toString(text);

src/test/java/org/htmlunit/javascript/host/canvas/CanvasRenderingContext2DTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void test() throws Exception {
7171
+ " ctx.closePath();\n"
7272
+ " ctx.rotate(1.234);\n"
7373
+ " log('done');\n"
74-
+ " } catch(e) { log('exception'); }\n"
74+
+ " } catch(e) { log(e.name); }\n"
7575
+ "}\n"
7676
+ "</script>\n"
7777
+ "</head>\n"
@@ -112,7 +112,7 @@ public void methods() throws Exception {
112112
+ " log(methods[i]);\n"
113113
+ " }\n"
114114
+ " log(nbMethods + ' methods');\n"
115-
+ " } catch(e) { log('exception'); }\n"
115+
+ " } catch(e) { log(e.name); }\n"
116116
+ "</script></body></html>";
117117

118118
loadPageVerifyTextArea2(html);
@@ -772,7 +772,7 @@ public void drawImageDataUrlSvg() throws Exception {
772772
* @throws Exception if an error occurs
773773
*/
774774
@Test
775-
@Alerts({"exception", "0", "true", "true"})
775+
@Alerts({"TypeError", "0", "true", "true"})
776776
public void measureText() throws Exception {
777777
final String html =
778778
"<html>\n"
@@ -785,7 +785,7 @@ public void measureText() throws Exception {
785785
+ " ctx = canvas.getContext('2d');\n"
786786
+ " try {\n"
787787
+ " log(ctx.measureText());\n"
788-
+ " } catch(e) { log('exception'); }\n"
788+
+ " } catch(e) { log(e.name); }\n"
789789

790790
+ " var metrics = ctx.measureText('');\n"
791791
+ " log(metrics.width);\n"
@@ -936,7 +936,7 @@ public void globalAlpha() throws Exception {
936936
+ " log(ctx.globalAlpha);\n"
937937
+ " ctx.globalAlpha = null;\n"
938938
+ " log(ctx.globalAlpha);\n"
939-
+ " } catch(e) { log('exception'); }\n"
939+
+ " } catch(e) { log(e.name); }\n"
940940
+ "}\n"
941941
+ "</script>\n"
942942
+ "</head>\n"
@@ -970,7 +970,7 @@ public void globalAlphaInvalid() throws Exception {
970970
+ " log(ctx.globalAlpha);\n"
971971
+ " ctx.globalAlpha = undefined;\n"
972972
+ " log(ctx.globalAlpha);\n"
973-
+ " } catch(e) { log('exception'); }\n"
973+
+ " } catch(e) { log(e.name); }\n"
974974
+ "}\n"
975975
+ "</script>\n"
976976
+ "</head>\n"

0 commit comments

Comments
 (0)