Is It Wise To Use Jquery For Whitelisting Tags? Are There Existing Solutions In Javascript?
Solution 1:
If you leverage the browser's HTML correcting abilities (e.g. you copy the rich text to the innerHTML
of an empty div
and take the resulting DOM tree), the HTML will be guaranteed to be valid (the way it will be corrected is somewhat browser-dependent). Although this is probably done by rich editor anyways.
jQuery's own text-top DOM transform is probably also safe, but definitely slower, so I would avoid it.
Using a whitelist based on the jQuery selector engine might be somewhat tricky because removing an element while preserving its children might make the document invalid, so the browser would correct it by changing the DOM tree, which might confuse a script trying to iterate through invalid elements. (E.g. you allow ul
and li
but not ol
; the script removes the list root element, naked li
elements are invalid so the browser wraps them in ul
again, that ul
will be missed by the cleaning script.) If you throw away unwanted elements together with all their children, I don't see any problems with that.
Post a Comment for "Is It Wise To Use Jquery For Whitelisting Tags? Are There Existing Solutions In Javascript?"