1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-16 12:14:14 +02:00

Normalize target node in debug mutations (#2845)

* Normalize target node in debug mutations

* Added comments and fixed linting
This commit is contained in:
Sunny Hirai
2019-05-30 16:07:04 -07:00
committed by GitHub
parent d2f9b4167c
commit f15538d6a5

View File

@@ -26,6 +26,24 @@ const MUTATION_PROPERTIES = [
'previousSibling',
]
/**
* Takes a DOM node and returns an easily readable version of it.
*
* @param {DOMNode} node
*/
function normalizeNode(node) {
if (node.nodeType === window.Node.TEXT_NODE) {
return node.textContent
} else if (node.nodeType === window.Node.ELEMENT_NODE) {
const { outerHTML, innerHTML } = node
if (outerHTML == null) return JSON.stringify(node.textContent)
return outerHTML.slice(0, outerHTML.indexOf(innerHTML))
} else {
return `Node(type=${node.nodeType}`
}
}
/**
* A plugin that sends short easy to digest debug info about each dom mutation
* to browser.
@@ -46,7 +64,7 @@ function DebugMutationsPlugin() {
// Only add properties that provide meaningful values to the object
// to make the debug info easier to read
MUTATION_PROPERTIES.forEach(key => {
const value = mutationRecord[key]
let value = mutationRecord[key]
if (value == null) return
// Make NodeList easier to read
@@ -54,15 +72,16 @@ function DebugMutationsPlugin() {
if (value.length === 0) return
object[key] = Array.from(value)
.map(node => {
const { outerHTML, innerHTML } = node
if (outerHTML == null) return JSON.stringify(node.textContent)
return outerHTML.slice(0, outerHTML.indexOf(innerHTML))
})
.map(normalizeNode)
.join(', ')
return
}
// Make Node easier to read
if (value instanceof window.Node) {
value = normalizeNode(value)
}
object[key] = value
})