$.fn.data and html5 data- attributes - broken implementation
|Reported by:||nate@…||Owned by:|
The type detection built into retrieval of values from html5 data- attributes via $.fn.data in its current implementation is horribly broken.
Support was provided for booleans true and false, null, and complex objects (arrays and object hashes). These are all handled by setting the attribute's value to the JSON-encoded version of the data. However, the same treatment was not provided for parsing strings.
data-name='"joe"' currently parses as a string with the value '"joe"', when it should in fact be parsed as string 'joe' - no quotes. Current implementation does not allow for passing a string with the value "true", "false", "null", nor a stringified version of a complex JSON object.
Invalid JSON values (ie: data-name="joe") can be made to fallback on string value rather than generating a JSON error. But strings in their proper JSON encoded form must be properly supported.
If support for booleans, null and complex objects is going to stay, all data- attributes should be parsed with parseJSON, not a hacked-together soup of sometimes-parsing as JSON, sometimes not. If there is truly a performance boost from prematurely detecting booleans/nulls/numerics rather than parsing proper JSON, then these shortcut detections should be built directly into the parseJSON function, not in a single place within $.fn.data.
Change History (27)
comment:3 Changed 5 years ago by snover
- Component changed from unfiled to data
- Keywords needsreview added
comment:17 Changed 5 years ago by dmethvin
- Milestone 1.next deleted
- Resolution set to invalid
- Status changed from new to closed
comment:23 Changed 5 years ago by dmethvin
- Resolution invalid deleted
- Status changed from closed to reopened
comment:24 Changed 5 years ago by dmethvin
- Resolution set to duplicate
- Status changed from reopened to closed