cheeriojs/cheerio — pull request #474 by jugglinmike (2014-05-04 20:23:58+00:00)
Title: Rewrite `data` internals with caching behavior

In #283, we identified some strange behavior in jQuery's `$.fn.data` method. We also confirmed that it is intentional and not likely to change. Since it seems so surprising, no one rushed to implement it.

More recently, #472 was filed against `data`'s automatic type coercion behavior. The underlying problem there is that jQuery makes a distinction between data set via the `data` method and data read from the markup--only the latter is type coerced.

In order to properly resolve #472, I had to also resolve #238. One upside to this is that we defer the "read" operation for `data-*` attributes until they are actually needed, so parse times for large documents with lots of `data-*` attributes should improve.

Commit message:

> Defer the storage and type coercion of HTML5 `data-*` attributes until a
> read operation. At this time, cache the value of the data attribute so
> that the DOM is not referenced again, as per the jQuery implementation:
>
> > The data- attributes are pulled in the first time the data property is
> > accessed and then are no longer accessed or mutated (all data values
> > are then stored internally in jQuery). [1]
>
> [1] http://api.jquery.com/data/