|
2 | 2 |
|
3 | 3 | use super::Namespace;
|
4 | 4 | use crate::virtual_dom::{At, AtValue, Attrs, El, Mailbox, Node, Style, Text};
|
| 5 | +use std::cmp::Ordering; |
5 | 6 | use wasm_bindgen::JsCast;
|
6 | 7 | use web_sys::Document;
|
7 | 8 |
|
@@ -108,6 +109,7 @@ pub(crate) fn make_websys_el<Ms>(el: &mut El<Ms>, document: &web_sys::Document)
|
108 | 109 | .expect("Problem creating web-sys element"),
|
109 | 110 | };
|
110 | 111 |
|
| 112 | + fix_attrs_order(&mut el.attrs); |
111 | 113 | for (at, attr_value) in &el.attrs.vals {
|
112 | 114 | set_attr_value(&el_ws, at, attr_value);
|
113 | 115 | }
|
@@ -230,6 +232,8 @@ pub(crate) fn patch_el_details<Ms>(
|
230 | 232 | old_el_ws: &web_sys::Node,
|
231 | 233 | mailbox: &Mailbox<Ms>,
|
232 | 234 | ) {
|
| 235 | + fix_attrs_order(&mut new.attrs); |
| 236 | + |
233 | 237 | for (key, new_val) in &new.attrs.vals {
|
234 | 238 | match old.attrs.vals.get(key) {
|
235 | 239 | Some(old_val) => {
|
@@ -309,6 +313,22 @@ pub(crate) fn patch_el_details<Ms>(
|
309 | 313 | }
|
310 | 314 | }
|
311 | 315 |
|
| 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 | + |
312 | 332 | #[allow(clippy::too_many_lines)]
|
313 | 333 | impl<Ms> From<&web_sys::Element> for El<Ms> {
|
314 | 334 | /// Create a vdom node from a `web_sys::Element`. Used in creating elements from html
|
|
0 commit comments