dom.rs 16 KB

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