import postcss from 'postcss' import selectorParser from 'postcss-selector-parser' import valueParser from 'postcss-value-parser' const properties = new Set( `color background background-color background-image background-position background-size background-repeat font font-family font-size font-style font-weight font-variant font-stretch line-height letter-spacing word-spacing text-align text-decoration text-decoration-line text-decoration-color text-indent text-transform text-overflow text-size-adjust -webkit-text-size-adjust white-space word-break overflow-wrap word-wrap vertical-align direction unicode-bidi width height min-width min-height max-width max-height margin margin-top margin-right margin-bottom margin-left padding padding-top padding-right padding-bottom padding-left box-sizing border border-top border-right border-bottom border-left border-width border-style border-color border-radius border-top-width border-right-width border-bottom-width border-left-width border-top-style border-right-style border-bottom-style border-left-style border-top-color border-right-color border-bottom-color border-left-color border-collapse border-spacing display visibility opacity overflow overflow-x overflow-y float clear position top right bottom left z-index list-style list-style-type list-style-position list-style-image table-layout caption-side empty-cells object-fit object-position flex flex-basis flex-direction flex-wrap flex-grow flex-shrink align-items align-self align-content justify-content gap row-gap column-gap grid-template-columns grid-template-rows grid-column grid-row order`.split( /\s+/, ), ) const functions = new Set([ 'rgb', 'rgba', 'hsl', 'hsla', 'calc', 'min', 'max', 'clamp', 'var', 'linear-gradient', 'radial-gradient', 'repeating-linear-gradient', 'repeating-radial-gradient', ]) const unescapeCss = (value) => value.replace(/\\([\da-f]{1,6}\s?|[^\r\n])/gi, (_, code) => /^[\da-f]/i.test(code) ? String.fromCodePoint(Math.min(parseInt(code, 16) || 0xfffd, 0x10ffff)) : code, ) const safeText = (value) => !/[<>\x00-\x08\x0b\x0c\x0e-\x1f]/.test(value) function declaration(node, resolveImage) { const property = unescapeCss(node.prop).toLowerCase() if (!properties.has(property) && !/^--[a-z\d-]{1,64}$/.test(property)) return '' if ( property === 'position' && !/^(static|relative|absolute|inherit|initial)$/i.test(node.value.trim()) ) return '' const tree = valueParser(node.value) let valid = true tree.walk((part) => { if (part.type === 'comment') { part.value = '' return } if (part.type === 'function') { const name = unescapeCss(part.value).toLowerCase() if (name === 'url') { const raw = part.nodes.length === 1 && part.nodes[0].type === 'string' ? part.nodes[0].value : valueParser.stringify(part.nodes) const url = resolveImage(unescapeCss(raw).trim()) if (!url) valid = false else { part.value = 'url' part.nodes = [ { type: 'string', quote: '"', value: url.replaceAll('"', '%22').replaceAll('\\', '%5C'), }, ] } return false } if (!functions.has(name)) valid = false } if (part.type === 'word' || part.type === 'string') { const decoded = unescapeCss(part.value) if ( !safeText(decoded) || /(?:expression|javascript|vbscript|behavior|binding)\s*[:(]/i.test(decoded) ) valid = false } }) const value = tree.toString().trim() if (!valid || !safeText(value) || value.length > 200000) return '' return `${property}:${value}${node.important ? '!important' : ''};` } export function inlineMailCss(css = '', resolveImage = () => null) { if (css.length > 200000) return '' try { const rule = postcss.parse(`a{${css}}`, { from: undefined }).first return (rule?.nodes || []) .filter((node) => node.type === 'decl') .map((node) => declaration(node, resolveImage)) .join('') } catch { return '' } } function scopeSelectors(selector) { return selectorParser((root) => { root.each((item) => { // HTML/body styles refer to the mail's wrappers, never the preview's own document. item.walkTags((tag) => { if (tag.value.toLowerCase() === 'html' || tag.value.toLowerCase() === 'body') tag.replaceWith( selectorParser.attribute({ attribute: `data-mail-${tag.value.toLowerCase()}` }), ) }) item.walkPseudos((pseudo) => { if (pseudo.value === ':root') pseudo.replaceWith(selectorParser.attribute({ attribute: 'data-mail-html' })) }) item.walkIds((id) => { id.value = 'email-' + id.value }) if (item.toString().length > 2000 || /[<>\x00-\x1f]/.test(item.toString())) { item.remove() return } item.prepend(selectorParser.combinator({ value: ' ' })) item.prepend(selectorParser.id({ value: 'mail-content' })) }) }).processSync(selector) } export function stylesheetMailCss(css = '', resolveImage = () => null) { if (css.length > 500000) return '' try { const ast = postcss.parse(css, { from: undefined }) const render = (nodes, depth = 0) => depth > 5 ? '' : nodes .map((node) => { if (node.type === 'atrule') { // Remote stylesheets, fonts, keyframes and browser-specific executable rules are discarded. if ( node.name.toLowerCase() !== 'media' || !safeText(node.params) || /[;{}@\\]/.test(node.params) || node.params.length > 1000 ) return '' const content = render(node.nodes || [], depth + 1) return content ? `@media ${node.params}{${content}}` : '' } if (node.type !== 'rule') return '' const selector = scopeSelectors(node.selector) const content = (node.nodes || []) .filter((child) => child.type === 'decl') .map((child) => declaration(child, resolveImage)) .join('') return selector && content ? `${selector}{${content}}` : '' }) .join('') return render(ast.nodes) } catch { return '' } }