1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 15:02:51 +02:00

fix html serializer called with invalid arguments

This commit is contained in:
Ian Storm Taylor
2016-08-11 12:56:19 -07:00
parent 35cee2ede6
commit 17bfdee659
4 changed files with 52 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ import ReactDOMServer from 'react-dom/server'
import State from '../models/state'
import Text from '../models/text'
import cheerio from 'cheerio'
import typeOf from 'type-of'
import { Record } from 'immutable'
/**
@@ -133,11 +134,18 @@ class Html {
elements.forEach((element) => {
const node = this.deserializeElement(element)
if (!node) return
if (Array.isArray(node)) {
switch (typeOf(node)) {
case 'array':
nodes = nodes.concat(node)
} else {
break
case 'object':
nodes.push(node)
break
case 'null':
case 'undefined':
return
default:
throw new Error(`A rule returned an invalid deserialized representation: "${node}".`)
}
})
@@ -155,9 +163,17 @@ class Html {
let node
const next = (elements) => {
return Array.isArray(elements)
? this.deserializeElements(elements)
: this.deserializeElement(elements)
switch (typeOf(elements)) {
case 'array':
return this.deserializeElements(elements)
case 'object':
return this.deserializeElement(elements)
case 'null':
case 'undefined':
return
default:
throw new Error(`The \`next\` argument was called with invalid children: "${elements}".`)
}
}
for (const rule of this.rules) {
@@ -246,6 +262,7 @@ class Html {
const ret = rule.serialize(node, children)
if (ret) return addKey(ret)
}
throw new Error(`No serializer defined for node of type "${node.type}".`)
}
@@ -266,6 +283,7 @@ class Html {
const ret = rule.serialize(mark, children)
if (ret) return addKey(ret)
}
throw new Error(`No serializer defined for mark of type "${mark.type}".`)
}, text)
}

View File

@@ -0,0 +1,18 @@
export default {
rules: [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(undefined)
}
}
}
}
}
]
}

View File

@@ -0,0 +1 @@
<p></p>

View File

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