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

Element   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 20
c 3
b 0
f 0
dl 0
loc 113
rs 10

24 Methods

Rating   Name   Duplication   Size   Complexity  
A text=() 0 3 1
A text() 0 3 1
A element? 0 3 1
A attributes() 0 3 1
A class_names() 0 3 1
A input_type() 0 3 1
A []() 0 3 1
A add_class() 0 4 1
A width() 0 3 1
A left() 0 3 1
A height() 0 3 1
A create() 0 3 1
A Attributes.initialize() 0 3 1
A inner_html=() 0 3 1
A Attributes.remove() 0 3 1
A inner_dom=() 0 4 1
A document() 0 3 1
A add_child() 0 3 1
A Attributes.[]=() 0 3 1
A top() 0 3 1
A inner_html() 0 3 1
A to_s() 0 3 1
A style() 0 5 1
A Attributes.[]() 0 3 1
1
module Hyalite::DOM
2
  class Element
3
    include Native
4
    include Node
5
6
    alias_native :set_attribute, :setAttribute
7
    alias_native :get_attribute, :getAttribute
8
    alias_native :remove_attribute, :removeAttribute
9
    alias_native :tag_name, :tagName
10
11
    native_accessor :value
12
13
    def element?
14
      true
15
    end
16
17
    def input_type
18
      `self.native.type`
19
    end
20
21
    def [](prop_name)
22
      `self.native[#{prop_name}]`
23
    end
24
25
    def add_class(name)
26
      `self.native.classList.add(name)`
27
      self
28
    end
29
30
    def class_names
31
      Array.new(`self.native.classList`).to_a
32
    end
33
34
    def attributes
35
      @attributes ||= Attributes.new(self)
36
    end
37
38
    def text
39
      `self.native.textContent`
40
    end
41
42
    def text=(text)
43
      `self.native.textContent = text`
44
    end
45
46
    def width
47
      `#@native.clientWidth`
48
    end
49
50
    def height
51
      `#@native.clientHeight`
52
    end
53
54
    def top
55
      `#@native.clientTop`
56
    end
57
58
    def left
59
      `#@native.clientLeft`
60
    end
61
62
    def style(hash)
63
      hash.each do |key, value|
64
        `self.native.style[key] = value`
65
      end
66
    end
67
68
    def add_child(child)
69
      `self.native.appendChild(child.native)`
70
    end
71
72
    def inner_html
73
      `self.native.innerHTML`
74
    end
75
76
    def inner_html=(html)
77
      `self.native.innerHTML = html`
78
    end
79
80
    def inner_dom=(dom)
81
      clear
82
      self << dom
83
    end
84
85
    def document
86
      $document
87
    end
88
89
    def to_s
90
      "<#{`self.native.tagName`} class='#{self.class_names.join(' ')}' id='#{self['id']}'/>"
91
    end
92
93
    def self.create(tag)
94
      $document.create_element(tag)
95
    end
96
97
    class Attributes
98
      def initialize(element)
99
        @element = element
100
      end
101
102
      def [](name)
103
        @element.get_attribute(name)
104
      end
105
106
      def []=(name, value)
107
        @element.set_attribute(name, value)
108
      end
109
110
      def remove(name)
111
        @element.remove_attribute(name)
112
      end
113
    end
114
  end
115
end
116