Module: Hyalite::DOM::Node

Includes:
EventTarget
Included in:
Body, Document, Element, Text
Defined in:
lib/hyalite/dom/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventTarget

#on

Instance Attribute Details

#nativeObject (readonly)

Returns the value of attribute native.



5
6
7
# File 'lib/hyalite/dom/node.rb', line 5

def native
  @native
end

Class Method Details

.create(node) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/hyalite/dom/node.rb', line 7

def self.create(node)
  @classes ||= [nil, Element, nil, Text, nil, nil, nil, nil, nil, Document, nil, nil]

  if klass = @classes[`node.nodeType`]
    klass.new(node)
  else
    raise ArgumentError, 'cannot instantiate a non derived Node object'
  end
end

Instance Method Details

#<<(child) ⇒ Object



45
46
47
# File 'lib/hyalite/dom/node.rb', line 45

def <<(child)
  `#@native.appendChild(child.native)`
end

#==(other) ⇒ Object



77
78
79
# File 'lib/hyalite/dom/node.rb', line 77

def ==(other)
  `#@native === other.native`
end

#attr(name) ⇒ Object



29
30
31
# File 'lib/hyalite/dom/node.rb', line 29

def attr(name)
  `#@native[name] || #{nil}`
end

#childrenObject



64
65
66
# File 'lib/hyalite/dom/node.rb', line 64

def children
  Collection.new `#@native.childNodes`
end

#clearObject



49
50
51
52
53
54
55
56
# File 'lib/hyalite/dom/node.rb', line 49

def clear
  %x(
    var len = #@native.childNodes.length;
    for (var i = 0; i < len; i++) {
      #@native.childNodes[0].remove();
    }
  )
end

#data(name) ⇒ Object



33
34
35
# File 'lib/hyalite/dom/node.rb', line 33

def data(name)
  `#@native.dataset[#{name}] || #{nil}`
end

#document?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/hyalite/dom/node.rb', line 17

def document?
  false
end

#element?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/hyalite/dom/node.rb', line 21

def element?
  false
end

#insert_before(node) ⇒ Object



37
38
39
# File 'lib/hyalite/dom/node.rb', line 37

def insert_before(node)
  `#@native.parentNode.insertBefore(#{node.native}, #@native)`
end

#next_siblingObject



72
73
74
75
# File 'lib/hyalite/dom/node.rb', line 72

def next_sibling
  sib = `#@native.nextSibling`
  Node.create(sib) if sib
end

#node_nameObject



41
42
43
# File 'lib/hyalite/dom/node.rb', line 41

def node_name
  `#@native.tagName`
end

#parentObject



58
59
60
61
62
# File 'lib/hyalite/dom/node.rb', line 58

def parent
  if parent = `#@native.parentNode`
    Node.create(parent)
  end
end

#removeObject



68
69
70
# File 'lib/hyalite/dom/node.rb', line 68

def remove
  `#@native.remove()`
end

#text?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/hyalite/dom/node.rb', line 25

def text?
  false
end