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

Element   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 99
rs 10
wmc 17

21 Methods

Rating   Name   Duplication   Size   Complexity  
A element? 0 3 1
A input_type() 0 3 1
A text=() 0 3 1
A text() 0 3 1
A attributes() 0 3 1
A class_names() 0 3 1
A []() 0 3 1
A add_class() 0 4 1
A Attributes.initialize() 0 3 1
A create() 0 3 1
A inner_html=() 0 3 1
A Attributes.remove() 0 3 1
A inner_dom=() 0 4 1
A value() 0 3 1
A document() 0 3 1
A Attributes.[]=() 0 3 1
A add_child() 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
    def element?
12
      true
13
    end
14
15
    def input_type
16
      `self.native.type`
17
    end
18
19
    def [](prop_name)
20
      `self.native[#{prop_name}]`
21
    end
22
23
    def add_class(name)
24
      `self.native.classList.add(name)`
25
      self
26
    end
27
28
    def class_names
29
      Array.new(`self.native.classList`).to_a
30
    end
31
32
    def attributes
33
      @attributes ||= Attributes.new(self)
34
    end
35
36
    def text
37
      `self.native.textContent`
38
    end
39
40
    def text=(text)
41
      `self.native.textContent = text`
42
    end
43
44
    def value
45
      `self.native.value`
46
    end
47
48
    def style(hash)
49
      hash.each do |key, value|
50
        `self.native.style[key] = value`
51
      end
52
    end
53
54
    def add_child(child)
55
      `self.native.appendChild(child.native)`
56
    end
57
58
    def inner_html
59
      `self.native.innerHTML`
60
    end
61
62
    def inner_html=(html)
63
      `self.native.innerHTML = html`
64
    end
65
66
    def inner_dom=(dom)
67
      clear
68
      self << dom
69
    end
70
71
    def document
72
      $document
73
    end
74
75
    def to_s
76
      "<#{`self.native.tagName`} class='#{self.class_names.join(' ')}' id='#{self['id']}'/>"
77
    end
78
79
    def self.create(tag)
80
      $document.create_element(tag)
81
    end
82
83
    class Attributes
84
      def initialize(element)
85
        @element = element
86
      end
87
88
      def [](name)
89
        @element.get_attribute(name)
90
      end
91
92
      def []=(name, value)
93
        @element.set_attribute(name, value)
94
      end
95
96
      def remove(name)
97
        @element.remove_attribute(name)
98
      end
99
    end
100
  end
101
end
102