Permalink
Browse files

Core: set the base href of the context in parseHTML

Fixes gh-2965
Close gh-3022
  • Loading branch information...
timmywil committed Mar 28, 2016
1 parent 5cbb234 commit 10fc59007d717432ea126e49ce4142e6c4d5136e
Showing with 31 additions and 8 deletions.
  1. +22 −8 src/core/parseHTML.js
  2. +9 −0 test/unit/core.js
View
@@ -21,14 +21,28 @@ jQuery.parseHTML = function( data, context, keepScripts ) {
context = false;
}
// Stop scripts or inline event handlers from being executed immediately
// by using document.implementation
context = context || ( support.createHTMLDocument ?
document.implementation.createHTMLDocument( "" ) :
document );
var parsed = rsingleTag.exec( data ),
scripts = !keepScripts && [];
var base, parsed, scripts;
if ( !context ) {
// Stop scripts or inline event handlers from being executed immediately
// by using document.implementation
if ( support.createHTMLDocument ) {
context = document.implementation.createHTMLDocument( "" );
// Set the base href for the created document
// so any parsed elements with URLs
// are based on the document's URL (gh-2965)
base = context.createElement( "base" );
base.href = document.location.href;
context.head.appendChild( base );
} else {
context = document;
}
}
parsed = rsingleTag.exec( data );
scripts = !keepScripts && [];
// Single tag
if ( parsed ) {
View
@@ -1563,6 +1563,15 @@ QUnit.test( "jQuery.parseHTML", function( assert ) {
assert.ok( jQuery.parseHTML( "<#if><tr><p>This is a test.</p></tr><#/if>" ) || true, "Garbage input should not cause error" );
} );
QUnit.test( "jQuery.parseHTML(<a href>) - gh-2965", function( assert ) {
assert.expect( 1 );
var html = "<a href='test.html'></a>",
href = jQuery.parseHTML( html )[ 0 ].href;
assert.ok( /\/test\.html$/.test( href ), "href is not lost after parsing anchor" );

This comment has been minimized.

Show comment
Hide comment
@mgol

mgol Apr 4, 2016

Member

@timmywil @gibson042 I've just noticed that: shouldn't we test that the URL is absolute and not just that it contains test.html?

@mgol

mgol Apr 4, 2016

Member

@timmywil @gibson042 I've just noticed that: shouldn't we test that the URL is absolute and not just that it contains test.html?

This comment has been minimized.

Show comment
Hide comment
@gibson042

gibson042 Apr 4, 2016

Member

Doing so might be difficult. Asserting the introduction of the /, as this does, is probably sufficient.

@gibson042

gibson042 Apr 4, 2016

Member

Doing so might be difficult. Asserting the introduction of the /, as this does, is probably sufficient.

This comment has been minimized.

Show comment
Hide comment
@mgol

mgol Apr 4, 2016

Member

Why difficult? For the purposes of the test we might at least check if the URL starts with http:// or https://.

@mgol

mgol Apr 4, 2016

Member

Why difficult? For the purposes of the test we might at least check if the URL starts with http:// or https://.

} );
if ( jQuery.support.createHTMLDocument ) {
QUnit.asyncTest( "jQuery.parseHTML", function( assert ) {
assert.expect( 1 );

0 comments on commit 10fc590

Please sign in to comment.