1
1
module Browser ; module DOM ; class Element < Node
2
2
3
3
class Attributes
4
- @@normalize = `{}`
5
-
6
- if Browser . supports? 'Element.className'
7
- `#@@normalize ['class'] = 'className'`
8
- end
9
-
10
- if Browser . supports? 'Element.htmlFor'
11
- `#@@normalize ['for'] = 'htmlFor'`
12
- end
13
-
14
4
attr_reader :namespace
15
5
16
6
def initialize ( element , options )
@@ -19,23 +9,49 @@ def initialize(element, options)
19
9
@namespace = options [ :namespace ]
20
10
end
21
11
22
- def []( name , options = { } )
23
- name = `#@@normalize [name] || name`
24
-
25
- if namespace = options [ :namespace ] || @namespace
26
- `#@native .getAttributeNS(#{ namespace . to_s } , #{ name . to_s } ) || nil`
27
- else
28
- `#@native .getAttribute(#{ name . to_s } ) || nil`
12
+ if Browser . supports? ( 'Element.className' ) || Browser . supports? ( 'Element.htmlFor' )
13
+ def []( name , options = { } )
14
+ if name == :class && Browser . supports? ( 'Element.className' )
15
+ name = :className
16
+ elsif name == :for && Browser . supports? ( 'Element.htmlFor' )
17
+ name = :htmlFor
18
+ end
19
+
20
+ if namespace = options [ :namespace ] || @namespace
21
+ `#@native .getAttributeNS(#{ namespace . to_s } , #{ name . to_s } ) || nil`
22
+ else
23
+ `#@native .getAttribute(#{ name . to_s } ) || nil`
24
+ end
29
25
end
30
- end
31
26
32
- def []=( name , value , options = { } )
33
- name = `#@@normalize [name] || name`
27
+ def []=( name , value , options = { } )
28
+ if name == :class && Browser . supports? ( 'Element.className' )
29
+ name = :className
30
+ elsif name == :for && Browser . supports? ( 'Element.htmlFor' )
31
+ name = :htmlFor
32
+ end
33
+
34
+ if namespace = options [ :namespace ] || @namespace
35
+ `#@native .setAttributeNS(#{ namespace . to_s } , #{ name . to_s } , #{ value } )`
36
+ else
37
+ `#@native .setAttribute(#{ name . to_s } , #{ value . to_s } )`
38
+ end
39
+ end
40
+ else
41
+ def []( name , options = { } )
42
+ if namespace = options [ :namespace ] || @namespace
43
+ `#@native .getAttributeNS(#{ namespace . to_s } , #{ name . to_s } ) || nil`
44
+ else
45
+ `#@native .getAttribute(#{ name . to_s } ) || nil`
46
+ end
47
+ end
34
48
35
- if namespace = options [ :namespace ] || @namespace
36
- `#@native .setAttributeNS(#{ namespace . to_s } , #{ name . to_s } , #{ value } )`
37
- else
38
- `#@native .setAttribute(#{ name . to_s } , #{ value . to_s } )`
49
+ def []=( name , value , options = { } )
50
+ if namespace = options [ :namespace ] || @namespace
51
+ `#@native .setAttributeNS(#{ namespace . to_s } , #{ name . to_s } , #{ value } )`
52
+ else
53
+ `#@native .setAttribute(#{ name . to_s } , #{ value . to_s } )`
54
+ end
39
55
end
40
56
end
41
57
0 commit comments