Completed
Push — master ( 175dcd...faed65 )
by Yoh
01:23
created

Element.[]()   A

Complexity

Conditions 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 3

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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`
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 style(hash)
45
      hash.each do |key, value|
46
        `self.native.style[key] = value`
47
      end
48
    end
49
50
    def inner_html
51
      `self.native.innerHTML`
52
    end
53
54
    def inner_html=(html)
55
      `self.native.innerHTML = html`
56
    end
57
58
    def inner_dom=(dom)
59
      clear
60
      self << dom
61
    end
62
63
    def document
64
      $document
65
    end
66
67
    def to_s
68
      `self.native.toString()`
69
    end
70
71
    def self.create(tag)
72
      $document.create_element(tag)
73
    end
74
75
    class Attributes
76
      def initialize(element)
77
        @element = element
78
      end
79
80
      def [](name)
81
        @element.get_attribute(name)
82
      end
83
84
      def []=(name, value)
85
        @element.set_attribute(name, value)
86
      end
87
88
      def remove(name)
89
        @element.remove_attribute(name)
90
      end
91
    end
92
  end
93
end
94