1 import { jQuery } from "../core.js";
2 import { document } from "../var/document.js";
3 import { rsingleTag } from "./var/rsingleTag.js";
4 import { buildFragment } from "../manipulation/buildFragment.js";
5 import { isObviousHtml } from "./isObviousHtml.js";
7 // Argument "data" should be string of html or a TrustedHTML wrapper of obvious HTML
8 // context (optional): If specified, the fragment will be created in this context,
9 // defaults to document
10 // keepScripts (optional): If true, will include scripts passed in the html string
11 jQuery.parseHTML = function( data, context, keepScripts ) {
12 if ( typeof data !== "string" && !isObviousHtml( data + "" ) ) {
15 if ( typeof context === "boolean" ) {
16 keepScripts = context;
20 var base, parsed, scripts;
24 // Stop scripts or inline event handlers from being executed immediately
25 // by using document.implementation
26 context = document.implementation.createHTMLDocument( "" );
28 // Set the base href for the created document
29 // so any parsed elements with URLs
30 // are based on the document's URL (gh-2965)
31 base = context.createElement( "base" );
32 base.href = document.location.href;
33 context.head.appendChild( base );
36 parsed = rsingleTag.exec( data );
37 scripts = !keepScripts && [];
41 return [ context.createElement( parsed[ 1 ] ) ];
44 parsed = buildFragment( [ data ], context, scripts );
46 if ( scripts && scripts.length ) {
47 jQuery( scripts ).remove();
50 return jQuery.merge( [], parsed.childNodes );