mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-01 19:22:35 +02:00
fix to remove extra empty text nodes, closes #171
This commit is contained in:
@@ -877,16 +877,17 @@ const Node = {
|
|||||||
|
|
||||||
normalize() {
|
normalize() {
|
||||||
let node = this
|
let node = this
|
||||||
|
let keys = new Set()
|
||||||
|
let removals = new Set()
|
||||||
|
|
||||||
// Map this node's descendants, ensuring... ensuring there are no duplicate keys.
|
// Map this node's descendants, ensuring...
|
||||||
const keys = []
|
|
||||||
|
|
||||||
node = node.mapDescendants((desc) => {
|
node = node.mapDescendants((desc) => {
|
||||||
// That there are no duplicate keys.
|
|
||||||
if (includes(keys, desc.key)) desc = desc.set('key', uid())
|
|
||||||
keys.push(desc.key)
|
|
||||||
|
|
||||||
// That void nodes contain no text.
|
// ...that there are no duplicate keys.
|
||||||
|
if (keys.has(desc.key)) desc = desc.set('key', uid())
|
||||||
|
keys = keys.add(desc.key)
|
||||||
|
|
||||||
|
// ...that void nodes contain no text.
|
||||||
if (desc.isVoid && desc.length) {
|
if (desc.isVoid && desc.length) {
|
||||||
let text = desc.getTexts().first()
|
let text = desc.getTexts().first()
|
||||||
let characters = text.characters.clear()
|
let characters = text.characters.clear()
|
||||||
@@ -895,45 +896,40 @@ const Node = {
|
|||||||
desc = desc.merge({ nodes })
|
desc = desc.merge({ nodes })
|
||||||
}
|
}
|
||||||
|
|
||||||
// That no block or inline node is empty.
|
// ...that no block or inline node is empty.
|
||||||
if (desc.kind != 'text' && desc.nodes.size == 0) {
|
if (desc.kind != 'text' && desc.nodes.size == 0) {
|
||||||
const text = Text.create()
|
const text = Text.create()
|
||||||
const nodes = desc.nodes.push(text)
|
const nodes = desc.nodes.push(text)
|
||||||
desc = desc.merge({ nodes })
|
desc = desc.merge({ nodes })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (desc.kind == 'text') {
|
||||||
|
let next = node.getNextSibling(desc)
|
||||||
|
|
||||||
|
// ...that there are no adjacent text nodes.
|
||||||
|
while (next && next.kind == 'text') {
|
||||||
|
const characters = desc.characters.concat(next.characters)
|
||||||
|
desc = desc.merge({ characters })
|
||||||
|
removals = removals.add(next.key)
|
||||||
|
next = node.getNextSibling(next)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...that there are no extra empty text nodes.
|
||||||
|
if (desc.length == 0) {
|
||||||
|
const parent = node.getParent(desc)
|
||||||
|
if (parent.nodes.size != 1) removals = removals.add(desc.key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return desc
|
return desc
|
||||||
})
|
})
|
||||||
|
|
||||||
// See if there are any adjacent text nodes.
|
// Remove any nodes marked for removal.
|
||||||
let first = node.findDescendant((child) => {
|
removals.forEach((key) => {
|
||||||
if (child.kind != 'text') return
|
node = node.removeDescendant(key)
|
||||||
const parent = node.getParent(child)
|
|
||||||
const next = parent.getNextSibling(child)
|
|
||||||
return next && next.kind == 'text'
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// If no text nodes are adjacent, abort.
|
return node
|
||||||
if (!first) return node
|
|
||||||
|
|
||||||
// Fix an adjacent text node if one exists.
|
|
||||||
let parent = node.getParent(first)
|
|
||||||
const isParent = node == parent
|
|
||||||
const second = parent.getNextSibling(first)
|
|
||||||
const characters = first.characters.concat(second.characters)
|
|
||||||
first = first.merge({ characters })
|
|
||||||
parent = parent.updateDescendant(first)
|
|
||||||
|
|
||||||
// Then remove the second text node.
|
|
||||||
parent = parent.removeDescendant(second)
|
|
||||||
|
|
||||||
// And update the node.
|
|
||||||
node = isParent
|
|
||||||
? parent
|
|
||||||
: node.updateDescendant(parent)
|
|
||||||
|
|
||||||
// Recurse by normalizing again.
|
|
||||||
return node.normalize()
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -10,5 +10,3 @@ nodes:
|
|||||||
nodes:
|
nodes:
|
||||||
- kind: text
|
- kind: text
|
||||||
text: ""
|
text: ""
|
||||||
- kind: text
|
|
||||||
text: ""
|
|
||||||
|
Reference in New Issue
Block a user