mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-17 20:51:20 +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:
@@ -26,6 +26,24 @@ const MUTATION_PROPERTIES = [
|
|||||||
'previousSibling',
|
'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
|
* A plugin that sends short easy to digest debug info about each dom mutation
|
||||||
* to browser.
|
* to browser.
|
||||||
@@ -46,7 +64,7 @@ function DebugMutationsPlugin() {
|
|||||||
// Only add properties that provide meaningful values to the object
|
// Only add properties that provide meaningful values to the object
|
||||||
// to make the debug info easier to read
|
// to make the debug info easier to read
|
||||||
MUTATION_PROPERTIES.forEach(key => {
|
MUTATION_PROPERTIES.forEach(key => {
|
||||||
const value = mutationRecord[key]
|
let value = mutationRecord[key]
|
||||||
if (value == null) return
|
if (value == null) return
|
||||||
|
|
||||||
// Make NodeList easier to read
|
// Make NodeList easier to read
|
||||||
@@ -54,15 +72,16 @@ function DebugMutationsPlugin() {
|
|||||||
if (value.length === 0) return
|
if (value.length === 0) return
|
||||||
|
|
||||||
object[key] = Array.from(value)
|
object[key] = Array.from(value)
|
||||||
.map(node => {
|
.map(normalizeNode)
|
||||||
const { outerHTML, innerHTML } = node
|
|
||||||
if (outerHTML == null) return JSON.stringify(node.textContent)
|
|
||||||
return outerHTML.slice(0, outerHTML.indexOf(innerHTML))
|
|
||||||
})
|
|
||||||
.join(', ')
|
.join(', ')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make Node easier to read
|
||||||
|
if (value instanceof window.Node) {
|
||||||
|
value = normalizeNode(value)
|
||||||
|
}
|
||||||
|
|
||||||
object[key] = value
|
object[key] = value
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user