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

perf(element): add one sweep element creation #109

Merged
merged 6 commits into from Feb 26, 2023
Merged
Changes from 3 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
41 changes: 23 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,31 @@ 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 {
let name_local = a.name.local.deref();
let mut value = "";

if name_local == "id" {
j-mendez marked this conversation as resolved.
Show resolved Hide resolved
value = a.value.deref();
id = Some(LocalName::from(value));
}
if name_local == "class" {
if value.is_empty() {
value = a.value.deref();
}
classes.extend(value.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