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

Element.value()   A

Complexity

Conditions 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 3

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
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