Class: Dommy::DatasetMap

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/element.rb

Overview

Element#dataset proxy. el.dataset.fooBar reads / writes data-foo-bar per the HTMLOrForeignElement.dataset spec (camelCase ↔ kebab-case round-trip).

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ DatasetMap

Returns a new instance of DatasetMap.



984
985
986
# File 'lib/dommy/element.rb', line 984

def initialize(element)
  @element = element
end

Instance Method Details

#__js_call__(_method, _args) ⇒ Object



1006
1007
1008
# File 'lib/dommy/element.rb', line 1006

def __js_call__(_method, _args)
  nil
end

#__js_delete__(key) ⇒ Object

Named deleter (delete el.dataset.foo): removes the data-* attribute.



1001
1002
1003
1004
# File 'lib/dommy/element.rb', line 1001

def __js_delete__(key)
  @element.remove_attribute(attr_name(key))
  true
end

#__js_get__(key) ⇒ Object



988
989
990
991
992
993
# File 'lib/dommy/element.rb', line 988

def __js_get__(key)
  # A missing data-* attribute reads as JS `undefined` (and `"foo" in dataset`
  # is false), per DOMStringMap semantics.
  value = @element.__dommy_backend_node__[attr_name(key)]
  value.nil? ? Bridge::ABSENT : value
end

#__js_named_props__Object

WebIDL "supported property names" for DOMStringMap: each data-* attribute's name with the data- prefix stripped and -x sequences camel-cased (data-date-of-birthdateOfBirth, data- → ``).



1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/dommy/element.rb', line 1013

def __js_named_props__
  Backend.attribute_nodes(@element.__dommy_backend_node__).filter_map do |a|
    name = Backend.attribute_ns_info(a)[:qualified_name]
    next unless name.start_with?("data-")

    name.sub(/\Adata-/, "").gsub(/-([a-z])/) { ::Regexp.last_match(1).upcase }
  end
end

#__js_set__(key, value) ⇒ Object



995
996
997
998
# File 'lib/dommy/element.rb', line 995

def __js_set__(key, value)
  @element.set_attribute(attr_name(key), value.to_s)
  nil
end