mirror of
https://github.com/nocodb/nocodb.git
synced 2026-05-01 11:47:01 +00:00
38 lines
983 B
TypeScript
38 lines
983 B
TypeScript
export function elementFromString(value) {
|
|
// add a wrapper to preserve leading and trailing whitespace
|
|
const wrappedValue = `<body>${value}</body>`
|
|
|
|
return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body
|
|
}
|
|
|
|
export function escapeHTML(value) {
|
|
if (!ncIsString(value)) return value
|
|
|
|
return value?.replace(/</g, '<').replace(/>/g, '>')
|
|
}
|
|
|
|
export function extractElement(node) {
|
|
const parent = node.parentElement
|
|
const prepend = parent.cloneNode()
|
|
|
|
while (parent.firstChild && parent.firstChild !== node) {
|
|
prepend.appendChild(parent.firstChild)
|
|
}
|
|
|
|
if (prepend.childNodes.length > 0) {
|
|
parent.parentElement.insertBefore(prepend, parent)
|
|
}
|
|
parent.parentElement.insertBefore(node, parent)
|
|
if (parent.childNodes.length === 0) {
|
|
parent.remove()
|
|
}
|
|
}
|
|
|
|
export function unwrapElement(node) {
|
|
const parent = node.parentNode
|
|
|
|
while (node.firstChild) parent.insertBefore(node.firstChild, node)
|
|
|
|
parent.removeChild(node)
|
|
}
|