Skip to content

Commit aced0ab

Browse files
committed
dom/node_set: normalize various node searching methods
1 parent 15ed6cd commit aced0ab

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

opal/browser/dom/node_set.rb

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,47 @@ def method_missing(name, *args, &block)
3535
end
3636
end
3737

38+
# Get the first node matching the given CSS selectors.
39+
#
40+
# @param rules [Array<String>] the CSS selectors to match with
41+
#
42+
# @return [Node?]
43+
def at_css(*rules)
44+
each {|node|
45+
if node = node.at_css(*rules)
46+
return node
47+
end
48+
}
49+
50+
nil
51+
end
52+
53+
# Get the first node matching the given XPath.
54+
#
55+
# @param paths [Array<String>] the XPath to match with
56+
#
57+
# @return [Node?]
58+
def at_xpath(*paths)
59+
each {|node|
60+
if node = node.at_xpath(*paths)
61+
return node
62+
end
63+
}
64+
65+
nil
66+
end
67+
68+
# Query for children matching the given CSS selector.
69+
#
70+
# @param selector [String] the CSS selector
71+
#
72+
# @return [NodeSet]
73+
def css(path)
74+
NodeSet[@literal.map {|node|
75+
node.css(path)
76+
}]
77+
end
78+
3879
# Create another {NodeSet} with all the nodes that match the given
3980
# expression.
4081
#
@@ -47,7 +88,18 @@ def filter(expression)
4788

4889
# Search for multiple selectors
4990
def search(*what)
50-
map { |n| n.search(*what) }.flatten.uniq
91+
NodeSet[@literal.map { |node| node.search(*what) }]
92+
end
93+
94+
# Query for children matching the given XPath.
95+
#
96+
# @param path [String] the XPath
97+
#
98+
# @return [NodeSet]
99+
def xpath(path)
100+
NodeSet[@literal.map {|node|
101+
node.xpath(path)
102+
}]
51103
end
52104
end
53105

0 commit comments

Comments
 (0)