I want to extract all the HTML5 data attributes from a tag, just like this jQuery plugin.
For example, given:
<span data-age="50" data-location="London" class="highlight">Joe Bloggs</span>
I want to get a hash like:
{ 'data-age' => '50', 'data-location' => 'London' }
I was originally hoping use a wildcard as part of my CSS selector, e.g.
Nokogiri(html).css('span[@data-*]').size
but it seems that isn’t supported.
Option 1: Grab all data elements
If all you need is to list all the page’s data elements, here’s a one-liner:
Output:
Option 2: Group results by tag
If you want to group your results by tag (perhaps you need to do additional processing on each tag), you can do the following:
Then
tagsis an array containing key-value hash pairs, grouped by tag.Option 3: Behavior like the jQuery datasets plugin
If you’d prefer the plugin-like approach, the following will give you a
datasetmethod on every Nokogiri node.Then you can find the dataset for a single element:
Or get the dataset for a group of elements:
Example:
The following is the behavior of the
datasetmethod above. Given the following lines in the HTML:The output would be: