dom.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //! Implementation of a renderer for Dioxus on the web.
  2. //!
  3. //! Outstanding 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, MountedData, PlatformEventData};
  12. use dioxus_interpreter_js::get_node;
  13. use dioxus_interpreter_js::{minimal_bindings, save_template, Channel};
  14. use futures_channel::mpsc;
  15. use rustc_hash::FxHashMap;
  16. use wasm_bindgen::{closure::Closure, JsCast, JsValue};
  17. use web_sys::{Document, Element, Event};
  18. use crate::{load_document, virtual_event_from_websys_event, Config, WebEventConverter};
  19. pub struct WebsysDom {
  20. document: Document,
  21. #[allow(dead_code)]
  22. pub(crate) root: Element,
  23. templates: FxHashMap<String, u16>,
  24. max_template_id: u16,
  25. pub(crate) interpreter: Channel,
  26. #[cfg(feature = "mounted")]
  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: PlatformEventData,
  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::initialize(
  93. root.clone().unchecked_into(),
  94. handler.as_ref().unchecked_ref(),
  95. );
  96. dioxus_html::set_event_converter(Box::new(WebEventConverter));
  97. handler.forget();
  98. Self {
  99. document,
  100. root,
  101. interpreter,
  102. templates: FxHashMap::default(),
  103. max_template_id: 0,
  104. #[cfg(feature = "mounted")]
  105. event_channel,
  106. }
  107. }
  108. pub fn mount(&mut self) {
  109. self.interpreter.mount_to_root();
  110. }
  111. pub fn load_templates(&mut self, templates: &[Template]) {
  112. for template in templates {
  113. let mut roots = vec![];
  114. for root in template.roots {
  115. roots.push(self.create_template_node(root))
  116. }
  117. self.templates
  118. .insert(template.name.to_owned(), self.max_template_id);
  119. save_template(roots, self.max_template_id);
  120. self.max_template_id += 1
  121. }
  122. }
  123. fn create_template_node(&self, v: &TemplateNode) -> web_sys::Node {
  124. use TemplateNode::*;
  125. match v {
  126. Element {
  127. tag,
  128. namespace,
  129. attrs,
  130. children,
  131. ..
  132. } => {
  133. let el = match namespace {
  134. Some(ns) => self.document.create_element_ns(Some(ns), tag).unwrap(),
  135. None => self.document.create_element(tag).unwrap(),
  136. };
  137. for attr in *attrs {
  138. if let TemplateAttribute::Static {
  139. name,
  140. value,
  141. namespace,
  142. } = attr
  143. {
  144. minimal_bindings::setAttributeInner(
  145. el.clone().into(),
  146. name,
  147. JsValue::from_str(value),
  148. *namespace,
  149. );
  150. }
  151. }
  152. for child in *children {
  153. let _ = el.append_child(&self.create_template_node(child));
  154. }
  155. el.dyn_into().unwrap()
  156. }
  157. Text { text } => self.document.create_text_node(text).dyn_into().unwrap(),
  158. DynamicText { .. } => self.document.create_text_node("p").dyn_into().unwrap(),
  159. Dynamic { .. } => {
  160. let el = self.document.create_element("pre").unwrap();
  161. let _ = el.toggle_attribute("hidden");
  162. el.dyn_into().unwrap()
  163. }
  164. }
  165. }
  166. pub fn apply_edits(&mut self, mut edits: Vec<Mutation>) {
  167. use Mutation::*;
  168. let i = &mut self.interpreter;
  169. #[cfg(feature = "mounted")]
  170. // we need to apply the mount events last, so we collect them here
  171. let mut to_mount = Vec::new();
  172. for edit in &edits {
  173. match edit {
  174. AppendChildren { id, m } => i.append_children(id.0 as u32, *m as u16),
  175. AssignId { path, id } => {
  176. i.assign_id(path.as_ptr() as u32, path.len() as u8, id.0 as u32)
  177. }
  178. CreatePlaceholder { id } => i.create_placeholder(id.0 as u32),
  179. CreateTextNode { value, id } => i.create_text_node(value, id.0 as u32),
  180. HydrateText { path, value, id } => {
  181. i.hydrate_text(path.as_ptr() as u32, path.len() as u8, value, id.0 as u32)
  182. }
  183. LoadTemplate { name, index, id } => {
  184. if let Some(tmpl_id) = self.templates.get(*name) {
  185. i.load_template(*tmpl_id, *index as u16, id.0 as u32)
  186. }
  187. }
  188. ReplaceWith { id, m } => i.replace_with(id.0 as u32, *m as u16),
  189. ReplacePlaceholder { path, m } => {
  190. i.replace_placeholder(path.as_ptr() as u32, path.len() as u8, *m as u16)
  191. }
  192. InsertAfter { id, m } => i.insert_after(id.0 as u32, *m as u16),
  193. InsertBefore { id, m } => i.insert_before(id.0 as u32, *m as u16),
  194. SetAttribute {
  195. name,
  196. value,
  197. id,
  198. ns,
  199. } => match value {
  200. BorrowedAttributeValue::Text(txt) => {
  201. i.set_attribute(id.0 as u32, name, txt, ns.unwrap_or_default())
  202. }
  203. BorrowedAttributeValue::Float(f) => {
  204. i.set_attribute(id.0 as u32, name, &f.to_string(), ns.unwrap_or_default())
  205. }
  206. BorrowedAttributeValue::Int(n) => {
  207. i.set_attribute(id.0 as u32, name, &n.to_string(), ns.unwrap_or_default())
  208. }
  209. BorrowedAttributeValue::Bool(b) => i.set_attribute(
  210. id.0 as u32,
  211. name,
  212. if *b { "true" } else { "false" },
  213. ns.unwrap_or_default(),
  214. ),
  215. BorrowedAttributeValue::None => {
  216. i.remove_attribute(id.0 as u32, name, ns.unwrap_or_default())
  217. }
  218. _ => unreachable!(),
  219. },
  220. SetText { value, id } => i.set_text(id.0 as u32, value),
  221. NewEventListener { name, id, .. } => {
  222. match *name {
  223. // mounted events are fired immediately after the element is mounted.
  224. "mounted" => {
  225. #[cfg(feature = "mounted")]
  226. to_mount.push(*id);
  227. }
  228. _ => {
  229. i.new_event_listener(name, id.0 as u32, event_bubbles(name) as u8);
  230. }
  231. }
  232. }
  233. RemoveEventListener { name, id } => match *name {
  234. "mounted" => {}
  235. _ => {
  236. i.remove_event_listener(name, id.0 as u32, event_bubbles(name) as u8);
  237. }
  238. },
  239. Remove { id } => i.remove(id.0 as u32),
  240. PushRoot { id } => i.push_root(id.0 as u32),
  241. }
  242. }
  243. edits.clear();
  244. i.flush();
  245. #[cfg(feature = "mounted")]
  246. for id in to_mount {
  247. self.send_mount_event(id);
  248. }
  249. }
  250. pub(crate) fn send_mount_event(&self, id: ElementId) {
  251. let node = get_node(id.0 as u32);
  252. if let Some(element) = node.dyn_ref::<Element>() {
  253. let data: MountedData = element.into();
  254. let data = Box::new(data);
  255. let _ = self.event_channel.unbounded_send(UiEvent {
  256. name: "mounted".to_string(),
  257. bubbles: false,
  258. element: id,
  259. data: PlatformEventData::new(data),
  260. });
  261. }
  262. }
  263. }
  264. fn walk_event_for_id(event: &web_sys::Event) -> Option<(ElementId, web_sys::Element)> {
  265. let target = event
  266. .target()
  267. .expect("missing target")
  268. .dyn_into::<web_sys::Node>()
  269. .expect("not a valid node");
  270. let mut current_target_element = target.dyn_ref::<web_sys::Element>().cloned();
  271. loop {
  272. match (
  273. current_target_element
  274. .as_ref()
  275. .and_then(|el| el.get_attribute("data-dioxus-id").map(|f| f.parse())),
  276. current_target_element,
  277. ) {
  278. // This node is an element, and has a dioxus id, so we can stop walking
  279. (Some(Ok(id)), Some(target)) => return Some((ElementId(id), target)),
  280. // Walk the tree upwards until we actually find an event target
  281. (None, target_element) => {
  282. let parent = match target_element.as_ref() {
  283. Some(el) => el.parent_element(),
  284. // if this is the first node and not an element, we need to get the parent from the target node
  285. None => target.parent_element(),
  286. };
  287. match parent {
  288. Some(parent) => current_target_element = Some(parent),
  289. _ => return None,
  290. }
  291. }
  292. // This node is an element with an invalid dioxus id, give up
  293. _ => return None,
  294. }
  295. }
  296. }