Skip to content

Commit

Permalink
StyleSheetList.item() should return null if the provided index is not…
Browse files Browse the repository at this point in the history
… available
  • Loading branch information
rbri committed Nov 10, 2024
1 parent 89b9393 commit dbf4ef4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<action type="fix" dev="rbri">
Javadoc fixed for Document.elementFromPoint().
</action>
<action type="fix" dev="rbri">
StyleSheetList.item() should return null if the provided index is not available.
</action>
<action type="fix" dev="rbri">
HTMLOptionsCollection.item() should return null if the provided index is not available.
</action>
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/org/htmlunit/javascript/host/css/StyleSheetList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

// <style type="text/css"> ... </style>
if (element instanceof HTMLStyleElement) {
return ((HTMLStyleElement) element).getSheet();
}
// <link rel="stylesheet" type="text/css" href="..." />
return ((HTMLLinkElement) element).getSheet();
return item;
}

/**
Expand All @@ -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));

// <style type="text/css"> ... </style>
if (element instanceof HTMLStyleElement) {
return ((HTMLStyleElement) element).getSheet();
}
// <link rel="stylesheet" type="text/css" href="..." />
return ((HTMLLinkElement) element).getSheet();
}
return super.get(index, start);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"<html>\n"
+ " <head>\n"
+ " <link rel='stylesheet' type='text/css' href='foo.css'/>\n"
+ " <script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var sheets = document.styleSheets;\n"
+ " log(sheets.length);\n"
+ " log(sheets[-1]);\n"
+ " log(sheets[0]);\n"
+ " log(sheets[1]);\n"
+ " log(sheets[42]);\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ " <body onload='test()'>abc</body>\n"
+ "</html>";

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 =
"<html>\n"
+ " <head>\n"
+ " <link rel='stylesheet' type='text/css' href='foo.css'/>\n"
+ " <script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var sheets = document.styleSheets;\n"
+ " log(sheets.length);\n"
+ " log(sheets.item(-1));\n"
+ " log(sheets.item(0));\n"
+ " log(sheets.item(1));\n"
+ " log(sheets.item(42));\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ " <body onload='test()'>abc</body>\n"
+ "</html>";

final String css = "div {color:red}";
getMockWebConnection().setDefaultResponse(css, MimeType.TEXT_CSS);

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if an error occurs
*/
Expand Down

0 comments on commit dbf4ef4

Please sign in to comment.