Class: Browser::DOM::Element::Attributes
- Includes:
- Enumerable
- Defined in:
- lib/diamonds/opal/browser/dom/element/attributes.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
- #[](name, options = {}) ⇒ Object (also: #get)
- #[]=(name, value, options = {}) ⇒ Object (also: #set)
- #each(&block) ⇒ Object
- #has_key?(name) ⇒ Boolean
-
#initialize(element, options) ⇒ Attributes
constructor
A new instance of Attributes.
- #merge!(hash) ⇒ Object
Constructor Details
#initialize(element, options) ⇒ Attributes
Returns a new instance of Attributes.
6 7 8 9 10 |
# File 'lib/diamonds/opal/browser/dom/element/attributes.rb', line 6 def initialize(element, ) @element = element @native = element.to_n @namespace = [:namespace] end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
4 5 6 |
# File 'lib/diamonds/opal/browser/dom/element/attributes.rb', line 4 def namespace @namespace end |
Instance Method Details
#[](name, options = {}) ⇒ Object Also known as: get
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/diamonds/opal/browser/dom/element/attributes.rb', line 13 def [](name, = {}) if name == :class && Browser.supports?('Element.className') name = :className elsif name == :for && Browser.supports?('Element.htmlFor') name = :htmlFor end if namespace = [:namespace] || @namespace `#@native.getAttributeNS(#{namespace.to_s}, #{name.to_s}) || nil` else `#@native.getAttribute(#{name.to_s}) || nil` end end |
#[]=(name, value, options = {}) ⇒ Object Also known as: set
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/diamonds/opal/browser/dom/element/attributes.rb', line 27 def []=(name, value, = {}) if name == :class && Browser.supports?('Element.className') name = :className elsif name == :for && Browser.supports?('Element.htmlFor') name = :htmlFor end if namespace = [:namespace] || @namespace `#@native.setAttributeNS(#{namespace.to_s}, #{name.to_s}, #{value})` else `#@native.setAttribute(#{name.to_s}, #{value.to_s})` end end |
#each(&block) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/diamonds/opal/browser/dom/element/attributes.rb', line 60 def each(&block) return enum_for :each unless block_given? @element.attribute_nodes.each {|attr| yield attr.name, attr.value } self end |
#has_key?(name) ⇒ Boolean
72 73 74 |
# File 'lib/diamonds/opal/browser/dom/element/attributes.rb', line 72 def has_key?(name) !!self[name] end |
#merge!(hash) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/diamonds/opal/browser/dom/element/attributes.rb', line 76 def merge!(hash) hash.each {|name, value| self[name] = value } self end |