UNPKG

e2ed

Version:

E2E testing framework over Playwright

81 lines (80 loc) 3.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createJsxRuntime = void 0; const sanitizeHtml_1 = require("./sanitizeHtml"); const createSafeHtmlWithoutSanitize = sanitizeHtml_1.createSafeHtmlWithoutSanitize; const isSafeHtml = sanitizeHtml_1.isSafeHtml; const sanitizeHtml = sanitizeHtml_1.sanitizeHtml; /** * Creates JSX runtime (functions `createElement` and `Fragment`). * This client function should not use scope variables (except global functions). * @internal */ const createJsxRuntime = () => { const maxDepth = 8; const createElement = (type, properties, ...children) => { const flatChildren = children.flat(maxDepth); if (typeof type === 'function') { const propertiesWithChildren = flatChildren.length === 0 ? properties : { ...properties, children: flatChildren }; return type(propertiesWithChildren ?? undefined); } const childrenParts = flatChildren.map((child) => isSafeHtml(child) ? child : sanitizeHtml `${child}`); const childrenHtml = createSafeHtmlWithoutSanitize `${childrenParts.join('')}`; const isVoidElement = [ 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr', ].includes(type); let closePart = createSafeHtmlWithoutSanitize ``; if (isVoidElement) { if (childrenHtml.length > 0) { // eslint-disable-next-line no-console console.error(`Element <${type}> is void element, but has children`, childrenHtml); closePart = childrenHtml; } } else { closePart = sanitizeHtml `${childrenHtml}</${type}>`; } if (properties == null) { return sanitizeHtml `<${type}>${closePart}`; } const attributesParts = Object.entries(properties) .filter(([key, value]) => { if (value == null) { return false; } if (value !== false) { return true; } const lowerCaseKey = key.toLocaleLowerCase(); return lowerCaseKey.startsWith('aria-') || lowerCaseKey.startsWith('data-'); }) .map(([key, value]) => sanitizeHtml `${key.toLowerCase()}="${value}"`); const attributesHtml = createSafeHtmlWithoutSanitize `${attributesParts.join(' ')}`; return sanitizeHtml `<${type} ${attributesHtml}>${closePart}`; }; const Fragment = (properties) => { if (properties?.children == null) { return createSafeHtmlWithoutSanitize ``; } if (!Array.isArray(properties.children)) { return sanitizeHtml `${properties.children}`; } const flatChildren = properties.children.flat(maxDepth); const childrenParts = flatChildren.map((child) => isSafeHtml(child) ? child : sanitizeHtml `${child}`); return createSafeHtmlWithoutSanitize `${childrenParts.join('')}`; }; return { Fragment, createElement }; }; exports.createJsxRuntime = createJsxRuntime;