Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid computing the document order when there's only one node #125

Merged
merged 1 commit into from
Oct 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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