总有人间一两风,填我十万八千梦

jQuery .htmlPrefilter() 用法 手册 | 示例代码

jQuery开发手册 归档 169℃ 0评论

Description: Modify and filter HTML strings passed through jQuery manipulation methods.

  • version added: 1.12/2.2jQuery.htmlPrefilter( html )

    • html
      Type: String
      The HTML string on which to operate.

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>])[wW]*?</1s*>/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 ) {
// Return HTML strings unchanged
return 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]*?",
// Whitespace
// https://html.spec.whatwg.org/multipage/infrastructure.html#space-character
pspace = "[\x20\t\r\n\f]",
// End of tag name (whitespace or greater-than)
pnameEnd = pspace.replace( "]", ">]" ),
// Tag name (a leading letter, then almost anything)
// https://html.spec.whatwg.org/multipage/syntax.html#tag-open-state
// https://html.spec.whatwg.org/multipage/syntax.html#tag-name-state
pname = "[a-z]" + pnameEnd.replace( "[", "[^/\0" ) + "*",
// Void element (end tag prohibited)
// https://html.spec.whatwg.org/multipage/syntax.html#void-elements
pvoidName = "(?:area base br col embed hr img input keygen link menuitem meta param " +
"source track wbr)(?=" + pnameEnd + ")",
// Attributes (double-quoted value, single-quoted value, unquoted value, or no value)
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
pattrs = "(?:" + pspace + "+[^\0-\x20\x7f-\x9f="'/>]+(?:" + pspace + "*=" + pspace +
"*(?:"" + panything + "" '" + panything + "' " +
pnameEnd.replace( "[", "[^" ) + "*(?!/)" +
") ))*" + pspace + "*",
// Trailing content of a close tag
pcloseTail = "(?:" + pspace + panything + " )",
rspecialHtml = new RegExp(
// Non-void element that self-closes: $1–$5
"(<)(?!" + pvoidName + ")(" + pname + ")(" + pattrs + ")(\/)(>) " +
// No-innerHTML container (element, comment, or CDATA): $6
"(<(script style textarea)" + pattrs + ">" + panything + "<\/\7" + pcloseTail + "> " +
"<!--" + panything + "--)",
"gi"
),
// "<"; element name; attributes; ">"; "<"; "/"; element name; ">"; no-innerHTML container
pspecialReplacement = "$1$2$3$5$1$4$2$5$6";
$.htmlPrefilter = function( html ) {
return ( html + "" ).replace( rspecialHtml, pspecialReplacement );
};

转载请注明:悠然品鉴 » jQuery .htmlPrefilter() 用法 手册 | 示例代码

喜欢 (0)or分享 (0)
发表我的评论
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址