Skip to content

Commit 09ed178

Browse files
committed
fix: Attribute order
1 parent 8f99195 commit 09ed178

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/browser/dom/virtual_dom_bridge.rs

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use super::Namespace;
44
use crate::virtual_dom::{At, AtValue, Attrs, El, Mailbox, Node, Style, Text};
5+
use std::cmp::Ordering;
56
use wasm_bindgen::JsCast;
67
use web_sys::Document;
78

@@ -108,6 +109,7 @@ pub(crate) fn make_websys_el<Ms>(el: &mut El<Ms>, document: &web_sys::Document)
108109
.expect("Problem creating web-sys element"),
109110
};
110111

112+
fix_attrs_order(&mut el.attrs);
111113
for (at, attr_value) in &el.attrs.vals {
112114
set_attr_value(&el_ws, at, attr_value);
113115
}
@@ -230,6 +232,8 @@ pub(crate) fn patch_el_details<Ms>(
230232
old_el_ws: &web_sys::Node,
231233
mailbox: &Mailbox<Ms>,
232234
) {
235+
fix_attrs_order(&mut new.attrs);
236+
233237
for (key, new_val) in &new.attrs.vals {
234238
match old.attrs.vals.get(key) {
235239
Some(old_val) => {
@@ -309,6 +313,22 @@ pub(crate) fn patch_el_details<Ms>(
309313
}
310314
}
311315

316+
/// Some elements have order-sensitive attributes.
317+
///
318+
/// See the [example](https://github.com/seed-rs/seed/issues/335) of such element.
319+
#[allow(clippy::match_same_arms)]
320+
fn fix_attrs_order(attrs: &mut Attrs) {
321+
attrs.vals.sort_by(|at_a, _, at_b, _| {
322+
// Move `At::Value` at the end.
323+
match (at_a, at_b) {
324+
(At::Value, At::Value) => Ordering::Equal,
325+
(At::Value, _) => Ordering::Greater,
326+
(_, At::Value) => Ordering::Less,
327+
_ => Ordering::Equal,
328+
}
329+
});
330+
}
331+
312332
#[allow(clippy::too_many_lines)]
313333
impl<Ms> From<&web_sys::Element> for El<Ms> {
314334
/// Create a vdom node from a `web_sys::Element`. Used in creating elements from html

0 commit comments

Comments
 (0)