|
26 | 26 | "use strict";
|
27 | 27 | let defaultOptions = {
|
28 | 28 | indentation: " ",
|
29 |
| - remove: "h1 h2 h3 h4 h5 h6 p", |
30 | 29 | hide: false,
|
| 30 | + hideSelector: null, |
| 31 | + hideSelectorAdd: null, |
31 | 32 | skip: false,
|
| 33 | + skipSelector: null, |
| 34 | + skipSelectorAdd: null, |
32 | 35 | skipChildren: false,
|
33 | 36 | hidePlugin: true,
|
34 | 37 | removeAttributes: null,
|
|
37 | 40 | maxAttributesPerLine: null,
|
38 | 41 | separateElements: null
|
39 | 42 | };
|
40 |
| - function beautify(el, userOptions = {}, indent = "") { |
| 43 | + function extractCode(el, userOptions = {}, indent = "") { |
41 | 44 | let elOptions = {};
|
42 | 45 | if (el.dataset.showsourceIndentation !== undefined) {
|
43 | 46 | elOptions.indentation = el.dataset.showsourceIndentation;
|
44 | 47 | }
|
45 |
| - if (el.dataset.showsourceRemove !== undefined) { |
46 |
| - elOptions.remove = el.dataset.showsourceRemove; |
| 48 | + if (el.dataset.showsourceSkipSelector !== undefined) { |
| 49 | + elOptions.skipSelector = el.dataset.showsourceSkipSelector; |
| 50 | + } |
| 51 | + if (el.dataset.showsourceSkipSelectorAdd !== undefined) { |
| 52 | + elOptions.skipSelectorAdd = el.dataset.showsourceSkipSelectorAdd; |
47 | 53 | }
|
48 | 54 | if (el.dataset.showsourceHide !== undefined) {
|
49 | 55 | elOptions.hide = el.dataset.showsourceHide.toLowerCase() != "false";
|
50 | 56 | }
|
| 57 | + if (el.dataset.showsourceHideSelector !== undefined) { |
| 58 | + elOptions.hideSelector = el.dataset.showsourceHideSelector; |
| 59 | + } |
| 60 | + if (el.dataset.showsourceHideSelectorAdd !== undefined) { |
| 61 | + elOptions.hideSelectorAdd = el.dataset.showsourceHideSelectorAdd; |
| 62 | + } |
51 | 63 | if (el.dataset.showsourceSkip !== undefined) {
|
52 | 64 | elOptions.skip = el.dataset.showsourceSkip.toLowerCase() != "false";
|
53 | 65 | }
|
|
70 | 82 | if (options.skip) {
|
71 | 83 | return [];
|
72 | 84 | }
|
73 |
| - el = el.cloneNode(true); |
| 85 | + if (typeof options.skipSelector === "string") { |
| 86 | + let elementsSkip = options.skipSelector.split(",").filter(e => e.trim() !== "").join(","); |
| 87 | + if (elementsSkip.trim() != "") { |
| 88 | + if (el.matches(elementsSkip)) { |
| 89 | + return []; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + if (typeof options.skipSelectorAdd === "string") { |
| 94 | + if (options.skipSelector === undefined || options.skipSelector === null) { |
| 95 | + options.skipSelector = options.skipSelectorAdd; |
| 96 | + } else { |
| 97 | + options.skipSelector += ", " + options.skipSelectorAdd; |
| 98 | + } |
| 99 | + } |
| 100 | + if (typeof options.hideSelectorAdd === "string") { |
| 101 | + if (options.hideSelector === undefined || options.hideSelector === null) { |
| 102 | + options.hideSelector = options.hideSelectorAdd; |
| 103 | + } else { |
| 104 | + options.hideSelector += "," + options.hideSelectorAdd; |
| 105 | + } |
| 106 | + } |
74 | 107 | let childOptions = Object.assign({}, userOptions, {
|
75 | 108 | indentation: options.indentation,
|
76 | 109 | hidePlugin: options.hidePlugin,
|
77 | 110 | tagLineBreak: options.tagLineBreak,
|
78 | 111 | maxAttributesPerLine: options.maxAttributesPerLine,
|
79 |
| - separateElements: options.separateElements |
| 112 | + separateElements: options.separateElements, |
| 113 | + skipSelector: options.skipSelector, |
| 114 | + hideSelector: options.hideSelector |
80 | 115 | });
|
81 |
| - if (typeof options.remove === "string") { |
82 |
| - let elementsRemove = options.remove.split(" ").join(","); |
83 |
| - if (elementsRemove.trim() != "") { |
84 |
| - el.querySelectorAll(elementsRemove).forEach(function(e) { |
85 |
| - e.remove(); |
86 |
| - }); |
| 116 | + let beautifulElement = []; |
| 117 | + if (!options.hide) { |
| 118 | + if (typeof options.hideSelector === "string") { |
| 119 | + let elementsHide = options.hideSelector.split(",").filter(x => x.trim() !== "").join(","); |
| 120 | + if (elementsHide.trim() != "") { |
| 121 | + options.hide = el.matches(elementsHide); |
| 122 | + } |
87 | 123 | }
|
88 | 124 | }
|
89 |
| - let beautifulElement = []; |
90 | 125 | if (!options.hide) {
|
91 | 126 | beautifulElement.push(indent + "<" + el.tagName.toLowerCase());
|
92 | 127 | let removeAttributes = [];
|
|
164 | 199 | lines.pop();
|
165 | 200 | }
|
166 | 201 | beautifulElement.push(lines.join("\n"));
|
167 |
| - beautifulElement.push("</" + el.tagName.toLowerCase() + ">"); |
| 202 | + beautifulElement.push(indent + "</" + el.tagName.toLowerCase() + ">"); |
168 | 203 | } else {
|
169 | 204 | if (beautifulElement.length > 1) {
|
170 | 205 | beautifulElement[beautifulElement.length - 1] += ">" + el.innerHTML.trim();
|
|
186 | 221 | beautifulElement.push(indent + options.indentation + text);
|
187 | 222 | }
|
188 | 223 | } else {
|
189 |
| - let beautifulChild = beautify(child, childOptions, indent + (options.hide ? "" : options.indentation)); |
| 224 | + let beautifulChild = extractCode(child, childOptions, indent + (options.hide ? "" : options.indentation)); |
190 | 225 | beautifulElement = beautifulElement.concat(beautifulChild);
|
191 | 226 | }
|
192 | 227 | }
|
|
195 | 230 | beautifulElement.push(indent + "</" + el.tagName.toLowerCase() + ">");
|
196 | 231 | }
|
197 | 232 | }
|
198 |
| - el.remove(); |
199 | 233 | return beautifulElement;
|
200 | 234 | }
|
201 | 235 | function init() {
|
|
211 | 245 | let container = document.createElement("div");
|
212 | 246 | let pre = document.createElement("pre");
|
213 | 247 | let code = document.createElement("code");
|
214 |
| - code.textContent = beautify(el).join("\n"); |
| 248 | + code.textContent = extractCode(el).join("\n"); |
215 | 249 | pre.appendChild(code);
|
216 | 250 | container.appendChild(pre);
|
217 | 251 | classString.split(" ").forEach(function(c) {
|
|
227 | 261 | }
|
228 | 262 | window.showsource = {
|
229 | 263 | defaults: Object.assign({}, defaultOptions),
|
230 |
| - beautify: beautify, |
| 264 | + extract: extractCode, |
231 | 265 | init: init,
|
232 |
| - version: "1.1.1" |
| 266 | + version: "1.2.0" |
233 | 267 | };
|
234 | 268 | })(window, document);
|
0 commit comments