commonmarker 2.9.0 → 2.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +83 -40
- data/README.md +35 -17
- data/ext/commonmarker/Cargo.toml +3 -4
- data/ext/commonmarker/src/lib.rs +12 -2
- data/ext/commonmarker/src/nesting.rs +71 -0
- data/ext/commonmarker/src/node.rs +178 -87
- data/ext/commonmarker/src/options.rs +0 -4
- data/ext/commonmarker/src/plugins/syntax_highlighting.rs +9 -5
- data/ext/commonmarker/src/rctree.rs +841 -0
- data/lib/commonmarker/config.rb +3 -3
- data/lib/commonmarker/errors.rb +7 -0
- data/lib/commonmarker/node/inspect.rb +2 -4
- data/lib/commonmarker/node.rb +29 -4
- data/lib/commonmarker/version.rb +1 -1
- data/lib/commonmarker.rb +1 -0
- metadata +5 -30
|
@@ -0,0 +1,841 @@
|
|
|
1
|
+
//! A reference-counted tree, vendored from the `rctree` crate, version 0.6.0
|
|
2
|
+
//! (<https://github.com/RazrFalcon/rctree>), which is MIT licensed; the license
|
|
3
|
+
//! text follows below.
|
|
4
|
+
//!
|
|
5
|
+
//! The one substantive change from upstream is `Drop for NodeData`. Upstream's
|
|
6
|
+
//! version still recursed once per nesting level whenever the nested node was
|
|
7
|
+
//! not a first child (nested emphasis, for example), so freeing a deep tree
|
|
8
|
+
//! could overflow the native stack long after parsing it had succeeded. See
|
|
9
|
+
//! GHSA-c4fj-h48v-9fxp. The only other edits are two explicit `'_` lifetimes on
|
|
10
|
+
//! `borrow` and `borrow_mut`, which newer compilers lint on.
|
|
11
|
+
//!
|
|
12
|
+
//! Every departure from upstream is marked inline with a `commonmarker:`
|
|
13
|
+
//! comment that links to the corresponding upstream lines, so grepping for
|
|
14
|
+
//! that word finds them all. Upstream source at the vendored commit:
|
|
15
|
+
//! <https://github.com/RazrFalcon/rctree/blob/b904373a48d4d97cf9f17f7e96d7136d984f1aa7/src/lib.rs>
|
|
16
|
+
//!
|
|
17
|
+
//! The MIT License (MIT)
|
|
18
|
+
//!
|
|
19
|
+
//! Copyright (c) 2018 Simon Sapin
|
|
20
|
+
//! Copyright (c) 2018 Yevhenii Reizner
|
|
21
|
+
//!
|
|
22
|
+
//! Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
23
|
+
//! of this software and associated documentation files (the "Software"), to deal
|
|
24
|
+
//! in the Software without restriction, including without limitation the rights
|
|
25
|
+
//! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
26
|
+
//! copies of the Software, and to permit persons to whom the Software is
|
|
27
|
+
//! furnished to do so, subject to the following conditions:
|
|
28
|
+
//!
|
|
29
|
+
//! The above copyright notice and this permission notice shall be included in all
|
|
30
|
+
//! copies or substantial portions of the Software.
|
|
31
|
+
//!
|
|
32
|
+
//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
33
|
+
//! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
34
|
+
//! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
35
|
+
//! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
36
|
+
//! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
37
|
+
//! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
38
|
+
//! SOFTWARE.
|
|
39
|
+
|
|
40
|
+
/*!
|
|
41
|
+
|
|
42
|
+
*rctree* is a "DOM-like" tree implemented using reference counting.
|
|
43
|
+
|
|
44
|
+
"DOM-like" here means that data structures can be used to represent
|
|
45
|
+
the parsed content of an HTML or XML document,
|
|
46
|
+
like [*the* DOM](https://dom.spec.whatwg.org/) does,
|
|
47
|
+
but don't necessarily have the exact same API as the DOM.
|
|
48
|
+
That is:
|
|
49
|
+
|
|
50
|
+
* A tree is made up of nodes.
|
|
51
|
+
* Each node has zero or more *child* nodes, which are ordered.
|
|
52
|
+
* Each node has a no more than one *parent*, the node that it is a *child* of.
|
|
53
|
+
* A node without a *parent* is called a *root*.
|
|
54
|
+
* As a consequence, each node may also have *siblings*: its *parent*'s other *children*, if any.
|
|
55
|
+
* From any given node, access to its
|
|
56
|
+
parent, previous sibling, next sibling, first child, and last child (if any)
|
|
57
|
+
can take no more than *O(1)* time.
|
|
58
|
+
* Each node also has data associated to it,
|
|
59
|
+
which for the purpose of this project is purely generic.
|
|
60
|
+
For an HTML document, the data would be either the text of a text node,
|
|
61
|
+
or the name and attributes of an element node.
|
|
62
|
+
* The tree is mutable:
|
|
63
|
+
nodes (with their sub-trees) can be inserted or removed anywhere in the tree.
|
|
64
|
+
|
|
65
|
+
The lifetime of nodes is managed through *reference counting*.
|
|
66
|
+
To avoid reference cycles which would cause memory leaks, the tree is *asymmetric*:
|
|
67
|
+
each node holds optional *strong references* to its next sibling and first child,
|
|
68
|
+
but only optional *weak references* to its parent, previous sibling, and last child.
|
|
69
|
+
|
|
70
|
+
Nodes are destroyed as soon as there is no strong reference left to them.
|
|
71
|
+
The structure is such that holding a reference to the root
|
|
72
|
+
is sufficient to keep the entire tree alive.
|
|
73
|
+
However, if for example the only reference that exists from outside the tree
|
|
74
|
+
is one that you use to traverse it,
|
|
75
|
+
you will not be able to go back "up" the tree to ancestors and previous siblings after going "down",
|
|
76
|
+
as those nodes will have been destroyed.
|
|
77
|
+
|
|
78
|
+
Weak references to destroyed nodes are treated as if they were not set at all.
|
|
79
|
+
(E.g. a node can become a root when its parent is destroyed.)
|
|
80
|
+
|
|
81
|
+
Since nodes are *aliased* (have multiple references to them),
|
|
82
|
+
[`RefCell`](http://doc.rust-lang.org/std/cell/index.html) is used for interior mutability.
|
|
83
|
+
|
|
84
|
+
Advantages:
|
|
85
|
+
|
|
86
|
+
* A single `Node` user-visible type to manipulate the tree, with methods.
|
|
87
|
+
* Memory is freed as soon as it becomes unused (if parts of the tree are removed).
|
|
88
|
+
|
|
89
|
+
Disadvantages:
|
|
90
|
+
|
|
91
|
+
* The tree can only be accessed from the thread is was created in.
|
|
92
|
+
* Any tree manipulation, including read-only traversals,
|
|
93
|
+
requires incrementing and decrementing reference counts,
|
|
94
|
+
which causes run-time overhead.
|
|
95
|
+
* Nodes are allocated individually, which may cause memory fragmentation and hurt performance.
|
|
96
|
+
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
// commonmarker: the two `allow` attributes below are additions. The file is kept
|
|
100
|
+
// whole rather than trimmed to what this crate uses, so that it stays easy to
|
|
101
|
+
// diff against upstream, and upstream's `PartialEq for NodeEdge` trips a newer
|
|
102
|
+
// clippy lint. Upstream's crate-level `#![doc(html_root_url = ...)]` attribute
|
|
103
|
+
// is removed here because it is only valid at a crate root.
|
|
104
|
+
#![allow(dead_code)]
|
|
105
|
+
#![allow(clippy::needless_borrowed_reference)]
|
|
106
|
+
#![forbid(unsafe_code)]
|
|
107
|
+
#![warn(missing_docs)]
|
|
108
|
+
|
|
109
|
+
use std::cell::{Ref, RefCell, RefMut};
|
|
110
|
+
use std::fmt;
|
|
111
|
+
use std::rc::{Rc, Weak};
|
|
112
|
+
|
|
113
|
+
type Link<T> = Rc<RefCell<NodeData<T>>>;
|
|
114
|
+
type WeakLink<T> = Weak<RefCell<NodeData<T>>>;
|
|
115
|
+
|
|
116
|
+
/// A reference to a node holding a value of type `T`. Nodes form a tree.
|
|
117
|
+
///
|
|
118
|
+
/// Internally, this uses reference counting for lifetime tracking
|
|
119
|
+
/// and `std::cell::RefCell` for interior mutability.
|
|
120
|
+
///
|
|
121
|
+
/// **Note:** Cloning a `Node` only increments a reference count. It does not copy the data.
|
|
122
|
+
pub struct Node<T>(Link<T>);
|
|
123
|
+
|
|
124
|
+
/// A weak reference to a node holding a value of type `T`.
|
|
125
|
+
pub struct WeakNode<T>(WeakLink<T>);
|
|
126
|
+
|
|
127
|
+
struct NodeData<T> {
|
|
128
|
+
parent: Option<WeakLink<T>>,
|
|
129
|
+
first_child: Option<Link<T>>,
|
|
130
|
+
last_child: Option<WeakLink<T>>,
|
|
131
|
+
previous_sibling: Option<WeakLink<T>>,
|
|
132
|
+
next_sibling: Option<Link<T>>,
|
|
133
|
+
data: T,
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// Cloning a `Node` only increments a reference count. It does not copy the data.
|
|
137
|
+
impl<T> Clone for Node<T> {
|
|
138
|
+
fn clone(&self) -> Self {
|
|
139
|
+
Node(Rc::clone(&self.0))
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
impl<T> PartialEq for Node<T> {
|
|
144
|
+
fn eq(&self, other: &Node<T>) -> bool {
|
|
145
|
+
Rc::ptr_eq(&self.0, &other.0)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
impl<T: fmt::Debug> fmt::Debug for Node<T> {
|
|
150
|
+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
151
|
+
fmt::Debug::fmt(&*self.borrow(), f)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
impl<T: fmt::Display> fmt::Display for Node<T> {
|
|
156
|
+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
157
|
+
fmt::Display::fmt(&*self.borrow(), f)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
impl<T> Node<T> {
|
|
162
|
+
/// Creates a new node from its associated data.
|
|
163
|
+
pub fn new(data: T) -> Node<T> {
|
|
164
|
+
Node(Rc::new(RefCell::new(NodeData {
|
|
165
|
+
parent: None,
|
|
166
|
+
first_child: None,
|
|
167
|
+
last_child: None,
|
|
168
|
+
previous_sibling: None,
|
|
169
|
+
next_sibling: None,
|
|
170
|
+
data,
|
|
171
|
+
})))
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/// Returns a weak reference to a node.
|
|
175
|
+
pub fn downgrade(&self) -> WeakNode<T> {
|
|
176
|
+
WeakNode(Rc::downgrade(&self.0))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/// Returns a parent node, unless this node is the root of the tree.
|
|
180
|
+
///
|
|
181
|
+
/// # Panics
|
|
182
|
+
///
|
|
183
|
+
/// Panics if the node is currently mutably borrowed.
|
|
184
|
+
pub fn parent(&self) -> Option<Node<T>> {
|
|
185
|
+
Some(Node(self.0.borrow().parent.as_ref()?.upgrade()?))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/// Returns a first child of this node, unless it has no child.
|
|
189
|
+
///
|
|
190
|
+
/// # Panics
|
|
191
|
+
///
|
|
192
|
+
/// Panics if the node is currently mutably borrowed.
|
|
193
|
+
pub fn first_child(&self) -> Option<Node<T>> {
|
|
194
|
+
Some(Node(self.0.borrow().first_child.as_ref()?.clone()))
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/// Returns a last child of this node, unless it has no child.
|
|
198
|
+
///
|
|
199
|
+
/// # Panics
|
|
200
|
+
///
|
|
201
|
+
/// Panics if the node is currently mutably borrowed.
|
|
202
|
+
pub fn last_child(&self) -> Option<Node<T>> {
|
|
203
|
+
Some(Node(self.0.borrow().last_child.as_ref()?.upgrade()?))
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/// Returns the previous sibling of this node, unless it is a first child.
|
|
207
|
+
///
|
|
208
|
+
/// # Panics
|
|
209
|
+
///
|
|
210
|
+
/// Panics if the node is currently mutably borrowed.
|
|
211
|
+
pub fn previous_sibling(&self) -> Option<Node<T>> {
|
|
212
|
+
Some(Node(self.0.borrow().previous_sibling.as_ref()?.upgrade()?))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// Returns the next sibling of this node, unless it is a last child.
|
|
216
|
+
///
|
|
217
|
+
/// # Panics
|
|
218
|
+
///
|
|
219
|
+
/// Panics if the node is currently mutably borrowed.
|
|
220
|
+
pub fn next_sibling(&self) -> Option<Node<T>> {
|
|
221
|
+
Some(Node(self.0.borrow().next_sibling.as_ref()?.clone()))
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/// Returns a shared reference to this node's data.
|
|
225
|
+
///
|
|
226
|
+
/// # Panics
|
|
227
|
+
///
|
|
228
|
+
/// Panics if the node is currently mutably borrowed.
|
|
229
|
+
// commonmarker: `'_` added to the return type; upstream has `Ref<T>`
|
|
230
|
+
// (https://github.com/RazrFalcon/rctree/blob/b904373a48d4d97cf9f17f7e96d7136d984f1aa7/src/lib.rs#L184).
|
|
231
|
+
// Same meaning, just spelled out for the `mismatched_lifetime_syntaxes` lint.
|
|
232
|
+
pub fn borrow(&self) -> Ref<'_, T> {
|
|
233
|
+
Ref::map(self.0.borrow(), |v| &v.data)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/// Returns a unique/mutable reference to this node's data.
|
|
237
|
+
///
|
|
238
|
+
/// # Panics
|
|
239
|
+
///
|
|
240
|
+
/// Panics if the node is currently borrowed.
|
|
241
|
+
// commonmarker: `'_` added to the return type; upstream has `RefMut<T>`
|
|
242
|
+
// (https://github.com/RazrFalcon/rctree/blob/b904373a48d4d97cf9f17f7e96d7136d984f1aa7/src/lib.rs#L193).
|
|
243
|
+
// Same meaning, just spelled out for the `mismatched_lifetime_syntaxes` lint.
|
|
244
|
+
pub fn borrow_mut(&self) -> RefMut<'_, T> {
|
|
245
|
+
RefMut::map(self.0.borrow_mut(), |v| &mut v.data)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// Returns an iterator of nodes to this node and its ancestors.
|
|
249
|
+
///
|
|
250
|
+
/// Includes the current node.
|
|
251
|
+
pub fn ancestors(&self) -> Ancestors<T> {
|
|
252
|
+
Ancestors(Some(self.clone()))
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/// Returns an iterator of nodes to this node and the siblings before it.
|
|
256
|
+
///
|
|
257
|
+
/// Includes the current node.
|
|
258
|
+
pub fn preceding_siblings(&self) -> PrecedingSiblings<T> {
|
|
259
|
+
PrecedingSiblings(Some(self.clone()))
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/// Returns an iterator of nodes to this node and the siblings after it.
|
|
263
|
+
///
|
|
264
|
+
/// Includes the current node.
|
|
265
|
+
pub fn following_siblings(&self) -> FollowingSiblings<T> {
|
|
266
|
+
FollowingSiblings(Some(self.clone()))
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/// Returns an iterator of nodes to this node's children.
|
|
270
|
+
///
|
|
271
|
+
/// # Panics
|
|
272
|
+
///
|
|
273
|
+
/// Panics if the node is currently mutably borrowed.
|
|
274
|
+
pub fn children(&self) -> Children<T> {
|
|
275
|
+
Children {
|
|
276
|
+
next: self.first_child(),
|
|
277
|
+
next_back: self.last_child(),
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/// Returns `true` if this node has children nodes.
|
|
282
|
+
///
|
|
283
|
+
/// # Panics
|
|
284
|
+
///
|
|
285
|
+
/// Panics if the node is currently mutably borrowed.
|
|
286
|
+
pub fn has_children(&self) -> bool {
|
|
287
|
+
self.first_child().is_some()
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/// Returns an iterator of nodes to this node and its descendants, in tree order.
|
|
291
|
+
///
|
|
292
|
+
/// Includes the current node.
|
|
293
|
+
pub fn descendants(&self) -> Descendants<T> {
|
|
294
|
+
Descendants(self.traverse())
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/// Returns an iterator of nodes to this node and its descendants, in tree order.
|
|
298
|
+
pub fn traverse(&self) -> Traverse<T> {
|
|
299
|
+
Traverse {
|
|
300
|
+
root: self.clone(),
|
|
301
|
+
next: Some(NodeEdge::Start(self.clone())),
|
|
302
|
+
next_back: Some(NodeEdge::End(self.clone())),
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/// Detaches a node from its parent and siblings. Children are not affected.
|
|
307
|
+
///
|
|
308
|
+
/// # Panics
|
|
309
|
+
///
|
|
310
|
+
/// Panics if the node or one of its adjoining nodes is currently borrowed.
|
|
311
|
+
pub fn detach(&self) {
|
|
312
|
+
self.0.borrow_mut().detach();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/// Appends a new child to this node, after existing children.
|
|
316
|
+
///
|
|
317
|
+
/// # Panics
|
|
318
|
+
///
|
|
319
|
+
/// Panics if the node, the new child, or one of their adjoining nodes is currently borrowed.
|
|
320
|
+
pub fn append(&self, new_child: Node<T>) {
|
|
321
|
+
assert!(*self != new_child, "a node cannot be appended to itself");
|
|
322
|
+
|
|
323
|
+
let mut self_borrow = self.0.borrow_mut();
|
|
324
|
+
let mut last_child_opt = None;
|
|
325
|
+
{
|
|
326
|
+
let mut new_child_borrow = new_child.0.borrow_mut();
|
|
327
|
+
new_child_borrow.detach();
|
|
328
|
+
new_child_borrow.parent = Some(Rc::downgrade(&self.0));
|
|
329
|
+
if let Some(last_child_weak) = self_borrow.last_child.take() {
|
|
330
|
+
if let Some(last_child_strong) = last_child_weak.upgrade() {
|
|
331
|
+
new_child_borrow.previous_sibling = Some(last_child_weak);
|
|
332
|
+
last_child_opt = Some(last_child_strong);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
self_borrow.last_child = Some(Rc::downgrade(&new_child.0));
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if let Some(last_child_strong) = last_child_opt {
|
|
339
|
+
let mut last_child_borrow = last_child_strong.borrow_mut();
|
|
340
|
+
debug_assert!(last_child_borrow.next_sibling.is_none());
|
|
341
|
+
last_child_borrow.next_sibling = Some(new_child.0);
|
|
342
|
+
} else {
|
|
343
|
+
// No last child
|
|
344
|
+
debug_assert!(self_borrow.first_child.is_none());
|
|
345
|
+
self_borrow.first_child = Some(new_child.0);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/// Prepends a new child to this node, before existing children.
|
|
350
|
+
///
|
|
351
|
+
/// # Panics
|
|
352
|
+
///
|
|
353
|
+
/// Panics if the node, the new child, or one of their adjoining nodes is currently borrowed.
|
|
354
|
+
pub fn prepend(&self, new_child: Node<T>) {
|
|
355
|
+
assert!(*self != new_child, "a node cannot be prepended to itself");
|
|
356
|
+
|
|
357
|
+
let mut self_borrow = self.0.borrow_mut();
|
|
358
|
+
{
|
|
359
|
+
let mut new_child_borrow = new_child.0.borrow_mut();
|
|
360
|
+
new_child_borrow.detach();
|
|
361
|
+
new_child_borrow.parent = Some(Rc::downgrade(&self.0));
|
|
362
|
+
match self_borrow.first_child.take() {
|
|
363
|
+
Some(first_child_strong) => {
|
|
364
|
+
{
|
|
365
|
+
let mut first_child_borrow = first_child_strong.borrow_mut();
|
|
366
|
+
debug_assert!(first_child_borrow.previous_sibling.is_none());
|
|
367
|
+
first_child_borrow.previous_sibling = Some(Rc::downgrade(&new_child.0));
|
|
368
|
+
}
|
|
369
|
+
new_child_borrow.next_sibling = Some(first_child_strong);
|
|
370
|
+
}
|
|
371
|
+
None => {
|
|
372
|
+
debug_assert!(self_borrow.first_child.is_none());
|
|
373
|
+
self_borrow.last_child = Some(Rc::downgrade(&new_child.0));
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
self_borrow.first_child = Some(new_child.0);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/// Inserts a new sibling after this node.
|
|
381
|
+
///
|
|
382
|
+
/// # Panics
|
|
383
|
+
///
|
|
384
|
+
/// Panics if the node, the new sibling, or one of their adjoining nodes is currently borrowed.
|
|
385
|
+
pub fn insert_after(&self, new_sibling: Node<T>) {
|
|
386
|
+
assert!(
|
|
387
|
+
*self != new_sibling,
|
|
388
|
+
"a node cannot be inserted after itself"
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
let mut self_borrow = self.0.borrow_mut();
|
|
392
|
+
{
|
|
393
|
+
let mut new_sibling_borrow = new_sibling.0.borrow_mut();
|
|
394
|
+
new_sibling_borrow.detach();
|
|
395
|
+
new_sibling_borrow.parent = self_borrow.parent.clone();
|
|
396
|
+
new_sibling_borrow.previous_sibling = Some(Rc::downgrade(&self.0));
|
|
397
|
+
match self_borrow.next_sibling.take() {
|
|
398
|
+
Some(next_sibling_strong) => {
|
|
399
|
+
{
|
|
400
|
+
let mut next_sibling_borrow = next_sibling_strong.borrow_mut();
|
|
401
|
+
debug_assert!({
|
|
402
|
+
let weak = next_sibling_borrow.previous_sibling.as_ref().unwrap();
|
|
403
|
+
Rc::ptr_eq(&weak.upgrade().unwrap(), &self.0)
|
|
404
|
+
});
|
|
405
|
+
next_sibling_borrow.previous_sibling = Some(Rc::downgrade(&new_sibling.0));
|
|
406
|
+
}
|
|
407
|
+
new_sibling_borrow.next_sibling = Some(next_sibling_strong);
|
|
408
|
+
}
|
|
409
|
+
None => {
|
|
410
|
+
if let Some(parent_ref) = self_borrow.parent.as_ref() {
|
|
411
|
+
if let Some(parent_strong) = parent_ref.upgrade() {
|
|
412
|
+
let mut parent_borrow = parent_strong.borrow_mut();
|
|
413
|
+
parent_borrow.last_child = Some(Rc::downgrade(&new_sibling.0));
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
self_borrow.next_sibling = Some(new_sibling.0);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/// Inserts a new sibling before this node.
|
|
423
|
+
///
|
|
424
|
+
/// # Panics
|
|
425
|
+
///
|
|
426
|
+
/// Panics if the node, the new sibling, or one of their adjoining nodes is currently borrowed.
|
|
427
|
+
pub fn insert_before(&self, new_sibling: Node<T>) {
|
|
428
|
+
assert!(
|
|
429
|
+
*self != new_sibling,
|
|
430
|
+
"a node cannot be inserted before itself"
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
let mut self_borrow = self.0.borrow_mut();
|
|
434
|
+
let mut previous_sibling_opt = None;
|
|
435
|
+
{
|
|
436
|
+
let mut new_sibling_borrow = new_sibling.0.borrow_mut();
|
|
437
|
+
new_sibling_borrow.detach();
|
|
438
|
+
new_sibling_borrow.parent = self_borrow.parent.clone();
|
|
439
|
+
new_sibling_borrow.next_sibling = Some(self.0.clone());
|
|
440
|
+
if let Some(previous_sibling_weak) = self_borrow.previous_sibling.take() {
|
|
441
|
+
if let Some(previous_sibling_strong) = previous_sibling_weak.upgrade() {
|
|
442
|
+
new_sibling_borrow.previous_sibling = Some(previous_sibling_weak);
|
|
443
|
+
previous_sibling_opt = Some(previous_sibling_strong);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
self_borrow.previous_sibling = Some(Rc::downgrade(&new_sibling.0));
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if let Some(previous_sibling_strong) = previous_sibling_opt {
|
|
450
|
+
let mut previous_sibling_borrow = previous_sibling_strong.borrow_mut();
|
|
451
|
+
debug_assert!({
|
|
452
|
+
let rc = previous_sibling_borrow.next_sibling.as_ref().unwrap();
|
|
453
|
+
Rc::ptr_eq(rc, &self.0)
|
|
454
|
+
});
|
|
455
|
+
previous_sibling_borrow.next_sibling = Some(new_sibling.0);
|
|
456
|
+
} else {
|
|
457
|
+
// No previous sibling.
|
|
458
|
+
if let Some(parent_ref) = self_borrow.parent.as_ref() {
|
|
459
|
+
if let Some(parent_strong) = parent_ref.upgrade() {
|
|
460
|
+
let mut parent_borrow = parent_strong.borrow_mut();
|
|
461
|
+
parent_borrow.first_child = Some(new_sibling.0);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/// Returns a copy of a current node without children.
|
|
468
|
+
///
|
|
469
|
+
/// # Panics
|
|
470
|
+
///
|
|
471
|
+
/// Panics if the node is currently mutably borrowed.
|
|
472
|
+
pub fn make_copy(&self) -> Node<T>
|
|
473
|
+
where
|
|
474
|
+
T: Clone,
|
|
475
|
+
{
|
|
476
|
+
Node::new(self.borrow().clone())
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/// Returns a copy of a current node with children.
|
|
480
|
+
///
|
|
481
|
+
/// # Panics
|
|
482
|
+
///
|
|
483
|
+
/// Panics if any of the descendant nodes are currently mutably borrowed.
|
|
484
|
+
pub fn make_deep_copy(&self) -> Node<T>
|
|
485
|
+
where
|
|
486
|
+
T: Clone,
|
|
487
|
+
{
|
|
488
|
+
let mut root = self.make_copy();
|
|
489
|
+
Node::_make_deep_copy(&mut root, self);
|
|
490
|
+
root
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
fn _make_deep_copy(parent: &mut Node<T>, node: &Node<T>)
|
|
494
|
+
where
|
|
495
|
+
T: Clone,
|
|
496
|
+
{
|
|
497
|
+
for child in node.children() {
|
|
498
|
+
let mut new_node = child.make_copy();
|
|
499
|
+
parent.append(new_node.clone());
|
|
500
|
+
|
|
501
|
+
if child.has_children() {
|
|
502
|
+
Node::_make_deep_copy(&mut new_node, &child);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/// Cloning a `WeakNode` only increments a reference count. It does not copy the data.
|
|
509
|
+
impl<T> Clone for WeakNode<T> {
|
|
510
|
+
fn clone(&self) -> Self {
|
|
511
|
+
WeakNode(Weak::clone(&self.0))
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
impl<T: fmt::Debug> fmt::Debug for WeakNode<T> {
|
|
516
|
+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
517
|
+
f.write_str("(WeakNode)")
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
impl<T> WeakNode<T> {
|
|
522
|
+
/// Attempts to upgrade the WeakNode to a Node.
|
|
523
|
+
pub fn upgrade(&self) -> Option<Node<T>> {
|
|
524
|
+
self.0.upgrade().map(Node)
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
impl<T> NodeData<T> {
|
|
529
|
+
/// Detaches a node from its parent and siblings. Children are not affected.
|
|
530
|
+
fn detach(&mut self) {
|
|
531
|
+
let parent_weak = self.parent.take();
|
|
532
|
+
let previous_sibling_weak = self.previous_sibling.take();
|
|
533
|
+
let next_sibling_strong = self.next_sibling.take();
|
|
534
|
+
|
|
535
|
+
let previous_sibling_opt = previous_sibling_weak
|
|
536
|
+
.as_ref()
|
|
537
|
+
.and_then(|weak| weak.upgrade());
|
|
538
|
+
|
|
539
|
+
if let Some(next_sibling_ref) = next_sibling_strong.as_ref() {
|
|
540
|
+
let mut next_sibling_borrow = next_sibling_ref.borrow_mut();
|
|
541
|
+
next_sibling_borrow.previous_sibling = previous_sibling_weak;
|
|
542
|
+
} else if let Some(parent_ref) = parent_weak.as_ref() {
|
|
543
|
+
if let Some(parent_strong) = parent_ref.upgrade() {
|
|
544
|
+
let mut parent_borrow = parent_strong.borrow_mut();
|
|
545
|
+
parent_borrow.last_child = previous_sibling_weak;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if let Some(previous_sibling_strong) = previous_sibling_opt {
|
|
550
|
+
let mut previous_sibling_borrow = previous_sibling_strong.borrow_mut();
|
|
551
|
+
previous_sibling_borrow.next_sibling = next_sibling_strong;
|
|
552
|
+
} else if let Some(parent_ref) = parent_weak.as_ref() {
|
|
553
|
+
if let Some(parent_strong) = parent_ref.upgrade() {
|
|
554
|
+
let mut parent_borrow = parent_strong.borrow_mut();
|
|
555
|
+
parent_borrow.first_child = next_sibling_strong;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// commonmarker: BEGIN replaced block. This whole `impl` differs from upstream
|
|
562
|
+
// (https://github.com/RazrFalcon/rctree/blob/b904373a48d4d97cf9f17f7e96d7136d984f1aa7/src/lib.rs#L510-L534),
|
|
563
|
+
// which freed deep trees recursively whenever the nested node was not a first
|
|
564
|
+
// child (nested emphasis, for example) and could overflow the native stack
|
|
565
|
+
// during a later garbage collection. See GHSA-c4fj-h48v-9fxp.
|
|
566
|
+
impl<T> Drop for NodeData<T> {
|
|
567
|
+
fn drop(&mut self) {
|
|
568
|
+
// Dismantle the descendants iteratively, so that dropping a deep tree
|
|
569
|
+
// cannot overflow the stack however the tree is shaped.
|
|
570
|
+
//
|
|
571
|
+
// Every descendant is unlinked from its parent and siblings. One that
|
|
572
|
+
// nothing else holds also gives up its children, which join the
|
|
573
|
+
// worklist, so by the time it is dropped at the end of its iteration it
|
|
574
|
+
// has no links left and its own `drop` has nothing to do. One that is
|
|
575
|
+
// still held elsewhere keeps its subtree and becomes a root.
|
|
576
|
+
//
|
|
577
|
+
// Upstream's version only avoided recursion when the nested node was a
|
|
578
|
+
// first child, see the link on the BEGIN marker above.
|
|
579
|
+
self.last_child = None;
|
|
580
|
+
let mut worklist: Vec<Link<T>> = Vec::new();
|
|
581
|
+
worklist.extend(self.first_child.take());
|
|
582
|
+
worklist.extend(self.next_sibling.take());
|
|
583
|
+
|
|
584
|
+
while let Some(node) = worklist.pop() {
|
|
585
|
+
let mut node_data = node.borrow_mut();
|
|
586
|
+
node_data.parent = None;
|
|
587
|
+
node_data.previous_sibling = None;
|
|
588
|
+
|
|
589
|
+
if let Some(next_sibling) = node_data.next_sibling.take() {
|
|
590
|
+
worklist.push(next_sibling);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
if Rc::strong_count(&node) == 1 {
|
|
594
|
+
node_data.last_child = None;
|
|
595
|
+
if let Some(first_child) = node_data.first_child.take() {
|
|
596
|
+
worklist.push(first_child);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
// commonmarker: END replaced block.
|
|
603
|
+
|
|
604
|
+
/// An iterator of nodes to the ancestors a given node.
|
|
605
|
+
pub struct Ancestors<T>(Option<Node<T>>);
|
|
606
|
+
|
|
607
|
+
impl<T> Iterator for Ancestors<T> {
|
|
608
|
+
type Item = Node<T>;
|
|
609
|
+
|
|
610
|
+
/// # Panics
|
|
611
|
+
///
|
|
612
|
+
/// Panics if the node about to be yielded is currently mutably borrowed.
|
|
613
|
+
fn next(&mut self) -> Option<Self::Item> {
|
|
614
|
+
let node = self.0.take()?;
|
|
615
|
+
self.0 = node.parent();
|
|
616
|
+
Some(node)
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/// An iterator of nodes to the siblings before a given node.
|
|
621
|
+
pub struct PrecedingSiblings<T>(Option<Node<T>>);
|
|
622
|
+
|
|
623
|
+
impl<T> Iterator for PrecedingSiblings<T> {
|
|
624
|
+
type Item = Node<T>;
|
|
625
|
+
|
|
626
|
+
/// # Panics
|
|
627
|
+
///
|
|
628
|
+
/// Panics if the node about to be yielded is currently mutably borrowed.
|
|
629
|
+
fn next(&mut self) -> Option<Self::Item> {
|
|
630
|
+
let node = self.0.take()?;
|
|
631
|
+
self.0 = node.previous_sibling();
|
|
632
|
+
Some(node)
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/// An iterator of nodes to the siblings after a given node.
|
|
637
|
+
pub struct FollowingSiblings<T>(Option<Node<T>>);
|
|
638
|
+
|
|
639
|
+
impl<T> Iterator for FollowingSiblings<T> {
|
|
640
|
+
type Item = Node<T>;
|
|
641
|
+
|
|
642
|
+
/// # Panics
|
|
643
|
+
///
|
|
644
|
+
/// Panics if the node about to be yielded is currently mutably borrowed.
|
|
645
|
+
fn next(&mut self) -> Option<Self::Item> {
|
|
646
|
+
let node = self.0.take()?;
|
|
647
|
+
self.0 = node.next_sibling();
|
|
648
|
+
Some(node)
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/// A double ended iterator of nodes to the children of a given node.
|
|
653
|
+
pub struct Children<T> {
|
|
654
|
+
next: Option<Node<T>>,
|
|
655
|
+
next_back: Option<Node<T>>,
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
impl<T> Children<T> {
|
|
659
|
+
// true if self.next_back's next sibling is self.next
|
|
660
|
+
fn finished(&self) -> bool {
|
|
661
|
+
match self.next_back {
|
|
662
|
+
Some(ref next_back) => next_back.next_sibling() == self.next,
|
|
663
|
+
_ => true,
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
impl<T> Iterator for Children<T> {
|
|
669
|
+
type Item = Node<T>;
|
|
670
|
+
|
|
671
|
+
/// # Panics
|
|
672
|
+
///
|
|
673
|
+
/// Panics if the node about to be yielded is currently mutably borrowed.
|
|
674
|
+
fn next(&mut self) -> Option<Self::Item> {
|
|
675
|
+
if self.finished() {
|
|
676
|
+
return None;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
let node = self.next.take()?;
|
|
680
|
+
self.next = node.next_sibling();
|
|
681
|
+
Some(node)
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
impl<T> DoubleEndedIterator for Children<T> {
|
|
686
|
+
/// # Panics
|
|
687
|
+
///
|
|
688
|
+
/// Panics if the node about to be yielded is currently mutably borrowed.
|
|
689
|
+
fn next_back(&mut self) -> Option<Self::Item> {
|
|
690
|
+
if self.finished() {
|
|
691
|
+
return None;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
let node = self.next_back.take()?;
|
|
695
|
+
self.next_back = node.previous_sibling();
|
|
696
|
+
Some(node)
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/// An iterator of nodes to a given node and its descendants, in tree order.
|
|
701
|
+
pub struct Descendants<T>(Traverse<T>);
|
|
702
|
+
|
|
703
|
+
impl<T> Iterator for Descendants<T> {
|
|
704
|
+
type Item = Node<T>;
|
|
705
|
+
|
|
706
|
+
/// # Panics
|
|
707
|
+
///
|
|
708
|
+
/// Panics if the node about to be yielded is currently mutably borrowed.
|
|
709
|
+
fn next(&mut self) -> Option<Self::Item> {
|
|
710
|
+
loop {
|
|
711
|
+
match self.0.next() {
|
|
712
|
+
Some(NodeEdge::Start(node)) => return Some(node),
|
|
713
|
+
Some(NodeEdge::End(_)) => {}
|
|
714
|
+
None => return None,
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/// A node type during traverse.
|
|
721
|
+
#[derive(Clone, Debug)]
|
|
722
|
+
pub enum NodeEdge<T> {
|
|
723
|
+
/// Indicates that start of a node that has children.
|
|
724
|
+
/// Yielded by `Traverse::next` before the node's descendants.
|
|
725
|
+
/// In HTML or XML, this corresponds to an opening tag like `<div>`
|
|
726
|
+
Start(Node<T>),
|
|
727
|
+
|
|
728
|
+
/// Indicates that end of a node that has children.
|
|
729
|
+
/// Yielded by `Traverse::next` after the node's descendants.
|
|
730
|
+
/// In HTML or XML, this corresponds to a closing tag like `</div>`
|
|
731
|
+
End(Node<T>),
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// Implement PartialEq manually, because we do not need to require T: PartialEq
|
|
735
|
+
impl<T> PartialEq for NodeEdge<T> {
|
|
736
|
+
fn eq(&self, other: &NodeEdge<T>) -> bool {
|
|
737
|
+
match (self, other) {
|
|
738
|
+
(&NodeEdge::Start(ref n1), &NodeEdge::Start(ref n2)) => *n1 == *n2,
|
|
739
|
+
(&NodeEdge::End(ref n1), &NodeEdge::End(ref n2)) => *n1 == *n2,
|
|
740
|
+
_ => false,
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
impl<T> NodeEdge<T> {
|
|
746
|
+
fn next_edge(&self, root: &Node<T>) -> Option<NodeEdge<T>> {
|
|
747
|
+
match *self {
|
|
748
|
+
NodeEdge::Start(ref node) => match node.first_child() {
|
|
749
|
+
Some(first_child) => Some(NodeEdge::Start(first_child)),
|
|
750
|
+
None => Some(NodeEdge::End(node.clone())),
|
|
751
|
+
},
|
|
752
|
+
NodeEdge::End(ref node) => {
|
|
753
|
+
if *node == *root {
|
|
754
|
+
None
|
|
755
|
+
} else {
|
|
756
|
+
match node.next_sibling() {
|
|
757
|
+
Some(next_sibling) => Some(NodeEdge::Start(next_sibling)),
|
|
758
|
+
// `node.parent()` here can only be `None`
|
|
759
|
+
// if the tree has been modified during iteration,
|
|
760
|
+
// but silently stopping iteration
|
|
761
|
+
// seems a more sensible behavior than panicking.
|
|
762
|
+
None => node.parent().map(NodeEdge::End),
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
fn previous_edge(&self, root: &Node<T>) -> Option<NodeEdge<T>> {
|
|
770
|
+
match *self {
|
|
771
|
+
NodeEdge::End(ref node) => match node.last_child() {
|
|
772
|
+
Some(last_child) => Some(NodeEdge::End(last_child)),
|
|
773
|
+
None => Some(NodeEdge::Start(node.clone())),
|
|
774
|
+
},
|
|
775
|
+
NodeEdge::Start(ref node) => {
|
|
776
|
+
if *node == *root {
|
|
777
|
+
None
|
|
778
|
+
} else {
|
|
779
|
+
match node.previous_sibling() {
|
|
780
|
+
Some(previous_sibling) => Some(NodeEdge::End(previous_sibling)),
|
|
781
|
+
// `node.parent()` here can only be `None`
|
|
782
|
+
// if the tree has been modified during iteration,
|
|
783
|
+
// but silently stopping iteration
|
|
784
|
+
// seems a more sensible behavior than panicking.
|
|
785
|
+
None => node.parent().map(NodeEdge::Start),
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/// A double ended iterator of nodes to a given node and its descendants,
|
|
794
|
+
/// in tree order.
|
|
795
|
+
pub struct Traverse<T> {
|
|
796
|
+
root: Node<T>,
|
|
797
|
+
next: Option<NodeEdge<T>>,
|
|
798
|
+
next_back: Option<NodeEdge<T>>,
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
impl<T> Traverse<T> {
|
|
802
|
+
// true if self.next_back's next edge is self.next
|
|
803
|
+
fn finished(&self) -> bool {
|
|
804
|
+
match self.next_back {
|
|
805
|
+
Some(ref next_back) => next_back.next_edge(&self.root) == self.next,
|
|
806
|
+
_ => true,
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
impl<T> Iterator for Traverse<T> {
|
|
812
|
+
type Item = NodeEdge<T>;
|
|
813
|
+
|
|
814
|
+
/// # Panics
|
|
815
|
+
///
|
|
816
|
+
/// Panics if the node about to be yielded is currently mutably borrowed.
|
|
817
|
+
fn next(&mut self) -> Option<Self::Item> {
|
|
818
|
+
if self.finished() {
|
|
819
|
+
return None;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
let node = self.next.take()?;
|
|
823
|
+
self.next = node.next_edge(&self.root);
|
|
824
|
+
Some(node)
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
impl<T> DoubleEndedIterator for Traverse<T> {
|
|
829
|
+
/// # Panics
|
|
830
|
+
///
|
|
831
|
+
/// Panics if the node about to be yielded is currently mutably borrowed.
|
|
832
|
+
fn next_back(&mut self) -> Option<Self::Item> {
|
|
833
|
+
if self.finished() {
|
|
834
|
+
return None;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
let node = self.next_back.take()?;
|
|
838
|
+
self.next_back = node.previous_edge(&self.root);
|
|
839
|
+
Some(node)
|
|
840
|
+
}
|
|
841
|
+
}
|