Skip to content

Commit 1feda22

Browse files
committed
more tests
1 parent 460bbdd commit 1feda22

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

src/main/java/org/htmlunit/javascript/host/Storage.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,13 @@ public void removeItem(final String key) {
135135
*/
136136
@JsxFunction
137137
public String key(final int index) {
138-
int counter = 0;
139-
for (final String key : store_.keySet()) {
140-
if (counter++ == index) {
141-
return key;
138+
if (index >= 0) {
139+
int counter = 0;
140+
for (final String key : store_.keySet()) {
141+
if (counter == index) {
142+
return key;
143+
}
144+
counter++;
142145
}
143146
}
144147
return null;

src/test/java/org/htmlunit/javascript/host/StorageTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,34 @@ public void localStorageSizeManyEntries() throws Exception {
190190
loadPageVerifyTitle2(firstHtml);
191191
}
192192

193+
/**
194+
* @throws Exception if the test fails
195+
*/
196+
@Test
197+
@Alerts({"true", "true", "null", "null"})
198+
public void localStorageKey() throws Exception {
199+
final String html = DOCTYPE_HTML
200+
+ "<html>\n"
201+
+ "<body>\n"
202+
+ "<script>\n"
203+
+ LOG_TITLE_FUNCTION
204+
+ " if (window.localStorage) {\n"
205+
+ " localStorage.clear();\n"
206+
207+
+ " localStorage.setItem('HtmlUnit', '0');\n"
208+
+ " localStorage.setItem('HtmlUnit 1', '1');\n"
209+
+ " localStorage.setItem('HtmlUnit 2', '2');\n"
210+
211+
+ " log(localStorage.key(0).startsWith('HtmlUnit'));\n"
212+
+ " log(localStorage.key(1).startsWith('HtmlUnit'));\n"
213+
+ " log(localStorage.key(3));\n"
214+
+ " log(localStorage.key(-1));\n"
215+
+ " }\n"
216+
+ "</script>\n"
217+
+ "</body></html>";
218+
loadPageVerifyTitle2(html);
219+
}
220+
193221
/**
194222
* @throws Exception if the test fails
195223
*/

0 commit comments

Comments
 (0)