From 355d560754af9ef540cd3993a20bce075d3f4a9a Mon Sep 17 00:00:00 2001 From: CFV Date: Mon, 31 Oct 2022 19:26:56 +0100 Subject: [PATCH] Replace LocalNames with Strings, change hash function (see #45) --- Cargo.toml | 1 + src/element_ref/element.rs | 32 ++++++++++++++++---------------- src/node.rs | 16 ++++++++-------- src/selector.rs | 10 +++++----- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index db57729b..98fc77a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ selectors = "0.22.0" smallvec = "1.9.0" tendril = "0.4.3" indexmap = { version = "1.9.1", optional = true } +fnv = "1.0.7" [dependencies.getopts] version = "0.2.21" diff --git a/src/element_ref/element.rs b/src/element_ref/element.rs index 20e4d0de..603503d1 100644 --- a/src/element_ref/element.rs +++ b/src/element_ref/element.rs @@ -30,22 +30,6 @@ impl<'a> Element for ElementRef<'a> { false } - fn is_part(&self, _name: &LocalName) -> bool { - false - } - - fn is_same_type(&self, other: &Self) -> bool { - self.value().name == other.value().name - } - - fn exported_part(&self, _: &LocalName) -> Option { - None - } - - fn imported_part(&self, _: &LocalName) -> Option { - None - } - fn prev_sibling_element(&self) -> Option { self.prev_siblings() .find(|sibling| sibling.value().is_element()) @@ -71,6 +55,10 @@ impl<'a> Element for ElementRef<'a> { &self.value().name.ns == namespace } + fn is_same_type(&self, other: &Self) -> bool { + self.value().name == other.value().name + } + fn attr_matches( &self, ns: &NamespaceConstraint<&Namespace>, @@ -120,6 +108,18 @@ impl<'a> Element for ElementRef<'a> { self.value().has_class(name, case_sensitivity) } + fn exported_part(&self, _: &LocalName) -> Option { + None + } + + fn imported_part(&self, _: &LocalName) -> Option { + None + } + + fn is_part(&self, _name: &LocalName) -> bool { + false + } + fn is_empty(&self) -> bool { !self .children() diff --git a/src/node.rs b/src/node.rs index 78d5f940..f985e56f 100644 --- a/src/node.rs +++ b/src/node.rs @@ -1,7 +1,7 @@ //! HTML nodes. +use fnv::{FnvHashMap as HashMap, FnvHashSet as HashSet}; use std::collections::{hash_map, hash_set}; -use std::collections::{HashMap, HashSet}; use std::fmt; use std::ops::Deref; @@ -223,10 +223,10 @@ pub struct Element { pub name: QualName, /// The element ID. - pub id: Option, + pub id: Option, /// The element classes. - pub classes: HashSet, + pub classes: HashSet, /// The element attributes. pub attrs: Attributes, @@ -238,16 +238,16 @@ impl Element { let id = attrs .iter() .find(|a| a.name.local.deref() == "id") - .map(|a| LocalName::from(a.value.deref())); + .map(|a| String::from(a.value.deref())); - let classes: HashSet = attrs + let classes: HashSet = attrs .iter() .find(|a| a.name.local.deref() == "class") - .map_or(HashSet::new(), |a| { + .map_or(HashSet::default(), |a| { a.value .deref() .split_whitespace() - .map(LocalName::from) + .map(String::from) .collect() }); @@ -300,7 +300,7 @@ impl Element { #[allow(missing_debug_implementations)] #[derive(Clone)] pub struct Classes<'a> { - inner: hash_set::Iter<'a, LocalName>, + inner: hash_set::Iter<'a, String>, } impl<'a> Iterator for Classes<'a> { diff --git a/src/selector.rs b/src/selector.rs index d7b8b10b..635be7b1 100644 --- a/src/selector.rs +++ b/src/selector.rs @@ -65,21 +65,21 @@ impl<'i> parser::Parser<'i> for Parser { pub struct Simple; impl parser::SelectorImpl for Simple { + // see: https://github.com/servo/servo/pull/19747#issuecomment-357106065 + type ExtraMatchingData = String; type AttrValue = String; type Identifier = LocalName; type ClassName = LocalName; type PartName = LocalName; type LocalName = LocalName; - type NamespacePrefix = LocalName; type NamespaceUrl = Namespace; + type NamespacePrefix = LocalName; type BorrowedNamespaceUrl = Namespace; - type BorrowedLocalName = LocalName; + type BorrowedLocalName = LocalName; type NonTSPseudoClass = NonTSPseudoClass; - type PseudoElement = PseudoElement; - // see: https://github.com/servo/servo/pull/19747#issuecomment-357106065 - type ExtraMatchingData = String; + type PseudoElement = PseudoElement; } /// Non Tree-Structural Pseudo-Class.