Skip to content

Commit f00ab19

Browse files
committed
Core: detect incorrectly nested elements
1 parent bccaa47 commit f00ab19

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/core.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ var oldInit = jQuery.fn.init,
33
oldFind = jQuery.find,
44
rattrHashTest = /\[(\s*[-\w]+\s*)([~|^$*]?=)\s*([-\w#]*?#[-\w#]*)\s*\]/,
55
rattrHashGlob = /\[(\s*[-\w]+\s*)([~|^$*]?=)\s*([-\w#]*?#[-\w#]*)\s*\]/g,
6+
rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([a-z][^\/\0>\x20\t\r\n\f]*)[^>]*)\/>/gi,
7+
rsingleTag = ( /^<([a-z][^\/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i ),
68

79
// Support: Android <=4.0 only
810
// Make sure we trim BOM and NBSP
911
rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
1012

1113
jQuery.fn.init = function( arg1 ) {
12-
var args = Array.prototype.slice.call( arguments );
14+
var args = Array.prototype.slice.call( arguments ),
15+
changed;
1316

1417
if ( typeof arg1 === "string" && arg1 === "#" ) {
1518

@@ -18,6 +21,15 @@ jQuery.fn.init = function( arg1 ) {
1821
args[ 0 ] = [];
1922
}
2023

24+
if ( typeof arg1 === "string" && arg1.trim().charAt( 0 ) === "<" ) {
25+
if ( !rsingleTag.test( arg1 ) ) {
26+
changed = arg1.replace( rxhtmlTag, "<$1></$2>" );
27+
if ( changed !== arg1 ) {
28+
migrateWarn( "HTML tags must be properly nested and closed: " + arg1 );
29+
}
30+
}
31+
}
32+
2133
return oldInit.apply( this, args );
2234
};
2335
jQuery.fn.init.prototype = jQuery.fn;

test/core.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ QUnit.test( "jQuery( '#' )", function( assert ) {
6565
} );
6666
} );
6767

68+
QUnit.test( "Improperly closed elements", function( assert ) {
69+
assert.expect( 3 );
70+
71+
expectWarning( assert, "Elements not self-closable nested wrong", 3, function() {
72+
jQuery( "<div><p/><span/></div>" );
73+
jQuery( "<blockquote><p class='bad' /><span/></blockquote>" );
74+
jQuery( "<div data-borked='y'><span/> <p/></div>" );
75+
} );
76+
77+
expectWarning( assert, "Elements not self-closable", 3, function() {
78+
jQuery( "<div class=wonky />" );
79+
jQuery( "<p style='width: 2%' />" );
80+
jQuery( "<span aria-label='hello' />" );
81+
} );
82+
83+
expectNoWarning( assert, "Bare elements", function() {
84+
jQuery( "<p/>" );
85+
jQuery( "<abbr />" );
86+
jQuery( "<head />" );
87+
} );
88+
89+
} );
90+
6891
QUnit.test( "Attribute selectors with unquoted hashes", function( assert ) {
6992
assert.expect( 31 );
7093

0 commit comments

Comments
 (0)