From dbf4ef48439bb039d97f1ac5918927dab7dfd2e3 Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Sun, 10 Nov 2024 12:41:34 +0100 Subject: [PATCH] StyleSheetList.item() should return null if the provided index is not available --- src/changes/changes.xml | 3 + .../javascript/host/css/StyleSheetList.java | 28 ++++---- .../host/html/HTMLTableElement.java | 2 +- .../host/css/StyleSheetListTest.java | 64 +++++++++++++++++++ 4 files changed, 84 insertions(+), 13 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b942b04842f..94b5fb1d2db 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -11,6 +11,9 @@ Javadoc fixed for Document.elementFromPoint(). + + StyleSheetList.item() should return null if the provided index is not available. + HTMLOptionsCollection.item() should return null if the provided index is not available. diff --git a/src/main/java/org/htmlunit/javascript/host/css/StyleSheetList.java b/src/main/java/org/htmlunit/javascript/host/css/StyleSheetList.java index f67a7a2b033..333dafbe33c 100644 --- a/src/main/java/org/htmlunit/javascript/host/css/StyleSheetList.java +++ b/src/main/java/org/htmlunit/javascript/host/css/StyleSheetList.java @@ -136,18 +136,11 @@ public int getLength() { */ @JsxFunction public Object item(final int index) { - if (nodes_ == null || index < 0 || index >= nodes_.getLength()) { - return JavaScriptEngine.UNDEFINED; + final Object item = get(index, this); + if (JavaScriptEngine.UNDEFINED == item) { + return null; } - - final HTMLElement element = (HTMLElement) nodes_.item(Integer.valueOf(index)); - - // - if (element instanceof HTMLStyleElement) { - return ((HTMLStyleElement) element).getSheet(); - } - // - return ((HTMLLinkElement) element).getSheet(); + return item; } /** @@ -156,7 +149,18 @@ public Object item(final int index) { @Override public Object get(final int index, final Scriptable start) { if (this == start) { - return item(index); + if (nodes_ == null || index < 0 || index >= nodes_.getLength()) { + return JavaScriptEngine.UNDEFINED; + } + + final HTMLElement element = (HTMLElement) nodes_.item(Integer.valueOf(index)); + + // + if (element instanceof HTMLStyleElement) { + return ((HTMLStyleElement) element).getSheet(); + } + // + return ((HTMLLinkElement) element).getSheet(); } return super.get(index, start); } diff --git a/src/main/java/org/htmlunit/javascript/host/html/HTMLTableElement.java b/src/main/java/org/htmlunit/javascript/host/html/HTMLTableElement.java index 2f4f432d2d3..b7a3a20d0f9 100644 --- a/src/main/java/org/htmlunit/javascript/host/html/HTMLTableElement.java +++ b/src/main/java/org/htmlunit/javascript/host/html/HTMLTableElement.java @@ -264,7 +264,7 @@ protected boolean isContainedRow(final HtmlTableRow row) { * {@inheritDoc} */ @Override - public Object insertRow(final int index) { + public HtmlUnitScriptable insertRow(final int index) { // check if a tbody should be created if (index != 0) { for (final HtmlElement htmlElement : getDomNodeOrDie().getHtmlElementDescendants()) { diff --git a/src/test/java/org/htmlunit/javascript/host/css/StyleSheetListTest.java b/src/test/java/org/htmlunit/javascript/host/css/StyleSheetListTest.java index 94b7379721d..d2765f774e0 100644 --- a/src/test/java/org/htmlunit/javascript/host/css/StyleSheetListTest.java +++ b/src/test/java/org/htmlunit/javascript/host/css/StyleSheetListTest.java @@ -308,6 +308,70 @@ public void in() throws Exception { loadPageVerifyTitle2(html); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts({"1", "undefined", "[object CSSStyleSheet]", "undefined", "undefined"}) + public void index() throws Exception { + final String html = + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " abc\n" + + ""; + + final String css = "div {color:red}"; + getMockWebConnection().setDefaultResponse(css, MimeType.TEXT_CSS); + + loadPageVerifyTitle2(html); + } + + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts({"1", "null", "[object CSSStyleSheet]", "null", "null"}) + public void item() throws Exception { + final String html = + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " abc\n" + + ""; + + final String css = "div {color:red}"; + getMockWebConnection().setDefaultResponse(css, MimeType.TEXT_CSS); + + loadPageVerifyTitle2(html); + } + /** * @throws Exception if an error occurs */