Skip to content

Commit

Permalink
dom/element/attributes: make the best case faster
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Feb 4, 2014
1 parent d62e010 commit 5e0a28d
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions opal/browser/dom/element/attributes.rb
@@ -1,16 +1,6 @@
module Browser; module DOM; class Element < Node

class Attributes
@@normalize = `{}`

if Browser.supports? 'Element.className'
`#@@normalize['class'] = 'className'`
end

if Browser.supports? 'Element.htmlFor'
`#@@normalize['for'] = 'htmlFor'`
end

attr_reader :namespace

def initialize(element, options)
Expand All @@ -19,23 +9,49 @@ def initialize(element, options)
@namespace = options[:namespace]
end

def [](name, options = {})
name = `#@@normalize[name] || name`

if namespace = options[:namespace] || @namespace
`#@native.getAttributeNS(#{namespace.to_s}, #{name.to_s}) || nil`
else
`#@native.getAttribute(#{name.to_s}) || nil`
if Browser.supports?('Element.className') || Browser.supports?('Element.htmlFor')
def [](name, options = {})
if name == :class && Browser.supports?('Element.className')
name = :className
elsif name == :for && Browser.supports?('Element.htmlFor')
name = :htmlFor
end

if namespace = options[:namespace] || @namespace
`#@native.getAttributeNS(#{namespace.to_s}, #{name.to_s}) || nil`
else
`#@native.getAttribute(#{name.to_s}) || nil`
end
end
end

def []=(name, value, options = {})
name = `#@@normalize[name] || name`
def []=(name, value, options = {})
if name == :class && Browser.supports?('Element.className')
name = :className
elsif name == :for && Browser.supports?('Element.htmlFor')
name = :htmlFor
end

if namespace = options[:namespace] || @namespace
`#@native.setAttributeNS(#{namespace.to_s}, #{name.to_s}, #{value})`
else
`#@native.setAttribute(#{name.to_s}, #{value.to_s})`
end
end
else
def [](name, options = {})
if namespace = options[:namespace] || @namespace
`#@native.getAttributeNS(#{namespace.to_s}, #{name.to_s}) || nil`
else
`#@native.getAttribute(#{name.to_s}) || nil`
end
end

if namespace = options[:namespace] || @namespace
`#@native.setAttributeNS(#{namespace.to_s}, #{name.to_s}, #{value})`
else
`#@native.setAttribute(#{name.to_s}, #{value.to_s})`
def []=(name, value, options = {})
if namespace = options[:namespace] || @namespace
`#@native.setAttributeNS(#{namespace.to_s}, #{name.to_s}, #{value})`
else
`#@native.setAttribute(#{name.to_s}, #{value.to_s})`
end
end
end

Expand Down

0 comments on commit 5e0a28d

Please sign in to comment.