Module: Hyalite::DOM::Node

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(node) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/hyalite/dom/node.rb', line 4

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



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

def <<(child)
  `self.native.appendChild(child.native)`
end

#==(other) ⇒ Object



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

def ==(other)
  `self.native === other.native`
end

#childrenObject



49
50
51
# File 'lib/hyalite/dom/node.rb', line 49

def children
  Collection.new `self.native.childNodes`
end

#clearObject



34
35
36
37
38
39
40
41
# File 'lib/hyalite/dom/node.rb', line 34

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

#document?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/hyalite/dom/node.rb', line 14

def document?
  false
end

#element?Boolean

Returns:

  • (Boolean)


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

def element?
  false
end

#next_siblingObject



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

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

#node_nameObject



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

def node_name
  `self.native.tagName`
end

#on(name, &block) ⇒ Object



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

def on(name, &block)
  callback = Proc.new{|event| block.call(Event.create(event))}
  `self.native.addEventListener(name, callback)`
end

#parentObject



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

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

#removeObject



53
54
55
# File 'lib/hyalite/dom/node.rb', line 53

def remove
  `self.native.remove()`
end

#text?Boolean

Returns:

  • (Boolean)


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

def text?
  false
end