@social-mail/social-mail-client 1.9.96 → 1.9.97
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/css-for-email.js +78 -0
- package/package.json +1 -1
package/css-for-email.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const { currentScript } = document;
|
|
2
|
+
|
|
3
|
+
async function flattenStyleSheets() {
|
|
4
|
+
const styleSheets = document.querySelectorAll(`link[rel="stylesheet"]`);
|
|
5
|
+
const tasks = [];
|
|
6
|
+
for(let i=0;i<styleSheets.length;i++) {
|
|
7
|
+
const styleSheet = styleSheets[i];
|
|
8
|
+
const href = styleSheet.getAttribute("href");
|
|
9
|
+
tasks.push(fetch(href)
|
|
10
|
+
.then((x) => x.text())
|
|
11
|
+
.then((text) => ({ styleSheet, text }))
|
|
12
|
+
)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const results = await Promise.all(tasks);
|
|
16
|
+
for(const { styleSheet, text} of results) {
|
|
17
|
+
const style = document.createElement("style");
|
|
18
|
+
style.textContent = text;
|
|
19
|
+
document.head.appendChild(style);
|
|
20
|
+
styleSheet.remove();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const te = new TextEncoder();
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
function encodeTo36(text) {
|
|
28
|
+
const buffer = te.encode(text);
|
|
29
|
+
let n = 0n;
|
|
30
|
+
for(const byte of buffer) {
|
|
31
|
+
n = (n << 8n) | BigInt(byte);
|
|
32
|
+
}
|
|
33
|
+
return n.toString(36);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function flattenStyler() {
|
|
37
|
+
|
|
38
|
+
const replaceList = [];
|
|
39
|
+
|
|
40
|
+
const all = document.body.querySelectorAll("not(style)");
|
|
41
|
+
for(let i=0;i<all.length;i++) {
|
|
42
|
+
const e = all[i];
|
|
43
|
+
|
|
44
|
+
for(const name of e.getAttributeNames()) {
|
|
45
|
+
if (!name.startsWith("styler-")) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const value = e.getAttribute(name);
|
|
49
|
+
const cssValue = CSS.escape(value);
|
|
50
|
+
const rule = `[${name}=${cssValue}]`;
|
|
51
|
+
const className = encodeTo36(rule);
|
|
52
|
+
e.classList.add(className);
|
|
53
|
+
replaceList.push({ rule, className: "." + className});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const styles = document.querySelectorAll("style");
|
|
58
|
+
for(let i=0;i<styles.length;i++) {
|
|
59
|
+
const style = styles[i];
|
|
60
|
+
const { textContent } = style;
|
|
61
|
+
for (const { rule, className } of replaceList) {
|
|
62
|
+
const replaced = textContent.replaceAll(rule, className);
|
|
63
|
+
if (replaced !== textContent) {
|
|
64
|
+
style.textContent = replaced;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
(async function() {
|
|
71
|
+
|
|
72
|
+
await flattenStyleSheets();
|
|
73
|
+
|
|
74
|
+
flattenStyler();
|
|
75
|
+
|
|
76
|
+
currentScript.remove();
|
|
77
|
+
|
|
78
|
+
})().catch(console.error);
|