-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (134 loc) · 4.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
var pse = require('postcss-scopeify-everything');
module.exports = scopeifyHtml;
module.exports.extractCss = extractCss;
module.exports.insertCss = insertCss;
module.exports.getCss = pse.getCss;
function scopeifyHtml(opts) {
opts = opts || {};
var scopeify = pse.api(opts);
return {
sync: scopeifyFnSync.bind(this, scopeify, opts),
promise: scopeifyFnPromise.bind(this, scopeify, opts),
};
}
function scopeifyFnSync(scopeify, opts, doc) {
var css = extractCss(doc);
if (!css && opts.replaceClassName === false) {
return null;
}
var scoped = scopeify(css).sync();
iterateDom(doc, opts, scoped);
return scoped;
}
function scopeifyFnPromise(scopeify, opts, doc) {
var css = extractCss(doc);
if (!css && opts.replaceClassName === false) {
return Promise.resolve(null);
}
return scopeify(css)
.promise()
.then(function iterateDomPromise(scoped) {
return new Promise(function iterateDomPromiseResolve(resolve, reject) {
try {
iterateDom(doc, opts, scoped);
} catch (err) {
reject(err);
}
resolve(scoped);
});
})
.catch(function iterateDomCatch(err) { console.error(err); });
}
function extractCss(doc) {
var stylesEl = doc.querySelectorAll('style');
var styles = '';
for (var i = 0; i < stylesEl.length; i++) {
var child = stylesEl[i];
styles += child.innerHTML;
child.remove();
}
return styles;
}
function insertCss(css, doc, container) {
if (typeof container === 'undefined') {
container = doc.querySelector('head');
}
var style = doc.createElement('style');
style.setAttribute('type', 'text/css');
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(doc.createTextNode(css));
}
container.appendChild(style);
return style;
}
function iterateDom(doc, opts, scoped) {
var elements = doc.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
var el = elements[i];
replaceSelectors(el, scoped, opts.replaceClassName);
}
}
function replaceSelectors(el, scoped, replaceClassName) {
if (typeof replaceClassName === 'undefined') replaceClassName = false;
var name = el.tagName.toLowerCase();
var style = el.getAttribute('style');
var newClasses = [];
Object.keys(scoped.classes).forEach(function walkClass(scopeClass) {
var classes = scopeClass.split(' ');
// detect if a scopedClass is multiple classes and
// if so then add the collective class to the element
if (classes.length > 1) {
var classesNotFound = classes.filter(function filterClasses(c) {
return !el.classList.contains(c);
});
if (classesNotFound.length > 0) {
return;
}
if (replaceClassName) {
newClasses.push(scoped.classes[scopeClass]);
} else {
classes.forEach(c => el.classList.remove(c));
el.classList.add(scoped.classes[scopeClass]);
}
return;
}
if (el.classList.contains(scopeClass)) {
if (replaceClassName) {
newClasses.push(scoped.classes[scopeClass]);
} else {
el.classList.remove(scopeClass);
el.classList.add(scoped.classes[scopeClass]);
}
}
});
if (replaceClassName && el.className) {
el.className = newClasses.join(' ');
}
Object.keys(scoped.elements).forEach(function walkEl(scopeEl) {
if (scopeEl === name || scopeEl === '*') {
el.classList.add(scoped.elements[scopeEl]);
}
});
Object.keys(scoped.ids).forEach(function walkId(scopeId) {
if (scopeId === el.getAttribute('id')) {
el.setAttribute('id', scoped.ids[scopeId]);
}
});
Object.keys(scoped.fontFaces).forEach(function walkFaces(scopedFace) {
var re = new RegExp(scopedFace, 'gi');
if (style && re.test(style)) {
var scopedAttr = style.replace(re, scoped.fontFaces[scopedFace]);
el.setAttribute('style', scopedAttr);
}
});
Object.keys(scoped.keyframes).forEach(function walkFrames(scopedFrames) {
var re = new RegExp(scopedFrames, 'gi');
if (style && re.test(style)) {
var scopedAttr = style.replace(re, scoped.keyframes[scopedFrames]);
el.setAttribute('style', scopedAttr);
}
});
}