Skip to content

Commit

Permalink
Merge pull request #102 from jaboatman/master
Browse files Browse the repository at this point in the history
Added feature flag `atomic` to make use of atomic `StrTendril` type.
  • Loading branch information
cfvescovo committed Feb 28, 2023
2 parents aa479ea + 4b7fb13 commit ec91bf1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -32,6 +32,7 @@ optional = true
default = ["main"]
deterministic = ["indexmap"]
main = ["getopts"]
atomic = []

[[bin]]
name = "scraper"
Expand Down
7 changes: 7 additions & 0 deletions src/html/mod.rs
Expand Up @@ -204,4 +204,11 @@ mod tests {
.collect();
assert_eq!(result, vec!["element3", "element2", "element1"]);
}

#[cfg(feature = "atomic")]
#[test]
fn html_is_send() {
fn send_sync<S: Send>() {}
send_sync::<Html>();
}
}
20 changes: 16 additions & 4 deletions src/html/tree_sink.rs
@@ -1,12 +1,12 @@
use std::borrow::Cow;

use super::Html;
use crate::node::{Comment, Doctype, Element, Node, ProcessingInstruction, Text};
use crate::tendril_util::make as make_tendril;
use ego_tree::NodeId;
use html5ever::tendril::StrTendril;
use html5ever::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
use html5ever::Attribute;
use html5ever::{ExpandedName, QualName};
use std::borrow::Cow;

/// Note: does not support the `<template>` element.
impl TreeSink for Html {
Expand Down Expand Up @@ -74,7 +74,9 @@ impl TreeSink for Html {
// Create a comment node.
fn create_comment(&mut self, text: StrTendril) -> Self::Handle {
self.tree
.orphan(Node::Comment(Comment { comment: text }))
.orphan(Node::Comment(Comment {
comment: make_tendril(text),
}))
.id()
}

Expand All @@ -85,6 +87,9 @@ impl TreeSink for Html {
public_id: StrTendril,
system_id: StrTendril,
) {
let name = make_tendril(name);
let public_id = make_tendril(public_id);
let system_id = make_tendril(system_id);
let doctype = Doctype {
name,
public_id,
Expand All @@ -106,6 +111,7 @@ impl TreeSink for Html {
}

NodeOrText::AppendText(text) => {
let text = make_tendril(text);
let can_concat = parent
.last_child()
.map_or(false, |mut n| n.value().is_text());
Expand Down Expand Up @@ -148,6 +154,7 @@ impl TreeSink for Html {
}

NodeOrText::AppendText(text) => {
let text = make_tendril(text);
let can_concat = sibling
.prev_sibling()
.map_or(false, |mut n| n.value().is_text());
Expand Down Expand Up @@ -189,7 +196,10 @@ impl TreeSink for Html {
};

for attr in attrs {
element.attrs.entry(attr.name).or_insert(attr.value);
element
.attrs
.entry(attr.name)
.or_insert_with(|| make_tendril(attr.value));
}
}

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

// Create Processing Instruction.
fn create_pi(&mut self, target: StrTendril, data: StrTendril) -> Self::Handle {
let target = make_tendril(target);
let data = make_tendril(data);
self.tree
.orphan(Node::ProcessingInstruction(ProcessingInstruction {
target,
Expand Down
26 changes: 26 additions & 0 deletions src/lib.rs
Expand Up @@ -153,5 +153,31 @@ pub mod html;
pub mod node;
pub mod selector;

#[cfg(feature = "atomic")]
pub(crate) mod tendril_util {
use html5ever::tendril;
/// Atomic equivalent to the default `StrTendril` type.
pub type StrTendril = tendril::Tendril<tendril::fmt::UTF8, tendril::Atomic>;

/// Convert a standard tendril into an atomic one.
pub fn make(s: tendril::StrTendril) -> StrTendril {
s.into_send().into()
}
}

#[cfg(not(feature = "atomic"))]
pub(crate) mod tendril_util {
use html5ever::tendril;
/// Primary string tendril type.
pub type StrTendril = tendril::StrTendril;

/// Return unaltered.
pub fn make(s: StrTendril) -> StrTendril {
s
}
}

pub use tendril_util::StrTendril;

#[cfg(test)]
mod test;
4 changes: 2 additions & 2 deletions src/node.rs
Expand Up @@ -6,7 +6,7 @@ use std::collections::{hash_set, HashSet};
use std::fmt;
use std::ops::Deref;

use html5ever::tendril::StrTendril;
use crate::StrTendril;
use html5ever::{Attribute, LocalName, QualName};
use selectors::attr::CaseSensitivity;

Expand Down Expand Up @@ -252,7 +252,7 @@ impl Element {
}
_ => (),
};
attrs.insert(a.name, a.value);
attrs.insert(a.name, crate::tendril_util::make(a.value));
}

Element {
Expand Down

0 comments on commit ec91bf1

Please sign in to comment.