parseHTML.js 938 B

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