Skip to content

Commit cc0604b

Browse files
committed
jQuery.htmlPrefilter: add new entry
Fixes jquerygh-727 Close jquerygh-858
1 parent 82c1f4c commit cc0604b

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

entries/jQuery.htmlPrefilter.xml

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0"?>
2+
<entry type="method" name="jQuery.htmlPrefilter" return="String">
3+
<title>jQuery.htmlPrefilter()</title>
4+
<desc>Modify and filter HTML strings passed through <a href="/category/manipulation/">jQuery manipulation methods</a>.</desc>
5+
<signature>
6+
<added>1.12/2.2</added>
7+
<argument name="html" type="String">
8+
<desc>The HTML string on which to operate.</desc>
9+
</argument>
10+
</signature>
11+
<longdesc>
12+
<p>This method rarely needs to be called directly. Instead, use it as an entry point to modify existing <a href="/category/manipulation/">jQuery manipulation methods</a>. For instance, to remove all <code>&lt;del&gt;</code> tags from incoming HTML strings, do this:</p>
13+
<pre><code>
14+
var htmlPrefilter = $.htmlPrefilter,
15+
rdel = /&lt;(del)(?=[\s&gt;])[\w\W]*?&lt;\/\1\s*&gt;/gi;
16+
17+
$.htmlPrefilter = function( html ) {
18+
return htmlPrefilter.call( this, html ).replace( rdel, "" );
19+
};
20+
</code></pre>
21+
<p>This function can also be overwritten in order to bypass certain edge case issues. The default <code>htmlPrefilter</code> 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. <pre>&lt;a title="&lt;div /&gt;"&gt;&lt;&gt;</pre>). The <code>jQuery.htmlPrefilter()</code> function can be used to bypass this:</p>
22+
<pre><code>
23+
$.htmlPrefilter = function( html ) {
24+
// Return HTML strings unchanged
25+
return html;
26+
};
27+
</code></pre>
28+
<p>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:</p>
29+
<pre><code>
30+
var panything = "[\\w\\W]*?",
31+
32+
// Whitespace
33+
// https://html.spec.whatwg.org/multipage/infrastructure.html#space-character
34+
pspace = "[\\x20\\t\\r\\n\\f]",
35+
36+
// End of tag name (whitespace or greater-than)
37+
pnameEnd = pspace.replace( "]", "&gt;]" ),
38+
39+
// Tag name (a leading letter, then almost anything)
40+
// https://html.spec.whatwg.org/multipage/syntax.html#tag-open-state
41+
// https://html.spec.whatwg.org/multipage/syntax.html#tag-name-state
42+
pname = "[a-z]" + pnameEnd.replace( "[", "[^/\\0" ) + "*",
43+
44+
// Void element (end tag prohibited)
45+
// https://html.spec.whatwg.org/multipage/syntax.html#void-elements
46+
pvoidName = "(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|" +
47+
"source|track|wbr)(?=" + pnameEnd + ")",
48+
49+
// Attributes (double-quoted value, single-quoted value, unquoted value, or no value)
50+
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
51+
pattrs = "(?:" + pspace + "+[^\\0-\\x20\\x7f-\\x9f=\"'/&gt;]+(?:" + pspace + "*=" + pspace +
52+
"*(?:\"" + panything + "\"|'" + panything + "'|" +
53+
pnameEnd.replace( "[", "[^" ) + "*(?!/)" +
54+
")|))*" + pspace + "*",
55+
56+
// Trailing content of a close tag
57+
pcloseTail = "(?:" + pspace + panything + "|)",
58+
59+
rspecialHtml = new RegExp(
60+
// Non-void element that self-closes: $1–$5
61+
"(&lt;)(?!" + pvoidName + ")(" + pname + ")(" + pattrs + ")(\\/)(&gt;)|" +
62+
// No-innerHTML container (element, comment, or CDATA): $6
63+
"(&lt;(script|style|textarea)" + pattrs + "&gt;" + panything + "&lt;\\/\\7" + pcloseTail + "&gt;|" +
64+
"&lt;!--" + panything + "--)",
65+
"gi"
66+
),
67+
68+
// "&lt;"; element name; attributes; "&gt;"; "&lt;"; "/"; element name; "&gt;"; no-innerHTML container
69+
pspecialReplacement = "$1$2$3$5$1$4$2$5$6";
70+
71+
$.htmlPrefilter = function( html ) {
72+
return ( html + "" ).replace( rspecialHtml, pspecialReplacement );
73+
};
74+
</code></pre>
75+
</longdesc>
76+
<category slug="manipulation"/>
77+
</entry>

0 commit comments

Comments
 (0)