Skip to content

Commit a5ad9a7

Browse files
mgoldmethvin
andauthored
Upgrade guide: Add more details to the jQuery 3.5 upgrade guide
The commit adds information about jQuery Migrate 3.3.0+ logging of non-compliant HTML input. It also explicitly states the common example of using a single auto-closing tag works even in jQuery 3.5.0+. There are also tweaks in other places. Closes gh-202 Co-Authored-By: Dave Methvin <[email protected]>
1 parent 66548ee commit a5ad9a7

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

pages/upgrade-guide/3.5.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ If you want to upgrade to jQuery 3.5.0 or newer and don't have time to deal with
1111
jQuery.UNSAFE_restoreLegacyHtmlPrefilter();
1212
```
1313

14+
jQuery Migrate 3.3.0 or newer logs warnings for all input that's affected by this change, regardless of whether the `jQuery.UNSAFE_restoreLegacyHtmlPrefilter()` method was called or not. **Note**: if you overwrite `jQuery.htmlPrefilter` manually, you'll lose those warnings!
15+
1416
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:
1517
```js
1618
var rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([a-z][^\/\0>\x20\t\r\n\f]*)[^>]*)\/>/gi;
@@ -38,7 +40,7 @@ would create the following structure:
3840
```
3941
because `<div/>` was replaced with `<div></div>` & `<span/>` with `<span></span>`.
4042

41-
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:
43+
In jQuery 3.5.0, the `jQuery.htmlPrefilter` method always returns its argument unchanged. Modern browsers usually parse and render HTML strings in HTML mode. Elements such as `<p>` and `<span>` are never self-closing in HTML, so the browser interprets a string like `<p/><span/>` as `<p><span>` and leaves the tags open. The browser closes the tags at the end of the string or at an element that cannot be contained in the element, so another `<p>` tag closes an existing open `<p>` tag.
4244
```html
4345
<div>
4446
<span></span>
@@ -55,3 +57,9 @@ do this:
5557
```js
5658
jQuery( "<div></div><img/>" );
5759
```
60+
61+
One popular input that still works in jQuery 3.5.0 or newer is the one with a single tag with or without attributes:
62+
```js
63+
jQuery( "<div class='yellow' />" );
64+
```
65+
This is becuse it's XHTML-compliant and in HTML the parser first changes it to just the opening tag: `<div class='yellow'>` but then immediately auto-closes it as it reaches the end of input.

0 commit comments

Comments
 (0)