Skip to content

Commit

Permalink
perf(element): add one sweep element creation (#109)
Browse files Browse the repository at this point in the history
* perf(element): add one sweep element creation

* perf(element): add fast value dref handling

* chore(minor): add none usage default id

* chore(minor): add if else value deref

* chore(minor): revert a.value deref split

* chore(minor): add match local name deref
  • Loading branch information
j-mendez committed Feb 26, 2023
1 parent 9a6a638 commit aa479ea
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/node.rs
Expand Up @@ -8,7 +8,6 @@ use std::ops::Deref;

use html5ever::tendril::StrTendril;
use html5ever::{Attribute, LocalName, QualName};

use selectors::attr::CaseSensitivity;

/// An HTML node.
Expand Down Expand Up @@ -238,25 +237,26 @@ pub struct Element {

impl Element {
#[doc(hidden)]
pub fn new(name: QualName, attrs: Vec<Attribute>) -> Self {
let id = attrs
.iter()
.find(|a| a.name.local.deref() == "id")
.map(|a| LocalName::from(a.value.deref()));

let classes: HashSet<LocalName> = attrs
.iter()
.find(|a| a.name.local.deref() == "class")
.map_or_else(HashSet::new, |a| {
a.value
.deref()
.split_whitespace()
.map(LocalName::from)
.collect()
});
pub fn new(name: QualName, attributes: Vec<Attribute>) -> Self {
let mut classes: HashSet<LocalName> = HashSet::new();
let mut attrs = Attributes::with_capacity(attributes.len());
let mut id: Option<LocalName> = None;

for a in attributes {
match a.name.local.deref() {
"id" => {
id = Some(LocalName::from(a.value.deref()));
}
"class" => {
classes.extend(a.value.deref().split_whitespace().map(LocalName::from));
}
_ => (),
};
attrs.insert(a.name, a.value);
}

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

0 comments on commit aa479ea

Please sign in to comment.