Completed
Push — master ( 2c8d61...d12e3c )
by Yoh
48s
created

Node.remove()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
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
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
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 remove
54
      `self.native.remove()`
55
    end
56
57
    def next_sibling
58
      sib = `self.native.nextSibling`
59
      Node.create(sib) if sib
60
    end
61
62
    def on(name, &block)
63
      callback = Proc.new{|event| block.call(Event.create(event))}
64
      `self.native.addEventListener(name, callback)`
65
    end
66
67
    def ==(other)
68
      `self.native === other.native`
69
    end
70
  end
71
end
72