|
1 | | -import { isFalse, isText } from '../shared/nodes'; |
| 1 | +import { isUndefined, isFalse, isText } from '../shared/nodes'; |
2 | 2 | import { anchorableElement } from './anchorableNode'; |
3 | 3 | import client from './client'; |
4 | 4 | import render from './render'; |
5 | 5 |
|
6 | | -export default function rerender(selector, current, next) { |
| 6 | +function updateAttributes(selector, current, next) { |
| 7 | + const attributeNames = Object.keys({ ...current.attributes, ...next.attributes }); |
| 8 | + for (const name of attributeNames) { |
| 9 | + if (name === 'html') { |
| 10 | + if (next.attributes[name] !== current.attributes[name]) { |
| 11 | + selector.innerHTML = next.attributes[name]; |
| 12 | + } |
| 13 | + next.attributes['data-n'] === undefined && anchorableElement(selector); |
| 14 | + } else if (name === 'checked') { |
| 15 | + if (next.attributes[name] !== selector.value) { |
| 16 | + selector.checked = next.attributes[name]; |
| 17 | + } |
| 18 | + } else if (name === 'value') { |
| 19 | + if (next.attributes[name] !== selector.value) { |
| 20 | + selector.value = next.attributes[name]; |
| 21 | + } |
| 22 | + } else if (name.startsWith('on')) { |
| 23 | + const eventName = name.replace('on', ''); |
| 24 | + const key = '_event.' + eventName; |
| 25 | + selector.removeEventListener(eventName, current[key]); |
| 26 | + if (next.attributes[name]) { |
| 27 | + next[key] = (event) => { |
| 28 | + if (next.attributes.default !== true) { |
| 29 | + event.preventDefault(); |
| 30 | + } |
| 31 | + next.attributes[name]({ ...next.attributes, event }); |
| 32 | + }; |
| 33 | + selector.addEventListener(eventName, next[key]); |
| 34 | + } |
| 35 | + } else { |
| 36 | + const type = typeof (next.attributes[name]); |
| 37 | + if (type !== 'object' && type !== 'function') { |
| 38 | + if (current.attributes[name] !== undefined && next.attributes[name] === undefined) { |
| 39 | + selector.removeAttribute(name); |
| 40 | + } else if (current.attributes[name] !== next.attributes[name]) { |
| 41 | + if (name != 'value' && next.attributes[name] === false || next.attributes[name] === null || next.attributes[name] === undefined) { |
| 42 | + selector.removeAttribute(name); |
| 43 | + } else if (name != 'value' && next.attributes[name] === true) { |
| 44 | + selector.setAttribute(name, ''); |
| 45 | + } else { |
| 46 | + selector.setAttribute(name, next.attributes[name]); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function _rerender(current, next) { |
7 | 55 |
|
8 | | - current = current === undefined ? client.virtualDom : current; |
9 | | - next = next === undefined ? client.nextVirtualDom : next; |
| 56 | + const selector = current.element |
| 57 | + next.element = current.element |
10 | 58 |
|
11 | 59 | if (next.instance) { |
12 | 60 | next.instance._self.element = selector; |
13 | 61 | } |
14 | 62 |
|
15 | | - if (!client.hydrated && selector) { |
16 | | - for (const element of selector.childNodes) { |
17 | | - if (element.tagName && element.tagName.toLowerCase() == 'textarea' && element.childNodes.length == 0) { |
18 | | - element.appendChild(document.createTextNode('')); |
19 | | - } |
20 | | - if (element.COMMENT_NODE === 8 && element.textContent === '#') { |
21 | | - selector.removeChild(element); |
22 | | - } |
23 | | - } |
24 | | - } |
25 | | - |
26 | 63 | if (isFalse(current) && isFalse(next)) { |
27 | 64 | return; |
28 | 65 | } |
29 | 66 |
|
30 | 67 | if ((isFalse(current) || isFalse(next)) && current != next) { |
31 | 68 | const nextSelector = render(next); |
32 | | - return selector.replaceWith(nextSelector); |
| 69 | + selector.replaceWith(nextSelector); |
| 70 | + if (current.type !== 'head' && next.type !== 'head') { |
| 71 | + return |
| 72 | + } |
33 | 73 | } |
34 | 74 |
|
35 | | - if (current.type == 'head' && next.type == 'head') { |
36 | | - return; |
| 75 | + if (current.type == 'head' ^ next.type == 'head') { |
| 76 | + const nextSelector = render(next); |
| 77 | + selector.replaceWith(nextSelector); |
37 | 78 | } |
38 | 79 |
|
39 | | - if (current.type == 'head' || next.type == 'head') { |
40 | | - const nextSelector = render(next); |
41 | | - return selector.replaceWith(nextSelector); |
| 80 | + if (current.type !== 'head' && next.type === 'head') { |
| 81 | + const limit = next.children.length; |
| 82 | + for (let i = limit - 1; i > -1; i--) { |
| 83 | + const nextSelector = render(next.children[i]); |
| 84 | + document.querySelector('head').appendChild(nextSelector) |
| 85 | + } |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + if (current.type === 'head' && next.type !== 'head') { |
| 90 | + const limit = current.children.length; |
| 91 | + for (let i = limit - 1; i > -1; i--) { |
| 92 | + document.querySelector(`[data-n="${current.children[i].attributes['data-n']}"]`).remove() |
| 93 | + } |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + if (next.type === 'head') { |
| 98 | + const limit = Math.max(current.children.length, next.children.length); |
| 99 | + for (let i = limit - 1; i > -1; i--) { |
| 100 | + if (isUndefined(current.children[i]) && !isFalse(next.children[i])) { |
| 101 | + const nextSelector = render(next.children[i]); |
| 102 | + document.querySelector('head').appendChild(nextSelector) |
| 103 | + } else if (isUndefined(next.children[i]) && !isFalse(current.children[i])) { |
| 104 | + current.children[i].element.remove() |
| 105 | + } else if (!isFalse(current.children[i]) && !isFalse(next.children[i])) { |
| 106 | + if (current.children[i].type === next.children[i].type) { |
| 107 | + next.children[i].element = current.children[i].element |
| 108 | + const element = document.querySelector(`[data-n="${next.children[i].attributes['data-n']}"]`) |
| 109 | + updateAttributes(element, current.children[i], next.children[i]) |
| 110 | + } else { |
| 111 | + document.querySelector(`[data-n="${current.children[i].attributes['data-n']}"]`).remove() |
| 112 | + const nextSelector = render(next.children[i]); |
| 113 | + document.querySelector('head').appendChild(nextSelector) |
| 114 | + } |
| 115 | + } else if (isFalse(current.children[i]) && !isFalse(next.children[i])) { |
| 116 | + const nextSelector = render(next.children[i]); |
| 117 | + document.querySelector('head').appendChild(nextSelector) |
| 118 | + } else { |
| 119 | + current.children[i].element.remove() |
| 120 | + } |
| 121 | + } |
| 122 | + return |
42 | 123 | } |
43 | 124 |
|
44 | 125 | if (current.type !== next.type) { |
@@ -48,73 +129,28 @@ export default function rerender(selector, current, next) { |
48 | 129 |
|
49 | 130 | if (isText(current) && isText(next)) { |
50 | 131 | if (current != next) { |
51 | | - selector.nodeValue = next; |
| 132 | + selector.nodeValue = next.text; |
52 | 133 | } |
53 | 134 | return; |
54 | 135 | } |
55 | 136 |
|
56 | 137 | if (current.type === next.type) { |
57 | | - |
58 | | - const attributeNames = Object.keys({ ...current.attributes, ...next.attributes }); |
59 | | - for (const name of attributeNames) { |
60 | | - if (name === 'html') { |
61 | | - if (next.attributes[name] !== current.attributes[name]) { |
62 | | - selector.innerHTML = next.attributes[name]; |
63 | | - } |
64 | | - anchorableElement(selector); |
65 | | - } else if (name === 'checked') { |
66 | | - if (next.attributes[name] !== selector.value) { |
67 | | - selector.checked = next.attributes[name]; |
68 | | - } |
69 | | - } else if (name === 'value') { |
70 | | - if (next.attributes[name] !== selector.value) { |
71 | | - selector.value = next.attributes[name]; |
72 | | - } |
73 | | - } else if (name.startsWith('on')) { |
74 | | - const eventName = name.replace('on', ''); |
75 | | - const key = '_event.' + eventName; |
76 | | - selector.removeEventListener(eventName, current[key]); |
77 | | - if (next.attributes[name]) { |
78 | | - next[key] = (event) => { |
79 | | - if (next.attributes.default !== true) { |
80 | | - event.preventDefault(); |
81 | | - } |
82 | | - next.attributes[name]({ ...next.attributes, event }); |
83 | | - }; |
84 | | - selector.addEventListener(eventName, next[key]); |
85 | | - } |
86 | | - } else { |
87 | | - const type = typeof (next.attributes[name]); |
88 | | - if (type !== 'object' && type !== 'function') { |
89 | | - if (current.attributes[name] !== undefined && next.attributes[name] === undefined) { |
90 | | - selector.removeAttribute(name); |
91 | | - } else if (current.attributes[name] !== next.attributes[name]) { |
92 | | - if (name != 'value' && next.attributes[name] === false || next.attributes[name] === null || next.attributes[name] === undefined) { |
93 | | - selector.removeAttribute(name); |
94 | | - } else if (name != 'value' && next.attributes[name] === true) { |
95 | | - selector.setAttribute(name, ''); |
96 | | - } else { |
97 | | - selector.setAttribute(name, next.attributes[name]); |
98 | | - } |
99 | | - } |
100 | | - } |
101 | | - } |
102 | | - } |
| 138 | + updateAttributes(selector, current, next) |
103 | 139 |
|
104 | 140 | if (next.attributes.html) return; |
105 | 141 |
|
106 | 142 | const limit = Math.max(current.children.length, next.children.length); |
107 | 143 | if (next.children.length > current.children.length) { |
108 | 144 | for (let i = 0; i < current.children.length; i++) { |
109 | | - rerender(selector.childNodes[i], current.children[i], next.children[i]); |
| 145 | + _rerender(current.children[i], next.children[i]); |
110 | 146 | } |
111 | 147 | for (let i = current.children.length; i < next.children.length; i++) { |
112 | 148 | const nextSelector = render(next.children[i]); |
113 | 149 | selector.appendChild(nextSelector); |
114 | 150 | } |
115 | 151 | } else if (current.children.length > next.children.length) { |
116 | 152 | for (let i = 0; i < next.children.length; i++) { |
117 | | - rerender(selector.childNodes[i], current.children[i], next.children[i]); |
| 153 | + _rerender(current.children[i], next.children[i]); |
118 | 154 | } |
119 | 155 | for (let i = current.children.length - 1; i >= next.children.length; i--) { |
120 | 156 | selector.removeChild(selector.childNodes[i]); |
@@ -129,12 +165,12 @@ export default function rerender(selector, current, next) { |
129 | 165 | throw new Error('Virtual DOM does not match the DOM.') |
130 | 166 | return; |
131 | 167 | } |
132 | | - rerender(selector.childNodes[i], current.children[i], next.children[i]); |
| 168 | + _rerender(current.children[i], next.children[i]); |
133 | 169 | } |
134 | 170 | } |
135 | 171 |
|
136 | 172 | if (next.type == 'textarea') { |
137 | | - selector.value = next.children.join(""); |
| 173 | + selector.value = next.children.reduce((t, c) => t + c.text, ''); |
138 | 174 | } |
139 | 175 |
|
140 | 176 | if (next.type == 'select') { |
@@ -143,4 +179,29 @@ export default function rerender(selector, current, next) { |
143 | 179 |
|
144 | 180 | } |
145 | 181 |
|
| 182 | +} |
| 183 | + |
| 184 | +export default function rerender() { |
| 185 | + _rerender(client.virtualDom, client.nextVirtualDom) |
| 186 | +} |
| 187 | + |
| 188 | +export function hydrate(selector, node) { |
| 189 | + if (node?.attributes?.['data-n'] !== undefined) { |
| 190 | + node.element = document.querySelector(`[data-n="${node.attributes['data-n']}"]`) |
| 191 | + return |
| 192 | + } |
| 193 | + node.element = selector |
| 194 | + for (const element of selector.childNodes) { |
| 195 | + if (element.tagName && element.tagName.toLowerCase() == 'textarea' && element.childNodes.length == 0) { |
| 196 | + element.appendChild(document.createTextNode('')); |
| 197 | + } |
| 198 | + if (element.COMMENT_NODE === 8 && element.textContent === '#') { |
| 199 | + selector.removeChild(element); |
| 200 | + } |
| 201 | + } |
| 202 | + if (!node.children) return |
| 203 | + const limit = node.children.length; |
| 204 | + for (let i = limit - 1; i > -1; i--) { |
| 205 | + hydrate(selector.childNodes[i], node.children[i]) |
| 206 | + } |
146 | 207 | } |