Class | Nokogiri::XML::NodeSet
In: | lib/nokogiri/xml/node_set.rb ext/nokogiri/xml_sax_push_parser.c
Parent: | Object

xmlXPathFreeNodeSet() contains an implicit assumption that it is being called before any of its pointed-to nodes have been free()d. this assumption lies in the operation where it dereferences nodeTab pointers while searching for namespace nodes to free.

however, since Ruby‘s GC mechanism cannot guarantee the strict order in which ruby objects will be GC‘d, nodes may be garbage collected before a nodeset containing pointers to those nodes. (this is true regardless of how we declare dependencies between objects with rb_gc_mark().)

as a result, xmlXPathFreeNodeSet() will perform unsafe memory operations, and calling it would be evil.

on the bright side, though, Nokogiri‘s API currently does not cause namespace nodes to be included in node sets, ever.

armed with that fact, we examined xmlXPathFreeNodeSet() and related libxml code and determined that, within the Nokogiri abstraction, we will not leak memory if we simply free the node set‘s memory directly. that‘s only quasi-evil!

there‘s probably a lesson in here somewhere about intermingling, within a single array, structs with different memory-ownership semantics. or more generally, a lesson about building an API in C/C++ that does not contain assumptions about the strict order in which memory will be released. hey, that sounds like a great idea for a blog post! get to it!

"In Valgrind We Trust." seriously.

### Methods

% & - / == > [] add_class after at at_css at_xpath attr attribute before children css delete dup each empty? filter first include? index inner_html inner_text inspect last length new pop push remove_attr remove_class reverse search set shift slice text to_a to_html to_s to_xhtml to_xml unlink wrap xpath |

### Included Modules

Enumerable

### External Aliases

push | -> | <<
unlink | -> | remove
length | -> | size
to_a | -> | to_ary
| | -> | +

### Attributes

document | [RW] | The Document this NodeSet is associated with

### Public Class methods

new(document, list = []) {|self if block_given?| ...}

Create a NodeSet with `document` defaulting to `list`

### Public Instance methods

%(path, ns = document.root ? document.root.namespaces : {})

Alias for at

&(node_set)

Set Intersection — Returns a new NodeSet containing nodes common to the two NodeSets.

-(node_set)

Difference - returns a new NodeSet that is a copy of this NodeSet, removing each item that also appears in `node_set`

/(*paths)

Alias for search

==(other)

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 NodeSet

>(selector)

Search this NodeSet‘s nodes’ immediate children using CSS selector `selector`

[index] → Node or nil
[start, length] → NodeSet or nil
[range] → NodeSet or nil
slice(index) → Node or nil
slice(start, length) → NodeSet or nil
slice(range) → NodeSet or nil

Element reference - returns the node at `index`, or returns a NodeSet containing nodes starting at `start` and continuing for `length` elements, or returns a NodeSet containing nodes specified by `range`. Negative `indices` count backward from the end of the `node_set` (-1 is the last node). Returns nil if the `index` (or `start`) are out of range.

add_class(name)

Append the class attribute `name` to all Node objects in the NodeSet.

after(datum)

Insert `datum` after the last Node in this NodeSet

at(path, ns = document.root ? document.root.namespaces : {})

If path is a string, search this document for `path` returning the first Node. Otherwise, index in to the array with `path`.

at_css(*rules)

Search this NodeSet for the first occurrence of CSS `rules`. Equivalent to `css(rules).first` See NodeSet#css for more information.

at_xpath(*paths)

Search this NodeSet for the first occurrence of XPath `paths`. Equivalent to `xpath(paths).first` See NodeSet#xpath for more information.

attr(key, value = nil, &blk)

Set the attribute `key` to `value` or the return value of `blk` on all Node objects in the NodeSet.

attribute(key, value = nil, &blk)

Alias for attr

before(datum)

Insert `datum` before the first Node in this NodeSet

children()

Returns a new NodeSet containing all the children of all the nodes in the NodeSet

css(*paths)

Search this NodeSet for css `paths`

For more information see Nokogiri::XML::Node#css

delete(node)

Delete `node` from the Nodeset, if it is a member. Returns the deleted node if found, otherwise returns nil.

dup

Duplicate this node set

each() {|self[x]| ...}

Iterate over each node, yielding to `block`

empty?()

Is this NodeSet empty?

filter(expr)

Filter this list for nodes that match `expr`

first(n = nil)

Get the first element of the NodeSet.

include?(node)

Returns true if any member of node set equals `node`.

index(node)

Returns the index of the first node in self that is == to `node`. Returns nil if no match is found.

inner_html(*args)

Get the inner html of all contained Node objects

inner_text()

Get the inner text of all contained Node objects

inspect()

Return a nicely formated string representation

last()

Get the last element of the NodeSet.

length

Get the length of the node set

pop()

Removes the last element from set and returns it, or `nil` if the set is empty

push(node)

Append `node` to the NodeSet.

remove_attr(name)

Remove the attributed named `name` from all Node objects in the NodeSet

remove_class(name = nil)

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.

reverse()

Returns a new NodeSet containing all the nodes in the NodeSet in reverse order

search(*paths)

Search this document for `paths`

For more information see Nokogiri::XML::Node#css and Nokogiri::XML::Node#xpath

set(key, value = nil, &blk)

Alias for attr

shift()

Returns the first element of the NodeSet and removes it. Returns `nil` if the set is empty.

[index] → Node or nil
[start, length] → NodeSet or nil
[range] → NodeSet or nil
slice(index) → Node or nil
slice(start, length) → NodeSet or nil
slice(range) → NodeSet or nil

Element reference - returns the node at `index`, or returns a NodeSet containing nodes starting at `start` and continuing for `length` elements, or returns a NodeSet containing nodes specified by `range`. Negative `indices` count backward from the end of the `node_set` (-1 is the last node). Returns nil if the `index` (or `start`) are out of range.

text()

Alias for inner_text

to_a

Return this list as an Array

to_html(*args)

Convert this NodeSet to HTML

to_s()

Convert this NodeSet to a string.

to_xhtml(*args)

Convert this NodeSet to XHTML

to_xml(*args)

Convert this NodeSet to XML

unlink

Unlink this NodeSet and all Node objects it contains from their current context.

wrap(html, &blk)

Wrap this NodeSet with `html` or the results of the builder in `blk`

xpath(*paths)

Search this NodeSet for XPath `paths`

For more information see Nokogiri::XML::Node#xpath

|(node_set)

Returns a new set built by merging the set and the elements of the given set.

[Validate]