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() {
|
||||
let node = this
|
||||
let keys = new Set()
|
||||
let removals = new Set()
|
||||
|
||||
// Map this node's descendants, ensuring... ensuring there are no duplicate keys.
|
||||
const keys = []
|
||||
|
||||
// Map this node's descendants, ensuring...
|
||||
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) {
|
||||
let text = desc.getTexts().first()
|
||||
let characters = text.characters.clear()
|
||||
@@ -895,45 +896,40 @@ const Node = {
|
||||
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) {
|
||||
const text = Text.create()
|
||||
const nodes = desc.nodes.push(text)
|
||||
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
|
||||
})
|
||||
|
||||
// See if there are any adjacent text nodes.
|
||||
let first = node.findDescendant((child) => {
|
||||
if (child.kind != 'text') return
|
||||
const parent = node.getParent(child)
|
||||
const next = parent.getNextSibling(child)
|
||||
return next && next.kind == 'text'
|
||||
// Remove any nodes marked for removal.
|
||||
removals.forEach((key) => {
|
||||
node = node.removeDescendant(key)
|
||||
})
|
||||
|
||||
// If no text nodes are adjacent, abort.
|
||||
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()
|
||||
return node
|
||||
},
|
||||
|
||||
/**
|
||||
|
@@ -10,5 +10,3 @@ nodes:
|
||||
nodes:
|
||||
- kind: text
|
||||
text: ""
|
||||
- kind: text
|
||||
text: ""
|
||||
|
Reference in New Issue
Block a user