Class: Dommy::Attr
- Inherits:
-
Object
- Object
- Dommy::Attr
- Defined in:
- lib/dommy/attr.rb
Overview
‘Attr` — wraps an HTML attribute as a Node-like object. In real DOM each attribute on an element is an Attr; `el.getAttributeNode` returns the instance, `attr.value = “x”` mutates the element’s attribute, ‘attr.ownerElement` points back to the element.
We represent two states:
- "owned" — the Attr is attached to an Element. value reads/writes
go through the element's Nokogiri attribute slot.
- "detached" — created via `document.createAttribute(name)` but
not yet attached. Value is stored locally; `setAttributeNode`
transfers it to an element.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#__attach__(element) ⇒ Object
Internal: called by Element when the attr is being transferred to (or detached from) an Element.
- #__detach__ ⇒ Object
- #__js_call__(method, _args) ⇒ Object
- #__js_get__(key) ⇒ Object
- #__js_set__(key, val) ⇒ Object
-
#initialize(name, owner: nil, value: "") ⇒ Attr
constructor
A new instance of Attr.
-
#owner_element ⇒ Object
The Element this attr is on, or nil if detached.
- #value ⇒ Object
- #value=(new_value) ⇒ Object
Constructor Details
#initialize(name, owner: nil, value: "") ⇒ Attr
Returns a new instance of Attr.
18 19 20 21 22 |
# File 'lib/dommy/attr.rb', line 18 def initialize(name, owner: nil, value: "") @name = name.to_s.downcase @owner = owner @detached_value = value.to_s end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/dommy/attr.rb', line 16 def name @name end |
Instance Method Details
#__attach__(element) ⇒ Object
Internal: called by Element when the attr is being transferred to (or detached from) an Element.
84 85 86 87 88 |
# File 'lib/dommy/attr.rb', line 84 def __attach__(element) @owner = element @detached_value = "" nil end |
#__detach__ ⇒ Object
90 91 92 93 94 95 |
# File 'lib/dommy/attr.rb', line 90 def __detach__ cached = value @owner = nil @detached_value = cached nil end |
#__js_call__(method, _args) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/dommy/attr.rb', line 75 def __js_call__(method, _args) case method when "cloneNode" Attr.new(@name, owner: nil, value: value) end end |
#__js_get__(key) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dommy/attr.rb', line 45 def __js_get__(key) case key when "name" @name when "value" value when "nodeName" @name when "nodeValue" value when "ownerElement" @owner when "localName" @name when "namespaceURI" nil when "nodeType" 2 end end |
#__js_set__(key, val) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/dommy/attr.rb', line 66 def __js_set__(key, val) case key when "value", "nodeValue" self.value = val end nil end |
#owner_element ⇒ Object
The Element this attr is on, or nil if detached.
25 26 27 |
# File 'lib/dommy/attr.rb', line 25 def owner_element @owner end |
#value ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/dommy/attr.rb', line 29 def value if @owner @owner.__node__[@name].to_s else @detached_value end end |
#value=(new_value) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/dommy/attr.rb', line 37 def value=(new_value) if @owner @owner.set_attribute(@name, new_value.to_s) else @detached_value = new_value.to_s end end |