/
deka
/
buffer
Обзор
Документация
Войти
/
deka
/
buffer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
src/util/format.js
92 строки
3 KB
Maksim Ratnikov
format: restore \\u00a0 escape in cleanTableText regex
08 май 2026, 17:42
08 май 2026, 17:42
568bc60
Код
Авторство
О чём код?
export function escapeHtml(value) { return String(value ?? "") .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } export function cleanTableText(value) { return String(value ?? "") .replace(/\u00a0/g, " ") .replace(/[ \t\f\v\r\n]+/g, " ") .trim(); } export function layoutId() { if (window.crypto?.randomUUID) { return window.crypto.randomUUID(); } return `lab-${Math.random().toString(16).slice(2)}-${Date.now().toString(16)}`; } export function layoutWidths(widths, count) { if (Array.isArray(widths)) { return widths.join(","); } if (typeof widths === "string") { return widths; } const base = Math.floor(1500 / count); const values = Array.from({ length: count }, () => base); values[values.length - 1] += 1500 - values.reduce((sum, value) => sum + value, 0); return values.join(","); } export function formatHtml(html) { if (!html.trim()) { return ""; } const template = document.createElement("template"); template.innerHTML = html; const voidElements = new Set([ "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr" ]); function serializeAttributes(node) { return Array.from(node.attributes) .map((attribute) => ` ${attribute.name}="${escapeHtml(attribute.value)}"`) .join(""); } function serializeNode(node, depth, preserveWhitespace = false) { const indent = " ".repeat(depth); if (node.nodeType === Node.TEXT_NODE) { const value = preserveWhitespace ? node.textContent : node.textContent.replace(/\s+/g, " ").trim(); return value ? [`${indent}${escapeHtml(value)}`] : []; } if (node.nodeType === Node.COMMENT_NODE) { return [`${indent}<!--${node.textContent}-->`]; } if (node.nodeType !== Node.ELEMENT_NODE) { return []; } const tag = node.tagName.toLowerCase(); const open = `<${tag}${serializeAttributes(node)}>`; const preserveChildWhitespace = preserveWhitespace || tag === "pre" || tag === "code" || tag === "textarea"; if (voidElements.has(tag)) { return [`${indent}${open}`]; } const children = Array.from(node.childNodes).flatMap((child) => serializeNode(child, depth + 1, preserveChildWhitespace)); if (!children.length) { return [`${indent}${open}</${tag}>`]; } return [`${indent}${open}`, ...children, `${indent}</${tag}>`]; } return Array.from(template.content.childNodes) .flatMap((node) => serializeNode(node, 0)) .join("\n"); }