real_dom.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. use anymap::AnyMap;
  2. use fxhash::{FxHashMap, FxHashSet};
  3. use std::{
  4. any::TypeId,
  5. collections::VecDeque,
  6. ops::{Index, IndexMut},
  7. };
  8. use dioxus_core::{ElementId, Mutations, VNode, VirtualDom};
  9. use crate::state::{union_ordered_iter, AttributeMask, NodeMask, State};
  10. /// A Dom that can sync with the VirtualDom mutations intended for use in lazy renderers.
  11. /// The render state passes from parent to children and or accumulates state from children to parents.
  12. /// To get started implement [PushedDownState] and or [BubbledUpState] and call [RealDom::apply_mutations] to update the dom and [RealDom::update_state] to update the state of the nodes.
  13. #[derive(Debug)]
  14. pub struct RealDom<S: State> {
  15. root: usize,
  16. nodes: Vec<Option<Node<S>>>,
  17. nodes_listening: FxHashMap<&'static str, FxHashSet<usize>>,
  18. node_stack: smallvec::SmallVec<[usize; 10]>,
  19. }
  20. impl<S: State> Default for RealDom<S> {
  21. fn default() -> Self {
  22. Self::new()
  23. }
  24. }
  25. impl<S: State> RealDom<S> {
  26. pub fn new() -> RealDom<S> {
  27. RealDom {
  28. root: 0,
  29. nodes: {
  30. let v = vec![Some(Node::new(
  31. 0,
  32. NodeType::Element {
  33. tag: "Root".to_string(),
  34. namespace: Some("Root"),
  35. children: Vec::new(),
  36. },
  37. ))];
  38. v
  39. },
  40. nodes_listening: FxHashMap::default(),
  41. node_stack: smallvec::SmallVec::new(),
  42. }
  43. }
  44. /// Updates the dom, up and down state and return a set of nodes that were updated pass this to update_state.
  45. pub fn apply_mutations(&mut self, mutations_vec: Vec<Mutations>) -> Vec<(usize, NodeMask)> {
  46. let mut nodes_updated = Vec::new();
  47. for mutations in mutations_vec {
  48. for e in mutations.edits {
  49. use dioxus_core::DomEdit::*;
  50. match e {
  51. PushRoot { root } => self.node_stack.push(root as usize),
  52. AppendChildren { many } => {
  53. let target = if self.node_stack.len() > many as usize {
  54. *self
  55. .node_stack
  56. .get(self.node_stack.len() - (many as usize + 1))
  57. .unwrap()
  58. } else {
  59. 0
  60. };
  61. let drained: Vec<_> = self
  62. .node_stack
  63. .drain(self.node_stack.len() - many as usize..)
  64. .collect();
  65. for ns in drained {
  66. self.link_child(ns, target).unwrap();
  67. nodes_updated.push((ns, NodeMask::ALL));
  68. }
  69. }
  70. ReplaceWith { root, m } => {
  71. let root = self.remove(root as usize).unwrap();
  72. let target = root.parent.unwrap().0;
  73. let drained: Vec<_> = self.node_stack.drain(0..m as usize).collect();
  74. for ns in drained {
  75. nodes_updated.push((ns, NodeMask::ALL));
  76. self.link_child(ns, target).unwrap();
  77. }
  78. }
  79. InsertAfter { root, n } => {
  80. let target = self[root as usize].parent.unwrap().0;
  81. let drained: Vec<_> = self.node_stack.drain(0..n as usize).collect();
  82. for ns in drained {
  83. nodes_updated.push((ns, NodeMask::ALL));
  84. self.link_child(ns, target).unwrap();
  85. }
  86. }
  87. InsertBefore { root, n } => {
  88. let target = self[root as usize].parent.unwrap().0;
  89. let drained: Vec<_> = self.node_stack.drain(0..n as usize).collect();
  90. for ns in drained {
  91. nodes_updated.push((ns, NodeMask::ALL));
  92. self.link_child(ns, target).unwrap();
  93. }
  94. }
  95. Remove { root } => {
  96. if let Some(parent) = self[root as usize].parent {
  97. nodes_updated.push((parent.0, NodeMask::NONE));
  98. }
  99. self.remove(root as usize).unwrap();
  100. }
  101. CreateTextNode { root, text } => {
  102. let n = Node::new(
  103. root,
  104. NodeType::Text {
  105. text: text.to_string(),
  106. },
  107. );
  108. self.insert(n);
  109. self.node_stack.push(root as usize)
  110. }
  111. CreateElement { root, tag } => {
  112. let n = Node::new(
  113. root,
  114. NodeType::Element {
  115. tag: tag.to_string(),
  116. namespace: None,
  117. children: Vec::new(),
  118. },
  119. );
  120. self.insert(n);
  121. self.node_stack.push(root as usize)
  122. }
  123. CreateElementNs { root, tag, ns } => {
  124. let n = Node::new(
  125. root,
  126. NodeType::Element {
  127. tag: tag.to_string(),
  128. namespace: Some(ns),
  129. children: Vec::new(),
  130. },
  131. );
  132. self.insert(n);
  133. self.node_stack.push(root as usize)
  134. }
  135. CreatePlaceholder { root } => {
  136. let n = Node::new(root, NodeType::Placeholder);
  137. self.insert(n);
  138. self.node_stack.push(root as usize)
  139. }
  140. NewEventListener {
  141. event_name,
  142. scope: _,
  143. root,
  144. } => {
  145. if let Some(v) = self.nodes_listening.get_mut(event_name) {
  146. v.insert(root as usize);
  147. } else {
  148. let mut hs = FxHashSet::default();
  149. hs.insert(root as usize);
  150. self.nodes_listening.insert(event_name, hs);
  151. }
  152. }
  153. RemoveEventListener { root, event } => {
  154. let v = self.nodes_listening.get_mut(event).unwrap();
  155. v.remove(&(root as usize));
  156. }
  157. SetText {
  158. root,
  159. text: new_text,
  160. } => {
  161. let target = &mut self[root as usize];
  162. nodes_updated.push((
  163. root as usize,
  164. NodeMask::new(AttributeMask::NONE, false, false, true),
  165. ));
  166. match &mut target.node_type {
  167. NodeType::Text { text } => {
  168. *text = new_text.to_string();
  169. }
  170. _ => unreachable!(),
  171. }
  172. }
  173. SetAttribute { root, field, .. } => {
  174. nodes_updated.push((
  175. root as usize,
  176. NodeMask::new(AttributeMask::single(field), false, false, false),
  177. ));
  178. }
  179. RemoveAttribute {
  180. root, name: field, ..
  181. } => {
  182. nodes_updated.push((
  183. root as usize,
  184. NodeMask::new(AttributeMask::single(field), false, false, false),
  185. ));
  186. }
  187. PopRoot {} => {
  188. self.node_stack.pop();
  189. }
  190. }
  191. }
  192. }
  193. nodes_updated
  194. }
  195. /// Seperated from apply_mutations because Mutations require a mutable reference to the VirtualDom.
  196. pub fn update_state(
  197. &mut self,
  198. vdom: &VirtualDom,
  199. nodes_updated: Vec<(usize, NodeMask)>,
  200. ctx: AnyMap,
  201. ) -> Option<FxHashSet<usize>> {
  202. #[derive(PartialEq, Clone, Debug)]
  203. enum StatesToCheck {
  204. All,
  205. Some(Vec<TypeId>),
  206. }
  207. impl StatesToCheck {
  208. fn union(&self, other: &Self) -> Self {
  209. match (self, other) {
  210. (Self::Some(s), Self::Some(o)) => Self::Some(union_ordered_iter(
  211. s.iter().copied(),
  212. o.iter().copied(),
  213. s.len() + o.len(),
  214. )),
  215. _ => Self::All,
  216. }
  217. }
  218. }
  219. #[derive(Debug, Clone)]
  220. struct NodeRef {
  221. id: usize,
  222. height: u16,
  223. node_mask: NodeMask,
  224. to_check: StatesToCheck,
  225. }
  226. impl NodeRef {
  227. fn union_with(&mut self, other: &Self) {
  228. self.node_mask = self.node_mask.union(&other.node_mask);
  229. self.to_check = self.to_check.union(&other.to_check);
  230. }
  231. }
  232. impl Eq for NodeRef {}
  233. impl PartialEq for NodeRef {
  234. fn eq(&self, other: &Self) -> bool {
  235. self.id == other.id && self.height == other.height
  236. }
  237. }
  238. impl Ord for NodeRef {
  239. fn cmp(&self, other: &Self) -> std::cmp::Ordering {
  240. self.partial_cmp(other).unwrap()
  241. }
  242. }
  243. impl PartialOrd for NodeRef {
  244. fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
  245. // Sort nodes first by height, then if the height is the same id.
  246. // The order of the id does not matter it just helps with binary search later
  247. Some(self.height.cmp(&other.height).then(self.id.cmp(&other.id)))
  248. }
  249. }
  250. let mut to_rerender = FxHashSet::default();
  251. to_rerender.extend(nodes_updated.iter().map(|(id, _)| id));
  252. let mut nodes_updated: Vec<_> = nodes_updated
  253. .into_iter()
  254. .map(|(id, mask)| NodeRef {
  255. id,
  256. height: self[id].height,
  257. node_mask: mask,
  258. to_check: StatesToCheck::All,
  259. })
  260. .collect();
  261. nodes_updated.sort();
  262. // Combine mutations that affect the same node.
  263. let mut last_node: Option<NodeRef> = None;
  264. let mut new_nodes_updated = VecDeque::new();
  265. for current in nodes_updated.into_iter() {
  266. if let Some(node) = &mut last_node {
  267. if *node == current {
  268. node.union_with(&current);
  269. } else {
  270. new_nodes_updated.push_back(last_node.replace(current).unwrap());
  271. }
  272. } else {
  273. last_node = Some(current);
  274. }
  275. }
  276. if let Some(node) = last_node {
  277. new_nodes_updated.push_back(node);
  278. }
  279. let nodes_updated = new_nodes_updated;
  280. // update the state that only depends on nodes. The order does not matter.
  281. for node_ref in &nodes_updated {
  282. let mut changed = false;
  283. let node = &mut self[node_ref.id];
  284. let ids = match &node_ref.to_check {
  285. StatesToCheck::All => node.state.node_dep_types(&node_ref.node_mask),
  286. StatesToCheck::Some(_) => unreachable!(),
  287. };
  288. for ty in ids {
  289. let node = &mut self[node_ref.id];
  290. let vnode = node.element(vdom);
  291. changed |= node.state.update_node_dep_state(ty, vnode, vdom, &ctx);
  292. }
  293. if changed {
  294. to_rerender.insert(node_ref.id);
  295. }
  296. }
  297. // bubble up state. To avoid calling reduce more times than nessisary start from the bottom and go up.
  298. let mut to_bubble = nodes_updated.clone();
  299. while let Some(node_ref) = to_bubble.pop_back() {
  300. let NodeRef {
  301. id,
  302. height,
  303. node_mask,
  304. to_check,
  305. } = node_ref;
  306. let (node, children) = self.get_node_children_mut(id).unwrap();
  307. let children_state: Vec<_> = children.iter().map(|c| &c.state).collect();
  308. let ids = match to_check {
  309. StatesToCheck::All => node.state.child_dep_types(&node_mask),
  310. StatesToCheck::Some(ids) => ids,
  311. };
  312. let mut changed = Vec::new();
  313. for ty in ids {
  314. let vnode = node.element(vdom);
  315. if node
  316. .state
  317. .update_child_dep_state(ty, vnode, vdom, &children_state, &ctx)
  318. {
  319. changed.push(ty);
  320. }
  321. }
  322. if let Some(parent_id) = node.parent {
  323. if !changed.is_empty() {
  324. to_rerender.insert(id);
  325. let i = to_bubble.partition_point(
  326. |NodeRef {
  327. id: other_id,
  328. height: h,
  329. ..
  330. }| {
  331. *h < height - 1 || (*h == height - 1 && *other_id < parent_id.0)
  332. },
  333. );
  334. // make sure the parent is not already queued
  335. if i < to_bubble.len() && to_bubble[i].id == parent_id.0 {
  336. to_bubble[i].to_check =
  337. to_bubble[i].to_check.union(&StatesToCheck::Some(changed));
  338. } else {
  339. to_bubble.insert(
  340. i,
  341. NodeRef {
  342. id: parent_id.0,
  343. height: height - 1,
  344. node_mask: NodeMask::NONE,
  345. to_check: StatesToCheck::Some(changed),
  346. },
  347. );
  348. }
  349. }
  350. }
  351. }
  352. // push down state. To avoid calling reduce more times than nessisary start from the top and go down.
  353. let mut to_push = nodes_updated.clone();
  354. while let Some(node_ref) = to_push.pop_front() {
  355. let NodeRef {
  356. id,
  357. height,
  358. node_mask,
  359. to_check,
  360. } = node_ref;
  361. let node = &self[id];
  362. let ids = match to_check {
  363. StatesToCheck::All => node.state.parent_dep_types(&node_mask),
  364. StatesToCheck::Some(ids) => ids,
  365. };
  366. let mut changed = Vec::new();
  367. let (node, parent) = self.get_node_parent_mut(id).unwrap();
  368. for ty in ids {
  369. let vnode = node.element(vdom);
  370. let parent = parent.as_deref();
  371. let state = &mut node.state;
  372. if state.update_parent_dep_state(ty, vnode, vdom, parent.map(|n| &n.state), &ctx) {
  373. changed.push(ty);
  374. }
  375. }
  376. to_rerender.insert(id);
  377. if !changed.is_empty() {
  378. let node = &self[id];
  379. if let NodeType::Element { children, .. } = &node.node_type {
  380. for c in children {
  381. let i = to_push.partition_point(
  382. |NodeRef {
  383. id: other_id,
  384. height: h,
  385. ..
  386. }| {
  387. *h < height + 1 || (*h == height + 1 && *other_id < c.0)
  388. },
  389. );
  390. if i < to_push.len() && to_push[i].id == c.0 {
  391. to_push[i].to_check = to_push[i]
  392. .to_check
  393. .union(&StatesToCheck::Some(changed.clone()));
  394. } else {
  395. to_push.insert(
  396. i,
  397. NodeRef {
  398. id: c.0,
  399. height: height + 1,
  400. node_mask: NodeMask::NONE,
  401. to_check: StatesToCheck::Some(changed.clone()),
  402. },
  403. );
  404. }
  405. }
  406. }
  407. }
  408. }
  409. Some(to_rerender)
  410. }
  411. fn link_child(&mut self, child_id: usize, parent_id: usize) -> Option<()> {
  412. debug_assert_ne!(child_id, parent_id);
  413. let parent = &mut self[parent_id];
  414. parent.add_child(ElementId(child_id));
  415. let parent_height = parent.height + 1;
  416. self[child_id].set_parent(ElementId(parent_id));
  417. self.increase_height(child_id, parent_height);
  418. Some(())
  419. }
  420. fn increase_height(&mut self, id: usize, amount: u16) {
  421. let n = &mut self[id];
  422. n.height += amount;
  423. if let NodeType::Element { children, .. } = &n.node_type {
  424. for c in children.clone() {
  425. self.increase_height(c.0, amount);
  426. }
  427. }
  428. }
  429. // remove a node and it's children from the dom.
  430. fn remove(&mut self, id: usize) -> Option<Node<S>> {
  431. // We do not need to remove the node from the parent's children list for children.
  432. fn inner<S: State>(dom: &mut RealDom<S>, id: usize) -> Option<Node<S>> {
  433. let mut node = dom.nodes[id as usize].take()?;
  434. if let NodeType::Element { children, .. } = &mut node.node_type {
  435. for c in children {
  436. inner(dom, c.0)?;
  437. }
  438. }
  439. Some(node)
  440. }
  441. let mut node = self.nodes[id as usize].take()?;
  442. if let Some(parent) = node.parent {
  443. let parent = &mut self[parent];
  444. parent.remove_child(ElementId(id));
  445. }
  446. if let NodeType::Element { children, .. } = &mut node.node_type {
  447. for c in children {
  448. inner(self, c.0)?;
  449. }
  450. }
  451. Some(node)
  452. }
  453. fn insert(&mut self, node: Node<S>) {
  454. let current_len = self.nodes.len();
  455. let id = node.id.0;
  456. if current_len - 1 < node.id.0 {
  457. // self.nodes.reserve(1 + id - current_len);
  458. self.nodes.extend((0..1 + id - current_len).map(|_| None));
  459. }
  460. self.nodes[id] = Some(node);
  461. }
  462. pub fn get(&self, id: usize) -> Option<&Node<S>> {
  463. self.nodes.get(id)?.as_ref()
  464. }
  465. pub fn get_mut(&mut self, id: usize) -> Option<&mut Node<S>> {
  466. self.nodes.get_mut(id)?.as_mut()
  467. }
  468. // this is safe because no node will have itself as a child
  469. pub fn get_node_children_mut<'a>(
  470. &'a mut self,
  471. id: usize,
  472. ) -> Option<(&'a mut Node<S>, Vec<&'a mut Node<S>>)> {
  473. let ptr = self.nodes.as_mut_ptr();
  474. unsafe {
  475. if id >= self.nodes.len() {
  476. None
  477. } else {
  478. let node = &mut *ptr.add(id);
  479. if let Some(node) = node.as_mut() {
  480. let children = match &node.node_type {
  481. NodeType::Element { children, .. } => children
  482. .iter()
  483. .map(|id| (&mut *ptr.add(id.0)).as_mut().unwrap())
  484. .collect(),
  485. _ => Vec::new(),
  486. };
  487. return Some((node, children));
  488. } else {
  489. None
  490. }
  491. }
  492. }
  493. }
  494. // this is safe because no node will have itself as a parent
  495. pub fn get_node_parent_mut<'a>(
  496. &'a mut self,
  497. id: usize,
  498. ) -> Option<(&'a mut Node<S>, Option<&'a mut Node<S>>)> {
  499. let ptr = self.nodes.as_mut_ptr();
  500. unsafe {
  501. let node = &mut *ptr.add(id);
  502. if id >= self.nodes.len() {
  503. None
  504. } else {
  505. if let Some(node) = node.as_mut() {
  506. let parent = node
  507. .parent
  508. .map(|id| (&mut *ptr.add(id.0)).as_mut().unwrap());
  509. return Some((node, parent));
  510. } else {
  511. None
  512. }
  513. }
  514. }
  515. }
  516. pub fn get_listening_sorted(&self, event: &'static str) -> Vec<&Node<S>> {
  517. if let Some(nodes) = self.nodes_listening.get(event) {
  518. let mut listening: Vec<_> = nodes.iter().map(|id| &self[*id]).collect();
  519. listening.sort_by(|n1, n2| (n1.height).cmp(&n2.height).reverse());
  520. listening
  521. } else {
  522. Vec::new()
  523. }
  524. }
  525. /// Check if the dom contains a node and its children.
  526. pub fn contains_node(&self, node: &VNode) -> bool {
  527. match node {
  528. VNode::Component(_) => {
  529. todo!()
  530. }
  531. VNode::Element(e) => {
  532. if let Some(id) = e.id.get() {
  533. let dom_node = &self[id];
  534. match &dom_node.node_type {
  535. NodeType::Element {
  536. tag,
  537. namespace,
  538. children,
  539. } => {
  540. tag == e.tag
  541. && namespace == &e.namespace
  542. && children.iter().copied().collect::<FxHashSet<_>>()
  543. == e.children
  544. .iter()
  545. .map(|c| c.mounted_id())
  546. .collect::<FxHashSet<_>>()
  547. && e.children.iter().all(|c| {
  548. self.contains_node(c)
  549. && self[c.mounted_id()].parent == e.id.get()
  550. })
  551. }
  552. _ => false,
  553. }
  554. } else {
  555. true
  556. }
  557. }
  558. VNode::Fragment(f) => f.children.iter().all(|c| self.contains_node(c)),
  559. VNode::Placeholder(_) => true,
  560. VNode::Text(t) => {
  561. if let Some(id) = t.id.get() {
  562. let dom_node = &self[id];
  563. match &dom_node.node_type {
  564. NodeType::Text { text } => t.text == text,
  565. _ => false,
  566. }
  567. } else {
  568. true
  569. }
  570. }
  571. }
  572. }
  573. /// Return the number of nodes in the dom.
  574. pub fn size(&self) -> usize {
  575. // The dom has a root node, ignore it.
  576. self.nodes.iter().filter(|n| n.is_some()).count() - 1
  577. }
  578. /// Returns the id of the root node.
  579. pub fn root_id(&self) -> usize {
  580. self.root
  581. }
  582. /// Call a function for each node in the dom, depth first.
  583. pub fn traverse_depth_first(&self, mut f: impl FnMut(&Node<S>)) {
  584. fn inner<S: State>(dom: &RealDom<S>, id: ElementId, f: &mut impl FnMut(&Node<S>)) {
  585. let node = &dom[id];
  586. f(node);
  587. if let NodeType::Element { children, .. } = &node.node_type {
  588. for c in children {
  589. inner(dom, *c, f);
  590. }
  591. }
  592. }
  593. if let NodeType::Element { children, .. } = &self[self.root].node_type {
  594. for c in children {
  595. inner(self, *c, &mut f);
  596. }
  597. }
  598. }
  599. /// Call a function for each node in the dom, depth first.
  600. pub fn traverse_depth_first_mut(&mut self, mut f: impl FnMut(&mut Node<S>)) {
  601. fn inner<S: State>(dom: &mut RealDom<S>, id: ElementId, f: &mut impl FnMut(&mut Node<S>)) {
  602. let node = &mut dom[id];
  603. f(node);
  604. if let NodeType::Element { children, .. } = &mut node.node_type {
  605. for c in children.clone() {
  606. inner(dom, c, f);
  607. }
  608. }
  609. }
  610. let root = self.root;
  611. if let NodeType::Element { children, .. } = &mut self[root].node_type {
  612. for c in children.clone() {
  613. inner(self, c, &mut f);
  614. }
  615. }
  616. }
  617. }
  618. impl<S: State> Index<usize> for RealDom<S> {
  619. type Output = Node<S>;
  620. fn index(&self, idx: usize) -> &Self::Output {
  621. self.get(idx).expect("Node does not exist")
  622. }
  623. }
  624. impl<S: State> Index<ElementId> for RealDom<S> {
  625. type Output = Node<S>;
  626. fn index(&self, idx: ElementId) -> &Self::Output {
  627. &self[idx.0]
  628. }
  629. }
  630. impl<S: State> IndexMut<usize> for RealDom<S> {
  631. fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
  632. self.get_mut(idx).expect("Node does not exist")
  633. }
  634. }
  635. impl<S: State> IndexMut<ElementId> for RealDom<S> {
  636. fn index_mut(&mut self, idx: ElementId) -> &mut Self::Output {
  637. &mut self[idx.0]
  638. }
  639. }
  640. /// The node is stored client side and stores only basic data about the node. For more complete information about the node see [`domNode::element`].
  641. #[derive(Debug, Clone)]
  642. pub struct Node<S: State> {
  643. /// The id of the node this node was created from.
  644. pub id: ElementId,
  645. /// The parent id of the node.
  646. pub parent: Option<ElementId>,
  647. /// State of the node.
  648. pub state: S,
  649. /// Additional inforation specific to the node type
  650. pub node_type: NodeType,
  651. /// The number of parents before the root node. The root node has height 1.
  652. pub height: u16,
  653. }
  654. #[derive(Debug, Clone)]
  655. pub enum NodeType {
  656. Text {
  657. text: String,
  658. },
  659. Element {
  660. tag: String,
  661. namespace: Option<&'static str>,
  662. children: Vec<ElementId>,
  663. },
  664. Placeholder,
  665. }
  666. impl<S: State> Node<S> {
  667. fn new(id: u64, node_type: NodeType) -> Self {
  668. Node {
  669. id: ElementId(id as usize),
  670. parent: None,
  671. node_type,
  672. state: S::default(),
  673. height: 0,
  674. }
  675. }
  676. /// Returns a reference to the element that this node refrences.
  677. pub fn element<'b>(&self, vdom: &'b VirtualDom) -> &'b VNode<'b> {
  678. vdom.get_element(self.id).unwrap()
  679. }
  680. fn add_child(&mut self, child: ElementId) {
  681. if let NodeType::Element { children, .. } = &mut self.node_type {
  682. children.push(child);
  683. }
  684. }
  685. fn remove_child(&mut self, child: ElementId) {
  686. if let NodeType::Element { children, .. } = &mut self.node_type {
  687. children.retain(|c| c != &child);
  688. }
  689. }
  690. fn set_parent(&mut self, parent: ElementId) {
  691. self.parent = Some(parent);
  692. }
  693. }