diff --git a/Cargo.toml b/Cargo.toml index 7e3d30b..055236f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,4 +29,4 @@ mini-alloc = "0.6.0" [features] hashbrown = ["dep:hashbrown"] - +atomic = [] diff --git a/src/entities.rs b/src/entities.rs index b8d9e2f..495ca90 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -14,93 +14,105 @@ mod hash { pub type InnerHashMap = HashMap; } - pub(crate) use hash::{HashSetFx, InnerHashMap, NodeIdSet}; -//pub type DString = tendril::Tendril; - -use std::ops::{Deref, DerefMut}; -use html5ever::{Attribute, QualName}; -use tendril::{StrTendril, Tendril}; +#[cfg(feature = "atomic")] +mod str_wrap { + use html5ever::{Attribute, QualName}; + use std::ops::{Deref, DerefMut}; + use tendril::{StrTendril, Tendril}; -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Default)] -pub struct StrWrap(pub(crate) Tendril); + #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Default)] + pub struct StrWrap(pub(crate) Tendril); -impl StrWrap { - pub fn new() -> Self { - StrWrap(Tendril::new()) + impl StrWrap { + pub fn new() -> Self { + StrWrap(Tendril::new()) + } } -} -impl Deref for StrWrap { - type Target = Tendril; + impl Deref for StrWrap { + type Target = Tendril; - fn deref(&self) -> &Self::Target { - &self.0 + fn deref(&self) -> &Self::Target { + &self.0 + } } -} -impl DerefMut for StrWrap { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 + impl DerefMut for StrWrap { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } } -} -impl From for StrWrap { - fn from(value: StrTendril) -> Self { - StrWrap(value.into_send().into()) + impl From for StrWrap { + fn from(value: StrTendril) -> Self { + StrWrap(value.into_send().into()) + } } -} -impl From for StrWrap { - fn from(value: String) -> Self { - let v = Tendril::from(value); - StrWrap(v) + + impl From for StrWrap { + fn from(value: String) -> Self { + let v = Tendril::from(value); + StrWrap(v) + } } -} -impl From<&str> for StrWrap { - fn from(value: &str) -> Self { - let v = Tendril::from(value); - StrWrap(v) + impl From<&str> for StrWrap { + fn from(value: &str) -> Self { + let v = Tendril::from(value); + StrWrap(v) + } } -} -impl From for StrTendril { - fn from(value: StrWrap) -> Self { - value.0.into_send().into() + impl From for StrTendril { + fn from(value: StrWrap) -> Self { + value.0.into_send().into() + } } -} -/// A tag attribute, e.g. `class="test"` in `
`. -/// -/// The namespace on the attribute name is almost always ns!(""). -/// The tokenizer creates all attributes this way, but the tree -/// builder will adjust certain attribute names inside foreign -/// content (MathML, SVG). -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)] -pub struct Attr { - /// The name of the attribute (e.g. the `class` in `
`) - pub name: QualName, - /// The value of the attribute (e.g. the `"test"` in `
`) - pub value: StrWrap, -} + /// A tag attribute, e.g. `class="test"` in `
`. + /// + /// The namespace on the attribute name is almost always ns!(""). + /// The tokenizer creates all attributes this way, but the tree + /// builder will adjust certain attribute names inside foreign + /// content (MathML, SVG). + #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)] + pub struct Attr { + /// The name of the attribute (e.g. the `class` in `
`) + pub name: QualName, + /// The value of the attribute (e.g. the `"test"` in `
`) + pub value: StrWrap, + } -impl From for Attr { - fn from(val: Attribute) -> Self { - let value = val.value.into(); - Self { - name: val.name, - value, + impl From for Attr { + fn from(val: Attribute) -> Self { + let value = val.value.into(); + Self { + name: val.name, + value, + } } } -} -impl From for Attribute { - fn from(val: Attr) -> Self { - Self { - name: val.name, - value: val.value.into(), + impl From for Attribute { + fn from(val: Attr) -> Self { + Self { + name: val.name, + value: val.value.into(), + } } } } + +#[cfg(not(feature = "atomic"))] +mod str_wrap { + use html5ever::Attribute; + use tendril::StrTendril; + + pub type StrWrap = StrTendril; + pub type Attr = Attribute; +} + +pub use str_wrap::{Attr, StrWrap}; diff --git a/src/node/node_data.rs b/src/node/node_data.rs index 54bb453..bd8f218 100644 --- a/src/node/node_data.rs +++ b/src/node/node_data.rs @@ -172,7 +172,7 @@ impl Element { set.remove(remove); } - attr.value = StrWrap::from(set.into_iter().collect::>().join(" ")); + attr.value = set.into_iter().collect::>().join(" ").into(); } } @@ -188,11 +188,11 @@ impl Element { pub fn set_attr(&mut self, name: &str, val: &str) { let attr = self.attrs.iter_mut().find(|a| &a.name.local == name); match attr { - Some(attr) => attr.value = StrWrap::from(val), + Some(attr) => attr.value = val.into(), None => { - let value = StrWrap::from(val); // The namespace on the attribute name is almost always ns!(). let name = QualName::new(None, ns!(), LocalName::from(name)); + let value = val.into(); self.attrs.push(Attr { name, value }) } }