html-janitor
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -5,4 +5,10 @@ # HTML Janitor | ||
## 0.3.2 | ||
Adds IE NodeWalker compatibility change from [daniel-nelson](https://github.com/daniel-nelson), thanks. | ||
*Note*: there are no CI tests for IE so support is not guaranteed | ||
## 0.3.1 | ||
No functionality changes but corrects the package json for the NPM release |
{ | ||
"name": "html-janitor", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"main": "src/html-janitor.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -1,6 +0,7 @@ | ||
html-janitor | ||
============ | ||
# html-janitor | ||
Sanitises HTML. | ||
![](https://circleci.com/gh/guardian/html-janitor.png?circle-token=bd24300ee650966837a73bfe03386828f0192c06) | ||
Uses UMD for support in AMD and Common JS environments. | ||
@@ -7,0 +8,0 @@ |
@@ -11,2 +11,6 @@ (function (root, factory) { | ||
/** | ||
* @param {Object} config.tags Dictionary of allowed tags. | ||
* @param {boolean} config.keepNestedBlockElements Default false. | ||
*/ | ||
function HTMLJanitor(config) { | ||
@@ -80,4 +84,2 @@ this.config = config; | ||
var isNotTopContainer = !! parentNode.parentNode; | ||
// TODO: Don't hardcore this — this is not invalid markup. Should be | ||
// configurable. | ||
var isNestedBlockElement = | ||
@@ -90,3 +92,3 @@ isBlockElement(parentNode) && | ||
// is invalid. | ||
if (!this.config.tags[nodeName] || isInvalid || isNestedBlockElement) { | ||
if (!this.config.tags[nodeName] || isInvalid || (!this.config.keepNestedBlockElements && isNestedBlockElement)) { | ||
// Do not keep the inner text of SCRIPT/STYLE elements. | ||
@@ -130,3 +132,4 @@ if (! (node.nodeName === 'SCRIPT' || node.nodeName === 'STYLE')) { | ||
return document.createTreeWalker(node, | ||
NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT); | ||
NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT, | ||
null, false); | ||
} | ||
@@ -133,0 +136,0 @@ |
@@ -10,3 +10,4 @@ define([ 'html-janitor' ], function (HTMLJanitor) { | ||
ul: {}, | ||
li: {} | ||
li: {}, | ||
div: {} | ||
} | ||
@@ -36,6 +37,6 @@ | ||
it('should remove elements not in the whitelist', function () { | ||
var div = document.createElement('div'); | ||
var aside = document.createElement('aside'); | ||
var p = document.createElement('p'); | ||
div.appendChild(p); | ||
expect(janitor.clean(div.outerHTML)).toBe('<p></p>'); | ||
aside.appendChild(p); | ||
expect(janitor.clean(aside.outerHTML)).toBe('<p></p>'); | ||
}); | ||
@@ -88,4 +89,35 @@ | ||
}); | ||
it('should remove nested span elements', function() { | ||
var html ='<p><span>Hello <span>world</span></span></p>'; | ||
expect(janitor.clean(html)).toBe('<p>Hello world</p>'); | ||
}); | ||
it('should not allow nested block elements by default', function() { | ||
var html = '<div>Hello <div>world</div></div>'; | ||
expect(janitor.clean(html)).toBe('<div>Hello world</div>'); | ||
}); | ||
}); | ||
describe('janitor that allows nested block elements', function () { | ||
var janitor; | ||
var config = { | ||
tags: { | ||
div: {} | ||
}, | ||
keepNestedBlockElements: true | ||
}; | ||
beforeEach(function () { | ||
janitor = new HTMLJanitor(config); | ||
}); | ||
it('should allow nested block elements', function() { | ||
var html = '<div>Hello <div>world</div></div>'; | ||
expect(janitor.clean(html)).toBe('<div>Hello <div>world</div></div>'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
24087
307
16
0