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 4 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
35 changes: 17 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,25 @@ 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();

if name_local == "id" {
j-mendez marked this conversation as resolved.
Show resolved Hide resolved
id = Some(LocalName::from(a.value.deref()));
} else if name_local == "class" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was rushing and made this change without thinking. @adamreichold @cfvescovo feel free to squash the commits for this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, thanks

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