@shopgate/pwa-common
Version:
Common library for the Shopgate Connect PWA.
27 lines • 3.29 kB
JavaScript
function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import{logger}from'@shopgate/pwa-core/helpers';import CryptoJs from'crypto-js';import{embeddedMedia}from"../../collections";import{getExternalScripts,getInlineScripts,getHTMLContent,getDOMContainer,getStyles}from"./handleDOM";import decodeHTML from"./decodeHTML";/**
* Receives custom HTML from a widget configuration, parses possible
* script tags and executes them after loading external scripts.
* @param {string} html The HTML string. It might contain script tags.
* @param {boolean} decode Whether the html must be decoded.
* @param {Object} settings The settings are used to create a unique ID.
* @param {boolean} [processStyles=false] When true, found styles are also added to the DOM.
* @param {Object} [cookieConsentSettings] Additional settings related to cookie consent.
* @param {boolean} [cookieConsentSettings.comfortCookiesAccepted] Whether comfort cookies
* are accepted.
* @param {boolean} [cookieConsentSettings.statisticsCookiesAccepted] Whether statistics cookies
* are accepted.
* @returns {string} The HTML without any script tags.
*/var parseHTML=function parseHTML(html,decode,settings){var processStyles=arguments.length>3&&arguments[3]!==undefined?arguments[3]:false;var cookieConsentSettings=arguments.length>4&&arguments[4]!==undefined?arguments[4]:{};var id=CryptoJs.MD5(JSON.stringify(settings)).toString();var container=getDOMContainer("html-sanitizer-".concat(id));var cookieConsent=_extends({comfortCookiesAccepted:false,statisticsCookiesAccepted:false},cookieConsentSettings);try{var parser=new DOMParser();var unparsedHTML=decode?decodeHTML(html):html;// Parse the html string to a DOM object.
var dom=parser.parseFromString("<body>".concat(unparsedHTML,"</body>"),'text/html');// Run cookie consent logic from embedded media to remove markup that's not supposed to run
// when consent is not accepted.
embeddedMedia.handleCookieConsent(dom,cookieConsent);// How many onloads have been processed.
var onloads=0;var inlineScripts=[];var externalScripts=[];/**
* Handles the onload events for external scripts.
* @return {Array} The collection of external scripts.
*/var handleOnload=function handleOnload(){onloads+=1;/**
* If there are no external scripts or all external
* scripts are loaded, handle the inline scripts.
*/if(!externalScripts.length||onloads===externalScripts.length){inlineScripts.forEach(function(scriptTag){container.appendChild(scriptTag);});}return externalScripts;};// A collection of all the inline script tags.
inlineScripts=getInlineScripts(dom.childNodes);// A collection of all the external script tags.
externalScripts=getExternalScripts(dom.childNodes,handleOnload);// Append the external scripts.
externalScripts.forEach(function(scriptTag){container.appendChild(scriptTag);});if(processStyles){getStyles(dom).forEach(function(style){return container.appendChild(style);});}return getHTMLContent(dom.body.childNodes).innerHTML;}catch(err){logger.error(err);return html;}};export default parseHTML;