Skip to content

Commit fb9472c

Browse files
LeonardoBragagibson042
authored andcommitted
Manipulation: Bring tagname regexes up to spec
Fixes jquerygh-2005 Closes jquerygh-2634
1 parent df822ca commit fb9472c

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

src/core/var/rsingleTag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
define( function() {
22

33
// Match a standalone tag
4-
return ( /^<([\w-]+)\s*\/?>(?:<\/\1>|)$/ );
4+
return ( /^<([a-z][^\/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i );
55
} );

src/manipulation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ define( [
2727
dataPriv, dataUser, acceptData, DOMEval ) {
2828

2929
var
30-
rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:-]+)[^>]*)\/>/gi,
30+
rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([a-z][^\/\0>\x20\t\r\n\f]*)[^>]*)\/>/gi,
3131

3232
// Support: IE 10-11, Edge 10240+
3333
// In IE/Edge using regex groups here causes severe slowdowns.

src/manipulation/var/rtagName.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
define( function() {
2-
return ( /<([\w:-]+)/ );
2+
return ( /<([a-z][^\/\0>\x20\t\r\n\f]+)/i );
33
} );

test/unit/manipulation.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,73 @@ QUnit.test( "html(String) tag-hyphenated elements (Bug #1987)", function( assert
500500
assert.equal( j.children().text(), "text", "Tags with multiple hypens behave normally" );
501501
} );
502502

503+
QUnit.test( "Tag name processing respects the HTML Standard (gh-2005)", function( assert ) {
504+
505+
assert.expect( 240 );
506+
507+
var wrapper = jQuery( "<div></div>" ),
508+
nameTerminatingChars = "\x20\t\r\n\f".split( "" ),
509+
specialChars = "[ ] { } _ - = + \\ ( ) * & ^ % $ # @ ! ~ ` ' ; ? ¥ « µ λ ⊕ ≈ ξ ℜ ♣ €"
510+
.split( " " );
511+
512+
specialChars.push( specialChars.join( "" ) );
513+
514+
jQuery.each( specialChars, function( i, characters ) {
515+
assertSpecialCharsSupport( "html", characters );
516+
assertSpecialCharsSupport( "append", characters );
517+
} );
518+
519+
jQuery.each( nameTerminatingChars, function( i, character ) {
520+
assertNameTerminatingCharsHandling( "html", character );
521+
assertNameTerminatingCharsHandling( "append", character );
522+
} );
523+
524+
function buildChild( method, html ) {
525+
wrapper[ method ]( html );
526+
return wrapper.children()[ 0 ];
527+
}
528+
529+
function assertSpecialCharsSupport( method, characters ) {
530+
var child,
531+
codepoint = characters.charCodeAt( 0 ).toString( 16 ).toUpperCase(),
532+
description = characters.length === 1 ?
533+
"U+" + ( "000" + codepoint ).slice( -4 ) + " " + characters :
534+
"all special characters",
535+
nodeName = "valid" + characters + "tagname";
536+
537+
child = buildChild( method, "<" + nodeName + "></" + nodeName + ">" );
538+
assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
539+
method + "(): Paired tag name includes " + description );
540+
541+
child = buildChild( method, "<" + nodeName + ">" );
542+
assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
543+
method + "(): Unpaired tag name includes " + description );
544+
545+
child = buildChild( method, "<" + nodeName + "/>" );
546+
assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
547+
method + "(): Self-closing tag name includes " + description );
548+
}
549+
550+
function assertNameTerminatingCharsHandling( method, character ) {
551+
var child,
552+
codepoint = character.charCodeAt( 0 ).toString( 16 ).toUpperCase(),
553+
description = "U+" + ( "000" + codepoint ).slice( -4 ) + " " + character,
554+
nodeName = "div" + character + "this-will-be-discarded";
555+
556+
child = buildChild( method, "<" + nodeName + "></" + nodeName + ">" );
557+
assert.equal( child.nodeName.toUpperCase(), "DIV",
558+
method + "(): Paired tag name terminated by " + description );
559+
560+
child = buildChild( method, "<" + nodeName + ">" );
561+
assert.equal( child.nodeName.toUpperCase(), "DIV",
562+
method + "(): Unpaired open tag name terminated by " + description );
563+
564+
child = buildChild( method, "<" + nodeName + "/>" );
565+
assert.equal( child.nodeName.toUpperCase(), "DIV",
566+
method + "(): Self-closing tag name terminated by " + description );
567+
}
568+
} );
569+
503570
QUnit.test( "IE8 serialization bug", function( assert ) {
504571

505572
assert.expect( 2 );

0 commit comments

Comments
 (0)