Class: DOM::NODE
- Includes:
- Comparable, Events
- Defined in:
- opal/fron/dom/node.rb
Overview
Low level wrapper for the Node class
:reek:TooManyMethods
Instance Attribute Summary
Attributes included from Events
Class Method Summary collapse
-
.from_node(node) ⇒ DOM::NODE
Returns the ruby instance associated to the node, or creates a new instance if there is none.
-
.get_element(obj) ⇒ Native
Gets the native node from the given object.
Instance Method Summary collapse
-
#<<(other) ⇒ Object
Inserts other node into self.
-
#<=>(other) ⇒ Boolean
Compars self position with other node.
-
#==(other) ⇒ Boolean
Compars self with other node.
-
#>>(other) ⇒ Object
Inserts self into other node.
-
#children ⇒ DOM::NodeList
Returns the child nodes as a NodeList.
-
#dup ⇒ DOM::NODE
Clones the node without child nodes.
-
#dup! ⇒ DOM::NODE
Clones the node with child nodes.
-
#empty ⇒ Object
Removes all the child nodes.
-
#empty? ⇒ Boolean
Returns if the node is empty or not.
-
#index ⇒ Integer
Returns the index of the node.
-
#initialize(node = nil) ⇒ NODE
constructor
Initializes the node with the given native node.
-
#insert_before(what, where) ⇒ Object
Inserts the given node before the other given node.
-
#normalize! ⇒ Object
Normalizes the node removing extra text nodes.
-
#parent ⇒ DOM::NODE
Returns the parent element.
-
#parent_node ⇒ DOM::NODE
Returns the parent node.
-
#remove(el) ⇒ Object
Removes the given elment from the node.
-
#remove! ⇒ Object
Removes self from parent node.
-
#text ⇒ String
Returns the text content of the node.
-
#text=(text) ⇒ Object
Sets the text content of the node with the given value.
Methods included from Events
#add_listener, #delegate, #off, #old_trigger, #on, #on!, #remove_listeners, #trigger
Constructor Details
#initialize(node = nil) ⇒ NODE
Initializes the node with the given native node
27 28 29 30 31 32 |
# File 'opal/fron/dom/node.rb', line 27 def initialize(node = nil) raise 'A node must be provided!' unless node raise 'Not a HTML Node given!' unless `#{node} instanceof Node` @el = node `#{@el}._instance = #{self}` end |
Class Method Details
.from_node(node) ⇒ DOM::NODE
Returns the ruby instance associated to the node, or creates a new instance if there is none.
17 18 19 20 21 22 |
# File 'opal/fron/dom/node.rb', line 17 def self.from_node(node) return nil unless node instance = `#{node}._instance || Opal.nil` return instance if instance && instance.is_a?(DOM::NODE) new node end |
.get_element(obj) ⇒ Native
Gets the native node from the given object
169 170 171 172 173 174 175 176 177 |
# File 'opal/fron/dom/node.rb', line 169 def self.get_element(obj) if `#{obj} instanceof Node` obj elsif obj.is_a?(NODE) obj.instance_variable_get('@el') else nil end end |
Instance Method Details
#<<(other) ⇒ Object
Inserts other node into self
97 98 99 |
# File 'opal/fron/dom/node.rb', line 97 def <<(other) `#{@el}.appendChild(#{NODE.get_element other})` end |
#<=>(other) ⇒ Boolean
Compars self position with other node
150 151 152 153 154 |
# File 'opal/fron/dom/node.rb', line 150 def <=>(other) return 0 if other == self raise 'Nodes not Comparable!' if other.parent != parent other.index <=> index end |
#==(other) ⇒ Boolean
Compars self with other node
141 142 143 |
# File 'opal/fron/dom/node.rb', line 141 def ==(other) `#{NODE.get_element(other)} === #{@el}` end |
#>>(other) ⇒ Object
Inserts self into other node
104 105 106 |
# File 'opal/fron/dom/node.rb', line 104 def >>(other) `#{NODE.get_element other}.appendChild(#{@el})` end |
#children ⇒ DOM::NodeList
Returns the child nodes as a NodeList
77 78 79 |
# File 'opal/fron/dom/node.rb', line 77 def children NodeList.new `Array.prototype.slice.call(#{@el}.childNodes)` end |
#dup ⇒ DOM::NODE
Clones the node without child nodes
37 38 39 |
# File 'opal/fron/dom/node.rb', line 37 def dup self.class.new `#{@el}.cloneNode()` end |
#dup! ⇒ DOM::NODE
Clones the node with child nodes
44 45 46 |
# File 'opal/fron/dom/node.rb', line 44 def dup! self.class.new `#{@el}.cloneNode(true)` end |
#empty ⇒ Object
Removes all the child nodes
63 64 65 |
# File 'opal/fron/dom/node.rb', line 63 def empty children.each(&:remove!) end |
#empty? ⇒ Boolean
Returns if the node is empty or not
70 71 72 |
# File 'opal/fron/dom/node.rb', line 70 def empty? `#{@el}.childNodes.length === 0` end |
#index ⇒ Integer
Returns the index of the node
159 160 161 162 |
# File 'opal/fron/dom/node.rb', line 159 def index return nil unless parent NodeList.new(`Array.prototype.slice.call(#{@el}.parentNode.children)`).index self end |
#insert_before(what, where) ⇒ Object
Inserts the given node before the other given node
112 113 114 115 |
# File 'opal/fron/dom/node.rb', line 112 def insert_before(what, where) return what >> self unless where # Fix for firefox... `#{@el}.insertBefore(#{NODE.get_element what},#{NODE.get_element where})` end |
#normalize! ⇒ Object
Normalizes the node removing extra text nodes
132 133 134 |
# File 'opal/fron/dom/node.rb', line 132 def normalize! `#{@el}.normalize()` end |
#parent ⇒ DOM::NODE
Returns the parent element
58 59 60 |
# File 'opal/fron/dom/node.rb', line 58 def parent DOM::Element.from_node `#{@el}.parentElement || Opal.nil` end |
#parent_node ⇒ DOM::NODE
Returns the parent node
51 52 53 |
# File 'opal/fron/dom/node.rb', line 51 def parent_node DOM::Element.from_node `#{@el}.parentNode || Opal.nil` end |
#remove(el) ⇒ Object
Removes the given elment from the node
84 85 86 |
# File 'opal/fron/dom/node.rb', line 84 def remove(el) `#{@el}.removeChild(#{NODE.get_element el})` end |
#remove! ⇒ Object
Removes self from parent node
89 90 91 92 |
# File 'opal/fron/dom/node.rb', line 89 def remove! return unless parent parent.remove self end |
#text ⇒ String
Returns the text content of the node
120 121 122 |
# File 'opal/fron/dom/node.rb', line 120 def text `#{@el}.textContent` end |
#text=(text) ⇒ Object
Sets the text content of the node with the given value
127 128 129 |
# File 'opal/fron/dom/node.rb', line 127 def text=(text) `#{@el}.textContent = #{text}` end |