-
Notifications
You must be signed in to change notification settings - Fork 261
jQuery.htmlPrefilter: add new entry #858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?xml version="1.0"?> | ||
| <entry type="method" name="jQuery.htmlPrefilter" return="String"> | ||
| <title>jQuery.htmlPrefilter()</title> | ||
| <desc>Modify and filter HTML strings passed through <a href="/category/manipulation/">jQuery manipulation methods</a>.</desc> | ||
| <signature> | ||
| <added>1.12/2.2</added> | ||
| <argument name="html" type="String"> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type is correct but I wonder if we could use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I think String is more correct. Strings passed to this function do not need to conform to the constraints laid out by htmlString (for instance, starting with a "<"). |
||
| <desc>The HTML string on which to operate.</desc> | ||
| </argument> | ||
| </signature> | ||
| <longdesc> | ||
| <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><del></code> tags from incoming HTML strings, do this:</p> | ||
| <pre><code> | ||
| var htmlPrefilter = $.htmlPrefilter, | ||
| rdel = /<(del)(?=[\s>])[\w\W]*?<\/\1\s*>/gi; | ||
|
|
||
| $.htmlPrefilter = function( html ) { | ||
| return htmlPrefilter.call( this, html ).replace( rdel, "" ); | ||
| }; | ||
| </code></pre> | ||
| <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><a title="<div />"><></pre>). The <code>jQuery.htmlPrefilter()</code> function can be used to bypass this:</p> | ||
| <pre><code> | ||
| $.htmlPrefilter = function( html ) { | ||
| // Return HTML strings unchanged | ||
| return html; | ||
| }; | ||
| </code></pre> | ||
| <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> | ||
| <pre><code> | ||
| 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 ); | ||
| }; | ||
| </code></pre> | ||
| </longdesc> | ||
| <category slug="manipulation"/> | ||
| </entry> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't remember or find any method that has the double version. I'm fine with this line but I wonder if we have any other example for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried to find one, but I think this is the first. Not sure which I'd pick were I forced to pick one.