parseHTML.js (1604B)
1 define( [ 2 "../core", 3 "../var/document", 4 "./var/rsingleTag", 5 "../manipulation/buildFragment", 6 7 // This is the only module that needs core/support 8 "./support" 9 ], function( jQuery, document, rsingleTag, buildFragment, support ) { 10 11 "use strict"; 12 13 // Argument "data" should be string of html 14 // context (optional): If specified, the fragment will be created in this context, 15 // defaults to document 16 // keepScripts (optional): If true, will include scripts passed in the html string 17 jQuery.parseHTML = function( data, context, keepScripts ) { 18 if ( typeof data !== "string" ) { 19 return []; 20 } 21 if ( typeof context === "boolean" ) { 22 keepScripts = context; 23 context = false; 24 } 25 26 var base, parsed, scripts; 27 28 if ( !context ) { 29 30 // Stop scripts or inline event handlers from being executed immediately 31 // by using document.implementation 32 if ( support.createHTMLDocument ) { 33 context = document.implementation.createHTMLDocument( "" ); 34 35 // Set the base href for the created document 36 // so any parsed elements with URLs 37 // are based on the document's URL (gh-2965) 38 base = context.createElement( "base" ); 39 base.href = document.location.href; 40 context.head.appendChild( base ); 41 } else { 42 context = document; 43 } 44 } 45 46 parsed = rsingleTag.exec( data ); 47 scripts = !keepScripts && []; 48 49 // Single tag 50 if ( parsed ) { 51 return [ context.createElement( parsed[ 1 ] ) ]; 52 } 53 54 parsed = buildFragment( [ data ], context, scripts ); 55 56 if ( scripts && scripts.length ) { 57 jQuery( scripts ).remove(); 58 } 59 60 return jQuery.merge( [], parsed.childNodes ); 61 }; 62 63 return jQuery.parseHTML; 64 65 } );