Class: Browser::DOM::Node

Inherits:
Object show all
Includes:
NativeCachedWrapper
Defined in:
opal/browser/dom/node.rb

Overview

Abstract class for all DOM node types.

Direct Known Subclasses

CharacterData, Element

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NativeCachedWrapper

#restricted?, #set_native_reference

Instance Attribute Details

#childNode? (readonly)

Returns the first child of the node.

Returns:

  • (Node?)

    the first child of the node


250
251
252
# File 'opal/browser/dom/node.rb', line 250

def child
  children.first
end

#childrenNodeSet

Returns the children of the node.

Returns:

  • (NodeSet)

    the children of the node


256
257
258
# File 'opal/browser/dom/node.rb', line 256

def children
  NodeSet[Native::Array.new(`#@native.childNodes`)]
end

#documentDocument?

Returns the document the node is attached to.

Returns:

  • (Document?)

    the document the node is attached to


271
272
273
# File 'opal/browser/dom/node.rb', line 271

def document
  DOM(`#@native.ownerDocument`) if defined?(`#@native.ownerDocument`)
end

#element_childrenNodeSet (readonly) Also known as: elements

Returns all the children which are elements.

Returns:

  • (NodeSet)

    all the children which are elements


294
295
296
# File 'opal/browser/dom/node.rb', line 294

def element_children
  children.select(&:element?)
end

#first_element_childElement? (readonly)

Returns the first element child.

Returns:

  • (Element?)

    the first element child


302
303
304
# File 'opal/browser/dom/node.rb', line 302

def first_element_child
  element_children.first
end

#inner_htmlString

Returns the inner HTML of the node.

Returns:

  • (String)

    the inner HTML of the node


313
314
315
# File 'opal/browser/dom/node.rb', line 313

def inner_html
  `#@native.innerHTML`
end

#last_element_childElement? (readonly)

Returns the last element child.

Returns:

  • (Element?)

    the last element child


326
327
328
# File 'opal/browser/dom/node.rb', line 326

def last_element_child
  element_children.last
end

#nameString Also known as: node_name

Returns the name of the node.

Returns:

  • (String)

    the name of the node


332
333
334
# File 'opal/browser/dom/node.rb', line 332

def name
  `#@native.nodeName || nil`
end

#namespaceString (readonly)

Returns the namespace of the node.

Returns:

  • (String)

    the namespace of the node


342
343
344
# File 'opal/browser/dom/node.rb', line 342

def namespace
  `#@native.namespaceURI || nil`
end

#nextNode? Also known as: next_sibling

Returns the next sibling of the node.

Returns:

  • (Node?)

    the next sibling of the node


348
349
350
# File 'opal/browser/dom/node.rb', line 348

def next
  DOM(`#@native.nextSibling`) if `#@native.nextSibling != null`
end

#next_elementElement? (readonly)

Returns the next element sibling of the node.

Returns:

  • (Element?)

    the next element sibling of the node


356
357
358
359
360
361
362
363
364
# File 'opal/browser/dom/node.rb', line 356

def next_element
  current = self.next

  while current && !current.element?
    current = current.next
  end

  current
end

#node_typeSymbol (readonly) Also known as: type

Returns the type of the node.

Returns:

  • (Symbol)

    the type of the node


374
375
376
# File 'opal/browser/dom/node.rb', line 374

def node_type
  `#@native.nodeType`
end

#parentElement?

Returns the parent of the node.

Returns:

  • (Element?)

    the parent of the node


380
381
382
# File 'opal/browser/dom/node.rb', line 380

def parent
  DOM(`#@native.parentNode`) if `#@native.parentNode != null`
end

#previousNode? Also known as: previous_sibling

Returns the previous sibling of the node.

Returns:

  • (Node?)

    the previous sibling of the node


406
407
408
# File 'opal/browser/dom/node.rb', line 406

def previous
  DOM(`#@native.previousSibling`) if `#@native.previousSibling != null`
end

#previous_elementElement? (readonly)

Returns the previous element sibling of the node.

Returns:

  • (Element?)

    the previous element sibling of the node


414
415
416
417
418
419
420
421
422
# File 'opal/browser/dom/node.rb', line 414

def previous_element
  current = self.previous

  while current && !current.element?
    current = current.previous
  end

  current
end

#valueString

Returns the value of the node.

Returns:

  • (String)

    the value of the node


470
471
472
# File 'opal/browser/dom/node.rb', line 470

def value
  `#@native.nodeValue || nil`
end

Class Method Details

.new(value) ⇒ Node

Wrap a native DOM node.

Parameters:

  • value (native)

    the native DOM node

Returns:


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.

Parameters:

  • node (String, Node, #each, #to_n)

    the node to append

Returns:

  • (self)

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'opal/browser/dom/node.rb', line 69

def <<(node)
  if Opal.respond_to? node, :each
    node.each { |n| self << n }
    return self
  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.

Returns:

  • (Boolean)

44
45
46
# File 'opal/browser/dom/node.rb', line 44

def ==(other)
  `#@native === #{Native.convert(other)}`
end

#>>(node) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'opal/browser/dom/node.rb', line 88

def >>(node)
  if Opal.respond_to? node, :each
    node.each { |n| self >> n }
    return self
  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


111
112
113
114
115
116
117
# File 'opal/browser/dom/node.rb', line 111

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.

Parameters:

  • node (String, Node, #to_n) (defaults to: nil)

    the node to add


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'opal/browser/dom/node.rb', line 124

def add_next_sibling(node = nil, &block)
  unless node
    node = DOM(&block)
  end

  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.

Parameters:

  • node (String, Node, #to_n) (defaults to: nil)

    the node to add


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'opal/browser/dom/node.rb', line 145

def add_previous_sibling(node = nil, &block)
  unless node
    node = DOM(&block)
  end

  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.

Parameters:

  • expression (String) (defaults to: nil)

    the selector to use as filter

Returns:


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'opal/browser/dom/node.rb', line 178

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.

Parameters:

  • node (Node)

    the node to append to


166
167
168
169
# File 'opal/browser/dom/node.rb', line 166

def append_to(node)
  node << self
  self
end

#blank?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

239
240
241
# File 'opal/browser/dom/node.rb', line 239

def blank?
  raise NotImplementedError
end

#cdata?Boolean

Return true if the node is a CDATA section.

Returns:

  • (Boolean)

244
245
246
# File 'opal/browser/dom/node.rb', line 244

def cdata?
  node_type == CDATA_SECTION_NODE
end

#clearObject

Remove all the children of the node.


207
208
209
# File 'opal/browser/dom/node.rb', line 207

def clear
  children.remove
end

#comment?Boolean

Return true if the node is a comment.

Returns:

  • (Boolean)

265
266
267
# File 'opal/browser/dom/node.rb', line 265

def comment?
  node_type == COMMENT_NODE
end

#contentObject Also known as: inner_text, text

Raises:

  • (NotImplementedError)

214
215
216
# File 'opal/browser/dom/node.rb', line 214

def content
  `#@native.textContent`
end

#content=(value) ⇒ Object Also known as: inner_text=, text=

Raises:

  • (NotImplementedError)

218
219
220
# File 'opal/browser/dom/node.rb', line 218

def content=(value)
  `#@native.textContent = #{value}`
end

#document?Boolean

Return true if the node is a document.

Returns:

  • (Boolean)

281
282
283
# File 'opal/browser/dom/node.rb', line 281

def document?
  node_type == DOCUMENT_NODE
end

#elem?Boolean Also known as: element?

Return true if the node is an element.

Returns:

  • (Boolean)

286
287
288
# File 'opal/browser/dom/node.rb', line 286

def elem?
  node_type == ELEMENT_NODE
end

#fragment?Boolean

Return true if the node is a document fragment.

Returns:

  • (Boolean)

307
308
309
# File 'opal/browser/dom/node.rb', line 307

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.


55
56
57
# File 'opal/browser/dom/node.rb', line 55

def initialize_copy(old)
  set_native_reference `#{old.to_n}.cloneNode(true)`
end

#parse(text, options = {}) ⇒ Object

Raises:

  • (NotImplementedError)

388
389
390
# File 'opal/browser/dom/node.rb', line 388

def parse(text, options = {})
  raise NotImplementedError
end

#pathObject

Raises:

  • (NotImplementedError)

392
393
394
# File 'opal/browser/dom/node.rb', line 392

def path
  raise NotImplementedError
end

#prepend_to(node) ⇒ Object

Prepend the node to the passed one.

Parameters:

  • node (Node)

    the node to prepend to


399
400
401
402
# File 'opal/browser/dom/node.rb', line 399

def prepend_to(node)
  node >> self
  self
end

#removeObject

Remove the node from its parent.


201
202
203
204
# File 'opal/browser/dom/node.rb', line 201

def remove
  parent.remove_child(self) if parent
  self
end

#remove_child(node) ⇒ Object

Remove the given node from the children of this node.


427
428
429
430
# File 'opal/browser/dom/node.rb', line 427

def remove_child(node)
  `#@native.removeChild(#{Native.try_convert(node)})`
  self
end

#replace(node) ⇒ Node Also known as: replace_with

TODO:

implement for NodeSet

Replace the node with the given one.

Parameters:

  • node (Node)

    the node to replace with

Returns:

  • (Node)

    the passed node


438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'opal/browser/dom/node.rb', line 438

def replace(node)
  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.

Returns:

  • (Boolean)

458
459
460
# File 'opal/browser/dom/node.rb', line 458

def text?
  node_type == TEXT_NODE
end

#traverse(&block) ⇒ Object

Raises:

  • (NotImplementedError)

462
463
464
# File 'opal/browser/dom/node.rb', line 462

def traverse(&block)
  raise NotImplementedError
end