Completed
Push — master ( 0214c0...4b005a )
by Yoh
45s
created

Element.value()   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
  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 style(hash)
47
      hash.each do |key, value|
48
        `self.native.style[key] = value`
49
      end
50
    end
51
52
    def add_child(child)
53
      `self.native.appendChild(child.native)`
54
    end
55
56
    def inner_html
57
      `self.native.innerHTML`
58
    end
59
60
    def inner_html=(html)
61
      `self.native.innerHTML = html`
62
    end
63
64
    def inner_dom=(dom)
65
      clear
66
      self << dom
67
    end
68
69
    def document
70
      $document
71
    end
72
73
    def to_s
74
      "<#{`self.native.tagName`} class='#{self.class_names.join(' ')}' id='#{self['id']}'/>"
75
    end
76
77
    def self.create(tag)
78
      $document.create_element(tag)
79
    end
80
81
    class Attributes
82
      def initialize(element)
83
        @element = element
84
      end
85
86
      def [](name)
87
        @element.get_attribute(name)
88
      end
89
90
      def []=(name, value)
91
        @element.set_attribute(name, value)
92
      end
93
94
      def remove(name)
95
        @element.remove_attribute(name)
96
      end
97
    end
98
  end
99
end
100