/
githubmirror
/
tabler
Обзор
Документация
Войти
/
githubmirror
/
tabler
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
core/js/src/bootstrap/util/sanitizer.ts
100 строк
3 KB
Paweł Kuna
Unify workspace lint, format, and type-check quality gates (#2732)
30 июл 2026, 20:36
Не верифицирован
30 июл 2026, 20:36
6db32ea
Код
Авторство
О чём код?
/** * -------------------------------------------------------------------------- * Bootstrap util/sanitizer.ts * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */ import type { AllowList, SanitizeFn } from '../types' // js-docs-start allow-list const ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i export const DefaultAllowlist: AllowList = { '*': ['class', 'dir', 'id', 'lang', 'role', ARIA_ATTRIBUTE_PATTERN], 'a': ['target', 'href', 'title', 'rel'], 'area': [], 'b': [], 'br': [], 'col': [], 'code': [], 'dd': [], 'div': [], 'dl': [], 'dt': [], 'em': [], 'hr': [], 'h1': [], 'h2': [], 'h3': [], 'h4': [], 'h5': [], 'h6': [], 'i': [], 'img': ['src', 'srcset', 'alt', 'title', 'width', 'height'], 'li': [], 'ol': [], 'p': [], 'pre': [], 's': [], 'small': [], 'span': [], 'sub': [], 'sup': [], 'strong': [], 'u': [], 'ul': [], } // js-docs-end allow-list const uriAttributes = new Set(['background', 'cite', 'href', 'itemtype', 'longdesc', 'poster', 'src', 'xlink:href']) const SAFE_URL_PATTERN = /^(?!javascript:)(?:[a-z0-9+.-]+:|[^&:/?#]*(?:[/?#]|$))/i const allowedAttribute = (attribute: Attr, allowedAttributeList: (string | RegExp)[]): boolean => { const attributeName = attribute.nodeName.toLowerCase() if (allowedAttributeList.includes(attributeName)) { if (uriAttributes.has(attributeName)) { return Boolean(SAFE_URL_PATTERN.test(attribute.nodeValue!)) } return true } return allowedAttributeList.filter((attributeRegex) => attributeRegex instanceof RegExp).some((regex) => (regex as RegExp).test(attributeName)) } export function sanitizeHtml(unsafeHtml: string, allowList: AllowList, sanitizeFunction?: SanitizeFn): string { if (!unsafeHtml.length) { return unsafeHtml } if (sanitizeFunction && typeof sanitizeFunction === 'function') { return sanitizeFunction(unsafeHtml) } const domParser = new window.DOMParser() const createdDocument = domParser.parseFromString(unsafeHtml, 'text/html') const elements = Array.from(createdDocument.body.querySelectorAll('*')) for (const element of elements) { const elementName = element.nodeName.toLowerCase() if (!Object.keys(allowList).includes(elementName)) { element.remove() continue } const attributeList = Array.from(element.attributes) const allowedAttributes = [...(allowList['*'] || []), ...(allowList[elementName] || [])] for (const attribute of attributeList) { if (!allowedAttribute(attribute, allowedAttributes)) { element.removeAttribute(attribute.nodeName) } } } return createdDocument.body.innerHTML }