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

slate-hyperscript: Fix extra empty text at the end of children (#1379)

* Add failing test

* Don't push extra text node at the end of children

* Fix keys in tests

* Add option `normalize` to <value>

* Write Changelog

* Changelog on correct  package

* Fix preserve keys test

* Fix tests (which make more sense now)

* Mark failing test as skipped

* Account for skip option

* Lint and fix changelog version

* Use Unreleased instead of version

* Lint markdown
This commit is contained in:
Nicolas Gaborit
2018-04-27 22:16:11 +02:00
committed by Ian Storm Taylor
parent a943eada85
commit 099c6ada72
19 changed files with 131 additions and 27 deletions

View File

@@ -4,6 +4,14 @@ This document maintains a list of changes to the `slate-hyperscript` package wit
---
### Unreleased
* Accept `normalize` option for `<value>` tag. This allows to write
invalid values, on purpose, for example to test validation logic.
* Fixed a bug that added extra text nodes. You would not encounter these if you always wrapped things in a `<value>` tag, that was running a normalization.
---
### `0.5.0` — January 4, 2018
###### BREAKING

View File

@@ -64,7 +64,7 @@ const CREATORS = {
},
value(tagName, attributes, children) {
const { data } = attributes
const { data, normalize = true } = attributes
const document = children.find(Document.isDocument)
let selection = children.find(Range.isRange) || Range.create()
const props = {}
@@ -103,7 +103,7 @@ const CREATORS = {
selection = selection.merge(props).normalize(document)
}
const value = Value.create({ data, document, selection })
const value = Value.fromJSON({ data, document, selection }, { normalize })
return value
},
@@ -176,14 +176,15 @@ function createChildren(children, options = {}) {
node = next
}
children.forEach(child => {
children.forEach((child, index) => {
const isLast = index === children.length - 1
// If the child is a non-text node, push the current node and the new child
// onto the array, then creating a new node for future selection tracking.
if (Node.isNode(child) && !Text.isText(child)) {
if (node.text.length || node.__anchor != null || node.__focus != null)
array.push(node)
array.push(child)
node = Text.create()
node = isLast ? null : Text.create()
length = 0
}
@@ -223,7 +224,9 @@ function createChildren(children, options = {}) {
})
// Make sure the most recent node is added.
array.push(node)
if (node != null) {
array.push(node)
}
return array
}

View File

@@ -0,0 +1,75 @@
/** @jsx h */
import h from '../'
import assert from 'assert'
import { Value, Document, Block, Text } from 'slate'
describe('slate-hyperscript', () => {
it('should create a document with a single block', () => {
const output = (
<document>
<block type="paragraph">Single block</block>
</document>
)
const expected = Document.create({
nodes: [
Block.create({
type: 'paragraph',
nodes: [Text.create('Single block')],
}),
],
})
assert.deepEqual(output.toJSON(), expected.toJSON())
})
it('should normalize a value by default', () => {
const output = (
<value>
<document>
<block type="paragraph">Valid block</block>
<text>Invalid text</text>
</document>
</value>
)
const expected = Value.create({
document: Document.create({
nodes: [
Block.create({
type: 'paragraph',
nodes: [Text.create('Valid block')],
}),
],
}),
})
assert.deepEqual(output.toJSON(), expected.toJSON())
})
it('should not normalize a value, given the option', () => {
const output = (
<value normalize={false}>
<document>
<block type="paragraph">Valid block</block>
<text>Invalid text</text>
</document>
</value>
)
const expected = Value.fromJSON(
{
document: Document.create({
nodes: [
Block.create({
type: 'paragraph',
nodes: [Text.create('Valid block')],
}),
Text.create('Invalid text'),
],
}),
},
{ normalize: false }
)
assert.deepEqual(output.toJSON(), expected.toJSON())
})
})