Text is always represented as text vnodes, never as vnode.text

This commit is contained in:
Barney Carroll 2021-04-14 17:06:32 +01:00 committed by Stephan Hoyer
parent 38e2184c6d
commit 9999fbe2a4
3 changed files with 86 additions and 72 deletions

View file

@ -4,39 +4,89 @@ v1.x and v2.x are largely API-compatible with v0.2.x, but there are some breakin
If you are migrating, consider using the [mithril-codemods](https://www.npmjs.com/package/mithril-codemods) tool to help automate the most straightforward migrations.
- [`m.prop` removed](#mprop-removed)
- [`m.component` removed](#mcomponent-removed)
- [`m.withAttr` removed](#mwithattr-removed)
- [`m.version` removed](#mversion-removed)
- [`config` function](#config-function)
- [Changes in redraw behaviour](#changes-in-redraw-behaviour)
- [No more redraw locks](#no-more-redraw-locks)
- [Cancelling redraw from event handlers](#cancelling-redraw-from-event-handlers)
- [Synchronous redraw changed](#synchronous-redraw)
- [`m.startComputation`/`m.endComputation` removed](#mstartcomputationmendcomputation-removed)
- [Component `controller` function](#component-controller-function)
- [Component arguments](#component-arguments)
- [Component vnode children](#component-children)
- [DOM vnode children](#dom-vnode-children)
- [Keys](#keys)
- [`view()` parameters](#view-parameters)
- [Passing components to `m()`](#passing-components-to-m)
- [Passing vnodes to `m.mount()` and `m.route()`](#passing-vnodes-to-mmount-and-mroute)
- [`m.route.mode`](#mroutemode)
- [`m.route()` and anchor tags](#mroute-and-anchor-tags)
- [Path templates](#path-templates)
- [Reading/writing the current route](#readingwriting-the-current-route)
- [Accessing route params](#accessing-route-params)
- [Building/Parsing query strings](#buildingparsing-query-strings)
- [Preventing unmounting](#preventing-unmounting)
- [Run code on component removal](#run-code-on-component-removal)
- [`m.request`](#mrequest)
- [Default `responseType` for `m.request`](#default-responsetype-for-mrequest)
- [`m.deferred` removed](#mdeferred-removed)
- [`m.sync` removed](#msync-removed)
- [`xlink` namespace required](#xlink-namespace-required)
- [Nested arrays in views](#nested-arrays-in-views)
- [`vnode` equality checks](#vnode-equality-checks)
- [Migrating from v0.2.x](#migrating-from-v02x)
- [`m.prop` removed](#mprop-removed)
- [v0.2.x](#v02x)
- [v2.x](#v2x)
- [`m.component` removed](#mcomponent-removed)
- [v0.2.x](#v02x-1)
- [v2.x](#v2x-1)
- [`m.withAttr` removed](#mwithattr-removed)
- [v0.2.x](#v02x-2)
- [v2.x](#v2x-2)
- [`m.version` removed](#mversion-removed)
- [`config` function](#config-function)
- [v0.2.x](#v02x-3)
- [v2.x](#v2x-3)
- [Changes in redraw behaviour](#changes-in-redraw-behaviour)
- [No more redraw locks](#no-more-redraw-locks)
- [Cancelling redraw from event handlers](#cancelling-redraw-from-event-handlers)
- [v0.2.x](#v02x-4)
- [v2.x](#v2x-4)
- [Synchronous redraw changed](#synchronous-redraw-changed)
- [v0.2.x](#v02x-5)
- [v2.x](#v2x-5)
- [`m.startComputation`/`m.endComputation` removed](#mstartcomputationmendcomputation-removed)
- [Component `controller` function](#component-controller-function)
- [v0.2.x](#v02x-6)
- [v2.x](#v2x-6)
- [Component arguments](#component-arguments)
- [v0.2.x](#v02x-7)
- [v2.x](#v2x-7)
- [Component vnode children](#component-vnode-children)
- [v0.2.x](#v02x-8)
- [v2.x](#v2x-8)
- [DOM vnode children](#dom-vnode-children)
- [Keys](#keys)
- [`view()` parameters](#view-parameters)
- [v0.2.x](#v02x-9)
- [v2.x](#v2x-9)
- [Passing components to `m()`](#passing-components-to-m)
- [v0.2.x](#v02x-10)
- [v2.x](#v2x-10)
- [Passing vnodes to `m.mount()` and `m.route()`](#passing-vnodes-to-mmount-and-mroute)
- [v0.2.x](#v02x-11)
- [v2.x](#v2x-11)
- [`m.route.mode`](#mroutemode)
- [v0.2.x](#v02x-12)
- [v2.x](#v2x-12)
- [`m.route()` and anchor tags](#mroute-and-anchor-tags)
- [v0.2.x](#v02x-13)
- [v2.x](#v2x-13)
- [Path templates](#path-templates)
- [`m.request` URLs](#mrequest-urls)
- [`m.route(route, params, shouldReplaceHistoryEntry)` paths](#mrouteroute-params-shouldreplacehistoryentry-paths)
- [`m.route` route patterns](#mroute-route-patterns)
- [Reading/writing the current route](#readingwriting-the-current-route)
- [v0.2.x](#v02x-14)
- [v2.x](#v2x-14)
- [Accessing route params](#accessing-route-params)
- [v0.2.x](#v02x-15)
- [v2.x](#v2x-15)
- [Building/Parsing query strings](#buildingparsing-query-strings)
- [v0.2.x](#v02x-16)
- [v2.x](#v2x-16)
- [Preventing unmounting](#preventing-unmounting)
- [v0.2.x](#v02x-17)
- [v2.x](#v2x-17)
- [Run code on component removal](#run-code-on-component-removal)
- [v0.2.x](#v02x-18)
- [v2.x](#v2x-18)
- [`m.request`](#mrequest)
- [v0.2.x](#v02x-19)
- [v2.x](#v2x-19)
- [`m.request` headers](#mrequest-headers)
- [`m.deferred` removed](#mdeferred-removed)
- [v0.2.x](#v02x-20)
- [v2.x](#v2x-20)
- [`m.sync` removed](#msync-removed)
- [v0.2.x](#v02x-21)
- [v2.x](#v2x-21)
- [`xlink` namespace required](#xlink-namespace-required)
- [v0.2.x](#v02x-22)
- [v2.x](#v2x-22)
- [Nested arrays in views](#nested-arrays-in-views)
- [`vnode` equality checks](#vnode-equality-checks)
---
@ -381,20 +431,6 @@ m("div", "value", ["nested"])
}
```
If only a single text child is present on a DOM vnode, it instead sets `text` to that value.
```javascript
m("div", "value")
// Becomes roughly:
{
tag: "div",
attrs: null,
text: "",
children: undefined,
}
```
See [the vnode docs](vnodes.md) for more details on the v2.x vnode structure and how things are normalized.
*Most of the v2.x vnode properties here are omitted for brevity.*

View file

@ -32,13 +32,12 @@ function compileSelector(selector) {
function execSelector(state, vnode) {
var attrs = vnode.attrs
var children = Vnode.normalizeChildren(vnode.children)
var hasClass = hasOwn.call(attrs, "class")
var className = hasClass ? attrs.class : attrs.className
vnode.tag = state.tag
vnode.attrs = null
vnode.children = undefined
vnode.children = Vnode.normalizeChildren(vnode.children)
if (!isEmpty(state.attrs) && !isEmpty(attrs)) {
var newAttrs = {}
@ -73,12 +72,6 @@ function execSelector(state, vnode) {
}
}
if (Array.isArray(children) && children.length === 1 && children[0] != null && children[0].tag === "#") {
vnode.text = children[0].children
} else {
vnode.children = children
}
return vnode
}

View file

@ -125,10 +125,6 @@ module.exports = function($window) {
insertNode(parent, element, nextSibling)
if (!maybeSetContentEditable(vnode)) {
if (vnode.text != null) {
if (vnode.text !== "") element.textContent = vnode.text
else vnode.children = [Vnode("#", undefined, undefined, vnode.text, undefined, undefined)]
}
if (vnode.children != null) {
var children = vnode.children
createNodes(element, children, 0, children.length, hooks, null, ns)
@ -460,21 +456,10 @@ module.exports = function($window) {
if (vnode.tag === "textarea") {
if (vnode.attrs == null) vnode.attrs = {}
if (vnode.text != null) {
vnode.attrs.value = vnode.text //FIXME handle multiple children
vnode.text = undefined
}
}
updateAttrs(vnode, old.attrs, vnode.attrs, ns)
if (!maybeSetContentEditable(vnode)) {
if (old.text != null && vnode.text != null && vnode.text !== "") {
if (old.text.toString() !== vnode.text.toString()) old.dom.firstChild.nodeValue = vnode.text
}
else {
if (old.text != null) old.children = [Vnode("#", undefined, undefined, old.text, undefined, old.dom.firstChild)]
if (vnode.text != null) vnode.children = [Vnode("#", undefined, undefined, vnode.text, undefined, undefined)]
updateNodes(element, old.children, vnode.children, hooks, null, ns)
}
updateNodes(element, old.children, vnode.children, hooks, null, ns)
}
}
function updateComponent(parent, old, vnode, hooks, nextSibling, ns) {
@ -617,7 +602,7 @@ module.exports = function($window) {
var content = children[0].children
if (vnode.dom.innerHTML !== content) vnode.dom.innerHTML = content
}
else if (vnode.text != null || children != null && children.length !== 0) throw new Error("Child node of a contenteditable must be trusted.")
else if (children != null && children.length !== 0) throw new Error("Child node of a contenteditable must be trusted.")
return true
}