module Nokogiri::Decorators::Hpricot::NodeSet
def filter(rule)
node_set.filter('a#link2') # selects nodes from the set with child node
node_set.filter('.ohmy') # selects nodes from the set with class "ohmy"
example:
Note that positional rules (like :nth()) aren't currently supported.
Select nodes matching the supplied rule.
def filter(rule) filter_transformer( lambda {|j| j}, rule ) # identity transformer end
def filter_transformer(transformer, rule) # :nodoc:
def filter_transformer(transformer, rule) # :nodoc: sub_set = XML::NodeSet.new(document) document.decorate(sub_set) if rule.is_a?(XML::Node) each { |node| sub_set << node if transformer.call(node == rule) } return sub_set end ctx = CSS.parse(rule.to_s) visitor = CSS::XPathVisitor.new visitor.extend(Hpricot::XPathVisitor) each do |node| if transformer.call(node.at(".//self::" + visitor.accept(ctx.first))) sub_set << node end end sub_set end
def not(rule)
node_set.not(node_to_exclude) # selects all nodes EXCEPT node_to_exclude
present):
rule to remove that object from the node set (if it is
Also note that you can pass a XML::Node object instead of a
See filter for examples.
Note that positional rules (like :nth()) aren't currently supported.
The complement to filter, select nodes not matching the supplied rule.
def not(rule) filter_transformer( lambda {|j| !j}, rule ) # negation transformer end