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

Fix normalize empty inline (#1698)

* add test for normalization of nested empty inlines

* fix normalization of nested empty inlines

* add test for normalization of inlines with empty void

* fix normalization of inlines containing empty void

* fix linting errors
This commit is contained in:
urugator
2018-03-21 22:08:15 +01:00
committed by Ian Storm Taylor
parent 7cf6e1d5a8
commit edb7c3bd3c
3 changed files with 192 additions and 6 deletions

View File

@@ -95,20 +95,30 @@ const CORE_SCHEMA_RULES = [
/** /**
* Ensure that inline non-void nodes are never empty. * Ensure that inline non-void nodes are never empty.
* *
* This rule is applied to all blocks, because when they contain an empty * This rule is applied to all blocks and inlines, because when they contain an empty
* inline, we need to remove the inline from that parent block. If `validate` * inline, we need to remove the empty inline from that parent node. If `validate`
* was to be memoized, it should be against the parent node, not the inline * was to be memoized, it should be against the parent node, not the empty inline itself.
* themselves.
* *
* @type {Object} * @type {Object}
*/ */
{ {
validateNode(node) { validateNode(node) {
if (node.object != 'block') return if (node.object != 'inline' && node.object != 'block') return
function isEmpty(n) {
// text node is empty when text is empty
if (n.object === 'text') {
return n.text === ''
}
// void is always considered non-empty regardless of actual content
if (n.isVoid) return false
// otherwise node is empty if all children are empty
return !n.nodes.some(child => !isEmpty(child))
}
const invalids = node.nodes.filter( const invalids = node.nodes.filter(
n => n.object === 'inline' && n.isVoid === false && n.text === '' child => child.object === 'inline' && isEmpty(child)
) )
if (!invalids.size) return if (!invalids.size) return

View File

@@ -0,0 +1,101 @@
/** @jsx h */
import h from '../../helpers/h'
export const schema = {}
export const input = (
<value>
<document>
<paragraph>
<link>
<inline isVoid type="" />
</link>
</paragraph>
</document>
</value>
)
export const output = {
object: 'value',
document: {
object: 'document',
data: {},
nodes: [
{
object: 'block',
type: 'paragraph',
isVoid: false,
data: {},
nodes: [
{
object: 'text',
leaves: [
{
object: 'leaf',
text: '',
marks: [],
},
],
},
{
object: 'inline',
type: 'link',
isVoid: false,
data: {},
nodes: [
{
object: 'text',
leaves: [
{
object: 'leaf',
text: '',
marks: [],
},
],
},
{
object: 'inline',
type: '',
isVoid: true,
data: {},
nodes: [
{
object: 'text',
leaves: [
{
object: 'leaf',
text: '',
marks: [],
},
],
},
],
},
{
object: 'text',
leaves: [
{
object: 'leaf',
text: '',
marks: [],
},
],
},
],
},
{
object: 'text',
leaves: [
{
object: 'leaf',
text: '',
marks: [],
},
],
},
],
},
],
},
}

View File

@@ -0,0 +1,75 @@
/** @jsx h */
import h from '../../helpers/h'
export const schema = {}
export const input = (
<value>
<document>
<paragraph>
<link>
one
<link />
two
</link>
</paragraph>
</document>
</value>
)
export const output = {
object: 'value',
document: {
object: 'document',
data: {},
nodes: [
{
object: 'block',
type: 'paragraph',
isVoid: false,
data: {},
nodes: [
{
object: 'text',
leaves: [
{
object: 'leaf',
text: '',
marks: [],
},
],
},
{
object: 'inline',
type: 'link',
isVoid: false,
data: {},
nodes: [
{
object: 'text',
leaves: [
{
object: 'leaf',
text: 'onetwo',
marks: [],
},
],
},
],
},
{
object: 'text',
leaves: [
{
object: 'leaf',
text: '',
marks: [],
},
],
},
],
},
],
},
}