Skip to content

Commit

Permalink
dom/node_set: normalize various node searching methods
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Feb 24, 2014
1 parent 15ed6cd commit aced0ab
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion opal/browser/dom/node_set.rb
Expand Up @@ -35,6 +35,47 @@ def method_missing(name, *args, &block)
end
end

# Get the first node matching the given CSS selectors.
#
# @param rules [Array<String>] the CSS selectors to match with
#
# @return [Node?]
def at_css(*rules)
each {|node|
if node = node.at_css(*rules)
return node
end
}

nil
end

# Get the first node matching the given XPath.
#
# @param paths [Array<String>] the XPath to match with
#
# @return [Node?]
def at_xpath(*paths)
each {|node|
if node = node.at_xpath(*paths)
return node
end
}

nil
end

# Query for children matching the given CSS selector.
#
# @param selector [String] the CSS selector
#
# @return [NodeSet]
def css(path)
NodeSet[@literal.map {|node|
node.css(path)
}]
end

# Create another {NodeSet} with all the nodes that match the given
# expression.
#
Expand All @@ -47,7 +88,18 @@ def filter(expression)

# Search for multiple selectors
def search(*what)
map { |n| n.search(*what) }.flatten.uniq
NodeSet[@literal.map { |node| node.search(*what) }]
end

# Query for children matching the given XPath.
#
# @param path [String] the XPath
#
# @return [NodeSet]
def xpath(path)
NodeSet[@literal.map {|node|
node.xpath(path)
}]
end
end

Expand Down

0 comments on commit aced0ab

Please sign in to comment.