
Untitled
By: a guest on
May 9th, 2012 | syntax:
None | size: 0.90 KB | hits: 14 | expires: Never
Given a string of html code, how can I go through every tag and remove ones that is not in my whitelist (in JQuery)?
var whitelist = ['a','div','img', 'span'];
var canvas = '<div>'+canvas_html+'</div>';
var blacklist = ['script','object','param','embed','applet','app','iframe',
'form','input', 'link','meta','title','input','button','textarea'
'head','body','kbd'];
blacklist.forEach(function(r){
$(canvas).find(r).remove();
});
canvas_html = $(canvas).get('div').html();
var whitelist = ['a','div','img', 'span'];
var output = $('<div>'+canvas_html+'</div>').find('*').each(function() {
if($.inArray(this.nodeName.toLowerCase(), whitelist)==-1) {
$(this).remove();
}
}).html();
// output contains the HTML with everything except those in the whitelist stripped off
$(canvas).find(':not(' + whitelist.join(', ') + ')').remove().html();