mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-22 06:53:25 +02:00
fix html serializer called with invalid arguments
This commit is contained in:
@@ -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)) {
|
||||
nodes = nodes.concat(node)
|
||||
} else {
|
||||
nodes.push(node)
|
||||
switch (typeOf(node)) {
|
||||
case 'array':
|
||||
nodes = nodes.concat(node)
|
||||
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)
|
||||
}
|
||||
|
18
test/serializers/fixtures/html/deserialize/no-next/index.js
Normal file
18
test/serializers/fixtures/html/deserialize/no-next/index.js
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
export default {
|
||||
rules: [
|
||||
{
|
||||
deserialize(el, next) {
|
||||
switch (el.tagName) {
|
||||
case 'p': {
|
||||
return {
|
||||
kind: 'block',
|
||||
type: 'paragraph',
|
||||
nodes: next(undefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1 @@
|
||||
<p></p>
|
@@ -0,0 +1,7 @@
|
||||
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
data: {}
|
||||
nodes:
|
||||
- characters: []
|
Reference in New Issue
Block a user