Skip to content

Commit c8147a3

Browse files
committed
Remove leading/trailing text from $(html). Close gh-27.
1 parent 514a24b commit c8147a3

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/core.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var matched, browser,
33
oldInit = jQuery.fn.init,
44
oldParseJSON = jQuery.parseJSON,
5+
rignoreText = /^[^<]*(.*?)[^>]*$/,
56
// Note this does NOT include the #9521 XSS fix from 1.7!
67
rquickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*|#([\w\-]*))$/;
78

@@ -15,13 +16,17 @@ jQuery.fn.init = function( selector, context, rootjQuery ) {
1516
if ( selector.charAt( 0 ) !== "<" ) {
1617
migrateWarn("$(html) HTML strings must start with '<' character");
1718
}
19+
if ( selector.charAt( selector.length -1 ) !== ">" ) {
20+
migrateWarn("$(html) HTML string has stray text after last tag");
21+
}
1822
// Now process using loose rules; let pre-1.8 play too
1923
if ( context && context.context ) {
2024
// jQuery object as context; parseHTML expects a DOM object
2125
context = context.context;
2226
}
2327
if ( jQuery.parseHTML ) {
24-
return oldInit.call( this, jQuery.parseHTML( jQuery.trim(selector), context, true ),
28+
match = rignoreText.exec( selector );
29+
return oldInit.call( this, jQuery.parseHTML( match[1] || selector, context, true ),
2530
context, rootjQuery );
2631
}
2732
}

test/core.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test( "jQuery(html, props)", function() {
1212
});
1313

1414
test( "jQuery(html) loose rules", function() {
15-
expect( 19 );
15+
expect( 25 );
1616

1717
var w,
1818
nowarns = {
@@ -23,11 +23,13 @@ test( "jQuery(html) loose rules", function() {
2323
warns = {
2424
"leading space": " <div />",
2525
"leading newline": "\n<div />",
26-
"leading text": "don't<div>try this</div>"
26+
"leading text": "don't<div>try this</div>",
27+
"trailing text": "<div>try this</div>junk",
28+
"both text": "don't<div>try this</div>either"
2729
},
2830
generate = function( html ) {
2931
return function() {
30-
var el = jQuery( html ).last();
32+
var el = jQuery( html );
3133

3234
equal( el.length, 1, html + " succeeded" );
3335
equal( el.parent().length, 0, html + " generated new content" );

warnings.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ This is _not_ a warning, but a console log message the plugin shows when it firs
2424

2525
**Solution**: Use the `$.parseHTML()` method to parse arbitrary HTML, especially HTML from external sources. To obtain a jQuery object that has the parsed HTML without running scripts, use `$($.parseHTML("string"))`. To run scripts in the HTML as well, use `$($.parseHTML("string", document, true))` instead. We do not recommend running `$.trim()` on the string to circumvent this check.
2626

27+
### JQMIGRATE: $(html) text after last tag is ignored
28+
29+
**Cause:** HTML strings passed to `$()` should begin and end with tags. Any text following the last tag is ignored. When upgrading to jQuery 1.9 and using `$.parseHTML()`, note that leading or trailing text is _not_ ignored, and those text nodes will be part of the data returned.
30+
31+
**Solution**: Usually this warning is due to an error in the HTML string, where text is present when it should not be there. Remove the leading or trailing text before passing the string to `$.parseHTML()` if it should not be part of the collection. Alternatively you can use `$($.parseHTML(html)).filter("*")` to remove all top-level text nodes from the set and leave only elements.
32+
2733
### JQMIGRATE: Can't change the 'type' of an input or button in IE 6/7/8
2834

2935
**Cause:** IE 6, 7, and 8 throw an error if you attempt to change the type attribute of an input or button element, for example to change a radio button to a checkbox. Prior to 1.9, jQuery threw an error for every browser to create consistent behavior. As of jQuery 1.9 setting the type is allowed, but will still throw an error in oldIE.

0 commit comments

Comments
 (0)