This method rarely needs to be called directly. Instead, use it as an entry point to modify existing jQuery manipulation methods. For instance, to remove all <del> tags from incoming HTML strings, do this:
|
1
2
3
4
5
6
|
var htmlPrefilter = $.htmlPrefilter,
rdel = /<(del)(?=[\s>])[\w\W]*?<\/\1\s*>/gi;
$.htmlPrefilter = function( html ) {
return htmlPrefilter.call( this, html ).replace( rdel, "" );
|
This function can also be overwritten in order to bypass certain edge case issues. The default htmlPrefilter function in jQuery will greedily ensure that all tags are XHTML-compliant. This includes anything that looks like an HTML tag, but is actually within a string (e.g.
<a title="<div />"><>
). The
jQuery.htmlPrefilter() function can be used to bypass this:
|
1
2
3
4
|
$.htmlPrefilter = function( html ) {
|
However, while the above fix is short and simple, it puts the burden on you to ensure XHTML-compliant tags in any HTML strings. A more thorough fix for this issue would be this:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
var panything = "[\\w\\W]*?",
pspace = "[\\x20\\t\\r\\n\\f]",
pnameEnd = pspace.replace( "]", ">]" ),
pname = "[a-z]" + pnameEnd.replace( "[", "[^/\\0" ) + "*",
pvoidName = "(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|" +
"source|track|wbr)(?=" + pnameEnd + ")",
pattrs = "(?:" + pspace + "+[^\\0-\\x20\\x7f-\\x9f=\"'/>]+(?:" + pspace + "*=" + pspace +
"*(?:\"" + panything + "\"|'" + panything + "'|" +
pnameEnd.replace( "[", "[^" ) + "*(?!/)" +
pcloseTail = "(?:" + pspace + panything + "|)",
rspecialHtml = new RegExp(
"(<)(?!" + pvoidName + ")(" + pname + ")(" + pattrs + ")(\\/)(>)|" +
"(<(script|style|textarea)" + pattrs + ">" + panything + "<\\/\\7" + pcloseTail + ">|" +
"<!--" + panything + "--)",
pspecialReplacement = "$1$2$3$5$1$4$2$5$6";
$.htmlPrefilter = function( html ) {
return ( html + "" ).replace( rspecialHtml, pspecialReplacement );
|