mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-15 12:44:38 +01: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:
parent
29901f0888
commit
3fe60f3795
@ -106,19 +106,8 @@ const CORE_SCHEMA_RULES = [
|
||||
validateNode(node) {
|
||||
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(
|
||||
child => child.object === 'inline' && isEmpty(child)
|
||||
child => child.object === 'inline' && child.isEmpty
|
||||
)
|
||||
|
||||
if (!invalids.size) return
|
||||
|
@ -163,12 +163,14 @@ class Block extends Record(DEFAULTS) {
|
||||
|
||||
/**
|
||||
* 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}
|
||||
*/
|
||||
|
||||
get isEmpty() {
|
||||
return this.text == ''
|
||||
return !this.isVoid && !this.nodes.some(child => !child.isEmpty)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,12 +121,13 @@ class Document extends Record(DEFAULTS) {
|
||||
|
||||
/**
|
||||
* Check if the document is empty.
|
||||
* Returns true if all it's children nodes are empty.
|
||||
*
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
get isEmpty() {
|
||||
return this.text == ''
|
||||
return !this.nodes.some(child => !child.isEmpty)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -163,12 +163,14 @@ class Inline extends Record(DEFAULTS) {
|
||||
|
||||
/**
|
||||
* 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}
|
||||
*/
|
||||
|
||||
get isEmpty() {
|
||||
return this.text == ''
|
||||
return !this.isVoid && !this.nodes.some(child => !child.isEmpty)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -603,7 +603,7 @@ class Value extends Record(DEFAULTS) {
|
||||
get isEmpty() {
|
||||
if (this.isCollapsed) return true
|
||||
if (this.endOffset != 0 && this.startOffset != 0) return false
|
||||
return this.fragment.text.length == 0
|
||||
return this.fragment.isEmpty
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user