html-janitor
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -5,2 +5,8 @@ # HTML Janitor | ||
## 2.0.2 | ||
Validation functions can now return validation objects rather than simply outcomes. | ||
Thanks to [Brad Vogel](https://github.com/bradvogel) for this functionality. | ||
## 2.0.1 | ||
@@ -7,0 +13,0 @@ |
{ | ||
"name": "html-janitor", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"main": "src/html-janitor.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -59,3 +59,16 @@ # html-janitor | ||
Functions may return any value that's accepted as a regular value, including an object: | ||
``` | ||
blockquote: function(el) { | ||
if (el.classList.contains('indent')){ | ||
return { 'class': true, 'style': true }; // If blockquote has class 'indent', also allow style. | ||
} else { | ||
return {}; // Strip everything | ||
} | ||
} | ||
``` | ||
## Distribution | ||
@@ -62,0 +75,0 @@ |
@@ -101,4 +101,5 @@ (function (root, factory) { | ||
var nodeName = node.nodeName.toLowerCase(); | ||
var allowedAttrs = this.config.tags[nodeName]; | ||
var allowedAttrs = getAllowedAttrs(this.config, nodeName, node); | ||
var isInvalid = isInline && containsBlockElement; | ||
@@ -147,7 +148,15 @@ | ||
function getAllowedAttrs(config, nodeName, node){ | ||
if (typeof config.tags[nodeName] === 'function') { | ||
return config.tags[nodeName](node); | ||
} else { | ||
return config.tags[nodeName]; | ||
} | ||
} | ||
function shouldRejectNode(node, allowedAttrs){ | ||
if (typeof allowedAttrs === 'undefined') { | ||
return true; | ||
} else if (typeof allowedAttrs === 'function'){ | ||
return !allowedAttrs(node); | ||
} else if (typeof allowedAttrs === 'boolean') { | ||
return !allowedAttrs; | ||
} | ||
@@ -154,0 +163,0 @@ |
@@ -38,2 +38,10 @@ define([ 'html-janitor' ], function (HTMLJanitor) { | ||
} | ||
}, | ||
blockquote: function(el) { | ||
// If blockquote has class 'indent', also allow style. | ||
if (el.classList.contains('indent')){ | ||
return { 'class': true, 'style': true }; | ||
} else { | ||
return {}; | ||
} | ||
} | ||
@@ -190,3 +198,3 @@ } | ||
expect(output).toBe('<figure></figure>'); | ||
expect(output).toBe(''); | ||
}); | ||
@@ -217,2 +225,11 @@ | ||
}); | ||
it('should allow certain attributes', function() { | ||
var html = '<blockquote class="indent" style="display:inline" notallowedattr="1"></blockquote>'; | ||
expect(janitor.clean(html)).toBe('<blockquote class="indent" style="display:inline"></blockquote>'); | ||
html = '<blockquote style="display:inline"></blockquote>'; | ||
expect(janitor.clean(html)).toBe('<blockquote></blockquote>'); | ||
}); | ||
}); | ||
@@ -219,0 +236,0 @@ |
34088
460
97