lettersanitizer
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.allowedCssProperties = exports.removeWithContents = exports.allowedTags = void 0; | ||
exports.allowedTags = { | ||
export const allowedTags = { | ||
a: ['class', 'href', 'id', 'style', 'target'], | ||
@@ -155,3 +152,3 @@ address: ['class', 'id', 'style'], | ||
}; | ||
exports.removeWithContents = [ | ||
export const removeWithContents = [ | ||
'script', | ||
@@ -165,3 +162,3 @@ 'iframe', | ||
]; | ||
exports.allowedCssProperties = [ | ||
export const allowedCssProperties = [ | ||
'azimuth', | ||
@@ -168,0 +165,0 @@ 'background', |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.sanitize = void 0; | ||
const constants_1 = require("./constants"); | ||
import { allowedTags, allowedCssProperties, removeWithContents, } from './constants'; | ||
function prependIdToSelectorText(selectorText, id) { | ||
@@ -50,3 +47,3 @@ if (!id) | ||
for (const name of properties) { | ||
if (constants_1.allowedCssProperties.includes(name)) { | ||
if (allowedCssProperties.includes(name)) { | ||
const value = style.getPropertyValue(name); | ||
@@ -69,3 +66,2 @@ style.setProperty(name, sanitizeCssValue(value, allowedSchemas, rewriteExternalResources), preserveCssPriority ? style.getPropertyPriority(name) : undefined); | ||
.map(() => ((Math.random() * 25) % 25) + 65)), allowedSchemas = defaultAllowedSchemas, preserveCssPriority = true, noWrapper = false, }) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h; | ||
if (noWrapper) | ||
@@ -82,5 +78,5 @@ id = ''; | ||
while ((node = commentIter.nextNode())) { | ||
(_a = node.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(node); | ||
node.parentNode?.removeChild(node); | ||
} | ||
const removeTags = [...constants_1.removeWithContents]; | ||
const removeTags = [...removeWithContents]; | ||
if (dropAllHtmlTags) { | ||
@@ -113,11 +109,11 @@ removeTags.push('style'); | ||
const textNode = doc.createTextNode(node.textContent); | ||
(_b = node.parentNode) === null || _b === void 0 ? void 0 : _b.replaceChild(textNode, node); | ||
node.parentNode?.replaceChild(textNode, node); | ||
} | ||
else { | ||
(_c = node.parentNode) === null || _c === void 0 ? void 0 : _c.removeChild(node); | ||
node.parentNode?.removeChild(node); | ||
} | ||
continue; | ||
} | ||
if (tagName in constants_1.allowedTags) { | ||
const allowedAttributes = constants_1.allowedTags[tagName]; | ||
if (tagName in allowedTags) { | ||
const allowedAttributes = allowedTags[tagName]; | ||
for (const attribute of element.getAttributeNames()) { | ||
@@ -128,10 +124,13 @@ if (!allowedAttributes.includes(attribute)) { | ||
else if (attribute === 'class' && !noWrapper) { | ||
element.setAttribute(attribute, (_e = (_d = element | ||
.getAttribute(attribute)) === null || _d === void 0 ? void 0 : _d.split(' ').map(className => id + '_' + className).join(' ')) !== null && _e !== void 0 ? _e : ''); | ||
element.setAttribute(attribute, element | ||
.getAttribute(attribute) | ||
?.split(' ') | ||
.map(className => id + '_' + className) | ||
.join(' ') ?? ''); | ||
} | ||
else if (attribute === 'id' && !noWrapper) { | ||
element.setAttribute(attribute, id + '_' + ((_f = element.getAttribute(attribute)) !== null && _f !== void 0 ? _f : '')); | ||
element.setAttribute(attribute, id + '_' + (element.getAttribute(attribute) ?? '')); | ||
} | ||
else if (attribute === 'href' || attribute === 'src') { | ||
const value = (_g = element.getAttribute(attribute)) !== null && _g !== void 0 ? _g : ''; | ||
const value = element.getAttribute(attribute) ?? ''; | ||
if (attribute === 'href' && rewriteExternalLinks) { | ||
@@ -165,13 +164,13 @@ element.setAttribute(attribute, rewriteExternalLinks(value)); | ||
try { | ||
(_h = element.parentNode) === null || _h === void 0 ? void 0 : _h.removeChild(element); | ||
element.parentNode?.removeChild(element); | ||
} | ||
catch (_j) { | ||
catch { | ||
element.outerHTML = ''; | ||
} | ||
} | ||
catch (_k) { | ||
catch { | ||
try { | ||
element.remove(); | ||
} | ||
catch (_l) { } | ||
catch { } | ||
} | ||
@@ -234,5 +233,5 @@ } | ||
} | ||
function sanitize(html, text, options) { | ||
let contents = html !== null && html !== void 0 ? html : ''; | ||
if ((contents === null || contents === void 0 ? void 0 : contents.length) === 0 && text) { | ||
export function sanitize(html, text, options) { | ||
let contents = html ?? ''; | ||
if (contents?.length === 0 && text) { | ||
contents = sanitizeText(text) | ||
@@ -243,4 +242,3 @@ .split('\n') | ||
} | ||
return sanitizeHtml(contents, options !== null && options !== void 0 ? options : {}); | ||
return sanitizeHtml(contents, options ?? {}); | ||
} | ||
exports.sanitize = sanitize; |
{ | ||
"name": "lettersanitizer", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "DOM-based HTML email sanitizer for in-browser email rendering.", | ||
@@ -51,16 +51,14 @@ "main": "lib/index.js", | ||
"@types/jest": "24.9.0", | ||
"@typescript-eslint/eslint-plugin": "^3.1.0", | ||
"@typescript-eslint/parser": "^3.1.0", | ||
"eslint": "^7.1.0", | ||
"husky": "^4.2.3", | ||
"@typescript-eslint/eslint-plugin": "^5.37.0", | ||
"@typescript-eslint/parser": "^5.37.0", | ||
"eslint": "^8.23.1", | ||
"husky": "^8.0.1", | ||
"jest": "24.9.0", | ||
"jsdom": "11.12.0", | ||
"lint-staged": "^10.0.9", | ||
"prettier": "^2.0.2", | ||
"lint-staged": "^13.0.3", | ||
"prettier": "^2.7.1", | ||
"ts-jest": "^24.3.0", | ||
"typescript": "3.9.10" | ||
}, | ||
"jest": { | ||
"testEnvironment": "jsdom" | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.8.3" | ||
} | ||
} |
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"module": "commonjs", | ||
"target": "ES2020", | ||
"module": "ES2020", | ||
"declaration": true, | ||
"outDir": "./lib", | ||
"lib": ["es2016", "dom"], | ||
"lib": ["ES2020", "DOM"], | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"downlevelIteration": true, | ||
@@ -9,0 +11,0 @@ "strict": true |
23173
12
645