Completed
Push — master ( e348e7...94d493 )
by Yoh
57s
created

Node.==()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
module Hyalite::DOM
2
  module Node
3
    include EventTarget
4
5
    def self.create(node)
6
      @classes ||= [nil, Element, nil, Text, nil, nil, nil, nil, nil, Document, nil, nil]
7
8
      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...
9
        klass.new(node)
10
      else
11
        raise ArgumentError, 'cannot instantiate a non derived Node object'
12
      end
13
    end
14
15
    def document?
16
      false
17
    end
18
19
    def element?
20
      false
21
    end
22
23
    def text?
24
      false
25
    end
26
27
    def node_name
28
      `self.native.tagName`
29
    end
30
31
    def <<(child)
32
      `self.native.appendChild(child.native)`
33
    end
34
35
    def clear
36
      %x(
37
        var len = self.native.childNodes.length;
38
        for (var i = 0; i < len; i++) {
39
          self.native.childNodes[0].remove();
40
        }
41
      )
42
    end
43
44
    def parent
45
      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...
46
        Node.create(parent)
47
      end
48
    end
49
50
    def children
51
      Collection.new `self.native.childNodes`
52
    end
53
54
    def remove
55
      `self.native.remove()`
56
    end
57
58
    def next_sibling
59
      sib = `self.native.nextSibling`
60
      Node.create(sib) if sib
61
    end
62
63
    def ==(other)
64
      `self.native === other.native`
65
    end
66
  end
67
end
68