Skip to content

Commit 80b315f

Browse files
committed
Avoid using a Rc<RefCell<_>> in HtmlRewriteController::handlers_dispatcher().
1 parent 53469c5 commit 80b315f

File tree

3 files changed

+11
-31
lines changed

3 files changed

+11
-31
lines changed

src/rewriter/rewrite_controller.rs

+8-25
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use crate::rewritable_units::{DocumentEnd, Token, TokenCaptureFlags};
55
use crate::selectors_vm::{AuxStartTagInfoRequest, ElementData, SelectorMatchingVm, VmError};
66
use crate::transform_stream::*;
77
use hashbrown::HashSet;
8-
use std::cell::RefCell;
9-
use std::rc::Rc;
108

119
#[derive(Default)]
1210
pub struct ElementDescriptor {
@@ -25,7 +23,7 @@ impl ElementData for ElementDescriptor {
2523
}
2624

2725
pub struct HtmlRewriteController<'h> {
28-
handlers_dispatcher: Rc<RefCell<ContentHandlersDispatcher<'h>>>,
26+
handlers_dispatcher: ContentHandlersDispatcher<'h>,
2927
selector_matching_vm: Option<SelectorMatchingVm<ElementDescriptor>>,
3028
}
3129

@@ -36,32 +34,22 @@ impl<'h> HtmlRewriteController<'h> {
3634
selector_matching_vm: Option<SelectorMatchingVm<ElementDescriptor>>,
3735
) -> Self {
3836
HtmlRewriteController {
39-
handlers_dispatcher: Rc::new(RefCell::new(handlers_dispatcher)),
37+
handlers_dispatcher,
4038
selector_matching_vm,
4139
}
4240
}
4341
}
4442

45-
// NOTE: it's a macro instead of an instance method, so it can be executed
46-
// when we hold a mutable reference for the selector matching VM.
47-
macro_rules! create_match_handler {
48-
($self:tt) => {{
49-
let handlers_dispatcher = Rc::clone(&$self.handlers_dispatcher);
50-
51-
move |m| handlers_dispatcher.borrow_mut().start_matching(m)
52-
}};
53-
}
54-
5543
impl<'h> HtmlRewriteController<'h> {
5644
#[inline]
5745
fn respond_to_aux_info_request(
5846
aux_info_req: AuxStartTagInfoRequest<ElementDescriptor, SelectorHandlersLocator>,
5947
) -> StartTagHandlingResult<Self> {
6048
Err(DispatcherError::InfoRequest(Box::new(
6149
move |this, aux_info| {
62-
let mut match_handler = create_match_handler!(this);
63-
6450
if let Some(ref mut vm) = this.selector_matching_vm {
51+
let mut match_handler = |m| this.handlers_dispatcher.start_matching(m);
52+
6553
aux_info_req(vm, aux_info, &mut match_handler)
6654
.map_err(RewritingError::MemoryLimitExceeded)?;
6755
}
@@ -73,7 +61,7 @@ impl<'h> HtmlRewriteController<'h> {
7361

7462
#[inline]
7563
fn get_capture_flags(&self) -> TokenCaptureFlags {
76-
self.handlers_dispatcher.borrow().get_token_capture_flags()
64+
self.handlers_dispatcher.get_token_capture_flags()
7765
}
7866
}
7967

@@ -90,7 +78,7 @@ impl TransformController for HtmlRewriteController<'_> {
9078
) -> StartTagHandlingResult<Self> {
9179
match self.selector_matching_vm {
9280
Some(ref mut vm) => {
93-
let mut match_handler = create_match_handler!(self);
81+
let mut match_handler = |m| self.handlers_dispatcher.start_matching(m);
9482

9583
match vm.exec_for_start_tag(local_name, ns, &mut match_handler) {
9684
Ok(_) => Ok(self.get_capture_flags()),
@@ -108,10 +96,8 @@ impl TransformController for HtmlRewriteController<'_> {
10896

10997
fn handle_end_tag(&mut self, local_name: LocalName) -> TokenCaptureFlags {
11098
if let Some(ref mut vm) = self.selector_matching_vm {
111-
let handlers_dispatcher = Rc::clone(&self.handlers_dispatcher);
112-
113-
vm.exec_for_end_tag(local_name, move |elem_desc| {
114-
handlers_dispatcher.borrow_mut().stop_matching(elem_desc);
99+
vm.exec_for_end_tag(local_name, |elem_desc| {
100+
self.handlers_dispatcher.stop_matching(elem_desc);
115101
});
116102
}
117103

@@ -126,14 +112,12 @@ impl TransformController for HtmlRewriteController<'_> {
126112
.and_then(SelectorMatchingVm::current_element_data_mut);
127113

128114
self.handlers_dispatcher
129-
.borrow_mut()
130115
.handle_token(token, current_element_data)
131116
.map_err(RewritingError::ContentHandlerError)
132117
}
133118

134119
fn handle_end(&mut self, document_end: &mut DocumentEnd) -> Result<(), RewritingError> {
135120
self.handlers_dispatcher
136-
.borrow_mut()
137121
.handle_end(document_end)
138122
.map_err(RewritingError::ContentHandlerError)
139123
}
@@ -142,7 +126,6 @@ impl TransformController for HtmlRewriteController<'_> {
142126
fn should_emit_content(&self) -> bool {
143127
!self
144128
.handlers_dispatcher
145-
.borrow()
146129
.has_matched_elements_with_removed_content()
147130
}
148131
}

src/rewriter/settings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl Default for MemorySettings {
418418
fn default() -> Self {
419419
MemorySettings {
420420
preallocated_parsing_buffer_size: 1024,
421-
max_allowed_memory_usage: std::usize::MAX,
421+
max_allowed_memory_usage: usize::MAX,
422422
}
423423
}
424424
}

tests/harness/suites/html5lib_tests/decoder.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,8 @@ impl<'a> Decoder<'a> {
9292
if m.0 != 0 {
9393
if c != ';' && self.entities == Entities::Attribute {
9494
if let Some(&c) = self.chars.peek() {
95-
match c {
96-
'A'..='Z' | 'a'..='z' | '0'..='9' | '=' => {
97-
continue;
98-
}
99-
_ => {}
95+
if matches!(c, 'A'..='Z' | 'a'..='z' | '0'..='9' | '=') {
96+
continue;
10097
}
10198
}
10299
}

0 commit comments

Comments
 (0)