UNPKG

html-react-parser

Version:
104 lines 3.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.returnFirstArg = exports.canTextBeChildOfNode = exports.ELEMENTS_WITH_NO_TEXT_CHILDREN = exports.PRESERVE_CUSTOM_ATTRIBUTES = void 0; exports.isCustomComponent = isCustomComponent; exports.setStyleProp = setStyleProp; var react_1 = require("react"); var style_to_js_1 = __importDefault(require("style-to-js")); var RESERVED_SVG_MATHML_ELEMENTS = new Set([ 'annotation-xml', 'color-profile', 'font-face', 'font-face-src', 'font-face-uri', 'font-face-format', 'font-face-name', 'missing-glyph', ]); /** * Check if a tag is a custom component. * * @see {@link https://github.com/facebook/react/blob/v16.6.3/packages/react-dom/src/shared/isCustomComponent.js} * * @param tagName - Tag name. * @param props - Props passed to the element. * @returns - Whether the tag is custom component. */ function isCustomComponent(tagName, props) { if (!tagName.includes('-')) { return Boolean(props && typeof props.is === 'string'); } // These are reserved SVG and MathML elements. // We don't mind this whitelist too much because we expect it to never grow. // The alternative is to track the namespace in a few places which is convoluted. // https://w3c.github.io/webcomponents/spec/custom/#custom-elements-core-concepts if (RESERVED_SVG_MATHML_ELEMENTS.has(tagName)) { return false; } return true; } var styleOptions = { reactCompat: true, }; /** * Sets style prop. * * @param style - Inline style. * @param props - Props object. */ function setStyleProp(style, props) { if (typeof style !== 'string') { return; } if (!style.trim()) { props.style = {}; return; } try { props.style = (0, style_to_js_1.default)(style, styleOptions); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { props.style = {}; } } /** * @see https://reactjs.org/blog/2017/09/08/dom-attributes-in-react-16.html */ exports.PRESERVE_CUSTOM_ATTRIBUTES = Number(react_1.version.split('.')[0]) >= 16; /** * @see https://github.com/facebook/react/blob/cae635054e17a6f107a39d328649137b83f25972/packages/react-dom/src/client/validateDOMNesting.js#L213 */ exports.ELEMENTS_WITH_NO_TEXT_CHILDREN = new Set([ 'tr', 'tbody', 'thead', 'tfoot', 'colgroup', 'table', 'head', 'html', 'frameset', ]); /** * Checks if the given node can contain text nodes * * @param node - Element node. * @returns - Whether the node can contain text nodes. */ var canTextBeChildOfNode = function (node) { return !exports.ELEMENTS_WITH_NO_TEXT_CHILDREN.has(node.name); }; exports.canTextBeChildOfNode = canTextBeChildOfNode; /** * Returns the first argument as is. * * @param arg - The argument to be returned. * @returns - The input argument `arg`. */ var returnFirstArg = function (arg) { return arg; }; exports.returnFirstArg = returnFirstArg; //# sourceMappingURL=utilities.js.map