UNPKG

bc-node-sdk

Version:

BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.

88 lines (87 loc) 3.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const app_util_1 = __importDefault(require("./app-util")); const constants_1 = require("../domain/constants"); /** * Class {@link DOMUtil} encapsulates utility functions to manipulate the DOM. */ class DOMUtil { /** * Observes dom change by calling relevant callback to notify. * @param selector * @param domAddedCallback * @param domRemovedCallback */ static domObserver(selector, domAddedCallback, domRemovedCallback) { // The node to be monitored const target = document.querySelector(selector); if (target) { // Configuration of the observer: const config = { //attributes: true, childList: true, characterData: true, subtree: true, }; const observer = this.domObserverEvent(() => { if (domAddedCallback) { domAddedCallback(); } observer.disconnect(); }, () => { if (domRemovedCallback) { domRemovedCallback(); } }); observer.observe(target, config); } } /** * Creates a MutationObserver that calls the provided callbacks when the * observed element is added or removed from the DOM. * * @param domAddedCallback - Callback to call when the element is added to the DOM. * The callback will receive an array of added nodes as an argument. * @param domRemovedCallback - Callback to call when the element is removed from the DOM. * The callback will receive an array of removed nodes as an argument. */ static domObserverEvent(domAddedCallback, domRemovedCallback) { return new MutationObserver(function (mutations) { let addedNodes = new Array(), removedNodes = new Array(); mutations.forEach((record) => record.addedNodes.length & addedNodes.push(...record.addedNodes)); mutations.forEach((record) => record.removedNodes.length & removedNodes.push(...record.removedNodes)); if (addedNodes.length && domAddedCallback) { domAddedCallback(addedNodes); } if (removedNodes.length && domRemovedCallback) { domRemovedCallback(addedNodes); } }); } /** * Sanitizes the given HTML content by parsing it and extracting the inner HTML from the document's body. * * This method attempts to parse the provided HTML string using the DOMParser. If the parsing is successful, * it returns the inner HTML of the parsed document's body. In case of an error during parsing, the error is * logged and a default empty string is returned. * * @param html - The HTML string to be sanitized and parsed. * @returns The sanitized HTML content as a string, or a default empty string if an error occurs. */ static sanitizeHtmlContent(html) { var _a; try { const parser = new DOMParser(); const doc = parser.parseFromString(html, "text/html"); return ((_a = doc === null || doc === void 0 ? void 0 : doc.body) === null || _a === void 0 ? void 0 : _a.innerHTML) || constants_1.Defaults.String.Value; } catch (error) { app_util_1.default.logError(error); } return constants_1.Defaults.String.Value; } } exports.default = DOMUtil;