Class: Browser::DOM::Node
- Includes:
- NativeCachedWrapper
- Defined in:
- opal/browser/dom/node.rb
Overview
Abstract class for all DOM node types.
Direct Known Subclasses
Constant Summary collapse
- ELEMENT_NODE =
1
- ATTRIBUTE_NODE =
2
- TEXT_NODE =
3
- CDATA_SECTION_NODE =
4
- ENTITY_REFERENCE_NOCE =
5
- ENTITY_NODE =
6
- PROCESSING_INSTRUCTION_NODE =
7
- COMMENT_NODE =
8
- DOCUMENT_NODE =
9
- DOCUMENT_TYPE_NODE =
10
- DOCUMENT_FRAGMENT_NODE =
11
- NOTATION_NODE =
12
Instance Attribute Summary collapse
-
#child ⇒ Node?
readonly
The first child of the node.
-
#children ⇒ NodeSet
The children of the node.
-
#document ⇒ Document?
The document the node is attached to.
-
#element_children ⇒ NodeSet
(also: #elements)
readonly
All the children which are elements.
-
#first_element_child ⇒ Element?
readonly
The first element child.
-
#last_element_child ⇒ Element?
readonly
The last element child.
-
#name ⇒ String
(also: #node_name)
The name of the node.
-
#namespace ⇒ String
readonly
The namespace of the node.
-
#next ⇒ Node?
(also: #next_sibling)
The next sibling of the node.
-
#next_element ⇒ Element?
readonly
The next element sibling of the node.
-
#node_type ⇒ Symbol
(also: #type)
readonly
The type of the node.
-
#outer_html ⇒ String
The simulated outer html of the node.
-
#parent ⇒ Element?
The parent of the node.
-
#previous ⇒ Node?
(also: #previous_sibling)
The previous sibling of the node.
-
#previous_element ⇒ Element?
readonly
The previous element sibling of the node.
-
#value ⇒ String
The value of the node.
Class Method Summary collapse
-
.new(value) ⇒ Node
Wrap a native DOM node.
Instance Method Summary collapse
-
#<<(node) ⇒ self
Append a child to the node.
-
#==(other) ⇒ Boolean
Return true of the other element is the same underlying DOM node.
- #>>(node) ⇒ Object
- #add_child(node = nil, &block) ⇒ Object
-
#add_next_sibling(node = nil, &block) ⇒ Object
(also: #after, #next=)
Add the passed node after this one.
-
#add_previous_sibling(node = nil, &block) ⇒ Object
(also: #before, #previous=)
Add the passed node before this one.
-
#ancestors(expression = nil) ⇒ NodeSet
Get an array of ancestors.
-
#append_to(node) ⇒ Object
Append the node to the passed one.
- #attached? ⇒ Boolean
- #blank? ⇒ Boolean
-
#cdata? ⇒ Boolean
Return true if the node is a CDATA section.
-
#clear ⇒ Object
Remove all the children of the node.
-
#comment? ⇒ Boolean
Return true if the node is a comment.
- #content ⇒ Object (also: #inner_text, #text)
- #content=(value) ⇒ Object (also: #inner_text=, #text=)
-
#custom? ⇒ Boolean
Return true if the node is a custom element.
-
#document? ⇒ Boolean
Return true if the node is a document.
-
#elem? ⇒ Boolean
(also: #element?)
Return true if the node is an element.
-
#fragment? ⇒ Boolean
Return true if the node is a document fragment.
-
#initialize(node) ⇒ Node
constructor
A new instance of Node.
-
#initialize_copy(old) ⇒ Object
Initialize a new node after
#dup
or#clone
. - #parse(text, options = {}) ⇒ Object
- #path ⇒ Object
-
#prepend_to(node) ⇒ Object
Prepend the node to the passed one.
-
#remove ⇒ Object
Remove the node from its parent.
-
#remove_child(node) ⇒ Object
Remove the given node from the children of this node.
-
#replace(node) ⇒ Node
(also: #replace_with)
Replace the node with the given one.
-
#text? ⇒ Boolean
Return true if the node is a text node.
- #traverse(&block) ⇒ Object
Methods included from NativeCachedWrapper
#restricted?, #set_native_reference
Constructor Details
#initialize(node) ⇒ Node
Returns a new instance of Node.
41 42 43 44 |
# File 'opal/browser/dom/node.rb', line 41 def initialize(node) raise ArgumentError, "Please ensure that #initialize of #{self.class} accepts one argument" unless node super end |
Instance Attribute Details
#child ⇒ Node? (readonly)
Returns the first child of the node.
265 266 267 |
# File 'opal/browser/dom/node.rb', line 265 def child children.first end |
#children ⇒ NodeSet
Returns the children of the node.
271 272 273 |
# File 'opal/browser/dom/node.rb', line 271 def children NodeSet[Native::Array.new(`#@native.childNodes`)] end |
#document ⇒ Document?
Returns the document the node is attached to.
291 292 293 |
# File 'opal/browser/dom/node.rb', line 291 def document DOM(`#@native.ownerDocument`) if defined?(`#@native.ownerDocument`) end |
#element_children ⇒ NodeSet (readonly) Also known as: elements
Returns all the children which are elements.
314 315 316 |
# File 'opal/browser/dom/node.rb', line 314 def element_children children.select(&:element?) end |
#first_element_child ⇒ Element? (readonly)
Returns the first element child.
322 323 324 |
# File 'opal/browser/dom/node.rb', line 322 def first_element_child element_children.first end |
#last_element_child ⇒ Element? (readonly)
Returns the last element child.
336 337 338 |
# File 'opal/browser/dom/node.rb', line 336 def last_element_child element_children.last end |
#name ⇒ String Also known as: node_name
Returns the name of the node.
342 343 344 |
# File 'opal/browser/dom/node.rb', line 342 def name `#@native.nodeName || nil` end |
#namespace ⇒ String (readonly)
Returns the namespace of the node.
352 353 354 |
# File 'opal/browser/dom/node.rb', line 352 def namespace `#@native.namespaceURI || nil` end |
#next ⇒ Node? Also known as: next_sibling
Returns the next sibling of the node.
358 359 360 |
# File 'opal/browser/dom/node.rb', line 358 def next DOM(`#@native.nextSibling`) if `#@native.nextSibling != null` end |
#next_element ⇒ Element? (readonly)
Returns the next element sibling of the node.
366 367 368 369 370 371 372 373 374 |
# File 'opal/browser/dom/node.rb', line 366 def next_element current = self.next while current && !current.element? current = current.next end current end |
#node_type ⇒ Symbol (readonly) Also known as: type
Returns the type of the node.
384 385 386 |
# File 'opal/browser/dom/node.rb', line 384 def node_type `#@native.nodeType` end |
#outer_html ⇒ String
Returns the simulated outer html of the node.
390 391 392 393 394 |
# File 'opal/browser/dom/node.rb', line 390 def outer_html div = $document.create_element("DIV") div << self.dup div.inner_html end |
#parent ⇒ Element?
Returns the parent of the node.
398 399 400 |
# File 'opal/browser/dom/node.rb', line 398 def parent DOM(`#@native.parentNode`) if `#@native.parentNode != null` end |
#previous ⇒ Node? Also known as: previous_sibling
Returns the previous sibling of the node.
424 425 426 |
# File 'opal/browser/dom/node.rb', line 424 def previous DOM(`#@native.previousSibling`) if `#@native.previousSibling != null` end |
#previous_element ⇒ Element? (readonly)
Returns the previous element sibling of the node.
432 433 434 435 436 437 438 439 440 |
# File 'opal/browser/dom/node.rb', line 432 def previous_element current = self.previous while current && !current.element? current = current.previous end current end |
#value ⇒ String
Returns the value of the node.
490 491 492 |
# File 'opal/browser/dom/node.rb', line 490 def value `#@native.nodeValue || nil` end |
Class Method Details
.new(value) ⇒ Node
Wrap a native DOM node.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'opal/browser/dom/node.rb', line 27 def self.new(value) if self == Node @classes ||= [nil, Element, Attribute, Text, CDATA, nil, nil, nil, Comment, Document, nil, DocumentFragment] if klass = @classes[`value.nodeType`] klass.new(value) else raise ArgumentError, 'cannot instantiate a non derived Node object' end else super end end |
Instance Method Details
#<<(node) ⇒ self
Append a child to the node.
When passing a String a text node will be created.
When passing an Object that responds to #each, every yielded element will be added following the same logic.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'opal/browser/dom/node.rb', line 74 def <<(node) if Opal.respond_to? node, :each node.each { |n| self << n } return self elsif Opal.respond_to? node, :to_dom node = node.to_dom(document) end unless native?(node) if String === node node = `#@native.ownerDocument.createTextNode(node)` else node = Native.convert(node) end end `#@native.appendChild(node)` self end |
#==(other) ⇒ Boolean
Return true of the other element is the same underlying DOM node.
49 50 51 |
# File 'opal/browser/dom/node.rb', line 49 def ==(other) `#@native === #{Native.convert(other)}` end |
#>>(node) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'opal/browser/dom/node.rb', line 95 def >>(node) if Opal.respond_to? node, :each node.each { |n| self >> n } return self elsif Opal.respond_to? node, :to_dom node = node.to_dom(document) end unless native?(node) if String === node node = `#@native.ownerDocument.createTextNode(node)` else node = Native.convert(node) end end if `#@native.firstChild == null` `#@native.appendChild(node)` else `#@native.insertBefore(node, #@native.firstChild)` end self end |
#add_child(node = nil, &block) ⇒ Object
120 121 122 123 124 125 126 |
# File 'opal/browser/dom/node.rb', line 120 def add_child(node = nil, &block) unless node node = DOM(&block) end self << node end |
#add_next_sibling(node = nil, &block) ⇒ Object Also known as: after, next=
Add the passed node after this one.
When passing a String a text node will be created.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'opal/browser/dom/node.rb', line 133 def add_next_sibling(node = nil, &block) unless node node = DOM(&block) end node = node.to_dom(document) if Opal.respond_to? node, :to_dom unless native?(node) if String === node node = `#@native.ownerDocument.createTextNode(node)` else node = Native.convert(node) end end `#@native.parentNode.insertBefore(node, #@native.nextSibling)` end |
#add_previous_sibling(node = nil, &block) ⇒ Object Also known as: before, previous=
Add the passed node before this one.
When passing a String a text node will be created.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'opal/browser/dom/node.rb', line 155 def add_previous_sibling(node = nil, &block) unless node node = DOM(&block) end node = node.to_dom(document) if Opal.respond_to? node, :to_dom unless native?(node) if String === node node = `#@native.ownerDocument.createTextNode(node)` else node = Native.convert(node) end end `#@native.parentNode.insertBefore(node, #@native)` end |
#ancestors(expression = nil) ⇒ NodeSet
Get an array of ancestors.
Passing a selector will select the ancestors matching it.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'opal/browser/dom/node.rb', line 189 def ancestors(expression = nil) return NodeSet[] unless parent parents = [parent] while parent = parents.last.parent parents << parent end if Document === parents.last parents.pop end if expression parents.select! { |p| p =~ expression } end NodeSet.new(parents) end |
#append_to(node) ⇒ Object
Append the node to the passed one.
177 178 179 180 |
# File 'opal/browser/dom/node.rb', line 177 def append_to(node) node << self self end |
#attached? ⇒ Boolean
209 210 211 |
# File 'opal/browser/dom/node.rb', line 209 def attached? `#@native.isConnected` end |
#blank? ⇒ Boolean
254 255 256 |
# File 'opal/browser/dom/node.rb', line 254 def blank? raise NotImplementedError end |
#cdata? ⇒ Boolean
Return true if the node is a CDATA section.
259 260 261 |
# File 'opal/browser/dom/node.rb', line 259 def cdata? node_type == CDATA_SECTION_NODE end |
#clear ⇒ Object
Remove all the children of the node.
222 223 224 |
# File 'opal/browser/dom/node.rb', line 222 def clear children.remove end |
#comment? ⇒ Boolean
Return true if the node is a comment.
280 281 282 |
# File 'opal/browser/dom/node.rb', line 280 def comment? node_type == COMMENT_NODE end |
#content ⇒ Object Also known as: inner_text, text
229 230 231 |
# File 'opal/browser/dom/node.rb', line 229 def content `#@native.textContent` end |
#content=(value) ⇒ Object Also known as: inner_text=, text=
233 234 235 |
# File 'opal/browser/dom/node.rb', line 233 def content=(value) `#@native.textContent = #{value}` end |
#custom? ⇒ Boolean
Return true if the node is a custom element.
285 286 287 |
# File 'opal/browser/dom/node.rb', line 285 def custom? false end |
#document? ⇒ Boolean
Return true if the node is a document.
301 302 303 |
# File 'opal/browser/dom/node.rb', line 301 def document? node_type == DOCUMENT_NODE end |
#elem? ⇒ Boolean Also known as: element?
Return true if the node is an element.
306 307 308 |
# File 'opal/browser/dom/node.rb', line 306 def elem? node_type == ELEMENT_NODE end |
#fragment? ⇒ Boolean
Return true if the node is a document fragment.
327 328 329 |
# File 'opal/browser/dom/node.rb', line 327 def fragment? node_type == DOCUMENT_FRAGMENT_NODE end |
#initialize_copy(old) ⇒ Object
Initialize a new node after #dup
or #clone
.
This method is not to be called directly. Use Node#dup
or
Node#clone
.
This method creates a deep detached clone of a DOM subtree to be used in the same document. The new node will have all events detached.
60 61 62 |
# File 'opal/browser/dom/node.rb', line 60 def initialize_copy(old) set_native_reference `#{old.to_n}.cloneNode(true)` end |
#parse(text, options = {}) ⇒ Object
406 407 408 |
# File 'opal/browser/dom/node.rb', line 406 def parse(text, = {}) raise NotImplementedError end |
#path ⇒ Object
410 411 412 |
# File 'opal/browser/dom/node.rb', line 410 def path raise NotImplementedError end |
#prepend_to(node) ⇒ Object
Prepend the node to the passed one.
417 418 419 420 |
# File 'opal/browser/dom/node.rb', line 417 def prepend_to(node) node >> self self end |
#remove ⇒ Object
Remove the node from its parent.
216 217 218 219 |
# File 'opal/browser/dom/node.rb', line 216 def remove parent.remove_child(self) if parent self end |
#remove_child(node) ⇒ Object
Remove the given node from the children of this node.
445 446 447 448 |
# File 'opal/browser/dom/node.rb', line 445 def remove_child(node) `#@native.removeChild(#{Native.try_convert(node)})` self end |
#replace(node) ⇒ Node Also known as: replace_with
implement for NodeSet
Replace the node with the given one.
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'opal/browser/dom/node.rb', line 456 def replace(node) node = node.to_dom(document) if Opal.respond_to? node, :to_dom unless native?(node) if String === node node = `#@native.ownerDocument.createTextNode(node)` else node = Native.convert(node) end end `#@native.parentNode.replaceChild(node, #@native)` DOM(node) end |
#text? ⇒ Boolean
Return true if the node is a text node.
478 479 480 |
# File 'opal/browser/dom/node.rb', line 478 def text? node_type == TEXT_NODE end |
#traverse(&block) ⇒ Object
482 483 484 |
# File 'opal/browser/dom/node.rb', line 482 def traverse(&block) raise NotImplementedError end |