11use super :: {
22 document:: Document ,
33 element:: Element ,
4- node:: { Node , NodeData , NodeId } ,
4+ node:: { NodeData , NodeId } ,
55 selectors:: { ParseError , Selectors } ,
66 Specificity ,
77} ;
8- use std:: iter:: { Enumerate , Skip } ;
98
109/// Compile selectors from a string and create an element iterator that yields elements matching these selectors.
1110#[ inline]
@@ -16,8 +15,7 @@ pub(crate) fn select<'a, 'b>(
1615 Selectors :: compile ( selectors) . map ( |selectors| Select {
1716 elements : Elements {
1817 document,
19- // Skip the dummy & document nodes
20- iter : document. nodes . iter ( ) . enumerate ( ) . skip ( 2 ) ,
18+ iter : document. elements . iter ( ) ,
2119 } ,
2220 selectors,
2321 } )
@@ -26,24 +24,21 @@ pub(crate) fn select<'a, 'b>(
2624/// An internal iterator that traverses a document.
2725struct Elements < ' a > {
2826 document : & ' a Document ,
29- iter : Skip < Enumerate < std:: slice:: Iter < ' a , Node > > > ,
27+ iter : std:: slice:: Iter < ' a , NodeId > ,
3028}
3129
3230impl < ' a > Iterator for Elements < ' a > {
3331 type Item = Element < ' a > ;
3432
3533 fn next ( & mut self ) -> Option < Self :: Item > {
36- // Loop until we either run out of nodes or find an element node
37- loop {
38- if let Some ( ( id, node) ) = self . iter . next ( ) {
39- // If the current node is an element node, return it, else continue with the loop
40- if let NodeData :: Element { element, .. } = & node. data {
41- return Some ( Element :: new ( self . document , NodeId :: new ( id) , element) ) ;
42- }
43- } else {
44- // No more elements in the document
45- return None ;
46- }
34+ if let Some ( element_id) = self . iter . next ( ) {
35+ let NodeData :: Element { element, .. } = & self . document [ * element_id] . data else {
36+ unreachable ! ( "Element ids always point to element nodes" )
37+ } ;
38+ Some ( Element :: new ( self . document , * element_id, element) )
39+ } else {
40+ // No more elements in the document
41+ None
4742 }
4843 }
4944}
0 commit comments