Skip to content

Commit

Permalink
dom/node_set: cleanup code and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Feb 1, 2014
1 parent 3ad0b16 commit afd43f6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion opal/browser/dom.rb
Expand Up @@ -46,7 +46,7 @@ def DOM(*args, &block)
if roots.length == 1
roots.first
else
Browser::DOM::NodeSet.new(document, roots)
Browser::DOM::NodeSet.new(roots)
end
else
what = args.shift
Expand Down
16 changes: 8 additions & 8 deletions opal/browser/dom/element.rb
Expand Up @@ -253,22 +253,22 @@ def at_xpath(*paths)
end

def search(*selectors)
NodeSet.new document, selectors.map {|selector|
NodeSet.new selectors.map {|selector|
xpath(selector).to_a.concat(css(selector).to_a)
}.flatten.uniq
end

if Browser.supports? 'Query.css'
def css(path)
NodeSet.new(document, Native::Array.new(`#@native.querySelectorAll(path)`))
NodeSet[Native::Array.new(`#@native.querySelectorAll(path)`)]
rescue
NodeSet.new(document)
NodeSet[]
end
elsif Browser.loaded? 'Sizzle'
def css(path)
NodeSet.new(document, `Sizzle(path, #@native)`)
NodeSet.new(`Sizzle(path, #@native)`)
rescue
NodeSet.new(document)
NodeSet[]
end
else
def css(selector)
Expand All @@ -282,13 +282,13 @@ def css(selector)
end

def xpath(path)
NodeSet.new(document, Native::Array.new(
NodeSet[Native::Array.new(
`(#@native.ownerDocument || #@native).evaluate(path,
#@native, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)`,
get: :snapshotItem,
length: :snapshotLength))
length: :snapshotLength)]
rescue
NodeSet.new(document)
NodeSet[]
end
else
def xpath(path)
Expand Down
4 changes: 2 additions & 2 deletions opal/browser/dom/mutation_observer.rb
Expand Up @@ -49,7 +49,7 @@ def added
[]
end

NodeSet.new($document, array)
NodeSet[array]
end

# @!attribute [r] removed
Expand All @@ -61,7 +61,7 @@ def removed
[]
end

NodeSet.new($document, array)
NodeSet[array]
end

# @!attribute [r] target
Expand Down
6 changes: 3 additions & 3 deletions opal/browser/dom/node.rb
Expand Up @@ -121,7 +121,7 @@ def append_to(node)
#
# @return [NodeSet]
def ancestors(expression = nil)
return NodeSet.new(document) unless parent
return NodeSet[] unless parent

parents = [parent]

Expand All @@ -137,7 +137,7 @@ def ancestors(expression = nil)
parents.select! { |p| p =~ expression }
end

NodeSet.new document, parents
NodeSet.new(parents)
end

alias before add_previous_sibling
Expand Down Expand Up @@ -198,7 +198,7 @@ def child
# @!attribute children
# @return [NodeSet] the children of the node
def children
NodeSet.new(document, Native::Array.new(`#@native.childNodes`))
NodeSet[Native::Array.new(`#@native.childNodes`)]
end

def children=(node)
Expand Down
42 changes: 21 additions & 21 deletions opal/browser/dom/node_set.rb
@@ -1,51 +1,45 @@
module Browser; module DOM

# Allows manipulation of a set of {Node}s.
class NodeSet
attr_reader :document

def initialize(document, list = [])
@document = document
@literal = []
def self.[](*nodes)
new(nodes.flatten.map { |x| DOM(Native.convert(x)) })
end

list.each {|el|
if NodeSet === el
@literal.concat(el.to_a)
else
@literal.push DOM(Native.convert(el))
end
}
def initialize(array)
@array = array
end

def respond_to_missing?(name)
@literal.respond_to?(name)
@array.respond_to?(name)
end

def method_missing(name, *args, &block)
unless @literal.respond_to? name
unless @array.respond_to? name
each {|el|
el.__send__(name, *args, &block)
}

return self
end

result = @literal.__send__ name, *args, &block
result = @array.__send__ name, *args, &block

if `result === #@literal`
if `result === #@array`
self
elsif Array === result
NodeSet.new(@document, result)
NodeSet.new(result)
else
result
end
end

def dup
NodeSet.new(document, to_ary.dup)
NodeSet.new(@array.dup)
end

def filter(expression)
NodeSet.new(document, @literal.select { |node| node =~ expression })
NodeSet.new(@array.select { |node| node =~ expression })
end

def after(node)
Expand All @@ -69,7 +63,7 @@ def before
end

def children
result = NodeSet.new(document)
result = NodeSet.new([])

each { |n| result.concat(n.children) }

Expand All @@ -84,8 +78,14 @@ def search(*what)
map { |n| n.search(*what) }.flatten.uniq
end

def to_a
@array
end

alias to_ary to_a

def inspect
"#<DOM::NodeSet: #{@literal.inspect[1 .. -2]}"
"#<DOM::NodeSet: #{@array.inspect[1 .. -2]}"
end
end

Expand Down

0 comments on commit afd43f6

Please sign in to comment.