diff --git a/packages/slate-hyperscript/src/index.js b/packages/slate-hyperscript/src/index.js index 29a566d42..5b5332e9f 100644 --- a/packages/slate-hyperscript/src/index.js +++ b/packages/slate-hyperscript/src/index.js @@ -264,7 +264,8 @@ function createChildren(children, options = {}) { let length = 0 // 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 let node = Text.create({ key }) diff --git a/packages/slate-hyperscript/test/default/perserve-keys-in-right-text.js b/packages/slate-hyperscript/test/default/perserve-keys-in-right-text.js new file mode 100644 index 000000000..d475d4a00 --- /dev/null +++ b/packages/slate-hyperscript/test/default/perserve-keys-in-right-text.js @@ -0,0 +1,73 @@ +/** @jsx h */ + +import assert from 'assert' + +import h from '../..' + +export const input = ( + + + Cat is + cute + + +) + +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: [], + }, + ], + }, + ], + }, + ], +} diff --git a/packages/slate-hyperscript/test/index.js b/packages/slate-hyperscript/test/index.js index d7b324178..3f019758c 100644 --- a/packages/slate-hyperscript/test/index.js +++ b/packages/slate-hyperscript/test/index.js @@ -27,6 +27,7 @@ describe('slate-hyperscript', () => { const actual = input.toJSON() const expected = Value.isValue(output) ? output.toJSON() : output assert.deepEqual(actual, expected) + if (module.test) module.test() }) } })