html-janitor
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -5,2 +5,6 @@ # HTML Janitor | ||
## 2.0.0 | ||
Makes element configuration (e.g. p: true/false) symmetrical. Previously setting and element to false remove the element completely from the cleaned output. | ||
## 1.1.0 | ||
@@ -7,0 +11,0 @@ |
{ | ||
"name": "html-janitor", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"main": "src/html-janitor.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -24,4 +24,8 @@ # html-janitor | ||
E.g. `{tags: { p:{}, a: { href: true} }}` would limit the valid HTML subset to just paragraphs and anchor tags, the anchor tags would only have the `href` attribute preserved. | ||
E.g. `{tags: { p:{}, a: { href: true} }}` would limit the valid HTML subset to just paragraphs and anchor tags. Paragraph tags would have all attributes stripped, and the anchor tags would only have the `href` attribute preserved. | ||
#### Blacklisting and whitelisting attributes | ||
You can set an element to be `true` to allow all attributes on an element and `false` to remove all attributes. | ||
## Distribution | ||
@@ -28,0 +32,0 @@ |
@@ -16,2 +16,14 @@ (function (root, factory) { | ||
function HTMLJanitor(config) { | ||
var tagDefinitions = config['tags']; | ||
var tags = Object.keys(tagDefinitions); | ||
var validConfigValues = tags | ||
.map(function(k) { return typeof tagDefinitions[k]; }) | ||
.every(function(type) { return type === 'object' || type === 'boolean'; }); | ||
if(!validConfigValues) { | ||
throw new Error("The configuration was invalid"); | ||
} | ||
this.config = config; | ||
@@ -96,3 +108,3 @@ } | ||
// is invalid. | ||
if (!this.config.tags[nodeName] || isInvalid || (!this.config.keepNestedBlockElements && isNestedBlockElement)) { | ||
if (this.config.tags[nodeName] === undefined || isInvalid || (!this.config.keepNestedBlockElements && isNestedBlockElement)) { | ||
// Do not keep the inner text of SCRIPT/STYLE elements. | ||
@@ -99,0 +111,0 @@ if (! (node.nodeName === 'SCRIPT' || node.nodeName === 'STYLE')) { |
@@ -23,3 +23,4 @@ define([ 'html-janitor' ], function (HTMLJanitor) { | ||
small: true, | ||
div: {} | ||
div: {}, | ||
figure: false | ||
} | ||
@@ -169,2 +170,11 @@ | ||
it('should remove an element if blacklisted', function() { | ||
var el = document.createElement('figure'); | ||
el.setAttribute('class', 'test'); | ||
var output = janitor.clean(el.outerHTML); | ||
expect(output).toBe('<figure></figure>'); | ||
}); | ||
}); | ||
@@ -193,2 +203,16 @@ | ||
describe('janitor with invalid configuration', function() { | ||
var config = { | ||
tags: { | ||
strong: 53 | ||
} | ||
}; | ||
it('should throw an Error on invalid configuration', function() { | ||
expect(function() {new HTMLJanitor(config)}).toThrow(new Error('The configuration was invalid')); | ||
}); | ||
}); | ||
}); |
29762
388
54