-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathinline.js
More file actions
251 lines (217 loc) · 7.74 KB
/
inline.js
File metadata and controls
251 lines (217 loc) · 7.74 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import juice from 'juice'
import postcss from 'postcss'
import get from 'lodash-es/get.js'
import has from 'lodash-es/has.js'
import * as cheerio from 'cheerio/slim'
import remove from 'lodash-es/remove.js'
import { render } from 'posthtml-render'
import isEmpty from 'lodash-es/isEmpty.js'
import safeParser from 'postcss-safe-parser'
import isObject from 'lodash-es/isObject.js'
import { parser as parse } from 'posthtml-parser'
import { useAttributeSizes } from './useAttributeSizes.js'
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
const posthtmlPlugin = (options = {}) => tree => {
return inline(render(tree), options).then(html => parse(html, getPosthtmlOptions()))
}
export default posthtmlPlugin
export async function inline(html = '', options = {}) {
// Exit early if no HTML is passed
if (typeof html !== 'string' || html === '') {
return html
}
const removeStyleTags = get(options, 'removeStyleTags', false)
const css = get(options, 'customCSS', false)
options.removeInlinedSelectors = get(options, 'removeInlinedSelectors', true)
options.preferUnitlessValues = get(options, 'preferUnitlessValues', true)
options.safelist = new Set([
...get(options, 'safelist', []),
...[
'.body', // Gmail
'.gmail', // Gmail
'.apple', // Apple Mail
'.ios', // Mail on iOS
'.ox-', // Open-Xchange
'.outlook', // Outlook.com
'[data-ogs', // Outlook.com
'.bloop_container', // Airmail
'.Singleton', // Apple Mail 10
'.unused', // Notes 8
'.moz-text-html', // Thunderbird
'.mail-detail-content', // Comcast, Libero webmail
'edo', // Edison (all)
'#msgBody', // Freenet uses #msgBody
'.lang' // Fenced code blocks
],
])
juice.styleToAttribute = get(options, 'styleToAttribute', {})
juice.applyWidthAttributes = get(options, 'applyWidthAttributes', true)
juice.applyHeightAttributes = get(options, 'applyHeightAttributes', true)
juice.excludedProperties.push(...get(options, 'excludedProperties', []))
juice.widthElements = get(options, 'widthElements', ['img', 'video']).map(i => i.toUpperCase())
juice.heightElements = get(options, 'heightElements', ['img', 'video']).map(i => i.toUpperCase())
if (isObject(options.codeBlocks) && !isEmpty(options.codeBlocks)) {
Object.entries(options.codeBlocks).forEach(([k, v]) => {
juice.codeBlocks[k] = v
})
}
const $ = cheerio.load(html, {
xml: {
decodeEntities: false,
xmlMode: false,
}
})
// Add a `data-embed` attribute to style tags that have the embed attribute
$('style[embed]:not([data-embed])').each((i, el) => {
$(el).attr('data-embed', '')
})
$('style[data-embed]:not([embed])').each((i, el) => {
$(el).attr('embed', '')
})
/**
* Inline the CSS
*
* If customCSS is passed, inline that CSS specifically
* Otherwise, use Juice's default inlining
*/
$.root().html(
css
? juice.inlineContent($.html(), css, { removeStyleTags, ...options })
: juice($.html(), { removeStyleTags, ...options })
)
/**
* Prefer attribute sizes
*
* Prefer HTML `width` and `height` attributes over inline CSS.
* Useful for retina images in MSO Outlook, where CSS sizes
* aren't respected and will render the image in its
* natural size.
*/
if (options.useAttributeSizes) {
$.root().html(
await useAttributeSizes(html, {
width: juice.widthElements,
height: juice.heightElements,
})
)
}
/**
* Remove inlined selectors from the HTML
*/
// For each style tag
$('style:not([embed])').each((i, el) => {
// Parse the CSS
const { root } = postcss()
.process(
$(el).html(),
{
from: undefined,
parser: safeParser
}
)
// Precompile a single regex to match any substring from the preservedClasses set
const combinedPattern = Array.from(options.safelist)
.map(pattern => pattern.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')) // Escape special regex chars
.join('|') // Combine all patterns into a single regex pattern with 'OR' (|)
const combinedRegex = new RegExp(combinedPattern)
const selectors = new Set()
// Preserve selectors in at rules
root.walkAtRules(rule => {
if (['media', 'supports'].includes(rule.name)) {
rule.walkRules(rule => {
options.safelist.add(rule.selector)
})
}
})
// For each rule in the CSS block we're parsing
root.walkRules(rule => {
// Create a set of selectors
const { selector } = rule
// Add the selector to the set as long as it's not a pseudo selector
if (!/.+[^\\\s]::?\w+/.test(selector)) {
selectors.add({
name: selector,
prop: get(rule.nodes[0], 'prop')
})
}
// Preserve pseudo selectors
else {
options.safelist.add(selector)
}
if (options.removeInlinedSelectors) {
// Remove the rule in the <style> tag as long as it's not a preserved class
if (!options.safelist.has(selector) && !combinedRegex.test(selector)) {
rule.remove()
}
// Update the <style> tag contents
$(el).html(root.toString())
}
})
/**
* CSS optimizations
*
* 1. `preferUnitlessValues` - Replace unit values with `0` where possible
* 2. `removeInlinedSelectors` - Remove inlined selectors from the HTML
*/
// Loop over selectors that we found in the <style> tags
selectors.forEach(({ name, prop }) => {
const elements = $(name).get()
// If the property is excluded from inlining, skip
if (!juice.excludedProperties.includes(prop)) {
// Find the selector in the HTML
elements.forEach((el) => {
// Get a `property|value` list from the inline style attribute
const styleAttr = $(el).attr('style')
const inlineStyles = {}
// 1. `preferUnitlessValues`
if (styleAttr) {
try {
const root = postcss.parse(`* { ${styleAttr} }`)
root.first.each((decl) => {
const property = decl.prop
let value = decl.value
if (value && options.preferUnitlessValues) {
value = value.replace(
/\b0(px|rem|em|%|vh|vw|vmin|vmax|in|cm|mm|pt|pc|ex|ch)\b/g,
'0'
)
}
if (property) {
inlineStyles[property] = value
}
})
// Update the element's style attribute with the new value
$(el).attr(
'style',
Object.entries(inlineStyles).map(([property, value]) => `${property}: ${value}`).join('; ')
)
} catch {}
}
// Get the classes from the element's class attribute
const classes = $(el).attr('class')
// 2. `removeInlinedSelectors`
if (options.removeInlinedSelectors && classes) {
const classList = classes.split(' ')
// If the class has been inlined in the style attribute...
if (has(inlineStyles, prop)) {
// Try to remove the classes that have been inlined
if (![...options.safelist].some(item => item.includes(name))) {
remove(classList, classToRemove => name.includes(classToRemove))
}
// Update the class list on the element with the new classes
if (classList.length > 0) {
$(el).attr('class', classList.join(' '))
} else {
$(el).removeAttr('class')
}
}
}
})
}
})
})
$('style[embed]').each((i, el) => {
$(el).removeAttr('embed')
})
return $.html()
}