Skip to content

Commit

Permalink
Avoid computing the document order when there's only one node
Browse files Browse the repository at this point in the history
Partially addresses #121
  • Loading branch information
shepmaster committed Oct 31, 2018
1 parent 350c51e commit 5a4f6a3
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/nodeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,26 @@ impl<'d> Nodeset<'d> {
///
/// [document order]: https://www.w3.org/TR/xpath/#dt-document-order
pub fn document_order_first(&self) -> Option<Node<'d>> {
let doc = match self.nodes.iter().next() {
Some(n) => n.document(),
let node = match self.nodes.iter().next() {
Some(n) => n,
None => return None,
};

let order = DocOrder::new(doc);
if self.nodes.len() == 1 {
return Some(node.clone());
}

let order = DocOrder::new(node.document());

self.nodes.iter().min_by_key(|&&n| order.order_of(n)).cloned()
}

pub fn document_order(&self) -> Vec<Node<'d>> {
let mut nodes: Vec<_> = self.iter().collect();
if nodes.len() == 1 {
return nodes;
}

let doc = match nodes.first().map(Node::document) {
Some(doc) => doc,
None => return nodes,
Expand Down

0 comments on commit 5a4f6a3

Please sign in to comment.