Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature flag atomic to make use of atomic StrTendril type. #102

Merged
merged 3 commits into from Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;

cfvescovo marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(test)]
mod test;
7 changes: 5 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 @@ -256,7 +256,10 @@ impl Element {
});

Element {
attrs: attrs.into_iter().map(|a| (a.name, a.value)).collect(),
attrs: attrs
.into_iter()
.map(|a| (a.name, crate::tendril_util::make(a.value)))
.collect(),
name,
id,
classes,
Expand Down