Completed
Push — master ( 175dcd...faed65 )
by Yoh
01:23
created

Node.on()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
module Hyalite::DOM
2
  module Node
3
4
    def self.create(node)
5
      @classes ||= [nil, Element, nil, Text, nil, nil, nil, nil, nil, Document, nil, nil]
6
7
      if klass = @classes[`node.nodeType`]
0 ignored issues
show
Bug introduced by youchan
You have used an assignment in a condition. Did you perhaps intend to use == instead of =?
Loading history...
8
        klass.new(node)
9
      else
10
        raise ArgumentError, 'cannot instantiate a non derived Node object'
11
      end
12
    end
13
14
    def document?
15
      false
16
    end
17
18
    def element?
19
      false
20
    end
21
22
    def text?
23
      false
24
    end
25
26
    def node_name
27
      `self.native.tagName`
28
    end
29
30
    def <<(child)
31
      `self.native.appendChild(child.native)`
32
    end
33
34
    def clear
35
      %x(
36
        var len = self.native.childNodes.length;
37
        for (var i = 0; i < len; i++) {
38
          self.native.childNodes[0].remove();
39
        }
40
      )
41
    end
42
43
    def parent
44
      if parent = `self.native.parentNode`
0 ignored issues
show
Bug introduced by youchan
You have used an assignment in a condition. Did you perhaps intend to use == instead of =?
Loading history...
45
        Node.create(parent)
46
      end
47
    end
48
49
    def children
50
      Collection.new `self.native.childNodes`
51
    end
52
53
    def next_sibling
54
      sib = `self.native.nextSibling`
55
      Node.create(sib) if sib
56
    end
57
58
    def on(event, &block)
59
      `self.native.addEventListener(event, block)`
60
    end
61
  end
62
end
63