Files
once-campfire/app/javascript/helpers/dom_helpers.js
T
Rosa Gutierrez 8722057545 Keep one escapeHTML, and make it safe in an attribute
There were two: the one in dom_helpers escaped through a text node, which
leaves double quotes alone, so it could not have closed the hole in the
link preview's img src.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186eyzivcTn6wqjEE4Wnxdt
2026-09-11 21:10:57 +02:00

60 lines
1.3 KiB
JavaScript

export function scrollToBottom(container) {
container.scrollTop = container.scrollHeight
}
export function parseHTMLFragment(html) {
const template = document.createElement("template")
template.innerHTML = html
return template.content
}
export function insertHTMLFragment(fragment, container, top) {
if (top) {
container.prepend(fragment)
} else {
container.append(fragment)
}
}
export function ignoringBriefDisconnects(element, fn) {
requestAnimationFrame(() => {
if (!element.isConnected) fn()
})
}
export function trimChildren(count, container, top) {
const children = Array.from(container.children)
const elements = top ? children.slice(0, count) : children.slice(-count)
keepScroll(container, top, function() {
for (const element of elements) {
element.remove()
}
})
}
export async function keepScroll(container, top, fn) {
pauseInertiaScroll(container)
const scrollTop = container.scrollTop
const scrollHeight = container.scrollHeight
await fn()
if (top) {
container.scrollTop = scrollTop + (container.scrollHeight - scrollHeight)
} else {
container.scrollTop = scrollTop
}
}
function pauseInertiaScroll(container) {
container.style.overflow = "hidden"
requestAnimationFrame(() => {
container.style.overflow = ""
})
}