Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upHTMLCollections cannot be accessed with the `index` JSON Parser #913
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
process-bot
Oct 6, 2017
Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!
Here is what to expect next, and if anyone wants to comment, keep these things in mind.
process-bot
commented
Oct 6, 2017
|
Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it! Here is what to expect next, and if anyone wants to comment, keep these things in mind. |
antarestrader
changed the title from
JSON Parser 'index' is too strict
to
HTMLCollections cannot be accessed with the `index` JSON Parser
Oct 6, 2017
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
zwilias
Oct 7, 2017
Member
In case this is blocking anyone right now:
For a specific index
field (toString index) yourDecoder
For the whole collection
Decode the length field, then gather up the contents. This would work on a bunch of array-like structures.
https://ellie-app.com/jfWRHpCsga1/1
I may add that (or something like it, perhaps with a better name) to elm-community/json-extra, as the question of how to do it comes up regularly.
I've added this to elm-community/json-extra as collection
|
In case this is blocking anyone right now: For a specific index
For the whole collectionDecode the https://ellie-app.com/jfWRHpCsga1/1
I've added this to |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Oct 8, 2017
Member
Normally I would expect folks to track inputs in their Model. That would mean that the submit button would not need to know anything about the particular text fields in any form. That'd be tracked elsewhere.
Why is this case different?
|
Normally I would expect folks to track inputs in their Why is this case different? |
antarestrader commentedOct 6, 2017
•
edited
Edited 1 time
-
antarestrader
edited Oct 6, 2017 (most recent)
https://github.com/elm-lang/core/blob/b06aa4421f9016c820576eb6a38174b6137fe052/src/Native/Json.js#L360-L372
Arrays are not the only thing which would respond to an index like call. HTMLCollection Objects can also be indexed. The code in lines 366- 371 will in fact work fine with an HTMLCollection (and its decedents).
Use Case:
Here we have a custom
Eventfor theonsubmitevent in a form which wishes to return the value of the first element of that form.Json.index 0fails becausetarget.elementsis aHTMLFormControlsCollectionand not anArray. The check on line 362 prevents this from succeeding, but there is no other reason to fail. AsJson.Decodecurrently stands there is no way to decode anything within any kind of HTMLCollection.Possible Solutions:
HTMLCollectionalong with array on line 326:valuecan be indexed.Note that the same argument could be made for the
arraycombinator beginning on line 235