@gem-sdk/hash-class-names 1.58.0-dev.53 → 2.0.0-dev.1082
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/dist/cjs/css-escape.js +1 -57
- package/dist/cjs/css-parser.js +1 -122
- package/dist/cjs/index.js +1 -255
- package/dist/esm/css-escape.js +1 -55
- package/dist/esm/css-parser.js +1 -119
- package/dist/esm/index.js +1 -253
- package/package.json +1 -1
package/dist/cjs/css-escape.js
CHANGED
|
@@ -1,57 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// https://drafts.csswg.org/cssom/#serialize-an-identifier
|
|
4
|
-
// repo: https://github.com/mathiasbynens/CSS.escape
|
|
5
|
-
const cssEscape = function(value) {
|
|
6
|
-
if (arguments.length == 0) {
|
|
7
|
-
throw new TypeError('`CSS.escape` requires an argument.');
|
|
8
|
-
}
|
|
9
|
-
const string = String(value);
|
|
10
|
-
const length = string.length;
|
|
11
|
-
let index = -1;
|
|
12
|
-
let codeUnit;
|
|
13
|
-
let result = '';
|
|
14
|
-
const firstCodeUnit = string.charCodeAt(0);
|
|
15
|
-
if (// If the character is the first character and is a `-` (U+002D), and
|
|
16
|
-
// there is no second character, […]
|
|
17
|
-
length == 1 && firstCodeUnit == 0x002d) {
|
|
18
|
-
return '\\' + string;
|
|
19
|
-
}
|
|
20
|
-
while(++index < length){
|
|
21
|
-
codeUnit = string.charCodeAt(index);
|
|
22
|
-
// Note: there’s no need to special-case astral symbols, surrogate
|
|
23
|
-
// pairs, or lone surrogates.
|
|
24
|
-
// If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
|
|
25
|
-
// (U+FFFD).
|
|
26
|
-
if (codeUnit == 0x0000) {
|
|
27
|
-
result += '\uFFFD';
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
if (// If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
|
|
31
|
-
// U+007F, […]
|
|
32
|
-
codeUnit >= 0x0001 && codeUnit <= 0x001f || codeUnit == 0x007f || // If the character is the first character and is in the range [0-9]
|
|
33
|
-
// (U+0030 to U+0039), […]
|
|
34
|
-
index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039 || // If the character is the second character and is in the range [0-9]
|
|
35
|
-
// (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
|
|
36
|
-
index == 1 && codeUnit >= 0x0030 && codeUnit <= 0x0039 && firstCodeUnit == 0x002d) {
|
|
37
|
-
// https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
|
|
38
|
-
result += '\\' + codeUnit.toString(16) + ' ';
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
// If the character is not handled by one of the above rules and is
|
|
42
|
-
// greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
|
|
43
|
-
// is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
|
|
44
|
-
// U+005A), or [a-z] (U+0061 to U+007A), […]
|
|
45
|
-
if (codeUnit >= 0x0080 || codeUnit == 0x002d || codeUnit == 0x005f || codeUnit >= 0x0030 && codeUnit <= 0x0039 || codeUnit >= 0x0041 && codeUnit <= 0x005a || codeUnit >= 0x0061 && codeUnit <= 0x007a) {
|
|
46
|
-
// the character itself
|
|
47
|
-
result += string.charAt(index);
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
// Otherwise, the escaped character.
|
|
51
|
-
// https://drafts.csswg.org/cssom/#escape-a-character
|
|
52
|
-
result += '\\' + string.charAt(index);
|
|
53
|
-
}
|
|
54
|
-
return result;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
exports.cssEscape = cssEscape;
|
|
1
|
+
"use strict";const cssEscape=function(t){let e;if(0==arguments.length)throw TypeError("`CSS.escape` requires an argument.");let r=String(t),c=r.length,n=-1,s="",i=r.charCodeAt(0);if(1==c&&45==i)return"\\"+r;for(;++n<c;){if(0==(e=r.charCodeAt(n))){s+="�";continue}if(e>=1&&e<=31||127==e||0==n&&e>=48&&e<=57||1==n&&e>=48&&e<=57&&45==i){s+="\\"+e.toString(16)+" ";continue}if(e>=128||45==e||95==e||e>=48&&e<=57||e>=65&&e<=90||e>=97&&e<=122){s+=r.charAt(n);continue}s+="\\"+r.charAt(n)}return s};exports.cssEscape=cssEscape;
|
package/dist/cjs/css-parser.js
CHANGED
|
@@ -1,122 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const cssToJson = (str)=>{
|
|
4
|
-
let style = str;
|
|
5
|
-
const jsonCSS = {
|
|
6
|
-
children: {},
|
|
7
|
-
attributes: ''
|
|
8
|
-
};
|
|
9
|
-
const mediaQuery = {};
|
|
10
|
-
// eslint-disable-next-line regexp/prefer-d, no-useless-escape, regexp/no-dupe-characters-character-class, regexp/no-useless-escape, regexp/strict, regexp/no-obscure-range, regexp/no-useless-flag, regexp/no-super-linear-backtracking
|
|
11
|
-
const regex = /(@media[^{]+\)).*?{([\s\S]+?})\s*}/g;
|
|
12
|
-
let m;
|
|
13
|
-
while((m = regex.exec(str)) !== null){
|
|
14
|
-
// This is necessary to avoid infinite loops with zero-width matches
|
|
15
|
-
if (m.index === regex.lastIndex) {
|
|
16
|
-
regex.lastIndex++;
|
|
17
|
-
}
|
|
18
|
-
// The result can be accessed through the `m`-variable.
|
|
19
|
-
const css = {
|
|
20
|
-
media: '',
|
|
21
|
-
style: ''
|
|
22
|
-
};
|
|
23
|
-
m.forEach((match, groupIndex)=>{
|
|
24
|
-
if (groupIndex == 0) {
|
|
25
|
-
style = style.replaceAll(match, '');
|
|
26
|
-
}
|
|
27
|
-
if (groupIndex == 1) {
|
|
28
|
-
css.media = match;
|
|
29
|
-
}
|
|
30
|
-
if (groupIndex == 2) {
|
|
31
|
-
css.style = match;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
mediaQuery[css.media] = mediaQuery[css.media] || [];
|
|
35
|
-
mediaQuery[css.media]?.push(css.style);
|
|
36
|
-
}
|
|
37
|
-
// Parse css root
|
|
38
|
-
const cssRoot = parseCSS(style);
|
|
39
|
-
for(const key in cssRoot){
|
|
40
|
-
if (Object.prototype.hasOwnProperty.call(cssRoot, key)) {
|
|
41
|
-
const value = cssRoot[key];
|
|
42
|
-
jsonCSS.children[key] = {
|
|
43
|
-
children: {},
|
|
44
|
-
attributes: value
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
// Append css media
|
|
49
|
-
for(const media in mediaQuery){
|
|
50
|
-
if (Object.prototype.hasOwnProperty.call(mediaQuery, media)) {
|
|
51
|
-
const css = mediaQuery[media];
|
|
52
|
-
if (css?.length) {
|
|
53
|
-
const jsonQueryCss = {
|
|
54
|
-
children: {},
|
|
55
|
-
attributes: ''
|
|
56
|
-
};
|
|
57
|
-
const cssQuery = parseCSS(css?.join(''));
|
|
58
|
-
for(const key in cssQuery){
|
|
59
|
-
if (Object.prototype.hasOwnProperty.call(cssQuery, key)) {
|
|
60
|
-
const value = cssQuery[key];
|
|
61
|
-
jsonQueryCss.children[key] = {
|
|
62
|
-
children: {},
|
|
63
|
-
attributes: value
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
jsonCSS.children[media] = jsonQueryCss;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return jsonCSS;
|
|
72
|
-
};
|
|
73
|
-
const jsonToCss = (jsonCSS, selector)=>{
|
|
74
|
-
// Replace selector
|
|
75
|
-
let value = '';
|
|
76
|
-
for(const selector in jsonCSS.children){
|
|
77
|
-
if (Object.prototype.hasOwnProperty.call(jsonCSS.children, selector)) {
|
|
78
|
-
const style = jsonCSS.children[selector];
|
|
79
|
-
if (style) {
|
|
80
|
-
value += jsonToCss(style, selector);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if (!value) {
|
|
85
|
-
value = jsonCSS?.attributes || '';
|
|
86
|
-
}
|
|
87
|
-
if (selector) {
|
|
88
|
-
return `${selector}{${value}}`;
|
|
89
|
-
} else {
|
|
90
|
-
return value;
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
const parseCSS = (css)=>{
|
|
94
|
-
const cssJSON = {};
|
|
95
|
-
// eslint-disable-next-line regexp/prefer-d, no-useless-escape, regexp/no-dupe-characters-character-class, regexp/no-useless-escape, regexp/strict, regexp/no-obscure-range, regexp/no-useless-flag, regexp/no-super-linear-backtracking, regexp/prefer-character-class, regexp/no-dupe-disjunctions
|
|
96
|
-
const cssRegex = /((.|\n|\r|\t)*?){((.|\n|\r|\t)*?)}/gm;
|
|
97
|
-
let mCSS;
|
|
98
|
-
while((mCSS = cssRegex.exec(css)) !== null){
|
|
99
|
-
// This is necessary to avoid infinite loops with zero-width matches
|
|
100
|
-
if (mCSS.index === cssRegex.lastIndex) {
|
|
101
|
-
cssRegex.lastIndex++;
|
|
102
|
-
}
|
|
103
|
-
// The result can be accessed through the `m`-variable.
|
|
104
|
-
const selector = {
|
|
105
|
-
key: '',
|
|
106
|
-
value: ''
|
|
107
|
-
};
|
|
108
|
-
mCSS.forEach((match, groupIndex)=>{
|
|
109
|
-
if (groupIndex == 1) {
|
|
110
|
-
selector.key = match.trim();
|
|
111
|
-
}
|
|
112
|
-
if (groupIndex == 3) {
|
|
113
|
-
selector.value = match;
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
cssJSON[selector.key] = selector.value;
|
|
117
|
-
}
|
|
118
|
-
return cssJSON;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
exports.cssToJson = cssToJson;
|
|
122
|
-
exports.jsonToCss = jsonToCss;
|
|
1
|
+
"use strict";const cssToJson=e=>{let t,l=e,r={children:{},attributes:""},s={},n=/(@media[^{]+\)).*?{([\s\S]+?})\s*}/g;for(;null!==(t=n.exec(e));){t.index===n.lastIndex&&n.lastIndex++;let e={media:"",style:""};t.forEach((t,r)=>{0==r&&(l=l.replaceAll(t,"")),1==r&&(e.media=t),2==r&&(e.style=t)}),s[e.media]=s[e.media]||[],s[e.media]?.push(e.style)}let o=parseCSS(l);for(let e in o)if(Object.prototype.hasOwnProperty.call(o,e)){let t=o[e];r.children[e]={children:{},attributes:t}}for(let e in s)if(Object.prototype.hasOwnProperty.call(s,e)){let t=s[e];if(t?.length){let l={children:{},attributes:""},s=parseCSS(t?.join(""));for(let e in s)if(Object.prototype.hasOwnProperty.call(s,e)){let t=s[e];l.children[e]={children:{},attributes:t}}r.children[e]=l}}return r},jsonToCss=(e,t)=>{let l="";for(let t in e.children)if(Object.prototype.hasOwnProperty.call(e.children,t)){let r=e.children[t];r&&(l+=jsonToCss(r,t))}return(l||(l=e?.attributes||""),t)?`${t}{${l}}`:l},parseCSS=e=>{let t;let l={},r=/((.|\n|\r|\t)*?){((.|\n|\r|\t)*?)}/gm;for(;null!==(t=r.exec(e));){t.index===r.lastIndex&&r.lastIndex++;let e={key:"",value:""};t.forEach((t,l)=>{1==l&&(e.key=t.trim()),3==l&&(e.value=t)}),l[e.key]=e.value}return l};exports.cssToJson=cssToJson,exports.jsonToCss=jsonToCss;
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,255 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var parse5 = require('parse5');
|
|
4
|
-
var cssEscape = require('./css-escape.js');
|
|
5
|
-
var cssParser = require('./css-parser.js');
|
|
6
|
-
|
|
7
|
-
const hashClassNames = (html, css, options)=>{
|
|
8
|
-
const stringBreak = '<!-- End extra file -->';
|
|
9
|
-
const { html: htmlWithoutLiquid, arrayMatch } = replaceLiquidToBKPlaceholder(html.join(stringBreak));
|
|
10
|
-
const documentFragment = parse5.parseFragment(htmlWithoutLiquid);
|
|
11
|
-
const jsonHTML = documentFragment.childNodes;
|
|
12
|
-
if (jsonHTML?.length) {
|
|
13
|
-
const classes = {};
|
|
14
|
-
const hashClasses = {};
|
|
15
|
-
// Find & Obfuscate class in html
|
|
16
|
-
const loopNode = (childNodes, callback)=>{
|
|
17
|
-
if (childNodes?.length) {
|
|
18
|
-
for(let i = 0; i < childNodes.length; i++){
|
|
19
|
-
const childNode = childNodes[i];
|
|
20
|
-
if (childNode) {
|
|
21
|
-
const keepClass = childNode.attrs?.find((item)=>item.name == 'data-keep-class')?.value;
|
|
22
|
-
if (!keepClass) {
|
|
23
|
-
callback(childNode);
|
|
24
|
-
if (childNode.childNodes?.length) {
|
|
25
|
-
loopNode(childNode.childNodes, callback);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
loopNode(jsonHTML, (node)=>{
|
|
33
|
-
if (node?.attrs?.length) {
|
|
34
|
-
const attrClass = node.attrs.find((item)=>item.name == 'class');
|
|
35
|
-
if (attrClass?.value) {
|
|
36
|
-
const nodeClasses = attrClass.value.split(' ');
|
|
37
|
-
if (nodeClasses?.length) {
|
|
38
|
-
for(let i = 0; i < nodeClasses.length; i++){
|
|
39
|
-
let nodeClass = nodeClasses[i]?.trim() || '';
|
|
40
|
-
nodeClass = nodeClass.replace(/bk-liquid.*?bk-liquid/g, '');
|
|
41
|
-
nodeClass = replacePseudoClasses(nodeClass);
|
|
42
|
-
if (options?.ignoreClasses?.includes(nodeClass)) {
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
if (options?.classList && !options.classList.includes(nodeClass)) {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
if (nodeClass) {
|
|
49
|
-
const data = classes[nodeClass];
|
|
50
|
-
if (!data) {
|
|
51
|
-
// eslint-disable-next-line no-constant-condition
|
|
52
|
-
while(true){
|
|
53
|
-
const newClass = `a${ID()}`;
|
|
54
|
-
if (!hashClasses[newClass]) {
|
|
55
|
-
hashClasses[newClass] = true; // flag
|
|
56
|
-
// Cache
|
|
57
|
-
classes[nodeClass] = {
|
|
58
|
-
hash: newClass
|
|
59
|
-
};
|
|
60
|
-
// replace
|
|
61
|
-
replaceClass(node, nodeClass, newClass);
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
} else {
|
|
66
|
-
replaceClass(node, nodeClass, data.hash);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
let newHTML = '';
|
|
75
|
-
for(let i = 0; i < jsonHTML.length; i++){
|
|
76
|
-
const node = jsonHTML[i];
|
|
77
|
-
if (node) {
|
|
78
|
-
newHTML += parse5.serializeOuter(node);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
// Replace class in css
|
|
82
|
-
const orderClasses = [];
|
|
83
|
-
for(const oldClass in classes){
|
|
84
|
-
if (Object.prototype.hasOwnProperty.call(classes, oldClass)) {
|
|
85
|
-
const data = classes[oldClass];
|
|
86
|
-
if (data?.hash) {
|
|
87
|
-
orderClasses.push({
|
|
88
|
-
oldClass: oldClass,
|
|
89
|
-
newClass: data.hash
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
orderClasses.sort((a, b)=>{
|
|
95
|
-
if (a.oldClass.includes(b.oldClass)) {
|
|
96
|
-
return -1;
|
|
97
|
-
}
|
|
98
|
-
return 1;
|
|
99
|
-
});
|
|
100
|
-
let newCSS = css;
|
|
101
|
-
if (newCSS) {
|
|
102
|
-
const jsonCSS = cssParser.cssToJson(newCSS);
|
|
103
|
-
const loopCSSSelector = (style)=>{
|
|
104
|
-
// Replace selector
|
|
105
|
-
for(const selector in style.children){
|
|
106
|
-
if (Object.prototype.hasOwnProperty.call(style.children, selector)) {
|
|
107
|
-
let newSelector = selector;
|
|
108
|
-
// Replace selector new selector
|
|
109
|
-
const styleSelector = style.children[selector];
|
|
110
|
-
const selectors = newSelector.split(',');
|
|
111
|
-
for(let index = 0; index < selectors.length; index++){
|
|
112
|
-
const itemSelector = selectors[index];
|
|
113
|
-
if (itemSelector) {
|
|
114
|
-
const selectorClasses = itemSelector.split('.');
|
|
115
|
-
for(let i = 0; i < selectorClasses.length; i++){
|
|
116
|
-
let selectorClass = selectorClasses[i];
|
|
117
|
-
selectorClass = replacePseudoClasses(selectorClass || '');
|
|
118
|
-
if (options?.classList && !options.classList.includes(selectorClass)) continue;
|
|
119
|
-
if (selectorClass) {
|
|
120
|
-
for(let index = 0; index < orderClasses.length; index++){
|
|
121
|
-
const orderClass = orderClasses[index];
|
|
122
|
-
if (orderClass) {
|
|
123
|
-
let flag = false;
|
|
124
|
-
if (orderClass.oldClass == selectorClass) {
|
|
125
|
-
flag = true;
|
|
126
|
-
} else if (cssEscape.cssEscape(orderClass.oldClass) == selectorClass) {
|
|
127
|
-
flag = true;
|
|
128
|
-
}
|
|
129
|
-
if (flag) {
|
|
130
|
-
selectorClasses[i] = replaceSelector(selectorClasses[i] || '', orderClass.oldClass, orderClass.newClass);
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
selectors[index] = selectorClasses.join('.');
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
newSelector = selectors.join(',');
|
|
141
|
-
// replace
|
|
142
|
-
if (newSelector != selector && styleSelector) {
|
|
143
|
-
style.children[newSelector] = styleSelector;
|
|
144
|
-
delete style.children[selector];
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
// Loop
|
|
149
|
-
if (style.children) {
|
|
150
|
-
for(const selectorChild in style.children){
|
|
151
|
-
if (Object.prototype.hasOwnProperty.call(style.children, selectorChild)) {
|
|
152
|
-
const styleChild = style.children[selectorChild];
|
|
153
|
-
if (styleChild) {
|
|
154
|
-
loopCSSSelector(styleChild);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
loopCSSSelector(jsonCSS);
|
|
161
|
-
newCSS = cssParser.jsonToCss(jsonCSS);
|
|
162
|
-
}
|
|
163
|
-
newHTML = replaceBKPlaceholderToLiquid(newHTML, arrayMatch);
|
|
164
|
-
return options?.getOrderClass ? {
|
|
165
|
-
html: newHTML.split(stringBreak),
|
|
166
|
-
css: newCSS,
|
|
167
|
-
orderClass: options?.getOrderClass ? orderClasses : []
|
|
168
|
-
} : {
|
|
169
|
-
html: newHTML.split(stringBreak),
|
|
170
|
-
css: newCSS
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
// Return default
|
|
174
|
-
return options?.getOrderClass ? {
|
|
175
|
-
html,
|
|
176
|
-
css,
|
|
177
|
-
orderClass: []
|
|
178
|
-
} : {
|
|
179
|
-
html,
|
|
180
|
-
css
|
|
181
|
-
};
|
|
182
|
-
};
|
|
183
|
-
const ID = function() {
|
|
184
|
-
// Math.random should be unique because of its seeding algorithm.
|
|
185
|
-
// Convert it to base 36 (numbers + letters), and grab the first 9 characters
|
|
186
|
-
// after the decimal.
|
|
187
|
-
return Math.random().toString(36).substr(2, 5);
|
|
188
|
-
};
|
|
189
|
-
const replaceClass = (node, oldClass, newClass)=>{
|
|
190
|
-
if (node.attrs?.length) {
|
|
191
|
-
const attrClass = node.attrs.find((item)=>item.name == 'class');
|
|
192
|
-
if (attrClass?.value) {
|
|
193
|
-
const values = attrClass.value.split(' ');
|
|
194
|
-
for(let i = 0; i < values.length; i++){
|
|
195
|
-
const value = values[i];
|
|
196
|
-
if (value) {
|
|
197
|
-
const valueWithoutLiquid = replacePseudoClasses(value?.replace(/bk-liquid.*?bk-liquid/g, ''));
|
|
198
|
-
if (valueWithoutLiquid == oldClass) {
|
|
199
|
-
const newValue = value?.replace(oldClass, newClass) || value;
|
|
200
|
-
values[i] = newValue;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
attrClass.value = values.join(' ');
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
const replaceSelector = (selector, oldClass, newClass)=>{
|
|
209
|
-
let newSelector = selector;
|
|
210
|
-
newSelector = newSelector.replaceAll(oldClass, newClass);
|
|
211
|
-
newSelector = newSelector.replaceAll(cssEscape.cssEscape(oldClass), cssEscape.cssEscape(newClass));
|
|
212
|
-
return newSelector;
|
|
213
|
-
};
|
|
214
|
-
const replacePseudoClasses = (selectorClass)=>{
|
|
215
|
-
selectorClass = selectorClass?.trim();
|
|
216
|
-
// eslint-disable-next-line prefer-regex-literals, no-useless-escape, regexp/no-useless-flag
|
|
217
|
-
selectorClass = selectorClass?.replace(new RegExp(`\[.*?\]`, 'gm'), ''); // Replace [data]
|
|
218
|
-
selectorClass = selectorClass?.replace(')', '');
|
|
219
|
-
selectorClass = selectorClass?.replace(/:(a(ctive|ny(-link)*)|checked|d(efault|i(r\(\)|sabled))|e(mpty|nabled)|f(irst(-(child|of-type))*|ullscreen|ocus)|hover|in(determinate|valid|-range)|la(ng\(\)|(st-(child|of-type)))|l(eft|ink)|n(ot\(\)|th-((last-)*(child|of-type)\(\)))|o(nly-(child|of-type)|ptional|ut-of-range)|r(e(ad-(only|write)|quired)|ight|oot)|scope|target|v(alid|isited))/g, '');
|
|
220
|
-
selectorClass = selectorClass?.replace(/(active|hover|focus|disabled|first|last|mobile|tablet)\\:/g, '');
|
|
221
|
-
selectorClass = selectorClass?.replace(/(active|hover|focus|disabled|first|last|mobile|tablet):/g, '');
|
|
222
|
-
return selectorClass;
|
|
223
|
-
};
|
|
224
|
-
const replaceLiquidToBKPlaceholder = (html)=>{
|
|
225
|
-
// eslint-disable-next-line regexp/no-dupe-disjunctions
|
|
226
|
-
const regex = /\{%(.|[\n\r\t])*?%\}|\{\{(.|[\n\r\t])*?\}\}/g;
|
|
227
|
-
const arrayMatch = html.match(regex);
|
|
228
|
-
if (arrayMatch?.length) {
|
|
229
|
-
for(let i = 0; i < arrayMatch.length; i++){
|
|
230
|
-
const match = arrayMatch[i];
|
|
231
|
-
if (match) {
|
|
232
|
-
html = html.replace(match, 'bk-liquid' + i + 'bk-liquid');
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return {
|
|
237
|
-
html,
|
|
238
|
-
arrayMatch
|
|
239
|
-
};
|
|
240
|
-
};
|
|
241
|
-
const replaceBKPlaceholderToLiquid = (html, arrayMatch)=>{
|
|
242
|
-
if (arrayMatch?.length) {
|
|
243
|
-
for(let i = 0; i < arrayMatch.length; i++){
|
|
244
|
-
const match = arrayMatch[i];
|
|
245
|
-
if (match) {
|
|
246
|
-
html = html.replace('bk-liquid' + i + 'bk-liquid=""', match); // html data auto fix
|
|
247
|
-
html = html.replace('bk-liquid' + i + "bk-liquid=''", match); // html data auto fix
|
|
248
|
-
html = html.replace('bk-liquid' + i + 'bk-liquid', match);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return html;
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
exports.hashClassNames = hashClassNames;
|
|
1
|
+
"use strict";var parse5=require("parse5"),cssEscape=require("./css-escape.js"),cssParser=require("./css-parser.js");const hashClassNames=(e,l,s)=>{let t="<!-- End extra file -->",{html:a,arrayMatch:r}=replaceLiquidToBKPlaceholder(e.join(t)),i=parse5.parseFragment(a),c=i.childNodes;if(c?.length){let e={},a={},i=(e,l)=>{if(e?.length)for(let s=0;s<e.length;s++){let t=e[s];if(t){let e=t.attrs?.find(e=>"data-keep-class"==e.name)?.value;!e&&(l(t),t.childNodes?.length&&i(t.childNodes,l))}}};i(c,l=>{if(l?.attrs?.length){let t=l.attrs.find(e=>"class"==e.name);if(t?.value){let r=t.value.split(" ");if(r?.length)for(let t=0;t<r.length;t++){let i=r[t]?.trim()||"";if(i=i.replace(/bk-liquid.*?bk-liquid/g,""),i=replacePseudoClasses(i),!(s?.ignoreClasses?.includes(i)||s?.classList&&!s.classList.includes(i))&&i){let s=e[i];if(s)replaceClass(l,i,s.hash);else for(;;){let s=`a${ID()}`;if(!a[s]){a[s]=!0,e[i]={hash:s},replaceClass(l,i,s);break}}}}}}});let o="";for(let e=0;e<c.length;e++){let l=c[e];l&&(o+=parse5.serializeOuter(l))}let n=[];for(let l in e)if(Object.prototype.hasOwnProperty.call(e,l)){let s=e[l];s?.hash&&n.push({oldClass:l,newClass:s.hash})}n.sort((e,l)=>e.oldClass.includes(l.oldClass)?-1:1);let d=l;if(d){let e=cssParser.cssToJson(d),l=e=>{for(let l in e.children)if(Object.prototype.hasOwnProperty.call(e.children,l)){let t=l,a=e.children[l],r=t.split(",");for(let e=0;e<r.length;e++){let l=r[e];if(l){let t=l.split(".");for(let e=0;e<t.length;e++){let l=t[e];if(l=replacePseudoClasses(l||""),(!s?.classList||s.classList.includes(l))&&l)for(let s=0;s<n.length;s++){let a=n[s];if(a){let s=!1;if(a.oldClass==l?s=!0:cssEscape.cssEscape(a.oldClass)==l&&(s=!0),s){t[e]=replaceSelector(t[e]||"",a.oldClass,a.newClass);break}}}}r[e]=t.join(".")}}(t=r.join(","))!=l&&a&&(e.children[t]=a,delete e.children[l])}if(e.children){for(let s in e.children)if(Object.prototype.hasOwnProperty.call(e.children,s)){let t=e.children[s];t&&l(t)}}};l(e),d=cssParser.jsonToCss(e)}return o=replaceBKPlaceholderToLiquid(o,r),s?.getOrderClass?{html:o.split(t),css:d,orderClass:s?.getOrderClass?n:[]}:{html:o.split(t),css:d}}return s?.getOrderClass?{html:e,css:l,orderClass:[]}:{html:e,css:l}},ID=function(){return Math.random().toString(36).substr(2,5)},replaceClass=(e,l,s)=>{if(e.attrs?.length){let t=e.attrs.find(e=>"class"==e.name);if(t?.value){let e=t.value.split(" ");for(let t=0;t<e.length;t++){let a=e[t];if(a){let r=replacePseudoClasses(a?.replace(/bk-liquid.*?bk-liquid/g,""));if(r==l){let r=a?.replace(l,s)||a;e[t]=r}}}t.value=e.join(" ")}}},replaceSelector=(e,l,s)=>{let t=e;return(t=t.replaceAll(l,s)).replaceAll(cssEscape.cssEscape(l),cssEscape.cssEscape(s))},replacePseudoClasses=e=>(e=e?.trim(),e=e?.replace(RegExp("\\[.*?\\]","gm"),""),e=e?.replace(")",""),e=e?.replace(/:(a(ctive|ny(-link)*)|checked|d(efault|i(r\(\)|sabled))|e(mpty|nabled)|f(irst(-(child|of-type))*|ullscreen|ocus)|hover|in(determinate|valid|-range)|la(ng\(\)|(st-(child|of-type)))|l(eft|ink)|n(ot\(\)|th-((last-)*(child|of-type)\(\)))|o(nly-(child|of-type)|ptional|ut-of-range)|r(e(ad-(only|write)|quired)|ight|oot)|scope|target|v(alid|isited))/g,""),e=e?.replace(/(active|hover|focus|disabled|first|last|mobile|tablet)\\:/g,""),e=e?.replace(/(active|hover|focus|disabled|first|last|mobile|tablet):/g,"")),replaceLiquidToBKPlaceholder=e=>{let l=/\{%(.|[\n\r\t])*?%\}|\{\{(.|[\n\r\t])*?\}\}/g,s=e.match(l);if(s?.length)for(let l=0;l<s.length;l++){let t=s[l];t&&(e=e.replace(t,"bk-liquid"+l+"bk-liquid"))}return{html:e,arrayMatch:s}},replaceBKPlaceholderToLiquid=(e,l)=>{if(l?.length)for(let s=0;s<l.length;s++){let t=l[s];t&&(e=(e=(e=e.replace("bk-liquid"+s+'bk-liquid=""',t)).replace("bk-liquid"+s+"bk-liquid=''",t)).replace("bk-liquid"+s+"bk-liquid",t))}return e};exports.hashClassNames=hashClassNames;
|
package/dist/esm/css-escape.js
CHANGED
|
@@ -1,55 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// repo: https://github.com/mathiasbynens/CSS.escape
|
|
3
|
-
const cssEscape = function(value) {
|
|
4
|
-
if (arguments.length == 0) {
|
|
5
|
-
throw new TypeError('`CSS.escape` requires an argument.');
|
|
6
|
-
}
|
|
7
|
-
const string = String(value);
|
|
8
|
-
const length = string.length;
|
|
9
|
-
let index = -1;
|
|
10
|
-
let codeUnit;
|
|
11
|
-
let result = '';
|
|
12
|
-
const firstCodeUnit = string.charCodeAt(0);
|
|
13
|
-
if (// If the character is the first character and is a `-` (U+002D), and
|
|
14
|
-
// there is no second character, […]
|
|
15
|
-
length == 1 && firstCodeUnit == 0x002d) {
|
|
16
|
-
return '\\' + string;
|
|
17
|
-
}
|
|
18
|
-
while(++index < length){
|
|
19
|
-
codeUnit = string.charCodeAt(index);
|
|
20
|
-
// Note: there’s no need to special-case astral symbols, surrogate
|
|
21
|
-
// pairs, or lone surrogates.
|
|
22
|
-
// If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
|
|
23
|
-
// (U+FFFD).
|
|
24
|
-
if (codeUnit == 0x0000) {
|
|
25
|
-
result += '\uFFFD';
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
if (// If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
|
|
29
|
-
// U+007F, […]
|
|
30
|
-
codeUnit >= 0x0001 && codeUnit <= 0x001f || codeUnit == 0x007f || // If the character is the first character and is in the range [0-9]
|
|
31
|
-
// (U+0030 to U+0039), […]
|
|
32
|
-
index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039 || // If the character is the second character and is in the range [0-9]
|
|
33
|
-
// (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
|
|
34
|
-
index == 1 && codeUnit >= 0x0030 && codeUnit <= 0x0039 && firstCodeUnit == 0x002d) {
|
|
35
|
-
// https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
|
|
36
|
-
result += '\\' + codeUnit.toString(16) + ' ';
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
// If the character is not handled by one of the above rules and is
|
|
40
|
-
// greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
|
|
41
|
-
// is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
|
|
42
|
-
// U+005A), or [a-z] (U+0061 to U+007A), […]
|
|
43
|
-
if (codeUnit >= 0x0080 || codeUnit == 0x002d || codeUnit == 0x005f || codeUnit >= 0x0030 && codeUnit <= 0x0039 || codeUnit >= 0x0041 && codeUnit <= 0x005a || codeUnit >= 0x0061 && codeUnit <= 0x007a) {
|
|
44
|
-
// the character itself
|
|
45
|
-
result += string.charAt(index);
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
// Otherwise, the escaped character.
|
|
49
|
-
// https://drafts.csswg.org/cssom/#escape-a-character
|
|
50
|
-
result += '\\' + string.charAt(index);
|
|
51
|
-
}
|
|
52
|
-
return result;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export { cssEscape };
|
|
1
|
+
let cssEscape=function(e){let t;if(0==arguments.length)throw TypeError("`CSS.escape` requires an argument.");let r=String(e),n=r.length,c=-1,i="",o=r.charCodeAt(0);if(1==n&&45==o)return"\\"+r;for(;++c<n;){if(0==(t=r.charCodeAt(c))){i+="�";continue}if(t>=1&&t<=31||127==t||0==c&&t>=48&&t<=57||1==c&&t>=48&&t<=57&&45==o){i+="\\"+t.toString(16)+" ";continue}if(t>=128||45==t||95==t||t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122){i+=r.charAt(c);continue}i+="\\"+r.charAt(c)}return i};export{cssEscape};
|
package/dist/esm/css-parser.js
CHANGED
|
@@ -1,119 +1 @@
|
|
|
1
|
-
|
|
2
|
-
let style = str;
|
|
3
|
-
const jsonCSS = {
|
|
4
|
-
children: {},
|
|
5
|
-
attributes: ''
|
|
6
|
-
};
|
|
7
|
-
const mediaQuery = {};
|
|
8
|
-
// eslint-disable-next-line regexp/prefer-d, no-useless-escape, regexp/no-dupe-characters-character-class, regexp/no-useless-escape, regexp/strict, regexp/no-obscure-range, regexp/no-useless-flag, regexp/no-super-linear-backtracking
|
|
9
|
-
const regex = /(@media[^{]+\)).*?{([\s\S]+?})\s*}/g;
|
|
10
|
-
let m;
|
|
11
|
-
while((m = regex.exec(str)) !== null){
|
|
12
|
-
// This is necessary to avoid infinite loops with zero-width matches
|
|
13
|
-
if (m.index === regex.lastIndex) {
|
|
14
|
-
regex.lastIndex++;
|
|
15
|
-
}
|
|
16
|
-
// The result can be accessed through the `m`-variable.
|
|
17
|
-
const css = {
|
|
18
|
-
media: '',
|
|
19
|
-
style: ''
|
|
20
|
-
};
|
|
21
|
-
m.forEach((match, groupIndex)=>{
|
|
22
|
-
if (groupIndex == 0) {
|
|
23
|
-
style = style.replaceAll(match, '');
|
|
24
|
-
}
|
|
25
|
-
if (groupIndex == 1) {
|
|
26
|
-
css.media = match;
|
|
27
|
-
}
|
|
28
|
-
if (groupIndex == 2) {
|
|
29
|
-
css.style = match;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
mediaQuery[css.media] = mediaQuery[css.media] || [];
|
|
33
|
-
mediaQuery[css.media]?.push(css.style);
|
|
34
|
-
}
|
|
35
|
-
// Parse css root
|
|
36
|
-
const cssRoot = parseCSS(style);
|
|
37
|
-
for(const key in cssRoot){
|
|
38
|
-
if (Object.prototype.hasOwnProperty.call(cssRoot, key)) {
|
|
39
|
-
const value = cssRoot[key];
|
|
40
|
-
jsonCSS.children[key] = {
|
|
41
|
-
children: {},
|
|
42
|
-
attributes: value
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
// Append css media
|
|
47
|
-
for(const media in mediaQuery){
|
|
48
|
-
if (Object.prototype.hasOwnProperty.call(mediaQuery, media)) {
|
|
49
|
-
const css = mediaQuery[media];
|
|
50
|
-
if (css?.length) {
|
|
51
|
-
const jsonQueryCss = {
|
|
52
|
-
children: {},
|
|
53
|
-
attributes: ''
|
|
54
|
-
};
|
|
55
|
-
const cssQuery = parseCSS(css?.join(''));
|
|
56
|
-
for(const key in cssQuery){
|
|
57
|
-
if (Object.prototype.hasOwnProperty.call(cssQuery, key)) {
|
|
58
|
-
const value = cssQuery[key];
|
|
59
|
-
jsonQueryCss.children[key] = {
|
|
60
|
-
children: {},
|
|
61
|
-
attributes: value
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
jsonCSS.children[media] = jsonQueryCss;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return jsonCSS;
|
|
70
|
-
};
|
|
71
|
-
const jsonToCss = (jsonCSS, selector)=>{
|
|
72
|
-
// Replace selector
|
|
73
|
-
let value = '';
|
|
74
|
-
for(const selector in jsonCSS.children){
|
|
75
|
-
if (Object.prototype.hasOwnProperty.call(jsonCSS.children, selector)) {
|
|
76
|
-
const style = jsonCSS.children[selector];
|
|
77
|
-
if (style) {
|
|
78
|
-
value += jsonToCss(style, selector);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (!value) {
|
|
83
|
-
value = jsonCSS?.attributes || '';
|
|
84
|
-
}
|
|
85
|
-
if (selector) {
|
|
86
|
-
return `${selector}{${value}}`;
|
|
87
|
-
} else {
|
|
88
|
-
return value;
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
const parseCSS = (css)=>{
|
|
92
|
-
const cssJSON = {};
|
|
93
|
-
// eslint-disable-next-line regexp/prefer-d, no-useless-escape, regexp/no-dupe-characters-character-class, regexp/no-useless-escape, regexp/strict, regexp/no-obscure-range, regexp/no-useless-flag, regexp/no-super-linear-backtracking, regexp/prefer-character-class, regexp/no-dupe-disjunctions
|
|
94
|
-
const cssRegex = /((.|\n|\r|\t)*?){((.|\n|\r|\t)*?)}/gm;
|
|
95
|
-
let mCSS;
|
|
96
|
-
while((mCSS = cssRegex.exec(css)) !== null){
|
|
97
|
-
// This is necessary to avoid infinite loops with zero-width matches
|
|
98
|
-
if (mCSS.index === cssRegex.lastIndex) {
|
|
99
|
-
cssRegex.lastIndex++;
|
|
100
|
-
}
|
|
101
|
-
// The result can be accessed through the `m`-variable.
|
|
102
|
-
const selector = {
|
|
103
|
-
key: '',
|
|
104
|
-
value: ''
|
|
105
|
-
};
|
|
106
|
-
mCSS.forEach((match, groupIndex)=>{
|
|
107
|
-
if (groupIndex == 1) {
|
|
108
|
-
selector.key = match.trim();
|
|
109
|
-
}
|
|
110
|
-
if (groupIndex == 3) {
|
|
111
|
-
selector.value = match;
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
cssJSON[selector.key] = selector.value;
|
|
115
|
-
}
|
|
116
|
-
return cssJSON;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
export { cssToJson, jsonToCss };
|
|
1
|
+
let cssToJson=e=>{let t,l=e,r={children:{},attributes:""},n={},s=/(@media[^{]+\)).*?{([\s\S]+?})\s*}/g;for(;null!==(t=s.exec(e));){t.index===s.lastIndex&&s.lastIndex++;let e={media:"",style:""};t.forEach((t,r)=>{0==r&&(l=l.replaceAll(t,"")),1==r&&(e.media=t),2==r&&(e.style=t)}),n[e.media]=n[e.media]||[],n[e.media]?.push(e.style)}let i=parseCSS(l);for(let e in i)if(Object.prototype.hasOwnProperty.call(i,e)){let t=i[e];r.children[e]={children:{},attributes:t}}for(let e in n)if(Object.prototype.hasOwnProperty.call(n,e)){let t=n[e];if(t?.length){let l={children:{},attributes:""},n=parseCSS(t?.join(""));for(let e in n)if(Object.prototype.hasOwnProperty.call(n,e)){let t=n[e];l.children[e]={children:{},attributes:t}}r.children[e]=l}}return r},jsonToCss=(e,t)=>{let l="";for(let t in e.children)if(Object.prototype.hasOwnProperty.call(e.children,t)){let r=e.children[t];r&&(l+=jsonToCss(r,t))}return(l||(l=e?.attributes||""),t)?`${t}{${l}}`:l},parseCSS=e=>{let t;let l={},r=/((.|\n|\r|\t)*?){((.|\n|\r|\t)*?)}/gm;for(;null!==(t=r.exec(e));){t.index===r.lastIndex&&r.lastIndex++;let e={key:"",value:""};t.forEach((t,l)=>{1==l&&(e.key=t.trim()),3==l&&(e.value=t)}),l[e.key]=e.value}return l};export{cssToJson,jsonToCss};
|
package/dist/esm/index.js
CHANGED
|
@@ -1,253 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { cssEscape } from './css-escape.js';
|
|
3
|
-
import { cssToJson, jsonToCss } from './css-parser.js';
|
|
4
|
-
|
|
5
|
-
const hashClassNames = (html, css, options)=>{
|
|
6
|
-
const stringBreak = '<!-- End extra file -->';
|
|
7
|
-
const { html: htmlWithoutLiquid, arrayMatch } = replaceLiquidToBKPlaceholder(html.join(stringBreak));
|
|
8
|
-
const documentFragment = parseFragment(htmlWithoutLiquid);
|
|
9
|
-
const jsonHTML = documentFragment.childNodes;
|
|
10
|
-
if (jsonHTML?.length) {
|
|
11
|
-
const classes = {};
|
|
12
|
-
const hashClasses = {};
|
|
13
|
-
// Find & Obfuscate class in html
|
|
14
|
-
const loopNode = (childNodes, callback)=>{
|
|
15
|
-
if (childNodes?.length) {
|
|
16
|
-
for(let i = 0; i < childNodes.length; i++){
|
|
17
|
-
const childNode = childNodes[i];
|
|
18
|
-
if (childNode) {
|
|
19
|
-
const keepClass = childNode.attrs?.find((item)=>item.name == 'data-keep-class')?.value;
|
|
20
|
-
if (!keepClass) {
|
|
21
|
-
callback(childNode);
|
|
22
|
-
if (childNode.childNodes?.length) {
|
|
23
|
-
loopNode(childNode.childNodes, callback);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
loopNode(jsonHTML, (node)=>{
|
|
31
|
-
if (node?.attrs?.length) {
|
|
32
|
-
const attrClass = node.attrs.find((item)=>item.name == 'class');
|
|
33
|
-
if (attrClass?.value) {
|
|
34
|
-
const nodeClasses = attrClass.value.split(' ');
|
|
35
|
-
if (nodeClasses?.length) {
|
|
36
|
-
for(let i = 0; i < nodeClasses.length; i++){
|
|
37
|
-
let nodeClass = nodeClasses[i]?.trim() || '';
|
|
38
|
-
nodeClass = nodeClass.replace(/bk-liquid.*?bk-liquid/g, '');
|
|
39
|
-
nodeClass = replacePseudoClasses(nodeClass);
|
|
40
|
-
if (options?.ignoreClasses?.includes(nodeClass)) {
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
if (options?.classList && !options.classList.includes(nodeClass)) {
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
if (nodeClass) {
|
|
47
|
-
const data = classes[nodeClass];
|
|
48
|
-
if (!data) {
|
|
49
|
-
// eslint-disable-next-line no-constant-condition
|
|
50
|
-
while(true){
|
|
51
|
-
const newClass = `a${ID()}`;
|
|
52
|
-
if (!hashClasses[newClass]) {
|
|
53
|
-
hashClasses[newClass] = true; // flag
|
|
54
|
-
// Cache
|
|
55
|
-
classes[nodeClass] = {
|
|
56
|
-
hash: newClass
|
|
57
|
-
};
|
|
58
|
-
// replace
|
|
59
|
-
replaceClass(node, nodeClass, newClass);
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
replaceClass(node, nodeClass, data.hash);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
let newHTML = '';
|
|
73
|
-
for(let i = 0; i < jsonHTML.length; i++){
|
|
74
|
-
const node = jsonHTML[i];
|
|
75
|
-
if (node) {
|
|
76
|
-
newHTML += serializeOuter(node);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
// Replace class in css
|
|
80
|
-
const orderClasses = [];
|
|
81
|
-
for(const oldClass in classes){
|
|
82
|
-
if (Object.prototype.hasOwnProperty.call(classes, oldClass)) {
|
|
83
|
-
const data = classes[oldClass];
|
|
84
|
-
if (data?.hash) {
|
|
85
|
-
orderClasses.push({
|
|
86
|
-
oldClass: oldClass,
|
|
87
|
-
newClass: data.hash
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
orderClasses.sort((a, b)=>{
|
|
93
|
-
if (a.oldClass.includes(b.oldClass)) {
|
|
94
|
-
return -1;
|
|
95
|
-
}
|
|
96
|
-
return 1;
|
|
97
|
-
});
|
|
98
|
-
let newCSS = css;
|
|
99
|
-
if (newCSS) {
|
|
100
|
-
const jsonCSS = cssToJson(newCSS);
|
|
101
|
-
const loopCSSSelector = (style)=>{
|
|
102
|
-
// Replace selector
|
|
103
|
-
for(const selector in style.children){
|
|
104
|
-
if (Object.prototype.hasOwnProperty.call(style.children, selector)) {
|
|
105
|
-
let newSelector = selector;
|
|
106
|
-
// Replace selector new selector
|
|
107
|
-
const styleSelector = style.children[selector];
|
|
108
|
-
const selectors = newSelector.split(',');
|
|
109
|
-
for(let index = 0; index < selectors.length; index++){
|
|
110
|
-
const itemSelector = selectors[index];
|
|
111
|
-
if (itemSelector) {
|
|
112
|
-
const selectorClasses = itemSelector.split('.');
|
|
113
|
-
for(let i = 0; i < selectorClasses.length; i++){
|
|
114
|
-
let selectorClass = selectorClasses[i];
|
|
115
|
-
selectorClass = replacePseudoClasses(selectorClass || '');
|
|
116
|
-
if (options?.classList && !options.classList.includes(selectorClass)) continue;
|
|
117
|
-
if (selectorClass) {
|
|
118
|
-
for(let index = 0; index < orderClasses.length; index++){
|
|
119
|
-
const orderClass = orderClasses[index];
|
|
120
|
-
if (orderClass) {
|
|
121
|
-
let flag = false;
|
|
122
|
-
if (orderClass.oldClass == selectorClass) {
|
|
123
|
-
flag = true;
|
|
124
|
-
} else if (cssEscape(orderClass.oldClass) == selectorClass) {
|
|
125
|
-
flag = true;
|
|
126
|
-
}
|
|
127
|
-
if (flag) {
|
|
128
|
-
selectorClasses[i] = replaceSelector(selectorClasses[i] || '', orderClass.oldClass, orderClass.newClass);
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
selectors[index] = selectorClasses.join('.');
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
newSelector = selectors.join(',');
|
|
139
|
-
// replace
|
|
140
|
-
if (newSelector != selector && styleSelector) {
|
|
141
|
-
style.children[newSelector] = styleSelector;
|
|
142
|
-
delete style.children[selector];
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
// Loop
|
|
147
|
-
if (style.children) {
|
|
148
|
-
for(const selectorChild in style.children){
|
|
149
|
-
if (Object.prototype.hasOwnProperty.call(style.children, selectorChild)) {
|
|
150
|
-
const styleChild = style.children[selectorChild];
|
|
151
|
-
if (styleChild) {
|
|
152
|
-
loopCSSSelector(styleChild);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
loopCSSSelector(jsonCSS);
|
|
159
|
-
newCSS = jsonToCss(jsonCSS);
|
|
160
|
-
}
|
|
161
|
-
newHTML = replaceBKPlaceholderToLiquid(newHTML, arrayMatch);
|
|
162
|
-
return options?.getOrderClass ? {
|
|
163
|
-
html: newHTML.split(stringBreak),
|
|
164
|
-
css: newCSS,
|
|
165
|
-
orderClass: options?.getOrderClass ? orderClasses : []
|
|
166
|
-
} : {
|
|
167
|
-
html: newHTML.split(stringBreak),
|
|
168
|
-
css: newCSS
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
// Return default
|
|
172
|
-
return options?.getOrderClass ? {
|
|
173
|
-
html,
|
|
174
|
-
css,
|
|
175
|
-
orderClass: []
|
|
176
|
-
} : {
|
|
177
|
-
html,
|
|
178
|
-
css
|
|
179
|
-
};
|
|
180
|
-
};
|
|
181
|
-
const ID = function() {
|
|
182
|
-
// Math.random should be unique because of its seeding algorithm.
|
|
183
|
-
// Convert it to base 36 (numbers + letters), and grab the first 9 characters
|
|
184
|
-
// after the decimal.
|
|
185
|
-
return Math.random().toString(36).substr(2, 5);
|
|
186
|
-
};
|
|
187
|
-
const replaceClass = (node, oldClass, newClass)=>{
|
|
188
|
-
if (node.attrs?.length) {
|
|
189
|
-
const attrClass = node.attrs.find((item)=>item.name == 'class');
|
|
190
|
-
if (attrClass?.value) {
|
|
191
|
-
const values = attrClass.value.split(' ');
|
|
192
|
-
for(let i = 0; i < values.length; i++){
|
|
193
|
-
const value = values[i];
|
|
194
|
-
if (value) {
|
|
195
|
-
const valueWithoutLiquid = replacePseudoClasses(value?.replace(/bk-liquid.*?bk-liquid/g, ''));
|
|
196
|
-
if (valueWithoutLiquid == oldClass) {
|
|
197
|
-
const newValue = value?.replace(oldClass, newClass) || value;
|
|
198
|
-
values[i] = newValue;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
attrClass.value = values.join(' ');
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
const replaceSelector = (selector, oldClass, newClass)=>{
|
|
207
|
-
let newSelector = selector;
|
|
208
|
-
newSelector = newSelector.replaceAll(oldClass, newClass);
|
|
209
|
-
newSelector = newSelector.replaceAll(cssEscape(oldClass), cssEscape(newClass));
|
|
210
|
-
return newSelector;
|
|
211
|
-
};
|
|
212
|
-
const replacePseudoClasses = (selectorClass)=>{
|
|
213
|
-
selectorClass = selectorClass?.trim();
|
|
214
|
-
// eslint-disable-next-line prefer-regex-literals, no-useless-escape, regexp/no-useless-flag
|
|
215
|
-
selectorClass = selectorClass?.replace(new RegExp(`\[.*?\]`, 'gm'), ''); // Replace [data]
|
|
216
|
-
selectorClass = selectorClass?.replace(')', '');
|
|
217
|
-
selectorClass = selectorClass?.replace(/:(a(ctive|ny(-link)*)|checked|d(efault|i(r\(\)|sabled))|e(mpty|nabled)|f(irst(-(child|of-type))*|ullscreen|ocus)|hover|in(determinate|valid|-range)|la(ng\(\)|(st-(child|of-type)))|l(eft|ink)|n(ot\(\)|th-((last-)*(child|of-type)\(\)))|o(nly-(child|of-type)|ptional|ut-of-range)|r(e(ad-(only|write)|quired)|ight|oot)|scope|target|v(alid|isited))/g, '');
|
|
218
|
-
selectorClass = selectorClass?.replace(/(active|hover|focus|disabled|first|last|mobile|tablet)\\:/g, '');
|
|
219
|
-
selectorClass = selectorClass?.replace(/(active|hover|focus|disabled|first|last|mobile|tablet):/g, '');
|
|
220
|
-
return selectorClass;
|
|
221
|
-
};
|
|
222
|
-
const replaceLiquidToBKPlaceholder = (html)=>{
|
|
223
|
-
// eslint-disable-next-line regexp/no-dupe-disjunctions
|
|
224
|
-
const regex = /\{%(.|[\n\r\t])*?%\}|\{\{(.|[\n\r\t])*?\}\}/g;
|
|
225
|
-
const arrayMatch = html.match(regex);
|
|
226
|
-
if (arrayMatch?.length) {
|
|
227
|
-
for(let i = 0; i < arrayMatch.length; i++){
|
|
228
|
-
const match = arrayMatch[i];
|
|
229
|
-
if (match) {
|
|
230
|
-
html = html.replace(match, 'bk-liquid' + i + 'bk-liquid');
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
return {
|
|
235
|
-
html,
|
|
236
|
-
arrayMatch
|
|
237
|
-
};
|
|
238
|
-
};
|
|
239
|
-
const replaceBKPlaceholderToLiquid = (html, arrayMatch)=>{
|
|
240
|
-
if (arrayMatch?.length) {
|
|
241
|
-
for(let i = 0; i < arrayMatch.length; i++){
|
|
242
|
-
const match = arrayMatch[i];
|
|
243
|
-
if (match) {
|
|
244
|
-
html = html.replace('bk-liquid' + i + 'bk-liquid=""', match); // html data auto fix
|
|
245
|
-
html = html.replace('bk-liquid' + i + "bk-liquid=''", match); // html data auto fix
|
|
246
|
-
html = html.replace('bk-liquid' + i + 'bk-liquid', match);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return html;
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
export { hashClassNames };
|
|
1
|
+
import{parseFragment as e,serializeOuter as l}from"parse5";import{cssEscape as t}from"./css-escape.js";import{cssToJson as s,jsonToCss as i}from"./css-parser.js";let hashClassNames=(r,a,c)=>{let o="<!-- End extra file -->",{html:d,arrayMatch:n}=replaceLiquidToBKPlaceholder(r.join(o)),h=e(d),p=h.childNodes;if(p?.length){let e={},r={},d=(e,l)=>{if(e?.length)for(let t=0;t<e.length;t++){let s=e[t];if(s){let e=s.attrs?.find(e=>"data-keep-class"==e.name)?.value;!e&&(l(s),s.childNodes?.length&&d(s.childNodes,l))}}};d(p,l=>{if(l?.attrs?.length){let t=l.attrs.find(e=>"class"==e.name);if(t?.value){let s=t.value.split(" ");if(s?.length)for(let t=0;t<s.length;t++){let i=s[t]?.trim()||"";if(i=i.replace(/bk-liquid.*?bk-liquid/g,""),i=replacePseudoClasses(i),!(c?.ignoreClasses?.includes(i)||c?.classList&&!c.classList.includes(i))&&i){let t=e[i];if(t)replaceClass(l,i,t.hash);else for(;;){let t=`a${ID()}`;if(!r[t]){r[t]=!0,e[i]={hash:t},replaceClass(l,i,t);break}}}}}}});let h="";for(let e=0;e<p.length;e++){let t=p[e];t&&(h+=l(t))}let f=[];for(let l in e)if(Object.prototype.hasOwnProperty.call(e,l)){let t=e[l];t?.hash&&f.push({oldClass:l,newClass:t.hash})}f.sort((e,l)=>e.oldClass.includes(l.oldClass)?-1:1);let u=a;if(u){let e=s(u),l=e=>{for(let l in e.children)if(Object.prototype.hasOwnProperty.call(e.children,l)){let s=l,i=e.children[l],r=s.split(",");for(let e=0;e<r.length;e++){let l=r[e];if(l){let s=l.split(".");for(let e=0;e<s.length;e++){let l=s[e];if(l=replacePseudoClasses(l||""),(!c?.classList||c.classList.includes(l))&&l)for(let i=0;i<f.length;i++){let r=f[i];if(r){let i=!1;if(r.oldClass==l?i=!0:t(r.oldClass)==l&&(i=!0),i){s[e]=replaceSelector(s[e]||"",r.oldClass,r.newClass);break}}}}r[e]=s.join(".")}}(s=r.join(","))!=l&&i&&(e.children[s]=i,delete e.children[l])}if(e.children){for(let t in e.children)if(Object.prototype.hasOwnProperty.call(e.children,t)){let s=e.children[t];s&&l(s)}}};l(e),u=i(e)}return h=replaceBKPlaceholderToLiquid(h,n),c?.getOrderClass?{html:h.split(o),css:u,orderClass:c?.getOrderClass?f:[]}:{html:h.split(o),css:u}}return c?.getOrderClass?{html:r,css:a,orderClass:[]}:{html:r,css:a}},ID=function(){return Math.random().toString(36).substr(2,5)},replaceClass=(e,l,t)=>{if(e.attrs?.length){let s=e.attrs.find(e=>"class"==e.name);if(s?.value){let e=s.value.split(" ");for(let s=0;s<e.length;s++){let i=e[s];if(i){let r=replacePseudoClasses(i?.replace(/bk-liquid.*?bk-liquid/g,""));if(r==l){let r=i?.replace(l,t)||i;e[s]=r}}}s.value=e.join(" ")}}},replaceSelector=(e,l,s)=>{let i=e;return(i=i.replaceAll(l,s)).replaceAll(t(l),t(s))},replacePseudoClasses=e=>(e=e?.trim(),e=e?.replace(RegExp("\\[.*?\\]","gm"),""),e=e?.replace(")",""),e=e?.replace(/:(a(ctive|ny(-link)*)|checked|d(efault|i(r\(\)|sabled))|e(mpty|nabled)|f(irst(-(child|of-type))*|ullscreen|ocus)|hover|in(determinate|valid|-range)|la(ng\(\)|(st-(child|of-type)))|l(eft|ink)|n(ot\(\)|th-((last-)*(child|of-type)\(\)))|o(nly-(child|of-type)|ptional|ut-of-range)|r(e(ad-(only|write)|quired)|ight|oot)|scope|target|v(alid|isited))/g,""),e=e?.replace(/(active|hover|focus|disabled|first|last|mobile|tablet)\\:/g,""),e=e?.replace(/(active|hover|focus|disabled|first|last|mobile|tablet):/g,"")),replaceLiquidToBKPlaceholder=e=>{let l=/\{%(.|[\n\r\t])*?%\}|\{\{(.|[\n\r\t])*?\}\}/g,t=e.match(l);if(t?.length)for(let l=0;l<t.length;l++){let s=t[l];s&&(e=e.replace(s,"bk-liquid"+l+"bk-liquid"))}return{html:e,arrayMatch:t}},replaceBKPlaceholderToLiquid=(e,l)=>{if(l?.length)for(let t=0;t<l.length;t++){let s=l[t];s&&(e=(e=(e=e.replace("bk-liquid"+t+'bk-liquid=""',s)).replace("bk-liquid"+t+"bk-liquid=''",s)).replace("bk-liquid"+t+"bk-liquid",s))}return e};export{hashClassNames};
|