-
Notifications
You must be signed in to change notification settings - Fork 97
Add an upgrade guide for jQuery 3.5.0 #196
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
Merged
+60
−5
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <script>{ | ||
| "title": "jQuery Core 3.5 Upgrade Guide" | ||
| }</script> | ||
|
|
||
| ## jQuery.htmlPrefilter changes | ||
|
|
||
| ### A workaround | ||
|
|
||
| If you want to upgrade to jQuery 3.5.0 or newer and don't have time to deal with breaking changes at the moment and you use jQuery Migrate 3.2.0 or newer, you can revert to the previous behavior by invoking: | ||
| ```js | ||
| jQuery.UNSAFE_restoreLegacyHtmlPrefilter(); | ||
| ``` | ||
|
|
||
| If you don't use jQuery Migrate, don't add it just for this one workaround. Instead, you can revert to the previous behavior by redefining `jQuery.htmlPrefilter` after loading jQuery: | ||
| ```js | ||
| var rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([a-z][^\/\0>\x20\t\r\n\f]*)[^>]*)\/>/gi; | ||
| jQuery.htmlPrefilter = function( html ) { | ||
| return html.replace( rxhtmlTag, "<$1></$2>" ); | ||
| }; | ||
| ``` | ||
|
|
||
| Note that if you do this, you lose the jQuery 3.5.0 security fix and you have to be more careful with what HTML you pass to jQuery manipulation methods; regular HTML sanitizing will not be enough. Some security libraries have special sanitization settings for jQuery. For example, [DOMPurify](https://github.com/cure53/DOMPurify) has a `SAFE_FOR_JQUERY` flag: | ||
| ```js | ||
| var sanitizedHtml = DOMPurify.sanitize( unsafeHtml, { SAFE_FOR_JQUERY: true } ); | ||
| elem.html( sanitizedHtml ); | ||
| ``` | ||
|
|
||
| ### Description of the change | ||
|
|
||
| `jQuery.htmlPrefilter` modifies and filters HTML strings passed through [jQuery manipulation methods](/category/manipulation/). The default value of this function in jQueries older than 3.5.0 used to replace XHTML-like tags with versions that work in HTML. For example, previously the following: | ||
| ```js | ||
| jQuery( "<div/><span/>" ); | ||
| ``` | ||
| would create the following structure: | ||
| ```html | ||
| <div></div> | ||
| <span></span> | ||
| ``` | ||
| because `<div/>` was replaced with `<div></div>` & `<span/>` with `<span></span>`. | ||
|
|
||
| jQuery 3.5.0 has changed `jQuery.htmlPrefilter` to be an identity function. That means that the above `jQuery` call would now create above HTML structure only in XML mode of HTML (called also as XHTML) but in regular HTML mode you would now get: | ||
| ```html | ||
| <div> | ||
| <span></span> | ||
| </div> | ||
| ``` | ||
|
|
||
| To avoid this, don't use self-closing tags for tags that may have content unless you page runs in XHTML mode. Make sure you're sending a correct mime type: `application/xhtml+xml`; otherwise, your page will really run in HTML mode. | ||
|
|
||
| If you're writing a library and you want it to work both in HTML & XHTML modes, remember to use self-closing tags for empty elements, i.e. ones that don't have closing tags in HTML. For example, instead of: | ||
| ```js | ||
| jQuery( "<div/><img/>" ); | ||
| ``` | ||
| do this: | ||
| ```js | ||
| jQuery( "<div></div><img/>" ); | ||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This makes it easier to test locally with Vagrant.