UNPKG

938 BJavaScriptView Raw
1define([
2 "../core",
3 "./var/rsingleTag",
4 "../manipulation" // buildFragment
5], function( jQuery, rsingleTag ) {
6
7// data: string of html
8// context (optional): If specified, the fragment will be created in this context, defaults to document
9// keepScripts (optional): If true, will include scripts passed in the html string
10jQuery.parseHTML = function( data, context, keepScripts ) {
11 if ( !data || typeof data !== "string" ) {
12 return null;
13 }
14 if ( typeof context === "boolean" ) {
15 keepScripts = context;
16 context = false;
17 }
18 context = context || document;
19
20 var parsed = rsingleTag.exec( data ),
21 scripts = !keepScripts && [];
22
23 // Single tag
24 if ( parsed ) {
25 return [ context.createElement( parsed[1] ) ];
26 }
27
28 parsed = jQuery.buildFragment( [ data ], context, scripts );
29
30 if ( scripts && scripts.length ) {
31 jQuery( scripts ).remove();
32 }
33
34 return jQuery.merge( [], parsed.childNodes );
35};
36
37return jQuery.parseHTML;
38
39});