|
| 1 | +use crate::{RomRuntime, RuntimeConfig}; |
| 2 | + |
| 3 | +#[test] |
| 4 | +fn supports_innerhtml_fragment_parsing_and_serialization() { |
| 5 | + let runtime = RomRuntime::new(RuntimeConfig::default()).unwrap(); |
| 6 | + let result = runtime |
| 7 | + .eval_async_as_string( |
| 8 | + r##" |
| 9 | + (async () => { |
| 10 | + const host = document.createElement("div"); |
| 11 | + host.innerHTML = '<span class="greeting">Hello</span><strong data-kind="accent">world</strong>'; |
| 12 | +
|
| 13 | + const first = host.firstChild; |
| 14 | + const second = host.lastChild; |
| 15 | + const initial = { |
| 16 | + childCount: host.childNodes.length, |
| 17 | + firstTag: first.tagName, |
| 18 | + firstClass: first.className, |
| 19 | + firstText: first.textContent, |
| 20 | + secondTag: second.tagName, |
| 21 | + secondAttr: second.getAttribute("data-kind"), |
| 22 | + secondText: second.textContent, |
| 23 | + serialized: host.innerHTML, |
| 24 | + }; |
| 25 | +
|
| 26 | + host.innerHTML = '<em data-state="ready">updated</em>'; |
| 27 | +
|
| 28 | + return JSON.stringify({ |
| 29 | + initial, |
| 30 | + replaced: { |
| 31 | + childCount: host.childNodes.length, |
| 32 | + firstTag: host.firstChild.tagName, |
| 33 | + attr: host.firstChild.getAttribute("data-state"), |
| 34 | + text: host.textContent, |
| 35 | + serialized: host.innerHTML, |
| 36 | + }, |
| 37 | + }); |
| 38 | + })() |
| 39 | + "##, |
| 40 | + ) |
| 41 | + .unwrap(); |
| 42 | + |
| 43 | + let value: serde_json::Value = serde_json::from_str(&result).unwrap(); |
| 44 | + assert_eq!(value["initial"]["childCount"], 2); |
| 45 | + assert_eq!(value["initial"]["firstTag"], "SPAN"); |
| 46 | + assert_eq!(value["initial"]["firstClass"], "greeting"); |
| 47 | + assert_eq!(value["initial"]["firstText"], "Hello"); |
| 48 | + assert_eq!(value["initial"]["secondTag"], "STRONG"); |
| 49 | + assert_eq!(value["initial"]["secondAttr"], "accent"); |
| 50 | + assert_eq!(value["initial"]["secondText"], "world"); |
| 51 | + assert_eq!( |
| 52 | + value["initial"]["serialized"], |
| 53 | + "<span class=\"greeting\">Hello</span><strong data-kind=\"accent\">world</strong>" |
| 54 | + ); |
| 55 | + |
| 56 | + assert_eq!(value["replaced"]["childCount"], 1); |
| 57 | + assert_eq!(value["replaced"]["firstTag"], "EM"); |
| 58 | + assert_eq!(value["replaced"]["attr"], "ready"); |
| 59 | + assert_eq!(value["replaced"]["text"], "updated"); |
| 60 | + assert_eq!( |
| 61 | + value["replaced"]["serialized"], |
| 62 | + "<em data-state=\"ready\">updated</em>" |
| 63 | + ); |
| 64 | +} |
0 commit comments