From 94c10894ef9760b5d9f467d7a6221e3eec421378 Mon Sep 17 00:00:00 2001 From: Carlos A Date: Wed, 6 Sep 2023 01:02:03 +0200 Subject: [PATCH] update dist --- dist/showsource.js | 29 +++++++++++++++++++++-------- dist/showsource.min.js | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/dist/showsource.js b/dist/showsource.js index 09ab821..48864de 100644 --- a/dist/showsource.js +++ b/dist/showsource.js @@ -33,9 +33,9 @@ hidePlugin: true, removeAttributes: null, class: "showsource", - tagLengthLimit: 100, - maxAttributesInRow: 3, - separateElements: "data-*" + tagLineBreak: null, + maxAttributesPerLine: null, + separateElements: null }; function beautify(el, userOptions = {}, indent = "") { let elOptions = {}; @@ -57,14 +57,26 @@ if (el.dataset.showsourceHidePlugin !== undefined) { elOptions.hidePlugin = el.dataset.showsourceHidePlugin.toLowerCase() != "false"; } - let options = Object.assign({}, defaultOptions, elOptions, userOptions); + if (el.dataset.showsourceTagLineBreak != undefined) { + elOptions.tagLineBreak = parseInt(el.dataset.showsourceTagLineBreak); + } + if (el.dataset.showsourceMaxAttributesPerLine != undefined) { + elOptions.maxAttributesPerLine = parseInt(el.dataset.showsourceMaxAttributesPerLine); + } + if (el.dataset.showsourceSeparateElements != undefined) { + elOptions.separateElements = el.dataset.showsourceSeparateElements; + } + let options = Object.assign({}, defaultOptions, window.showsource.defaults, elOptions, userOptions); if (options.skip) { return []; } el = el.cloneNode(true); let childOptions = Object.assign({}, userOptions, { indentation: options.indentation, - hidePlugin: options.hidePlugin + hidePlugin: options.hidePlugin, + tagLineBreak: options.tagLineBreak, + maxAttributesPerLine: options.maxAttributesPerLine, + separateElements: options.separateElements }); if (typeof options.remove === "string") { let elementsRemove = options.remove.split(" ").join(","); @@ -122,12 +134,12 @@ separatingRegex = null; continue; } - if (attributesInRow >= options.maxAttributesInRow) { + if (options.maxAttributesPerLine !== null && attributesInRow >= options.maxAttributesPerLine) { beautifulElement.push(" " + indent + attributeString); attributesInRow = 1; continue; } - if (attributeString.length + beautifulElement[beautifulElement.length - 1].length > options.tagLengthLimit) { + if (options.tagLineBreak !== null && attributeString.length + beautifulElement[beautifulElement.length - 1].length > options.tagLineBreak) { beautifulElement.push(" " + indent + attributeString); attributesInRow = 1; continue; @@ -214,8 +226,9 @@ }); } window.showsource = { + defaults: Object.assign({}, defaultOptions), beautify: beautify, init: init, - version: "1.1.0" + version: "1.1.1" }; })(window, document); diff --git a/dist/showsource.min.js b/dist/showsource.min.js index 181f2b5..2ec0320 100644 --- a/dist/showsource.min.js +++ b/dist/showsource.min.js @@ -1,2 +1,2 @@ /* Copyright 2023 Carlos A. (https://github.com/dealfonso); License: https://opensource.org/license/mit/ */ -(function(e,o){"use strict";let a={indentation:" ",remove:"h1 h2 h3 h4 h5 h6 p",hide:false,skip:false,skipChildren:false,hidePlugin:true,removeAttributes:null,class:"showsource",tagLengthLimit:100,maxAttributesInRow:3,separateElements:"data-*"};function h(l,e={},r=""){let t={};if(l.dataset.showsourceIndentation!==undefined){t.indentation=l.dataset.showsourceIndentation}if(l.dataset.showsourceRemove!==undefined){t.remove=l.dataset.showsourceRemove}if(l.dataset.showsourceHide!==undefined){t.hide=l.dataset.showsourceHide.toLowerCase()!="false"}if(l.dataset.showsourceSkip!==undefined){t.skip=l.dataset.showsourceSkip.toLowerCase()!="false"}if(l.dataset.showsourceSkipChildren!==undefined){t.skipChildren=l.dataset.showsourceSkipChildren.toLowerCase()!="false"}if(l.dataset.showsourceHidePlugin!==undefined){t.hidePlugin=l.dataset.showsourceHidePlugin.toLowerCase()!="false"}let d=Object.assign({},a,t,e);if(d.skip){return[]}l=l.cloneNode(true);let i=Object.assign({},e,{indentation:d.indentation,hidePlugin:d.hidePlugin});if(typeof d.remove==="string"){let e=d.remove.split(" ").join(",");if(e.trim()!=""){l.querySelectorAll(e).forEach(function(e){e.remove()})}}let u=[];if(!d.hide){u.push(r+"<"+l.tagName.toLowerCase());let n=[];if(typeof d.removeAttributes==="string"){n=d.removeAttributes.split(" ")}let s=[];if(typeof d.separateElements==="string"){let t=d.separateElements.split(" ");for(let e of t){e=e.replace(/\*/g,".*");s.push(new RegExp(e))}}let o=0;let a=null;for(let i of l.attributes){if(i.name.startsWith("data-showsource")){if(d.hidePlugin){continue}}if(n.includes(i.name)){continue}let e=`${i.name}="${i.value}"`;let t=null;if(s.length>0){for(let e of s){if(e.test(i.name)){t=e;break}}}if(t!==null){u.push(" "+r+e);o=1;a=t;continue}if(a!==null){u.push(" "+r+e);o=1;a=null;continue}if(o>=d.maxAttributesInRow){u.push(" "+r+e);o=1;continue}if(e.length+u[u.length-1].length>d.tagLengthLimit){u.push(" "+r+e);o=1;continue}u[u.length-1]+=" "+e;o++}}if(l.children.length==0&&!d.hide){let e=["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"];if(e.includes(l.tagName.toLowerCase())){u[u.length-1]+=">"+l.innerHTML.trim()}else{let e=["script","style"];if(e.includes(l.tagName.toLowerCase())){u[u.length-1]+=">";let e=l.innerHTML.split("\n");while(e.length>0&&e[0].trim()==""){e.shift()}while(e.length>0&&e[e.length-1].trim()==""){e.pop()}u.push(e.join("\n"));u.push("")}else{if(u.length>1){u[u.length-1]+=">"+l.innerHTML.trim();u.push(r+"")}else{u[u.length-1]+=">"+l.innerHTML.trim()+""}}}}else{if(!d.hide){u[u.length-1]+=">"}if(!d.skipChildren){for(let t of l.childNodes){if(t.nodeType==Node.TEXT_NODE){let e=t.textContent.trim();if(e!==""){u.push(r+d.indentation+e)}}else{let e=h(t,i,r+(d.hide?"":d.indentation));u=u.concat(e)}}}if(!d.hide){u.push(r+"")}}l.remove();return u}function t(){o.querySelectorAll("div[data-showsource]").forEach(function(e){let t=a.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=o.createElement("div");let n=o.createElement("pre");let s=o.createElement("code");s.textContent=h(e).join("\n");n.appendChild(s);i.appendChild(n);t.split(" ").forEach(function(e){i.classList.add(e)});e.parentNode.insertBefore(i,e.nextSibling)})}if(o.addEventListener){o.addEventListener("DOMContentLoaded",function(){t()})}e.showsource={beautify:h,init:t,version:"1.1.0"}})(window,document); +(function(i,a){"use strict";let o={indentation:" ",remove:"h1 h2 h3 h4 h5 h6 p",hide:false,skip:false,skipChildren:false,hidePlugin:true,removeAttributes:null,class:"showsource",tagLineBreak:null,maxAttributesPerLine:null,separateElements:null};function h(r,e={},l=""){let t={};if(r.dataset.showsourceIndentation!==undefined){t.indentation=r.dataset.showsourceIndentation}if(r.dataset.showsourceRemove!==undefined){t.remove=r.dataset.showsourceRemove}if(r.dataset.showsourceHide!==undefined){t.hide=r.dataset.showsourceHide.toLowerCase()!="false"}if(r.dataset.showsourceSkip!==undefined){t.skip=r.dataset.showsourceSkip.toLowerCase()!="false"}if(r.dataset.showsourceSkipChildren!==undefined){t.skipChildren=r.dataset.showsourceSkipChildren.toLowerCase()!="false"}if(r.dataset.showsourceHidePlugin!==undefined){t.hidePlugin=r.dataset.showsourceHidePlugin.toLowerCase()!="false"}if(r.dataset.showsourceTagLineBreak!=undefined){t.tagLineBreak=parseInt(r.dataset.showsourceTagLineBreak)}if(r.dataset.showsourceMaxAttributesPerLine!=undefined){t.maxAttributesPerLine=parseInt(r.dataset.showsourceMaxAttributesPerLine)}if(r.dataset.showsourceSeparateElements!=undefined){t.separateElements=r.dataset.showsourceSeparateElements}let u=Object.assign({},o,i.showsource.defaults,t,e);if(u.skip){return[]}r=r.cloneNode(true);let s=Object.assign({},e,{indentation:u.indentation,hidePlugin:u.hidePlugin,tagLineBreak:u.tagLineBreak,maxAttributesPerLine:u.maxAttributesPerLine,separateElements:u.separateElements});if(typeof u.remove==="string"){let e=u.remove.split(" ").join(",");if(e.trim()!=""){r.querySelectorAll(e).forEach(function(e){e.remove()})}}let d=[];if(!u.hide){d.push(l+"<"+r.tagName.toLowerCase());let i=[];if(typeof u.removeAttributes==="string"){i=u.removeAttributes.split(" ")}let n=[];if(typeof u.separateElements==="string"){let t=u.separateElements.split(" ");for(let e of t){e=e.replace(/\*/g,".*");n.push(new RegExp(e))}}let a=0;let o=null;for(let s of r.attributes){if(s.name.startsWith("data-showsource")){if(u.hidePlugin){continue}}if(i.includes(s.name)){continue}let e=`${s.name}="${s.value}"`;let t=null;if(n.length>0){for(let e of n){if(e.test(s.name)){t=e;break}}}if(t!==null){d.push(" "+l+e);a=1;o=t;continue}if(o!==null){d.push(" "+l+e);a=1;o=null;continue}if(u.maxAttributesPerLine!==null&&a>=u.maxAttributesPerLine){d.push(" "+l+e);a=1;continue}if(u.tagLineBreak!==null&&e.length+d[d.length-1].length>u.tagLineBreak){d.push(" "+l+e);a=1;continue}d[d.length-1]+=" "+e;a++}}if(r.children.length==0&&!u.hide){let e=["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"];if(e.includes(r.tagName.toLowerCase())){d[d.length-1]+=">"+r.innerHTML.trim()}else{let e=["script","style"];if(e.includes(r.tagName.toLowerCase())){d[d.length-1]+=">";let e=r.innerHTML.split("\n");while(e.length>0&&e[0].trim()==""){e.shift()}while(e.length>0&&e[e.length-1].trim()==""){e.pop()}d.push(e.join("\n"));d.push("")}else{if(d.length>1){d[d.length-1]+=">"+r.innerHTML.trim();d.push(l+"")}else{d[d.length-1]+=">"+r.innerHTML.trim()+""}}}}else{if(!u.hide){d[d.length-1]+=">"}if(!u.skipChildren){for(let t of r.childNodes){if(t.nodeType==Node.TEXT_NODE){let e=t.textContent.trim();if(e!==""){d.push(l+u.indentation+e)}}else{let e=h(t,s,l+(u.hide?"":u.indentation));d=d.concat(e)}}}if(!u.hide){d.push(l+"")}}r.remove();return d}function e(){a.querySelectorAll("div[data-showsource]").forEach(function(e){let t=o.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 s=a.createElement("div");let i=a.createElement("pre");let n=a.createElement("code");n.textContent=h(e).join("\n");i.appendChild(n);s.appendChild(i);t.split(" ").forEach(function(e){s.classList.add(e)});e.parentNode.insertBefore(s,e.nextSibling)})}if(a.addEventListener){a.addEventListener("DOMContentLoaded",function(){e()})}i.showsource={defaults:Object.assign({},o),beautify:h,init:e,version:"1.1.1"}})(window,document);