purgecss-from-html 6.0.0 → 7.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Full Human
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1 +1,82 @@
1
- import*as s from"parse5";import*as t from"parse5-htmlparser2-tree-adapter";const e=(s,t)=>({attributes:{names:[...s.attributes.names,...t.attributes.names],values:[...s.attributes.values,...t.attributes.values]},classes:[...s.classes,...t.classes],ids:[...s.ids,...t.ids],tags:[...s.tags,...t.tags],undetermined:[]}),a=s=>{const t={attributes:{names:[],values:[]},classes:[],ids:[],tags:[s.name],undetermined:[]};for(const[e,a]of Object.entries(s.attribs))"class"===e?t.classes.push(...a.split(" ")):"id"===e&&t.ids.push(...a.split(" ")),t.attributes.names.push(e),t.attributes.values.push(...a.split(" "));return e(r(s),t)},r=s=>{let t={attributes:{names:[],values:[]},classes:[],ids:[],tags:[],undetermined:[]};for(const i of s.childNodes){const s=i;switch(s.type){case"tag":t=e(t,a(s));break;case"root":t=e(t,r(s))}}return t},i=e=>{const a=s.parse(e,{treeAdapter:t.adapter});return r(a)};export{i as default};
1
+ import * as parse5 from 'parse5';
2
+ import * as htmlparser2 from 'parse5-htmlparser2-tree-adapter';
3
+
4
+ const mergedExtractorResults = (resultLeft, resultRight) => {
5
+ return {
6
+ attributes: {
7
+ names: [...resultLeft.attributes.names, ...resultRight.attributes.names],
8
+ values: [
9
+ ...resultLeft.attributes.values,
10
+ ...resultRight.attributes.values,
11
+ ],
12
+ },
13
+ classes: [...resultLeft.classes, ...resultRight.classes],
14
+ ids: [...resultLeft.ids, ...resultRight.ids],
15
+ tags: [...resultLeft.tags, ...resultRight.tags],
16
+ undetermined: [],
17
+ };
18
+ };
19
+ const getSelectorsInElement = (element) => {
20
+ const result = {
21
+ attributes: {
22
+ names: [],
23
+ values: [],
24
+ },
25
+ classes: [],
26
+ ids: [],
27
+ tags: [element.name],
28
+ undetermined: [],
29
+ };
30
+ for (const [name, value] of Object.entries(element.attribs)) {
31
+ if (name === "class") {
32
+ result.classes.push(...value.split(" "));
33
+ }
34
+ else if (name === "id") {
35
+ result.ids.push(...value.split(" "));
36
+ }
37
+ // always add to attributes, to handle things like [class*=foo]
38
+ result.attributes.names.push(name);
39
+ result.attributes.values.push(...value.split(" "));
40
+ }
41
+ return mergedExtractorResults(getSelectorsInNodes(element), result);
42
+ };
43
+ const getSelectorsInNodes = (node) => {
44
+ let result = {
45
+ attributes: {
46
+ names: [],
47
+ values: [],
48
+ },
49
+ classes: [],
50
+ ids: [],
51
+ tags: [],
52
+ undetermined: [],
53
+ };
54
+ for (const childNode of node.childNodes) {
55
+ const element = childNode;
56
+ switch (element.type) {
57
+ case "tag":
58
+ result = mergedExtractorResults(result, getSelectorsInElement(element));
59
+ break;
60
+ case "root":
61
+ result = mergedExtractorResults(result, getSelectorsInNodes(element));
62
+ break;
63
+ }
64
+ }
65
+ return result;
66
+ };
67
+ /**
68
+ * Get the potential selectors from HTML code
69
+ *
70
+ * @param content - HTML code
71
+ * @returns the attributes, classes, ids, and tags from the HTML code
72
+ *
73
+ * @public
74
+ */
75
+ const purgecssFromHtml = (content) => {
76
+ const tree = parse5.parse(content, {
77
+ treeAdapter: htmlparser2.adapter,
78
+ });
79
+ return getSelectorsInNodes(tree);
80
+ };
81
+
82
+ export { purgecssFromHtml as default };
@@ -1 +1,104 @@
1
- "use strict";var e=require("parse5"),t=require("parse5-htmlparser2-tree-adapter");function s(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})}})),t.default=e,Object.freeze(t)}var r=s(e),a=s(t);const u=(e,t)=>({attributes:{names:[...e.attributes.names,...t.attributes.names],values:[...e.attributes.values,...t.attributes.values]},classes:[...e.classes,...t.classes],ids:[...e.ids,...t.ids],tags:[...e.tags,...t.tags],undetermined:[]}),i=e=>{const t={attributes:{names:[],values:[]},classes:[],ids:[],tags:[e.name],undetermined:[]};for(const[s,r]of Object.entries(e.attribs))"class"===s?t.classes.push(...r.split(" ")):"id"===s&&t.ids.push(...r.split(" ")),t.attributes.names.push(s),t.attributes.values.push(...r.split(" "));return u(n(e),t)},n=e=>{let t={attributes:{names:[],values:[]},classes:[],ids:[],tags:[],undetermined:[]};for(const s of e.childNodes){const e=s;switch(e.type){case"tag":t=u(t,i(e));break;case"root":t=u(t,n(e))}}return t};module.exports=e=>{const t=r.parse(e,{treeAdapter:a.adapter});return n(t)};
1
+ 'use strict';
2
+
3
+ var parse5 = require('parse5');
4
+ var htmlparser2 = require('parse5-htmlparser2-tree-adapter');
5
+
6
+ function _interopNamespaceDefault(e) {
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n.default = e;
20
+ return Object.freeze(n);
21
+ }
22
+
23
+ var parse5__namespace = /*#__PURE__*/_interopNamespaceDefault(parse5);
24
+ var htmlparser2__namespace = /*#__PURE__*/_interopNamespaceDefault(htmlparser2);
25
+
26
+ const mergedExtractorResults = (resultLeft, resultRight) => {
27
+ return {
28
+ attributes: {
29
+ names: [...resultLeft.attributes.names, ...resultRight.attributes.names],
30
+ values: [
31
+ ...resultLeft.attributes.values,
32
+ ...resultRight.attributes.values,
33
+ ],
34
+ },
35
+ classes: [...resultLeft.classes, ...resultRight.classes],
36
+ ids: [...resultLeft.ids, ...resultRight.ids],
37
+ tags: [...resultLeft.tags, ...resultRight.tags],
38
+ undetermined: [],
39
+ };
40
+ };
41
+ const getSelectorsInElement = (element) => {
42
+ const result = {
43
+ attributes: {
44
+ names: [],
45
+ values: [],
46
+ },
47
+ classes: [],
48
+ ids: [],
49
+ tags: [element.name],
50
+ undetermined: [],
51
+ };
52
+ for (const [name, value] of Object.entries(element.attribs)) {
53
+ if (name === "class") {
54
+ result.classes.push(...value.split(" "));
55
+ }
56
+ else if (name === "id") {
57
+ result.ids.push(...value.split(" "));
58
+ }
59
+ // always add to attributes, to handle things like [class*=foo]
60
+ result.attributes.names.push(name);
61
+ result.attributes.values.push(...value.split(" "));
62
+ }
63
+ return mergedExtractorResults(getSelectorsInNodes(element), result);
64
+ };
65
+ const getSelectorsInNodes = (node) => {
66
+ let result = {
67
+ attributes: {
68
+ names: [],
69
+ values: [],
70
+ },
71
+ classes: [],
72
+ ids: [],
73
+ tags: [],
74
+ undetermined: [],
75
+ };
76
+ for (const childNode of node.childNodes) {
77
+ const element = childNode;
78
+ switch (element.type) {
79
+ case "tag":
80
+ result = mergedExtractorResults(result, getSelectorsInElement(element));
81
+ break;
82
+ case "root":
83
+ result = mergedExtractorResults(result, getSelectorsInNodes(element));
84
+ break;
85
+ }
86
+ }
87
+ return result;
88
+ };
89
+ /**
90
+ * Get the potential selectors from HTML code
91
+ *
92
+ * @param content - HTML code
93
+ * @returns the attributes, classes, ids, and tags from the HTML code
94
+ *
95
+ * @public
96
+ */
97
+ const purgecssFromHtml = (content) => {
98
+ const tree = parse5__namespace.parse(content, {
99
+ treeAdapter: htmlparser2__namespace.adapter,
100
+ });
101
+ return getSelectorsInNodes(tree);
102
+ };
103
+
104
+ module.exports = purgecssFromHtml;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "purgecss-from-html",
3
- "version": "6.0.0",
3
+ "version": "7.0.2",
4
4
  "description": "HTML extractor for PurgeCSS",
5
5
  "author": "Ffloriel",
6
6
  "homepage": "https://github.com/FullHuman/purgecss#readme",
@@ -31,5 +31,6 @@
31
31
  "publishConfig": {
32
32
  "access": "public",
33
33
  "registry": "https://registry.npmjs.org/"
34
- }
34
+ },
35
+ "gitHead": "e95398d961e03a809b5e3da75e3dae1813b46b68"
35
36
  }