Skip to content

Commit d21a933

Browse files
committed
Full implementation@
1 parent 6875c02 commit d21a933

File tree

5 files changed

+346
-1
lines changed

5 files changed

+346
-1
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
1-
# sanitize-mail
1+
# Sanitize mail
22
A sanitize-html wrapper optimised for sanitising HTML for email clients
3+
4+
## Installation
5+
6+
NPM
7+
```bash
8+
npm i --save sanitize-mail
9+
```
10+
11+
Yarn
12+
```bash
13+
yarn add sanitize-mail
14+
```
15+
16+
## Usage
17+
Node
18+
```js
19+
const sanitizeMail = require('sanitize-mail');
20+
21+
let html = '<h1>Hello world</h1><canvas></canvas>';
22+
let clean = sanitizeMail(html);
23+
24+
console.log(clean); // Will output without canvas element
25+
```

package-lock.json

Lines changed: 227 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "sanitize-mail",
3+
"version": "1.0.0",
4+
"description": "A sanitize-html wrapper optimised for sanitising HTML for email clients",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Truemedia/sanitize-mail.git"
12+
},
13+
"keywords": [
14+
"sanitize",
15+
"html",
16+
"email",
17+
"mail",
18+
"client",
19+
"clean"
20+
],
21+
"author": "Wade Penistone",
22+
"license": "ISC",
23+
"bugs": {
24+
"url": "https://github.com/Truemedia/sanitize-mail/issues"
25+
},
26+
"homepage": "https://github.com/Truemedia/sanitize-mail#readme",
27+
"dependencies": {
28+
"sanitize-html": "^1.19.1"
29+
}
30+
}

src/index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const sanitizeHtml = require('sanitize-html');
2+
3+
module.exports = function(html)
4+
{
5+
let headingAttributes = [
6+
'align', 'class', 'dir', 'id', 'style'
7+
];
8+
9+
return sanitizeHtml(html, {
10+
allowedTags: [
11+
'a', 'b', 'br', 'div', 'font',
12+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
13+
'hr', 'img', 'label', 'li', 'ol', 'p',
14+
'span', 'strong', 'table', 'td', 'th', 'tr', 'u', 'ul'
15+
],
16+
allowedAttributes: {
17+
'a': ['class', 'href', 'id', 'style', 'target'],
18+
'b': ['class', 'id', 'style'],
19+
'br': ['class', 'id', 'style'],
20+
'div': ['align', 'class', 'dir', 'id', 'style'],
21+
'font': ['class', 'color', 'face', 'id', 'size', 'style'],
22+
'h1': headingAttributes,
23+
'h2': headingAttributes,
24+
'h3': headingAttributes,
25+
'h4': headingAttributes,
26+
'h5': headingAttributes,
27+
'h6': headingAttributes,
28+
'hr': ['align', 'size', 'width'],
29+
'img': [
30+
'align', 'border', 'class', 'height', 'hspace',
31+
'id', 'src', 'style', 'usemap', 'vspace', 'width'
32+
],
33+
'label': ['class', 'id', 'style'],
34+
'li': ['class', 'dir', 'id', 'style', 'type'],
35+
'ol': ['class', 'dir', 'id', 'style', 'type'],
36+
'p': ['align', 'class', 'dir', 'id', 'style'],
37+
'span': ['class', 'id', 'style'],
38+
'strong': ['class', 'id', 'style'],
39+
'table': [
40+
'align', 'bgcolor', 'border', 'cellpadding', 'cellspacing',
41+
'class', 'dir', 'frame', 'id', 'rules', 'style', 'width'
42+
],
43+
'td': [
44+
'abbr', 'align', 'bgcolor', 'class', 'colspan', 'dir',
45+
'height', 'id', 'lang', 'rowspan', 'scope', 'style', 'valign', 'width'
46+
],
47+
'th': [
48+
'abbr', 'align', 'background', 'bgcolor', 'class', 'colspan',
49+
'dir', 'height', 'id', 'lang', 'scope', 'style', 'valign', 'width'
50+
],
51+
'tr': ['align', 'bgcolor', 'class', 'dir', 'id', 'style', 'valign'],
52+
'u': ['class', 'id', 'style'],
53+
'ul': ['class', 'dir', 'id', 'style']
54+
},
55+
selfClosing: ['img', 'br', 'hr'],
56+
allowedSchemes: ['http', 'https', 'mailto', 'tel', 'sms'],
57+
allowedSchemesAppliedToAttributes: ['href', 'src'],
58+
allowProtocolRelative: false
59+
});
60+
};

test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const sanitizeMail = require('./src/index');
2+
3+
let html = '<h1>Hello world</h1><canvas></canvas>';
4+
let clean = sanitizeMail(html);
5+
console.log(clean);

0 commit comments

Comments
 (0)