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

Fix a hyperscript bug in perserving text-key (#1846)

* Perserve the text key in the right way

* rename assertation to test
This commit is contained in:
Jinxuan Zhu
2018-06-10 19:34:06 -04:00
committed by Ian Storm Taylor
parent dd3e9effff
commit 727a7ece26
3 changed files with 76 additions and 1 deletions

View File

@@ -264,7 +264,8 @@ function createChildren(children, options = {}) {
let length = 0 let length = 0
// When creating the new node, try to preserve a key if one exists. // When creating the new node, try to preserve a key if one exists.
const firstText = children.find(c => Text.isText(c)) const firstNodeOrText = children.find(c => typeof c !== 'string')
const firstText = Text.isText(firstNodeOrText) ? firstNodeOrText : null
const key = options.key ? options.key : firstText ? firstText.key : undefined const key = options.key ? options.key : firstText ? firstText.key : undefined
let node = Text.create({ key }) let node = Text.create({ key })

View File

@@ -0,0 +1,73 @@
/** @jsx h */
import assert from 'assert'
import h from '../..'
export const input = (
<document>
<block type="paragraph">
Cat <inline type="link">is</inline>
<text key="a"> cute</text>
</block>
</document>
)
export function test() {
const block = input.nodes.first()
assert.notEqual(block.nodes.first().key, 'a')
assert.equal(block.nodes.last().key, 'a')
}
export const output = {
object: 'document',
data: {},
nodes: [
{
object: 'block',
type: 'paragraph',
isVoid: false,
data: {},
nodes: [
{
object: 'text',
leaves: [
{
object: 'leaf',
text: 'Cat ',
marks: [],
},
],
},
{
object: 'inline',
type: 'link',
data: {},
isVoid: false,
nodes: [
{
object: 'text',
leaves: [
{
object: 'leaf',
text: 'is',
marks: [],
},
],
},
],
},
{
object: 'text',
leaves: [
{
object: 'leaf',
text: ' cute',
marks: [],
},
],
},
],
},
],
}

View File

@@ -27,6 +27,7 @@ describe('slate-hyperscript', () => {
const actual = input.toJSON() const actual = input.toJSON()
const expected = Value.isValue(output) ? output.toJSON() : output const expected = Value.isValue(output) ? output.toJSON() : output
assert.deepEqual(actual, expected) assert.deepEqual(actual, expected)
if (module.test) module.test()
}) })
} }
}) })