moduleNokogirimoduleXML##### A NodeSet contains a list of Nokogiri::XML::Node objects. Typically# a NodeSet is return as a result of searching a Document via# Nokogiri::XML::Searchable#css or Nokogiri::XML::Searchable#xpathclassNodeSetincludeNokogiri::XML::SearchableincludeEnumerable# The Document this NodeSet is associated withattr_accessor:document# Create a NodeSet with +document+ defaulting to +list+definitializedocument,list=[]@document=documentdocument.decorate(self)list.each{|x|self<<x}yieldselfifblock_given?end#### Get the first element of the NodeSet.deffirstn=nilreturnself[0]unlessnlist=[]n.times{|i|list<<self[i]}listend#### Get the last element of the NodeSet.deflastself[-1]end#### Is this NodeSet empty?defempty?length==0end#### Returns the index of the first node in self that is == to +node+. Returns nil if no match is found.defindex(node)each_with_index{|member,j|returnjifmember==node}nilend#### Insert +datum+ before the first Node in this NodeSetdefbeforedatumfirst.beforedatumend#### Insert +datum+ after the last Node in this NodeSetdefafterdatumlast.afterdatumendalias:<<:pushalias:remove:unlink#### call-seq: css *rules, [namespace-bindings, custom-pseudo-class]## Search this node set for CSS +rules+. +rules+ must be one or more CSS# selectors. For example:## For more information see Nokogiri::XML::Searchable#cssdefcss*argsrules,handler,ns,_=extract_params(args)inject(NodeSet.new(document))do|set,node|set+=css_internalnode,rules,handler,nsendend#### call-seq: xpath *paths, [namespace-bindings, variable-bindings, custom-handler-class]## Search this node set for XPath +paths+. +paths+ must be one or more XPath# queries.## For more information see Nokogiri::XML::Searchable#xpathdefxpath*argspaths,handler,ns,binds=extract_params(args)inject(NodeSet.new(document))do|set,node|set+=node.xpath(*(paths+[ns,handler,binds].compact))endend#### Search this NodeSet's nodes' immediate children using CSS selector +selector+def>selectorns=document.root.namespacesxpathCSS.xpath_for(selector,:prefix=>"./",:ns=>ns).firstend#### call-seq: search *paths, [namespace-bindings, xpath-variable-bindings, custom-handler-class]## Search this object for +paths+, and return only the first# result. +paths+ must be one or more XPath or CSS queries.## See Searchable#search for more information.## Or, if passed an integer, index into the NodeSet:## node_set.at(3) # same as node_set[3]#defat*argsifargs.length==1&&args.first.is_a?(Numeric)returnself[args.first]endsuper(*args)endalias:%:at#### Filter this list for nodes that match +expr+deffilterexprfind_all{|node|node.matches?(expr)}end#### Append the class attribute +name+ to all Node objects in the NodeSet.defadd_classnameeachdo|el|classes=el['class'].to_s.split(/\s+/)el['class']=classes.push(name).uniq.join" "endselfend#### Remove the class attribute +name+ from all Node objects in the NodeSet.# If +name+ is nil, remove the class attribute from all Nodes in the# NodeSet.defremove_classname=nileachdo|el|ifnameclasses=el['class'].to_s.split(/\s+/)ifclasses.empty?el.delete'class'elseel['class']=(classes-[name]).uniq.join" "endelseel.delete"class"endendselfend#### Set the attribute +key+ to +value+ or the return value of +blk+# on all Node objects in the NodeSet.defattrkey,value=nil,&blkunlessHash===key||key&&(value||blk)returnfirst.attribute(key)endhash=key.is_a?(Hash)?key:{key=>value}hash.each{|k,v|each{|el|el[k]=v||blk[el]}}selfendalias:set:attralias:attribute:attr#### Remove the attributed named +name+ from all Node objects in the NodeSetdefremove_attrnameeach{|el|el.deletename}selfend#### Iterate over each node, yielding to +block+defeach(&block)0.upto(length-1)do|x|yieldself[x]endend#### Get the inner text of all contained Node objects## Note: This joins the text of all Node objects in the NodeSet:## doc = Nokogiri::XML('<xml><a><d>foo</d><d>bar</d></a></xml>')# doc.css('d').text # => "foobar"## Instead, if you want to return the text of all nodes in the NodeSet:## doc.css('d').map(&:text) # => ["foo", "bar"]## See Nokogiri::XML::Node#content for more information.definner_textcollect(&:inner_text).join('')endalias:text:inner_text#### Get the inner html of all contained Node objectsdefinner_html*argscollect{|j|j.inner_html(*args)}.join('')end#### Wrap this NodeSet with +html+ or the results of the builder in +blk+defwrap(html,&blk)eachdo|j|new_parent=document.parse(html).firstj.add_next_sibling(new_parent)new_parent.add_child(j)endselfend#### Convert this NodeSet to a string.defto_smap(&:to_s).joinend#### Convert this NodeSet to HTMLdefto_html*argsifNokogiri.jruby?options=args.first.is_a?(Hash)?args.shift:{}if!options[:save_with]options[:save_with]=Node::SaveOptions::NO_DECLARATION|Node::SaveOptions::NO_EMPTY_TAGS|Node::SaveOptions::AS_HTMLendargs.insert(0,options)endmap{|x|x.to_html(*args)}.joinend#### Convert this NodeSet to XHTMLdefto_xhtml*argsmap{|x|x.to_xhtml(*args)}.joinend#### Convert this NodeSet to XMLdefto_xml*argsmap{|x|x.to_xml(*args)}.joinendalias:size:lengthalias:to_ary:to_a#### Removes the last element from set and returns it, or +nil+ if# the set is emptydefpopreturnniliflength==0deletelastend#### Returns the first element of the NodeSet and removes it. Returns# +nil+ if the set is empty.defshiftreturnniliflength==0deletefirstend#### Equality -- Two NodeSets are equal if the contain the same number# of elements and if each element is equal to the corresponding# element in the other NodeSetdef==otherreturnfalseunlessother.is_a?(Nokogiri::XML::NodeSet)returnfalseunlesslength==other.lengtheach_with_indexdo|node,i|returnfalseunlessnode==other[i]endtrueend#### Returns a new NodeSet containing all the children of all the nodes in# the NodeSetdefchildreninject(NodeSet.new(document)){|set,node|set+=node.children}end#### Returns a new NodeSet containing all the nodes in the NodeSet# in reverse orderdefreversenode_set=NodeSet.new(document)(length-1).downto(0)do|x|node_set.pushself[x]endnode_setend#### Return a nicely formated string representationdefinspect"[#{map(&:inspect).join', '}]"endalias:+:|privatedefimplied_xpath_contexts# :nodoc:[".//","self::"]endendendend