mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 22:45:18 +02:00
Allow HTMLSerializer to deserialize an empty doc (#915)
* Allow HTML serializer to deserialize an empty document This specially handles the case where the HTML is empty. Test by running: ``` const html = new HTML() html.deserialize('') ``` Closes #539 * Use defaultBlockType in HTML serializer * Add test case for deserialising empty string * Use fixtures
This commit is contained in:
committed by
Ian Storm Taylor
parent
a1d4de3b20
commit
30f1a9ef1d
@@ -98,6 +98,11 @@ class Html {
|
|||||||
const children = $.children().toArray()
|
const children = $.children().toArray()
|
||||||
let nodes = this.deserializeElements(children)
|
let nodes = this.deserializeElements(children)
|
||||||
|
|
||||||
|
const { defaultBlockType } = this
|
||||||
|
const defaults = typeof defaultBlockType == 'string'
|
||||||
|
? { type: defaultBlockType }
|
||||||
|
: defaultBlockType
|
||||||
|
|
||||||
// HACK: ensure for now that all top-level inline are wrapped into a block.
|
// HACK: ensure for now that all top-level inline are wrapped into a block.
|
||||||
nodes = nodes.reduce((memo, node, i, original) => {
|
nodes = nodes.reduce((memo, node, i, original) => {
|
||||||
if (node.kind == 'block') {
|
if (node.kind == 'block') {
|
||||||
@@ -111,11 +116,6 @@ class Html {
|
|||||||
return memo
|
return memo
|
||||||
}
|
}
|
||||||
|
|
||||||
const { defaultBlockType } = this
|
|
||||||
const defaults = typeof defaultBlockType == 'string'
|
|
||||||
? { type: defaultBlockType }
|
|
||||||
: defaultBlockType
|
|
||||||
|
|
||||||
const block = {
|
const block = {
|
||||||
kind: 'block',
|
kind: 'block',
|
||||||
nodes: [node],
|
nodes: [node],
|
||||||
@@ -126,6 +126,14 @@ class Html {
|
|||||||
return memo
|
return memo
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
if (nodes.length === 0) {
|
||||||
|
nodes = [{
|
||||||
|
kind: 'block',
|
||||||
|
nodes: [],
|
||||||
|
...defaults
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
const raw = {
|
const raw = {
|
||||||
kind: 'state',
|
kind: 'state',
|
||||||
document: {
|
document: {
|
||||||
|
@@ -0,0 +1 @@
|
|||||||
|
export default {}
|
@@ -0,0 +1,7 @@
|
|||||||
|
data: {}
|
||||||
|
nodes:
|
||||||
|
- type: paragraph
|
||||||
|
isVoid: false
|
||||||
|
data: {}
|
||||||
|
nodes:
|
||||||
|
- characters: []
|
Reference in New Issue
Block a user