-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcssWork.js
More file actions
60 lines (57 loc) · 1.38 KB
/
cssWork.js
File metadata and controls
60 lines (57 loc) · 1.38 KB
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
// it's just work
async function fetchCSS(url) {
const response = await fetch(url)
return response.text()
}
const inlineClasses = new Set()
const useClasses = new Set()
class cssCleaner {
/**
* @param {boolean} isStyle
*/
constructor(isStyle) {
this.isStyle = isStyle
}
async element(element) {
if (this.isStyle) {
let new_style = element.innerHTML
const uses_cls = [...useClasses]
const inl_cls = [...inlineClasses]
for (const cls of inl_cls) {
if (!uses_cls.includes(cls)) {
const regex = new RegExp(`(${cls}\\s*\\{[^}]+\\})`)
new_style = new_style.replace(regex, '')
}
}
} else {
if (element.getAttribute('class')) {
const onElClasses = element.getAttribute('class').split(' ')
onElClasses.forEach(cls => useClasses.add(cls))
}
}
}
}
class RemoveElementHandler {
element(element) {
element.remove()
}
}
class cssInline {
constructor(attributeName) {
this.attributeName = attributeName
}
async element(element) {
const attribute = element.getAttribute(this.attributeName)
if (attribute) {
const styles = await fetchCSS(attribute)
const stylesFontFix = styles.replaceAll(
'https://uploads-ssl.webflow.com/',
'https://assets-global.website-files.com/'
)
element.replace(`<style replaced>${stylesFontFix}</style>`, {
html: true,
})
}
}
}
export { fetchCSS, cssCleaner, cssInline, RemoveElementHandler }