dom.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. //! Implementation of a renderer for Dioxus on the web.
  2. //!
  3. //! Oustanding todos:
  4. //! - Passive event listeners
  5. //! - no-op event listener patch for safari
  6. //! - tests to ensure dyn_into works for various event types.
  7. //! - Partial delegation?
  8. use dioxus_core::{
  9. BorrowedAttributeValue, ElementId, Mutation, Template, TemplateAttribute, TemplateNode,
  10. };
  11. use dioxus_html::{event_bubbles, CompositionData, FormData, MountedData};
  12. use dioxus_interpreter_js::{get_node, minimal_bindings, save_template, Channel};
  13. use futures_channel::mpsc;
  14. use js_sys::Array;
  15. use rustc_hash::FxHashMap;
  16. use std::{any::Any, rc::Rc};
  17. use wasm_bindgen::{closure::Closure, prelude::wasm_bindgen, JsCast, JsValue};
  18. use web_sys::{Document, Element, Event};
  19. use crate::Config;
  20. pub struct WebsysDom {
  21. document: Document,
  22. #[allow(dead_code)]
  23. pub(crate) root: Element,
  24. templates: FxHashMap<String, u32>,
  25. max_template_id: u32,
  26. pub(crate) interpreter: Channel,
  27. event_channel: mpsc::UnboundedSender<UiEvent>,
  28. }
  29. pub struct UiEvent {
  30. pub name: String,
  31. pub bubbles: bool,
  32. pub element: ElementId,
  33. pub data: Rc<dyn Any>,
  34. }
  35. impl WebsysDom {
  36. pub fn new(cfg: Config, event_channel: mpsc::UnboundedSender<UiEvent>) -> Self {
  37. // eventually, we just want to let the interpreter do all the work of decoding events into our event type
  38. // a match here in order to avoid some error during runtime browser test
  39. let document = load_document();
  40. let root = match document.get_element_by_id(&cfg.rootname) {
  41. Some(root) => root,
  42. None => {
  43. web_sys::console::error_1(
  44. &format!(
  45. "element '#{}' not found. mounting to the body.",
  46. cfg.rootname
  47. )
  48. .into(),
  49. );
  50. document.create_element("body").ok().unwrap()
  51. }
  52. };
  53. let interpreter = Channel::default();
  54. let handler: Closure<dyn FnMut(&Event)> = Closure::wrap(Box::new({
  55. let event_channel = event_channel.clone();
  56. move |event: &web_sys::Event| {
  57. let name = event.type_();
  58. let element = walk_event_for_id(event);
  59. let bubbles = dioxus_html::event_bubbles(name.as_str());
  60. if let Some((element, target)) = element {
  61. let prevent_event;
  62. if let Some(prevent_requests) = target
  63. .get_attribute("dioxus-prevent-default")
  64. .as_deref()
  65. .map(|f| f.split_whitespace())
  66. {
  67. prevent_event = prevent_requests
  68. .map(|f| f.trim_start_matches("on"))
  69. .any(|f| f == name);
  70. } else {
  71. prevent_event = false;
  72. }
  73. // Prevent forms from submitting and redirecting
  74. if name == "submit" {
  75. // On forms the default behavior is not to submit, if prevent default is set then we submit the form
  76. if !prevent_event {
  77. event.prevent_default();
  78. }
  79. } else if prevent_event {
  80. event.prevent_default();
  81. }
  82. let data = virtual_event_from_websys_event(event.clone(), target);
  83. let _ = event_channel.unbounded_send(UiEvent {
  84. name,
  85. bubbles,
  86. element,
  87. data,
  88. });
  89. }
  90. }
  91. }));
  92. dioxus_interpreter_js::initilize(
  93. root.clone().unchecked_into(),
  94. handler.as_ref().unchecked_ref(),
  95. );
  96. handler.forget();
  97. Self {
  98. document,
  99. root,
  100. interpreter,
  101. templates: FxHashMap::default(),
  102. max_template_id: 0,
  103. event_channel,
  104. }
  105. }
  106. pub fn mount(&mut self) {
  107. self.interpreter.mount_to_root();
  108. }
  109. pub fn load_templates(&mut self, templates: &[Template]) {
  110. for template in templates {
  111. let mut roots = vec![];
  112. for root in template.roots {
  113. roots.push(self.create_template_node(root))
  114. }
  115. self.templates
  116. .insert(template.name.to_owned(), self.max_template_id);
  117. save_template(roots, self.max_template_id);
  118. self.max_template_id += 1
  119. }
  120. }
  121. fn create_template_node(&self, v: &TemplateNode) -> web_sys::Node {
  122. use TemplateNode::*;
  123. match v {
  124. Element {
  125. tag,
  126. namespace,
  127. attrs,
  128. children,
  129. ..
  130. } => {
  131. let el = match namespace {
  132. Some(ns) => self.document.create_element_ns(Some(ns), tag).unwrap(),
  133. None => self.document.create_element(tag).unwrap(),
  134. };
  135. for attr in *attrs {
  136. if let TemplateAttribute::Static {
  137. name,
  138. value,
  139. namespace,
  140. } = attr
  141. {
  142. minimal_bindings::setAttributeInner(
  143. el.clone().into(),
  144. name,
  145. JsValue::from_str(value),
  146. *namespace,
  147. );
  148. }
  149. }
  150. for child in *children {
  151. let _ = el.append_child(&self.create_template_node(child));
  152. }
  153. el.dyn_into().unwrap()
  154. }
  155. Text { text } => self.document.create_text_node(text).dyn_into().unwrap(),
  156. DynamicText { .. } => self.document.create_text_node("p").dyn_into().unwrap(),
  157. Dynamic { .. } => {
  158. let el = self.document.create_element("pre").unwrap();
  159. let _ = el.toggle_attribute("hidden");
  160. el.dyn_into().unwrap()
  161. }
  162. }
  163. }
  164. pub fn apply_edits(&mut self, mut edits: Vec<Mutation>) {
  165. use Mutation::*;
  166. let i = &mut self.interpreter;
  167. // we need to apply the mount events last, so we collect them here
  168. let mut to_mount = Vec::new();
  169. for edit in &edits {
  170. match edit {
  171. AppendChildren { id, m } => i.append_children(id.0 as u32, *m as u32),
  172. AssignId { path, id } => {
  173. i.assign_id(path.as_ptr() as u32, path.len() as u8, id.0 as u32)
  174. }
  175. CreatePlaceholder { id } => i.create_placeholder(id.0 as u32),
  176. CreateTextNode { value, id } => i.create_text_node(value, id.0 as u32),
  177. HydrateText { path, value, id } => {
  178. i.hydrate_text(path.as_ptr() as u32, path.len() as u8, value, id.0 as u32)
  179. }
  180. LoadTemplate { name, index, id } => {
  181. if let Some(tmpl_id) = self.templates.get(*name) {
  182. i.load_template(*tmpl_id, *index as u32, id.0 as u32)
  183. }
  184. }
  185. ReplaceWith { id, m } => i.replace_with(id.0 as u32, *m as u32),
  186. ReplacePlaceholder { path, m } => {
  187. i.replace_placeholder(path.as_ptr() as u32, path.len() as u8, *m as u32)
  188. }
  189. InsertAfter { id, m } => i.insert_after(id.0 as u32, *m as u32),
  190. InsertBefore { id, m } => i.insert_before(id.0 as u32, *m as u32),
  191. SetAttribute {
  192. name,
  193. value,
  194. id,
  195. ns,
  196. } => match value {
  197. BorrowedAttributeValue::Text(txt) => {
  198. i.set_attribute(id.0 as u32, name, txt, ns.unwrap_or_default())
  199. }
  200. BorrowedAttributeValue::Float(f) => {
  201. i.set_attribute(id.0 as u32, name, &f.to_string(), ns.unwrap_or_default())
  202. }
  203. BorrowedAttributeValue::Int(n) => {
  204. i.set_attribute(id.0 as u32, name, &n.to_string(), ns.unwrap_or_default())
  205. }
  206. BorrowedAttributeValue::Bool(b) => i.set_attribute(
  207. id.0 as u32,
  208. name,
  209. if *b { "true" } else { "false" },
  210. ns.unwrap_or_default(),
  211. ),
  212. BorrowedAttributeValue::None => {
  213. i.remove_attribute(id.0 as u32, name, ns.unwrap_or_default())
  214. }
  215. _ => unreachable!(),
  216. },
  217. SetText { value, id } => i.set_text(id.0 as u32, value),
  218. NewEventListener { name, id, .. } => {
  219. match *name {
  220. // mounted events are fired immediately after the element is mounted.
  221. "mounted" => {
  222. to_mount.push(*id);
  223. }
  224. _ => {
  225. i.new_event_listener(name, id.0 as u32, event_bubbles(name) as u8);
  226. }
  227. }
  228. }
  229. RemoveEventListener { name, id } => match *name {
  230. "mounted" => {}
  231. _ => {
  232. i.remove_event_listener(name, id.0 as u32, event_bubbles(name) as u8);
  233. }
  234. },
  235. Remove { id } => i.remove(id.0 as u32),
  236. PushRoot { id } => i.push_root(id.0 as u32),
  237. }
  238. }
  239. edits.clear();
  240. i.flush();
  241. for id in to_mount {
  242. self.send_mount_event(id);
  243. }
  244. }
  245. pub(crate) fn send_mount_event(&self, id: ElementId) {
  246. let node = get_node(id.0 as u32);
  247. if let Some(element) = node.dyn_ref::<Element>() {
  248. let data: MountedData = element.into();
  249. let data = Rc::new(data);
  250. let _ = self.event_channel.unbounded_send(UiEvent {
  251. name: "mounted".to_string(),
  252. bubbles: false,
  253. element: id,
  254. data,
  255. });
  256. }
  257. }
  258. }
  259. // todo: some of these events are being casted to the wrong event type.
  260. // We need tests that simulate clicks/etc and make sure every event type works.
  261. pub fn virtual_event_from_websys_event(event: web_sys::Event, target: Element) -> Rc<dyn Any> {
  262. use dioxus_html::events::*;
  263. match event.type_().as_str() {
  264. "copy" | "cut" | "paste" => Rc::new(ClipboardData {}),
  265. "compositionend" | "compositionstart" | "compositionupdate" => {
  266. make_composition_event(&event)
  267. }
  268. "keydown" | "keypress" | "keyup" => Rc::new(KeyboardData::from(event)),
  269. "focus" | "blur" | "focusout" | "focusin" => Rc::new(FocusData {}),
  270. "change" | "input" | "invalid" | "reset" | "submit" => read_input_to_data(target),
  271. "click" | "contextmenu" | "dblclick" | "doubleclick" | "mousedown" | "mouseenter"
  272. | "mouseleave" | "mousemove" | "mouseout" | "mouseover" | "mouseup" => {
  273. Rc::new(MouseData::from(event))
  274. }
  275. "drag" | "dragend" | "dragenter" | "dragexit" | "dragleave" | "dragover" | "dragstart"
  276. | "drop" => {
  277. let mouse = MouseData::from(event);
  278. Rc::new(DragData { mouse })
  279. }
  280. "pointerdown" | "pointermove" | "pointerup" | "pointercancel" | "gotpointercapture"
  281. | "lostpointercapture" | "pointerenter" | "pointerleave" | "pointerover" | "pointerout" => {
  282. Rc::new(PointerData::from(event))
  283. }
  284. "select" => Rc::new(SelectionData {}),
  285. "touchcancel" | "touchend" | "touchmove" | "touchstart" => Rc::new(TouchData::from(event)),
  286. "scroll" => Rc::new(ScrollData {}),
  287. "wheel" => Rc::new(WheelData::from(event)),
  288. "animationstart" | "animationend" | "animationiteration" => {
  289. Rc::new(AnimationData::from(event))
  290. }
  291. "transitionend" => Rc::new(TransitionData::from(event)),
  292. "abort" | "canplay" | "canplaythrough" | "durationchange" | "emptied" | "encrypted"
  293. | "ended" | "loadeddata" | "loadedmetadata" | "loadstart" | "pause" | "play"
  294. | "playing" | "progress" | "ratechange" | "seeked" | "seeking" | "stalled" | "suspend"
  295. | "timeupdate" | "volumechange" | "waiting" => Rc::new(MediaData {}),
  296. "error" => Rc::new(ImageData { load_error: true }),
  297. "load" => Rc::new(ImageData { load_error: false }),
  298. "toggle" => Rc::new(ToggleData {}),
  299. _ => Rc::new(()),
  300. }
  301. }
  302. fn make_composition_event(event: &Event) -> Rc<CompositionData> {
  303. let evt: &web_sys::CompositionEvent = event.dyn_ref().unwrap();
  304. Rc::new(CompositionData {
  305. data: evt.data().unwrap_or_default(),
  306. })
  307. }
  308. pub(crate) fn load_document() -> Document {
  309. web_sys::window()
  310. .expect("should have access to the Window")
  311. .document()
  312. .expect("should have access to the Document")
  313. }
  314. fn read_input_to_data(target: Element) -> Rc<FormData> {
  315. // todo: these handlers might get really slow if the input box gets large and allocation pressure is heavy
  316. // don't have a good solution with the serialized event problem
  317. let value: String = target
  318. .dyn_ref()
  319. .map(|input: &web_sys::HtmlInputElement| {
  320. // todo: special case more input types
  321. match input.type_().as_str() {
  322. "checkbox" => {
  323. match input.checked() {
  324. true => "true".to_string(),
  325. false => "false".to_string(),
  326. }
  327. },
  328. _ => {
  329. input.value()
  330. }
  331. }
  332. })
  333. .or_else(|| {
  334. target
  335. .dyn_ref()
  336. .map(|input: &web_sys::HtmlTextAreaElement| input.value())
  337. })
  338. // select elements are NOT input events - because - why woudn't they be??
  339. .or_else(|| {
  340. target
  341. .dyn_ref()
  342. .map(|input: &web_sys::HtmlSelectElement| input.value())
  343. })
  344. .or_else(|| {
  345. target
  346. .dyn_ref::<web_sys::HtmlElement>()
  347. .unwrap()
  348. .text_content()
  349. })
  350. .expect("only an InputElement or TextAreaElement or an element with contenteditable=true can have an oninput event listener");
  351. let mut values = std::collections::HashMap::new();
  352. // try to fill in form values
  353. if let Some(form) = target.dyn_ref::<web_sys::HtmlFormElement>() {
  354. let form_data = get_form_data(form);
  355. for value in form_data.entries().into_iter().flatten() {
  356. if let Ok(array) = value.dyn_into::<Array>() {
  357. if let Some(name) = array.get(0).as_string() {
  358. if let Ok(item_values) = array.get(1).dyn_into::<Array>() {
  359. let item_values =
  360. item_values.iter().filter_map(|v| v.as_string()).collect();
  361. values.insert(name, item_values);
  362. }
  363. }
  364. }
  365. }
  366. }
  367. #[cfg(not(feature = "file_engine"))]
  368. let files = None;
  369. #[cfg(feature = "file_engine")]
  370. let files = target
  371. .dyn_ref()
  372. .and_then(|input: &web_sys::HtmlInputElement| {
  373. input.files().and_then(|files| {
  374. #[allow(clippy::arc_with_non_send_sync)]
  375. crate::file_engine::WebFileEngine::new(files)
  376. .map(|f| std::sync::Arc::new(f) as std::sync::Arc<dyn dioxus_html::FileEngine>)
  377. })
  378. });
  379. Rc::new(FormData {
  380. value,
  381. values,
  382. files,
  383. })
  384. }
  385. // web-sys does not expose the keys api for form data, so we need to manually bind to it
  386. #[wasm_bindgen(inline_js = r#"
  387. export function get_form_data(form) {
  388. let values = new Map();
  389. const formData = new FormData(form);
  390. for (let name of formData.keys()) {
  391. values.set(name, formData.getAll(name));
  392. }
  393. return values;
  394. }
  395. "#)]
  396. extern "C" {
  397. fn get_form_data(form: &web_sys::HtmlFormElement) -> js_sys::Map;
  398. }
  399. fn walk_event_for_id(event: &web_sys::Event) -> Option<(ElementId, web_sys::Element)> {
  400. let target = event
  401. .target()
  402. .expect("missing target")
  403. .dyn_into::<web_sys::Node>()
  404. .expect("not a valid node");
  405. let mut current_target_element = target.dyn_ref::<web_sys::Element>().cloned();
  406. loop {
  407. match (
  408. current_target_element
  409. .as_ref()
  410. .and_then(|el| el.get_attribute("data-dioxus-id").map(|f| f.parse())),
  411. current_target_element,
  412. ) {
  413. // This node is an element, and has a dioxus id, so we can stop walking
  414. (Some(Ok(id)), Some(target)) => return Some((ElementId(id), target)),
  415. // Walk the tree upwards until we actually find an event target
  416. (None, target_element) => {
  417. let parent = match target_element.as_ref() {
  418. Some(el) => el.parent_element(),
  419. // if this is the first node and not an element, we need to get the parent from the target node
  420. None => target.parent_element(),
  421. };
  422. match parent {
  423. Some(parent) => current_target_element = Some(parent),
  424. _ => return None,
  425. }
  426. }
  427. // This node is an element with an invalid dioxus id, give up
  428. _ => return None,
  429. }
  430. }
  431. }