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

Fix node.isEmpty (#1718)

* 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

* Fix node.isEmpty to work properly with void nodes of zero text length

* Take adavantage of fixed node.isEmpty to simplify normalization of empty inlines

* Fix linting errors
This commit is contained in:
urugator
2018-03-22 22:22:03 +01:00
committed by Ian Storm Taylor
parent 29901f0888
commit 3fe60f3795
5 changed files with 10 additions and 16 deletions

View File

@@ -106,19 +106,8 @@ const CORE_SCHEMA_RULES = [
validateNode(node) { validateNode(node) {
if (node.object != 'inline' && 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(
child => child.object === 'inline' && isEmpty(child) child => child.object === 'inline' && child.isEmpty
) )
if (!invalids.size) return if (!invalids.size) return

View File

@@ -163,12 +163,14 @@ class Block extends Record(DEFAULTS) {
/** /**
* Check if the block is empty. * Check if the block is empty.
* Returns true if block is not void and all it's children nodes are empty.
* Void node is never empty, regardless of it's content.
* *
* @return {Boolean} * @return {Boolean}
*/ */
get isEmpty() { get isEmpty() {
return this.text == '' return !this.isVoid && !this.nodes.some(child => !child.isEmpty)
} }
/** /**

View File

@@ -121,12 +121,13 @@ class Document extends Record(DEFAULTS) {
/** /**
* Check if the document is empty. * Check if the document is empty.
* Returns true if all it's children nodes are empty.
* *
* @return {Boolean} * @return {Boolean}
*/ */
get isEmpty() { get isEmpty() {
return this.text == '' return !this.nodes.some(child => !child.isEmpty)
} }
/** /**

View File

@@ -163,12 +163,14 @@ class Inline extends Record(DEFAULTS) {
/** /**
* Check if the inline is empty. * Check if the inline is empty.
* Returns true if inline is not void and all it's children nodes are empty.
* Void node is never empty, regardless of it's content.
* *
* @return {Boolean} * @return {Boolean}
*/ */
get isEmpty() { get isEmpty() {
return this.text == '' return !this.isVoid && !this.nodes.some(child => !child.isEmpty)
} }
/** /**

View File

@@ -603,7 +603,7 @@ class Value extends Record(DEFAULTS) {
get isEmpty() { get isEmpty() {
if (this.isCollapsed) return true if (this.isCollapsed) return true
if (this.endOffset != 0 && this.startOffset != 0) return false if (this.endOffset != 0 && this.startOffset != 0) return false
return this.fragment.text.length == 0 return this.fragment.isEmpty
} }
/** /**