Skip to content

Commit ec91bf1

Browse files
authored
Merge pull request #102 from jaboatman/master
Added feature flag `atomic` to make use of atomic `StrTendril` type.
2 parents aa479ea + 4b7fb13 commit ec91bf1

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ optional = true
3232
default = ["main"]
3333
deterministic = ["indexmap"]
3434
main = ["getopts"]
35+
atomic = []
3536

3637
[[bin]]
3738
name = "scraper"

src/html/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,11 @@ mod tests {
204204
.collect();
205205
assert_eq!(result, vec!["element3", "element2", "element1"]);
206206
}
207+
208+
#[cfg(feature = "atomic")]
209+
#[test]
210+
fn html_is_send() {
211+
fn send_sync<S: Send>() {}
212+
send_sync::<Html>();
213+
}
207214
}

src/html/tree_sink.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::borrow::Cow;
2-
31
use super::Html;
42
use crate::node::{Comment, Doctype, Element, Node, ProcessingInstruction, Text};
3+
use crate::tendril_util::make as make_tendril;
54
use ego_tree::NodeId;
65
use html5ever::tendril::StrTendril;
76
use html5ever::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
87
use html5ever::Attribute;
98
use html5ever::{ExpandedName, QualName};
9+
use std::borrow::Cow;
1010

1111
/// Note: does not support the `<template>` element.
1212
impl TreeSink for Html {
@@ -74,7 +74,9 @@ impl TreeSink for Html {
7474
// Create a comment node.
7575
fn create_comment(&mut self, text: StrTendril) -> Self::Handle {
7676
self.tree
77-
.orphan(Node::Comment(Comment { comment: text }))
77+
.orphan(Node::Comment(Comment {
78+
comment: make_tendril(text),
79+
}))
7880
.id()
7981
}
8082

@@ -85,6 +87,9 @@ impl TreeSink for Html {
8587
public_id: StrTendril,
8688
system_id: StrTendril,
8789
) {
90+
let name = make_tendril(name);
91+
let public_id = make_tendril(public_id);
92+
let system_id = make_tendril(system_id);
8893
let doctype = Doctype {
8994
name,
9095
public_id,
@@ -106,6 +111,7 @@ impl TreeSink for Html {
106111
}
107112

108113
NodeOrText::AppendText(text) => {
114+
let text = make_tendril(text);
109115
let can_concat = parent
110116
.last_child()
111117
.map_or(false, |mut n| n.value().is_text());
@@ -148,6 +154,7 @@ impl TreeSink for Html {
148154
}
149155

150156
NodeOrText::AppendText(text) => {
157+
let text = make_tendril(text);
151158
let can_concat = sibling
152159
.prev_sibling()
153160
.map_or(false, |mut n| n.value().is_text());
@@ -189,7 +196,10 @@ impl TreeSink for Html {
189196
};
190197

191198
for attr in attrs {
192-
element.attrs.entry(attr.name).or_insert(attr.value);
199+
element
200+
.attrs
201+
.entry(attr.name)
202+
.or_insert_with(|| make_tendril(attr.value));
193203
}
194204
}
195205

@@ -206,6 +216,8 @@ impl TreeSink for Html {
206216

207217
// Create Processing Instruction.
208218
fn create_pi(&mut self, target: StrTendril, data: StrTendril) -> Self::Handle {
219+
let target = make_tendril(target);
220+
let data = make_tendril(data);
209221
self.tree
210222
.orphan(Node::ProcessingInstruction(ProcessingInstruction {
211223
target,

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,31 @@ pub mod html;
153153
pub mod node;
154154
pub mod selector;
155155

156+
#[cfg(feature = "atomic")]
157+
pub(crate) mod tendril_util {
158+
use html5ever::tendril;
159+
/// Atomic equivalent to the default `StrTendril` type.
160+
pub type StrTendril = tendril::Tendril<tendril::fmt::UTF8, tendril::Atomic>;
161+
162+
/// Convert a standard tendril into an atomic one.
163+
pub fn make(s: tendril::StrTendril) -> StrTendril {
164+
s.into_send().into()
165+
}
166+
}
167+
168+
#[cfg(not(feature = "atomic"))]
169+
pub(crate) mod tendril_util {
170+
use html5ever::tendril;
171+
/// Primary string tendril type.
172+
pub type StrTendril = tendril::StrTendril;
173+
174+
/// Return unaltered.
175+
pub fn make(s: StrTendril) -> StrTendril {
176+
s
177+
}
178+
}
179+
180+
pub use tendril_util::StrTendril;
181+
156182
#[cfg(test)]
157183
mod test;

src/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::collections::{hash_set, HashSet};
66
use std::fmt;
77
use std::ops::Deref;
88

9-
use html5ever::tendril::StrTendril;
9+
use crate::StrTendril;
1010
use html5ever::{Attribute, LocalName, QualName};
1111
use selectors::attr::CaseSensitivity;
1212

@@ -252,7 +252,7 @@ impl Element {
252252
}
253253
_ => (),
254254
};
255-
attrs.insert(a.name, a.value);
255+
attrs.insert(a.name, crate::tendril_util::make(a.value));
256256
}
257257

258258
Element {

0 commit comments

Comments
 (0)