diff --git a/js/showsource.js b/js/showsource.js index 3d3d0c2..3acfa06 100644 --- a/js/showsource.js +++ b/js/showsource.js @@ -22,7 +22,9 @@ SOFTWARE. */ -defaultOptions = { +'use strict'; + +let defaultOptions = { // The indentation to be used (the spaces at the beginning of the line) (inheritable) indentation: " ", // Remove the children of these types of elements @@ -109,7 +111,7 @@ function beautify(el, userOptions = {}, indent = "") { if (typeof options.removeAttributes === "string") { removeAttributes = options.removeAttributes.split(" "); } - for (attribute of el.attributes) { + for (let attribute of el.attributes) { if (attribute.name.startsWith("data-showsource")) { if (options.hidePlugin) { continue; @@ -164,7 +166,7 @@ function beautify(el, userOptions = {}, indent = "") { beautifulElement[beautifulElement.length - 1] += ">"; } if (!options.skipChildren) { - for (child of el.childNodes) { + for (let child of el.childNodes) { if (child.nodeType == Node.TEXT_NODE) { let text = child.textContent.trim(); if (text !== "") { diff --git a/showsource.js b/showsource.js index e49cc6c..1603036 100644 --- a/showsource.js +++ b/showsource.js @@ -23,7 +23,8 @@ */ (function(window, document) { - defaultOptions = { + "use strict"; + let defaultOptions = { indentation: " ", remove: "h1 h2 h3 h4 h5 h6 p", hide: false, @@ -78,7 +79,7 @@ if (typeof options.removeAttributes === "string") { removeAttributes = options.removeAttributes.split(" "); } - for (attribute of el.attributes) { + for (let attribute of el.attributes) { if (attribute.name.startsWith("data-showsource")) { if (options.hidePlugin) { continue; @@ -126,7 +127,7 @@ beautifulElement[beautifulElement.length - 1] += ">"; } if (!options.skipChildren) { - for (child of el.childNodes) { + for (let child of el.childNodes) { if (child.nodeType == Node.TEXT_NODE) { let text = child.textContent.trim(); if (text !== "") { diff --git a/showsource.min.js b/showsource.min.js index d7ca8ea..786f0ff 100644 --- a/showsource.min.js +++ b/showsource.min.js @@ -1,2 +1,2 @@ /* Copyright 2023 Carlos A. (https://github.com/dealfonso); License: https://opensource.org/license/mit/ */ -(function(e,a){defaultOptions={indentation:" ",remove:"h1 h2 h3 h4 h5 h6 p",hide:false,skip:false,skipChildren:false,hidePlugin:true,removeAttributes:null,class:"showsource"};function r(i,e={},s=""){let t={};if(i.dataset.showsourceIndentation!==undefined){t.indentation=i.dataset.showsourceIndentation}if(i.dataset.showsourceRemove!==undefined){t.remove=i.dataset.showsourceRemove}if(i.dataset.showsourceHide!==undefined){t.hide=i.dataset.showsourceHide.toLowerCase()!="false"}if(i.dataset.showsourceSkip!==undefined){t.skip=i.dataset.showsourceSkip.toLowerCase()!="false"}if(i.dataset.showsourceSkipChildren!==undefined){t.skipChildren=i.dataset.showsourceSkipChildren.toLowerCase()!="false"}if(i.dataset.showsourceHidePlugin!==undefined){t.hidePlugin=i.dataset.showsourceHidePlugin.toLowerCase()!="false"}let n=Object.assign({},defaultOptions,t,e);if(n.skip){return[]}i=i.cloneNode(true);let a=Object.assign({},e,{indentation:n.indentation,hidePlugin:n.hidePlugin});if(typeof n.remove==="string"){let e=n.remove.split(" ").join(",");if(e.trim()!=""){i.querySelectorAll(e).forEach(function(e){e.remove()})}}let o=[];if(!n.hide){o.push(s+"<"+i.tagName.toLowerCase());let e=false;let t=[];if(typeof n.removeAttributes==="string"){t=n.removeAttributes.split(" ")}for(attribute of i.attributes){if(attribute.name.startsWith("data-showsource")){if(n.hidePlugin){continue}}if(t.includes(attribute.name)){continue}if(attribute.name.startsWith("data-")||e){o.push(" "+s+attribute.name+'="'+attribute.value+'"')}else{o[o.length-1]+=" "+attribute.name+'="'+attribute.value+'"'}e=attribute.name.startsWith("data-")}}if(i.children.length==0&&!n.hide){let e=["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"];if(e.includes(i.tagName.toLowerCase())){o[o.length-1]+=">"+i.innerHTML.trim()}else{let e=["script","style"];if(e.includes(i.tagName.toLowerCase())){o[o.length-1]+=">";let e=i.innerHTML.split("\n");while(e.length>0&&e[0].trim()==""){e.shift()}while(e.length>0&&e[e.length-1].trim()==""){e.pop()}o.push(e.join("\n"));o.push("")}else{if(o.length>1){o[o.length-1]+=">"+i.innerHTML.trim();o.push(s+"")}else{o[o.length-1]+=">"+i.innerHTML.trim()+""}}}}else{if(!n.hide){o[o.length-1]+=">"}if(!n.skipChildren){for(child of i.childNodes){if(child.nodeType==Node.TEXT_NODE){let e=child.textContent.trim();if(e!==""){o.push(s+n.indentation+e)}}else{let e=r(child,a,s+(n.hide?"":n.indentation));o=o.concat(e)}}}if(!n.hide){o.push(s+"")}}i.remove();return o}function t(){a.querySelectorAll("div[data-showsource]").forEach(function(e){let t=defaultOptions.class;if(e.dataset.showsourceClass!==undefined){if(typeof e.dataset.showsourceClass==="string"){t=e.dataset.showsourceClass}else{console.error("The value of the data-showsource-class attribute must be a string")}}let i=a.createElement("div");let s=a.createElement("pre");let n=a.createElement("code");n.textContent=r(e).join("\n");s.appendChild(n);i.appendChild(s);t.split(" ").forEach(function(e){i.classList.add(e)});e.parentNode.insertBefore(i,e.nextSibling)})}if(a.addEventListener){a.addEventListener("DOMContentLoaded",function(){t()})}e.showsource={beautify:r,init:t}})(window,document); +(function(e,a){"use strict";let r={indentation:" ",remove:"h1 h2 h3 h4 h5 h6 p",hide:false,skip:false,skipChildren:false,hidePlugin:true,removeAttributes:null,class:"showsource"};function l(s,e={},n=""){let t={};if(s.dataset.showsourceIndentation!==undefined){t.indentation=s.dataset.showsourceIndentation}if(s.dataset.showsourceRemove!==undefined){t.remove=s.dataset.showsourceRemove}if(s.dataset.showsourceHide!==undefined){t.hide=s.dataset.showsourceHide.toLowerCase()!="false"}if(s.dataset.showsourceSkip!==undefined){t.skip=s.dataset.showsourceSkip.toLowerCase()!="false"}if(s.dataset.showsourceSkipChildren!==undefined){t.skipChildren=s.dataset.showsourceSkipChildren.toLowerCase()!="false"}if(s.dataset.showsourceHidePlugin!==undefined){t.hidePlugin=s.dataset.showsourceHidePlugin.toLowerCase()!="false"}let a=Object.assign({},r,t,e);if(a.skip){return[]}s=s.cloneNode(true);let i=Object.assign({},e,{indentation:a.indentation,hidePlugin:a.hidePlugin});if(typeof a.remove==="string"){let e=a.remove.split(" ").join(",");if(e.trim()!=""){s.querySelectorAll(e).forEach(function(e){e.remove()})}}let o=[];if(!a.hide){o.push(n+"<"+s.tagName.toLowerCase());let t=false;let i=[];if(typeof a.removeAttributes==="string"){i=a.removeAttributes.split(" ")}for(let e of s.attributes){if(e.name.startsWith("data-showsource")){if(a.hidePlugin){continue}}if(i.includes(e.name)){continue}if(e.name.startsWith("data-")||t){o.push(" "+n+e.name+'="'+e.value+'"')}else{o[o.length-1]+=" "+e.name+'="'+e.value+'"'}t=e.name.startsWith("data-")}}if(s.children.length==0&&!a.hide){let e=["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"];if(e.includes(s.tagName.toLowerCase())){o[o.length-1]+=">"+s.innerHTML.trim()}else{let e=["script","style"];if(e.includes(s.tagName.toLowerCase())){o[o.length-1]+=">";let e=s.innerHTML.split("\n");while(e.length>0&&e[0].trim()==""){e.shift()}while(e.length>0&&e[e.length-1].trim()==""){e.pop()}o.push(e.join("\n"));o.push("")}else{if(o.length>1){o[o.length-1]+=">"+s.innerHTML.trim();o.push(n+"")}else{o[o.length-1]+=">"+s.innerHTML.trim()+""}}}}else{if(!a.hide){o[o.length-1]+=">"}if(!a.skipChildren){for(let t of s.childNodes){if(t.nodeType==Node.TEXT_NODE){let e=t.textContent.trim();if(e!==""){o.push(n+a.indentation+e)}}else{let e=l(t,i,n+(a.hide?"":a.indentation));o=o.concat(e)}}}if(!a.hide){o.push(n+"")}}s.remove();return o}function t(){a.querySelectorAll("div[data-showsource]").forEach(function(e){let t=r.class;if(e.dataset.showsourceClass!==undefined){if(typeof e.dataset.showsourceClass==="string"){t=e.dataset.showsourceClass}else{console.error("The value of the data-showsource-class attribute must be a string")}}let i=a.createElement("div");let s=a.createElement("pre");let n=a.createElement("code");n.textContent=l(e).join("\n");s.appendChild(n);i.appendChild(s);t.split(" ").forEach(function(e){i.classList.add(e)});e.parentNode.insertBefore(i,e.nextSibling)})}if(a.addEventListener){a.addEventListener("DOMContentLoaded",function(){t()})}e.showsource={beautify:l,init:t}})(window,document);