Skip to content

Commit 6ddb0ac

Browse files
authored
Merge pull request #136 from adamreichold/fix-nth-index-cache-lifecycle
Stop re-using NthIndexCache to avoid ABA problem
2 parents ab207c9 + 7fdac0a commit 6ddb0ac

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/selector.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! CSS selectors.
22
3-
use std::cell::RefCell;
43
use std::convert::TryFrom;
54
use std::fmt;
65

@@ -10,7 +9,6 @@ use html5ever::{LocalName, Namespace};
109
use selectors::{
1110
matching,
1211
parser::{self, ParseRelative, SelectorParseErrorKind},
13-
NthIndexCache,
1412
};
1513

1614
use crate::error::SelectorErrorKind;
@@ -46,25 +44,19 @@ impl Selector {
4644
/// The optional `scope` argument is used to specify which element has `:scope` pseudo-class.
4745
/// When it is `None`, `:scope` will match the root element.
4846
pub fn matches_with_scope(&self, element: &ElementRef, scope: Option<ElementRef>) -> bool {
49-
thread_local! {
50-
static NTH_INDEX_CACHE: RefCell<NthIndexCache> = Default::default();
51-
}
52-
53-
NTH_INDEX_CACHE.with(|nth_index_cache| {
54-
let mut nth_index_cache = nth_index_cache.borrow_mut();
55-
let mut context = matching::MatchingContext::new(
56-
matching::MatchingMode::Normal,
57-
None,
58-
&mut nth_index_cache,
59-
matching::QuirksMode::NoQuirks,
60-
matching::NeedsSelectorFlags::No,
61-
matching::IgnoreNthChildForInvalidation::No,
62-
);
63-
context.scope_element = scope.map(|x| selectors::Element::opaque(&x));
64-
self.selectors
65-
.iter()
66-
.any(|s| matching::matches_selector(s, 0, None, element, &mut context))
67-
})
47+
let mut nth_index_cache = Default::default();
48+
let mut context = matching::MatchingContext::new(
49+
matching::MatchingMode::Normal,
50+
None,
51+
&mut nth_index_cache,
52+
matching::QuirksMode::NoQuirks,
53+
matching::NeedsSelectorFlags::No,
54+
matching::IgnoreNthChildForInvalidation::No,
55+
);
56+
context.scope_element = scope.map(|x| selectors::Element::opaque(&x));
57+
self.selectors
58+
.iter()
59+
.any(|s| matching::matches_selector(s, 0, None, element, &mut context))
6860
}
6961
}
7062

0 commit comments

Comments
 (0)