Feat: remove old code

This commit is contained in:
Jonathan Kelley committed 2021-05-16 03:01:36 -04:00
1 parent 2284b35782
commit 3de54d0b52
9 files changed
+37 -2054

No files matched your search

+29
View File
@@ -497,3 +497,32 @@ fn try_parse_bracketed(stream: &ParseBuffer) -> Result<Expr> {
syn::braced!(content in stream);
content.parse()
}
// // Used to uniquely identify elements that contain closures so that the DomUpdater can
// // look them up by their unique id.
// // When the DomUpdater sees that the element no longer exists it will drop all of it's
// // Rc'd Closures for those events.
// // It doesn't quite make sense to keep this here, perhaps just in the html crate?
// // Dioxus itself shouldn't be concerned with the attribute names
// // a ftk!
// static SELF_CLOSING_TAGS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
// [
// "area", "base", "br", "col", "hr", "img", "input", "link", "meta", "param", "command",
// "keygen", "source",
// ]
// .iter()
// .cloned()
// .collect()
// });
// /// Whether or not this tag is self closing
// ///
// /// ```ignore
// /// use dioxus_core::validation::is_self_closing;
// /// assert_eq!(is_self_closing("br"), true);
// /// assert_eq!(is_self_closing("div"), false);
// /// ```
// pub fn is_self_closing(tag: &str) -> bool {
// SELF_CLOSING_TAGS.contains(tag)
// // SELF_CLOSING_TAGS.contains(tag) || is_self_closing_svg_tag(tag)
// }
-589
View File
@@ -1,589 +0,0 @@
// we don't want to necessarily adhere to the spec for dioxus core
// 3rd party libs can implement their own builders if they want
// macro_rules! builder_constructors {
// ( $(
// $(#[$attr:meta])*
// $name:ident;
// )* ) => {
// $(
// $(#[$attr])*
// #[inline]
// pub fn $name<'a, B>(
// bump: B,
// ) -> ElementBuilder<
// 'a,
// bumpalo::collections::Vec<'a, Listener<'a>>,
// bumpalo::collections::Vec<'a, Attribute<'a>>,
// bumpalo::collections::Vec<'a, VNode<'a>>,
// >
// where
// B: Into<&'a Bump>
// {
// ElementBuilder::new(bump, stringify!($name))
// }
// )*
// };
// ( $(
// $(#[$attr:meta])*
// $name:ident <> $namespace:tt;
// )* ) => {
// $(
// $(#[$attr])*
// #[inline]
// pub fn $name<'a>(
// bump: &'a Bump,
// ) -> ElementBuilder<
// 'a,
// bumpalo::collections::Vec<'a, Listener<'a>>,
// bumpalo::collections::Vec<'a, Attribute<'a>>,
// bumpalo::collections::Vec<'a, VNode<'a>>,
// > {
// let builder = ElementBuilder::new(bump, stringify!($name));
// builder.namespace(Some($namespace))
// }
// )*
// }
// }
// // Organized in the same order as
// // https://developer.mozilla.org/en-US/docs/Web/HTML/Element
// //
// // Does not include obsolete elements.
// builder_constructors! {
// // Document metadata
// /// Build a
// /// [`<base>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base)
// /// element.
// base;
// /// Build a
// /// [`<head>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head)
// /// element.
// head;
// /// Build a
// /// [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
// /// element.
// link;
// /// Build a
// /// [`<meta>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta)
// /// element.
// meta;
// /// Build a
// /// [`<style>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style)
// /// element.
// style;
// /// Build a
// /// [`<title>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title)
// /// element.
// title;
// // Sectioning root
// /// Build a
// /// [`<body>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body)
// /// element.
// body;
// // Content sectioning
// /// Build a
// /// [`<address>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/address)
// /// element.
// address;
// /// Build a
// /// [`<article>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/article)
// /// element.
// article;
// /// Build a
// /// [`<aside>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/aside)
// /// element.
// aside;
// /// Build a
// /// [`<footer>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/footer)
// /// element.
// footer;
// /// Build a
// /// [`<header>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/header)
// /// element.
// header;
// /// Build a
// /// [`<h1>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h1)
// /// element.
// h1;
// /// Build a
// /// [`<h2>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h2)
// /// element.
// h2;
// /// Build a
// /// [`<h3>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h3)
// /// element.
// h3;
// /// Build a
// /// [`<h4>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h4)
// /// element.
// h4;
// /// Build a
// /// [`<h5>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h5)
// /// element.
// h5;
// /// Build a
// /// [`<h6>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h6)
// /// element.
// h6;
// /// Build a
// /// [`<hgroup>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hgroup)
// /// element.
// hgroup;
// /// Build a
// /// [`<main>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/main)
// /// element.
// main;
// /// Build a
// /// [`<nav>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/nav)
// /// element.
// nav;
// /// Build a
// /// [`<section>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section)
// /// element.
// section;
// // Text content
// /// Build a
// /// [`<blockquote>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/blockquote)
// /// element.
// blockquote;
// /// Build a
// /// [`<dd>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dd)
// /// element.
// dd;
// /// Build a
// /// [`<div>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div)
// /// element.
// div;
// /// Build a
// /// [`<dl>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dl)
// /// element.
// dl;
// /// Build a
// /// [`<dt>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dt)
// /// element.
// dt;
// /// Build a
// /// [`<figcaption>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption)
// /// element.
// figcaption;
// /// Build a
// /// [`<figure>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure)
// /// element.
// figure;
// /// Build a
// /// [`<hr>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hr)
// /// element.
// hr;
// /// Build a
// /// [`<li>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/li)
// /// element.
// li;
// /// Build a
// /// [`<ol>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol)
// /// element.
// ol;
// /// Build a
// /// [`<p>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/p)
// /// element.
// p;
// /// Build a
// /// [`<pre>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre)
// /// element.
// pre;
// /// Build a
// /// [`<ul>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ul)
// /// element.
// ul;
// // Inline text semantics
// /// Build a
// /// [`<a>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a)
// /// element.
// a;
// /// Build a
// /// [`<abbr>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/abbr)
// /// element.
// abbr;
// /// Build a
// /// [`<b>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/b)
// /// element.
// b;
// /// Build a
// /// [`<bdi>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/bdi)
// /// element.
// bdi;
// /// Build a
// /// [`<bdo>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/bdo)
// /// element.
// bdo;
// /// Build a
// /// [`<br>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br)
// /// element.
// br;
// /// Build a
// /// [`<cite>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/cite)
// /// element.
// cite;
// /// Build a
// /// [`<code>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/code)
// /// element.
// code;
// /// Build a
// /// [`<data>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/data)
// /// element.
// data;
// /// Build a
// /// [`<dfn>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dfn)
// /// element.
// dfn;
// /// Build a
// /// [`<em>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/em)
// /// element.
// em;
// /// Build a
// /// [`<i>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/i)
// /// element.
// i;
// /// Build a
// /// [`<kbd>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/kbd)
// /// element.
// kbd;
// /// Build a
// /// [`<mark>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/mark)
// /// element.
// mark;
// /// Build a
// /// [`<q>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/q)
// /// element.
// q;
// /// Build a
// /// [`<rb>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/rb)
// /// element.
// rb;
// /// Build a
// /// [`<rp>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/rp)
// /// element.
// rp;
// /// Build a
// /// [`<rt>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/rt)
// /// element.
// rt;
// /// Build a
// /// [`<rtc>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/rtc)
// /// element.
// rtc;
// /// Build a
// /// [`<ruby>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby)
// /// element.
// ruby;
// /// Build a
// /// [`<s>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/s)
// /// element.
// s;
// /// Build a
// /// [`<samp>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/samp)
// /// element.
// samp;
// /// Build a
// /// [`<small>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/small)
// /// element.
// small;
// /// Build a
// /// [`<span>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span)
// /// element.
// span;
// /// Build a
// /// [`<strong>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/strong)
// /// element.
// strong;
// /// Build a
// /// [`<sub>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sub)
// /// element.
// sub;
// /// Build a
// /// [`<sup>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sup)
// /// element.
// sup;
// /// Build a
// /// [`<time>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/time)
// /// element.
// time;
// /// Build a
// /// [`<u>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/u)
// /// element.
// u;
// /// Build a
// /// [`<var>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/var)
// /// element.
// var;
// /// Build a
// /// [`<wbr>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/wbr)
// /// element.
// wbr;
// // Image and multimedia
// /// Build a
// /// [`<area>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area)
// /// element.
// area;
// /// Build a
// /// [`<audio>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio)
// /// element.
// audio;
// /// Build a
// /// [`<img>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img)
// /// element.
// img;
// /// Build a
// /// [`<map>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/map)
// /// element.
// map;
// /// Build a
// /// [`<track>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/track)
// /// element.
// track;
// /// Build a
// /// [`<video>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video)
// /// element.
// video;
// // Embedded content
// /// Build a
// /// [`<embed>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/embed)
// /// element.
// embed;
// /// Build a
// /// [`<iframe>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe)
// /// element.
// iframe;
// /// Build a
// /// [`<object>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object)
// /// element.
// object;
// /// Build a
// /// [`<param>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/param)
// /// element.
// param;
// /// Build a
// /// [`<picture>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture)
// /// element.
// picture;
// /// Build a
// /// [`<source>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/source)
// /// element.
// source;
// // Scripting
// /// Build a
// /// [`<canvas>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas)
// /// element.
// canvas;
// /// Build a
// /// [`<noscript>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript)
// /// element.
// noscript;
// /// Build a
// /// [`<script>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
// /// element.
// script;
// // Demarcating edits
// /// Build a
// /// [`<del>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/del)
// /// element.
// del;
// /// Build a
// /// [`<ins>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ins)
// /// element.
// ins;
// // Table content
// /// Build a
// /// [`<caption>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption)
// /// element.
// caption;
// /// Build a
// /// [`<col>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/col)
// /// element.
// col;
// /// Build a
// /// [`<colgroup>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/colgroup)
// /// element.
// colgroup;
// /// Build a
// /// [`<table>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table)
// /// element.
// table;
// /// Build a
// /// [`<tbody>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tbody)
// /// element.
// tbody;
// /// Build a
// /// [`<td>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td)
// /// element.
// td;
// /// Build a
// /// [`<tfoot>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tfoot)
// /// element.
// tfoot;
// /// Build a
// /// [`<th>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/th)
// /// element.
// th;
// /// Build a
// /// [`<thead>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/thead)
// /// element.
// thead;
// /// Build a
// /// [`<tr>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tr)
// /// element.
// tr;
// // Forms
// /// Build a
// /// [`<button>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button)
// /// element.
// button;
// /// Build a
// /// [`<datalist>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist)
// /// element.
// datalist;
// /// Build a
// /// [`<fieldset>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset)
// /// element.
// fieldset;
// /// Build a
// /// [`<form>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form)
// /// element.
// form;
// /// Build a
// /// [`<input>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input)
// /// element.
// input;
// /// Build a
// /// [`<label>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label)
// /// element.
// label;
// /// Build a
// /// [`<legend>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/legend)
// /// element.
// legend;
// /// Build a
// /// [`<meter>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meter)
// /// element.
// meter;
// /// Build a
// /// [`<optgroup>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup)
// /// element.
// optgroup;
// /// Build a
// /// [`<option>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option)
// /// element.
// option;
// /// Build a
// /// [`<output>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/output)
// /// element.
// output;
// /// Build a
// /// [`<progress>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/progress)
// /// element.
// progress;
// /// Build a
// /// [`<select>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select)
// /// element.
// select;
// /// Build a
// /// [`<textarea>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea)
// /// element.
// textarea;
// // Interactive elements
// /// Build a
// /// [`<details>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details)
// /// element.
// details;
// /// Build a
// /// [`<dialog>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog)
// /// element.
// dialog;
// /// Build a
// /// [`<menu>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/menu)
// /// element.
// menu;
// /// Build a
// /// [`<menuitem>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/menuitem)
// /// element.
// menuitem;
// /// Build a
// /// [`<summary>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/summary)
// /// element.
// summary;
// // Web components
// /// Build a
// /// [`<slot>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot)
// /// element.
// slot;
// /// Build a
// /// [`<template>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template)
// /// element.
// template;
// }
// builder_constructors! {
// // SVG components
// /// Build a
// /// [`<svg>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg)
// /// element.
// svg <> "http://www.w3.org/2000/svg" ;
// /// Build a
// /// [`<path>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path)
// /// element.
// path <> "http://www.w3.org/2000/svg";
// /// Build a
// /// [`<circle>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/circle)
// /// element.
// circle <> "http://www.w3.org/2000/svg";
// /// Build a
// /// [`<ellipse>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/ellipse)
// /// element.
// ellipse <> "http://www.w3.org/2000/svg";
// /// Build a
// /// [`<line>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/line)
// /// element.
// line <> "http://www.w3.org/2000/svg";
// /// Build a
// /// [`<polygon>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/polygon)
// /// element.
// polygon <> "http://www.w3.org/2000/svg";
// /// Build a
// /// [`<polyline>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/polyline)
// /// element.
// polyline <> "http://www.w3.org/2000/svg";
// /// Build a
// /// [`<rect>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/rect)
// /// element.
// rect <> "http://www.w3.org/2000/svg";
// /// Build a
// /// [`<image>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/image)
// /// element.
// image <> "http://www.w3.org/2000/svg";
// }
File diff suppressed because it is too large. Load diff
-80
View File
@@ -1,80 +0,0 @@
use fxhash::FxHashMap;
use crate::innerlude::{VNode, VText};
/// A Patch encodes an operation that modifies a real DOM element.
///
/// To update the real DOM that a user sees you'll want to first diff your
/// old virtual dom and new virtual dom.
///
/// This diff operation will generate `Vec<Patch>` with zero or more patches that, when
/// applied to your real DOM, will make your real DOM look like your new virtual dom.
///
/// Each Patch has a u32 node index that helps us identify the real DOM node that it applies to.
///
/// Our old virtual dom's nodes are indexed depth first, as shown in this illustration
/// (0 being the root node, 1 being it's first child, 2 being it's first child's first child).
///
/// ```text
/// .─.
/// ( 0 )
/// `┬'
/// ┌────┴──────┐
/// │ │
/// ▼ ▼
/// .─. .─.
/// ( 1 ) ( 4 )
/// `┬' `─'
/// ┌────┴───┐ ├─────┬─────┐
/// │ │ │ │ │
/// ▼ ▼ ▼ ▼ ▼
/// .─. .─. .─. .─. .─.
/// ( 2 ) ( 3 ) ( 5 ) ( 6 ) ( 7 )
/// `─' `─' `─' `─' `─'
/// ```
///
/// The patching process is tested in a real browser in crates/virtual-dom-rs/tests/diff_patch.rs
// #[derive(serde::Serialize, serde::Deserialize)]
pub enum Patch<'a> {
/// Append a vector of child nodes to a parent node id.
AppendChildren(NodeIdx, Vec<&'a VNode<'a>>),
/// For a `node_i32`, remove all children besides the first `len`
TruncateChildren(NodeIdx, usize),
/// Replace a node with another node. This typically happens when a node's tag changes.
/// ex: <div> becomes <span>
Replace(NodeIdx, &'a VNode<'a>),
/// Add attributes that the new node has that the old node does not
AddAttributes(NodeIdx, FxHashMap<&'a str, &'a str>),
/// Remove attributes that the old node had that the new node doesn't
RemoveAttributes(NodeIdx, Vec<&'a str>),
/// Change the text of a Text node.
ChangeText(NodeIdx, &'a VText<'a>),
}
type NodeIdx = usize;
impl<'a> Patch<'a> {
/// Every Patch is meant to be applied to a specific node within the DOM. Get the
/// index of the DOM node that this patch should apply to. DOM nodes are indexed
/// depth first with the root node in the tree having index 0.
pub fn node_idx(&self) -> usize {
match self {
Patch::AppendChildren(node_idx, _) => *node_idx,
Patch::TruncateChildren(node_idx, _) => *node_idx,
Patch::Replace(node_idx, _) => *node_idx,
Patch::AddAttributes(node_idx, _) => *node_idx,
Patch::RemoveAttributes(node_idx, _) => *node_idx,
Patch::ChangeText(node_idx, _) => *node_idx,
}
}
}
pub struct PatchList<'a> {
patches: Vec<Patch<'a>>,
}
-329
View File
@@ -1,329 +0,0 @@
//! A primitive diffing algorithm
//!
//!
//!
//!
//!
use std::{collections::HashMap, mem};
use crate::innerlude::*;
use crate::patch::Patch;
use fxhash::{FxBuildHasher, FxHashMap, FxHashSet};
use generational_arena::Index;
pub struct DiffMachine {
immediate_queue: Vec<Index>,
diffed: FxHashSet<Index>,
need_to_diff: FxHashSet<Index>,
marked_for_removal: Vec<Index>,
}
impl DiffMachine {
pub fn new() -> Self {
Self {
immediate_queue: vec![],
diffed: FxHashSet::default(),
need_to_diff: FxHashSet::default(),
marked_for_removal: vec![],
}
}
/// Given two VirtualNode's generate Patch's that would turn the old virtual node's
/// real DOM node equivalent into the new VirtualNode's real DOM node equivalent.
pub fn diff<'a>(&mut self, old: &'a VNode, new: &'a VNode) -> Vec<Patch<'a>> {
self.diff_recursive(&old, &new, &mut 0)
}
pub fn diff_recursive<'a, 'b>(
&mut self,
old: &'a VNode,
new: &'a VNode,
cur_node_idx: &'b mut usize,
) -> Vec<Patch<'a>> {
let mut patches = vec![];
let mut replace = false;
// Different enum variants, replace!
if mem::discriminant(old) != mem::discriminant(new) {
replace = true;
}
if let (VNode::Element(old_element), VNode::Element(new_element)) = (old, new) {
// Replace if there are different element tags
if old_element.tag_name != new_element.tag_name {
// if old_element.tag != new_element.tag {
replace = true;
}
// Replace if two elements have different keys
// TODO: More robust key support. This is just an early stopgap to allow you to force replace
// an element... say if it's event changed. Just change the key name for now.
// In the future we want keys to be used to create a Patch::ReOrder to re-order siblings
// todo!
// if old_element.attributes.get("key").is_some()
// && old_element.attrs.get("key") != new_element.attrs.get("key")
// {
// replace = true;
// }
}
// Handle replacing of a node
if replace {
patches.push(Patch::Replace(*cur_node_idx, &new));
if let VNode::Element(old_element_node) = old {
for child in old_element_node.children.iter() {
increment_node_idx_for_children(child, cur_node_idx);
}
}
return patches;
}
// The following comparison can only contain identical variants, other
// cases have already been handled above by comparing variant
// discriminants.
match (old, new) {
// We're comparing two text nodes
(VNode::Text(old_text), VNode::Text(new_text)) => {
if old_text != new_text {
patches.push(Patch::ChangeText(*cur_node_idx, &new_text));
}
}
// We're comparing two element nodes
(VNode::Element(old_element), VNode::Element(new_element)) => {
// let b: HashMap<&str, &str, FxBuildHasher> = HashMap::new()
let old_attrs = old_element
.attributes
.iter()
.map(|f| (f.name, f.value))
.collect::<HashMap<&'static str, &str, FxBuildHasher>>();
let new_attrs = old_element
.attributes
.iter()
.map(|f| (f.name, f.value))
.collect::<HashMap<&'static str, &str, FxBuildHasher>>();
let mut add_attributes = FxHashMap::<&'static str, &str>::default();
// [("blah", "blah")]
// .into_iter()
// .map(|f| (f.0, f.1))
// .collect::<HashMap<&'static str, &str, FxBuildHasher>>();
// let mut add_attribute = HashMap::<&str, &str, FxBuildHasher>::new();
let mut remove_attributes: Vec<&str> = vec![];
// TODO: -> split out into func
for (new_attr_name, new_attr_val) in new_attrs.iter() {
// for (new_attr_name, new_attr_val) in new_element.attrs.iter() {
match old_attrs.get(new_attr_name) {
// match old_element.attrs.get(new_attr_name) {
Some(ref old_attr_val) => {
if old_attr_val != &new_attr_val {
add_attributes.insert(new_attr_name, new_attr_val);
}
}
None => {
add_attributes.insert(new_attr_name, new_attr_val);
}
};
}
// TODO: -> split out into func
for (old_attr_name, old_attr_val) in old_attrs.iter() {
// for (old_attr_name, old_attr_val) in old_element.attrs.iter() {
if add_attributes.get(&old_attr_name[..]).is_some() {
continue;
};
match new_attrs.get(old_attr_name) {
// match new_element.attrs.get(old_attr_name) {
Some(ref new_attr_val) => {
if new_attr_val != &old_attr_val {
remove_attributes.push(old_attr_name);
}
}
None => {
remove_attributes.push(old_attr_name);
}
};
}
if add_attributes.len() > 0 {
patches.push(Patch::AddAttributes(*cur_node_idx, add_attributes));
}
if remove_attributes.len() > 0 {
patches.push(Patch::RemoveAttributes(*cur_node_idx, remove_attributes));
}
let old_child_count = old_element.children.len();
let new_child_count = new_element.children.len();
if new_child_count > old_child_count {
let append_patch: Vec<&'a VNode> =
new_element.children[old_child_count..].iter().collect();
patches.push(Patch::AppendChildren(*cur_node_idx, append_patch))
}
if new_child_count < old_child_count {
patches.push(Patch::TruncateChildren(*cur_node_idx, new_child_count))
}
let min_count = std::cmp::min(old_child_count, new_child_count);
for index in 0..min_count {
*cur_node_idx = *cur_node_idx + 1;
let old_child = &old_element.children[index];
let new_child = &new_element.children[index];
patches.append(&mut self.diff_recursive(&old_child, &new_child, cur_node_idx))
}
if new_child_count < old_child_count {
for child in old_element.children[min_count..].iter() {
increment_node_idx_for_children(child, cur_node_idx);
}
}
}
(VNode::Suspended, _)
| (_, VNode::Suspended)
| (VNode::Component(_), _)
| (_, VNode::Component(_)) => {
todo!("cant yet handle these two")
}
(VNode::Text(_), VNode::Element(_))
| (VirtualNode::Element(_), VirtualNode::Text(_)) => {
unreachable!("Unequal variant discriminants should already have been handled");
}
};
// new_root.create_element()
patches
}
}
fn increment_node_idx_for_children<'a, 'b>(old: &'a VirtualNode, cur_node_idx: &'b mut usize) {
*cur_node_idx += 1;
if let VirtualNode::Element(element_node) = old {
for child in element_node.children.iter() {
increment_node_idx_for_children(&child, cur_node_idx);
}
}
}
// #[cfg(test)]
mod tests {
use bumpalo::Bump;
use super::*;
fn test_diff(
tree1: impl Fn(&Bump) -> VNode<'_>,
tree2: impl Fn(&Bump) -> VNode<'_>,
expected_patches: Vec<Patch>,
description: &'static str,
) {
let bump = Bump::new();
let nodes1 = tree1(&bump);
let nodes2 = tree1(&bump);
let mut machine = DiffMachine::new();
let patches = machine.diff(&nodes1, &nodes2);
patches
.iter()
.zip(expected_patches.iter())
.for_each(|f| assert_eq!(compare_patch(f.0, f.1), true, "{}", description));
}
// todo: make this actually perform real comparisons
// by default, nothing is derived for vnodes or patches
fn compare_patch(patch1: &Patch, patch2: &Patch) -> bool {
match (patch1, patch2) {
(Patch::AppendChildren(_, _), Patch::AppendChildren(_, _)) => true,
(Patch::AppendChildren(_, _), _) => false,
(Patch::TruncateChildren(_, _), Patch::TruncateChildren(_, _)) => true,
(Patch::TruncateChildren(_, _), _) => false,
(Patch::Replace(_, _), Patch::Replace(_, _)) => true,
(Patch::Replace(_, _), _) => false,
(Patch::AddAttributes(_, _), Patch::AddAttributes(_, _)) => true,
(Patch::AddAttributes(_, _), _) => false,
(Patch::RemoveAttributes(_, _), Patch::RemoveAttributes(_, _)) => true,
(Patch::RemoveAttributes(_, _), _) => false,
(Patch::ChangeText(_, _), Patch::ChangeText(_, _)) => true,
(Patch::ChangeText(_, _), _) => false,
}
}
fn printdiff(
tree1: impl for<'a> Fn(&'a Bump) -> VNode<'a>,
tree2: impl for<'a> Fn(&'a Bump) -> VNode<'a>,
desc: &'static str,
) {
let bump = Bump::new();
let nodes1 = tree1(&bump);
let nodes2 = tree2(&bump);
let mut machine = DiffMachine::new();
let patches = machine.diff(&nodes1, &nodes2);
patches.iter().for_each(|f| match f {
Patch::AppendChildren(idx, a) => {
println!("AppendChildren");
}
Patch::TruncateChildren(idx, a) => {
println!("TruncateChildren");
}
Patch::Replace(idx, a) => {
println!("Replace");
}
Patch::AddAttributes(idx, a) => {
println!("AddAttributes");
}
Patch::RemoveAttributes(idx, a) => {
println!("RemoveAttributes");
}
Patch::ChangeText(idx, a) => {
println!("ChangeText");
}
});
}
#[test]
fn example_diff() {
printdiff(
html! { <div> </div> },
html! { <div>"Hello world!" </div> },
"demo the difference between two simple dom tree",
);
printdiff(
html! {
<div>
"Hello world!"
</div>
},
html! {
<div>
<div>
"Hello world!"
"Hello world!"
"Hello world!"
"Hello world!"
"Hello world!"
</div>
</div>
},
"demo the difference between two simple dom tree",
);
}
}
-40
View File
@@ -1,40 +0,0 @@
/// TODO @Jon
/// Figure out if validation should be its own crate, or embedded directly into dioxus
///
/// Should we even be bothered with validation?
///
///
///
mod validation {
use once_cell::sync::Lazy;
use std::collections::HashSet;
// Used to uniquely identify elements that contain closures so that the DomUpdater can
// look them up by their unique id.
// When the DomUpdater sees that the element no longer exists it will drop all of it's
// Rc'd Closures for those events.
// It doesn't quite make sense to keep this here, perhaps just in the html crate?
// Dioxus itself shouldn't be concerned with the attribute names
// a ftk!
static SELF_CLOSING_TAGS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
[
"area", "base", "br", "col", "hr", "img", "input", "link", "meta", "param", "command",
"keygen", "source",
]
.iter()
.cloned()
.collect()
});
/// Whether or not this tag is self closing
///
/// ```ignore
/// use dioxus_core::validation::is_self_closing;
/// assert_eq!(is_self_closing("br"), true);
/// assert_eq!(is_self_closing("div"), false);
/// ```
pub fn is_self_closing(tag: &str) -> bool {
SELF_CLOSING_TAGS.contains(tag)
// SELF_CLOSING_TAGS.contains(tag) || is_self_closing_svg_tag(tag)
}
}
View File
Whitespace-only changes.
+1 -1
View File
@@ -41,7 +41,7 @@ pub struct VirtualDom {
///
/// This is wrapped in an UnsafeCell because we will need to get mutable access to unique values in unique bump arenas
/// and rusts's guartnees cannot prove that this is safe. We will need to maintain the safety guarantees manually.
components: ScopeArena,
pub components: ScopeArena,
/// The index of the root component
/// Should always be the first (gen0, id0)
+7 -9
View File
@@ -9,14 +9,7 @@ struct SsrRenderer {
impl Display for SsrRenderer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let node = self
.dom
.base_scope()
// .components
// .get(self.dom.base_scope)
// .unwrap()
.frames
.current_head_node();
let node = self.dom.base_scope().frames.current_head_node();
html_render(&self.dom, node, f)
}
@@ -45,7 +38,12 @@ fn html_render(
VNode::Suspended => todo!(),
VNode::Component(vcomp) => {
let id = vcomp.ass_scope.as_ref().borrow().unwrap();
let new_node = dom.components.get(id).unwrap().frames.current_head_node();
let new_node = dom
.components
.try_get(id)
.unwrap()
.frames
.current_head_node();
html_render(&dom, new_node, f)
}
}