1
0
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:
Conor Cussell
2017-07-19 03:44:10 +02:00
committed by Ian Storm Taylor
parent a1d4de3b20
commit 30f1a9ef1d
4 changed files with 21 additions and 5 deletions

View File

@@ -98,6 +98,11 @@ class Html {
const children = $.children().toArray()
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.
nodes = nodes.reduce((memo, node, i, original) => {
if (node.kind == 'block') {
@@ -111,11 +116,6 @@ class Html {
return memo
}
const { defaultBlockType } = this
const defaults = typeof defaultBlockType == 'string'
? { type: defaultBlockType }
: defaultBlockType
const block = {
kind: 'block',
nodes: [node],
@@ -126,6 +126,14 @@ class Html {
return memo
}, [])
if (nodes.length === 0) {
nodes = [{
kind: 'block',
nodes: [],
...defaults
}]
}
const raw = {
kind: 'state',
document: {

View File

@@ -0,0 +1 @@
export default {}

View File

@@ -0,0 +1,7 @@
data: {}
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- characters: []