Skip to content

Commit 81ea581

Browse files
committed
Ensure all rcdom nodes are destroyed iterately.
1 parent dd6210a commit 81ea581

File tree

1 file changed

+4
-38
lines changed

1 file changed

+4
-38
lines changed

markup5ever/rcdom.rs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ pub struct Node {
109109
pub children: RefCell<Vec<Handle>>,
110110
/// Represents this node's data.
111111
pub data: NodeData,
112-
/// Flag to control whether to free any children on destruction.
113-
leak_children_on_drop: Cell<bool>,
114112
}
115113

116114
impl Node {
@@ -120,36 +118,16 @@ impl Node {
120118
data: data,
121119
parent: Cell::new(None),
122120
children: RefCell::new(Vec::new()),
123-
leak_children_on_drop: Cell::new(true),
124121
})
125122
}
126-
127-
/// Drop any child nodes remaining in this node at destruction.
128-
///
129-
/// RcDom's destructor automatically drops any nodes and children that are
130-
/// present in the document. This setting only affects nodes that are dropped
131-
/// by manipulating the tree before RcDom's destructor runs (such as manually
132-
/// removing children from a node after parsing is complete).
133-
///
134-
/// Unsafety: due to the representation of children, this can trigger
135-
/// stack overflow if dropping a node with a very deep tree of children.
136-
/// This is not a recommended configuration to use when interacting with
137-
/// arbitrary HTML content.
138-
pub unsafe fn free_child_nodes_on_drop(&self) {
139-
self.leak_children_on_drop.set(false);
140-
}
141123
}
142124

143125
impl Drop for Node {
144126
fn drop(&mut self) {
145-
if !self.children.borrow().is_empty() {
146-
if self.leak_children_on_drop.get() {
147-
warn!("Dropping node with children outside of RcDom's destructor. \
148-
Leaking memory for {} children.", self.children.borrow().len());
149-
for child in mem::replace(&mut *self.children.borrow_mut(), vec![]) {
150-
mem::forget(child);
151-
}
152-
}
127+
let mut nodes = mem::replace(&mut *self.children.borrow_mut(), vec![]);
128+
while let Some(node) = nodes.pop() {
129+
let children = mem::replace(&mut *node.children.borrow_mut(), vec![]);
130+
nodes.extend(children.into_iter());
153131
}
154132
}
155133
}
@@ -227,18 +205,6 @@ pub struct RcDom {
227205
pub quirks_mode: QuirksMode,
228206
}
229207

230-
impl Drop for RcDom {
231-
fn drop(&mut self) {
232-
// Ensure that node destructors execute linearly, rather
233-
// than recursing through a tree of arbitrary depth.
234-
let mut to_be_processed = vec![self.document.clone()];
235-
while let Some(node) = to_be_processed.pop() {
236-
to_be_processed.extend_from_slice(&*node.children.borrow());
237-
node.children.borrow_mut().clear();
238-
}
239-
}
240-
}
241-
242208
impl TreeSink for RcDom {
243209
type Output = Self;
244210
fn finish(self) -> Self {

0 commit comments

Comments
 (0)