Skip to content

Commit

Permalink
fix: preserve email client target selectors when inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Sep 28, 2024
1 parent 20eb507 commit 24a1a9b
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/transformers/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,31 @@ export async function inline(html = '', options = {}) {
}
)

const preservedClasses = new Set()
const preservedClasses = new Set([
'.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
])

// Precompile a single regex to match any substring from the preservedClasses set
const combinedPattern = Array.from(preservedClasses)
.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
Expand Down Expand Up @@ -166,7 +190,7 @@ export async function inline(html = '', options = {}) {

if (options.removeInlinedSelectors) {
// Remove the rule in the <style> tag as long as it's not a preserved class
if (!preservedClasses.has(selector)) {
if (!preservedClasses.has(selector) && !combinedRegex.test(selector)) {
rule.remove()
}

Expand Down

0 comments on commit 24a1a9b

Please sign in to comment.