File tree Expand file tree Collapse file tree
main/java/org/htmlunit/javascript/host
test/java/org/htmlunit/javascript/host Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 */
You can’t perform that action at this time.
0 commit comments