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

Attributes: Fix apply_diff_index_maps #2653

Merged
merged 2 commits into from May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 12 additions & 38 deletions packages/yew/src/dom_bundle/btag/attributes.rs
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::iter;
use std::ops::Deref;

use indexmap::IndexMap;
Expand Down Expand Up @@ -91,45 +90,20 @@ impl Attributes {
new: &IndexMap<AttrValue, AttrValue>,
old: &IndexMap<AttrValue, AttrValue>,
) {
let mut old_iter = old.iter();
let mut new_iter = new.iter();
loop {
match (new_iter.next(), old_iter.next()) {
(Some((new_key, new_value)), Some((old_key, old_value))) => {
if new_key != old_key {
break;
for (key, value) in new.iter() {
match old.get(key) {
Some(old_value) => {
if value != old_value {
Self::set_attribute(el, key, value);
}
if new_value != old_value {
Self::set_attribute(el, new_key, new_value);
}
}
// new attributes
(Some(attr), None) => {
for (key, value) in iter::once(attr).chain(new_iter) {
match old.get(key) {
Some(old_value) => {
if value != old_value {
Self::set_attribute(el, key, value);
}
}
None => {
Self::set_attribute(el, key, value);
}
}
}
break;
}
// removed attributes
(None, Some(attr)) => {
for (key, _) in iter::once(attr).chain(old_iter) {
let key = key;
if !new.contains_key(key) {
Self::remove_attribute(el, key);
}
}
break;
}
(None, None) => break,
None => Self::set_attribute(el, key, value),
}
}

for (key, _value) in old.iter() {
if !new.contains_key(key) {
Self::remove_attribute(el, key);
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions packages/yew/src/dom_bundle/btag/mod.rs
Expand Up @@ -931,6 +931,48 @@ mod tests {
"<div id=\"after\"></div>"
);
}

// test for bug: https://github.com/yewstack/yew/pull/2653
#[test]
fn test_index_map_attribute_diff() {
let (root, scope, parent) = setup_parent();

let test_ref = NodeRef::default();

// We want to test appy_diff with Attributes::IndexMap, so we
// need to create the VTag manually

// Create <div disabled="disabled" tabindex="0">
let mut vtag = VTag::new("div");
vtag.node_ref = test_ref.clone();
vtag.add_attribute("disabled", "disabled");
vtag.add_attribute("tabindex", "0");

let elem = VNode::VTag(Box::new(vtag));

let (_, mut elem) = elem.attach(&root, &scope, &parent, NodeRef::default());

// Create <div tabindex="0"> (removed first attribute "disabled")
let mut vtag = VTag::new("div");
vtag.node_ref = test_ref.clone();
vtag.add_attribute("tabindex", "0");
let next_elem = VNode::VTag(Box::new(vtag));
let elem_vtag = assert_vtag(next_elem);

// Sync happens here
// this should remove the the "disabled" attribute
elem_vtag.reconcile_node(&root, &scope, &parent, NodeRef::default(), &mut elem);

assert_eq!(
test_ref
.get()
.unwrap()
.dyn_ref::<web_sys::Element>()
.unwrap()
.outer_html(),
"<div tabindex=\"0\"></div>"
)
}
}

#[cfg(all(test, feature = "wasm_test"))]
Expand Down