UNPKG

e2ed

Version:

E2E testing framework over Playwright

43 lines (42 loc) 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createJsxRuntime = createJsxRuntime; 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 */ function 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('')}`; if (properties == null) { return sanitizeHtml `<${type}>${childrenHtml}</${type}>`; } const attributesParts = Object.entries(properties).map(([key, value]) => sanitizeHtml `${key}="${value}"`); const attributesHtml = createSafeHtmlWithoutSanitize `${attributesParts.join('')}`; return sanitizeHtml `<${type} ${attributesHtml}>${childrenHtml}</${type}>`; }; 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 }; }