fix: issue with ordering of class attribute and class=("fancy-name-200", true) (closes #907) (#914)

This commit is contained in:
Greg Johnston 2023-04-21 12:42:35 -04:00 committed by GitHub
parent 328d42656d
commit b80f9e3871
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -658,6 +658,15 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
}
/// Adds a class to an element.
///
/// **Note**: In the builder syntax, this will be overwritten by the `class`
/// attribute if you use `.attr("class", /* */)`. In the `view` macro, they
/// are automatically re-ordered so that this over-writing does not happen.
///
/// # Panics
/// This directly uses the browsers `classList` API, which means it will throw
/// a runtime error if you pass more than a single class name. If you want to
/// pass more than one class name at a time, you can use [HtmlElement::classes].
#[track_caller]
pub fn class(
self,

View file

@ -842,7 +842,8 @@ fn element_to_tokens(
};
let attrs = node.attributes.iter().filter_map(|node| {
if let Node::Attribute(node) = node {
if node.key.to_string().trim().starts_with("class:") {
let name = node.key.to_string();
if name.trim().starts_with("class:") || fancy_class_name(&name, cx, node).is_some() {
None
} else {
Some(attribute_to_tokens(cx, node, global_class))
@ -853,7 +854,11 @@ fn element_to_tokens(
});
let class_attrs = node.attributes.iter().filter_map(|node| {
if let Node::Attribute(node) = node {
if node.key.to_string().trim().starts_with("class:") {
let name = node.key.to_string();
if let Some((fancy, _, _)) = fancy_class_name(&name, cx, node) {
Some(fancy)
}
else if name.trim().starts_with("class:") {
Some(attribute_to_tokens(cx, node, global_class))
} else {
None