parseHTML.js 1000 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. define( [
  2. "../core",
  3. "../var/document",
  4. "./var/rsingleTag",
  5. "../manipulation/buildFragment"
  6. ], function( jQuery, document, rsingleTag, buildFragment ) {
  7. // Argument "data" should be string of 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 ( !data || typeof data !== "string" ) {
  13. return null;
  14. }
  15. if ( typeof context === "boolean" ) {
  16. keepScripts = context;
  17. context = false;
  18. }
  19. context = context || document;
  20. var parsed = rsingleTag.exec( data ),
  21. scripts = !keepScripts && [];
  22. // Single tag
  23. if ( parsed ) {
  24. return [ context.createElement( parsed[ 1 ] ) ];
  25. }
  26. parsed = buildFragment( [ data ], context, scripts );
  27. if ( scripts && scripts.length ) {
  28. jQuery( scripts ).remove();
  29. }
  30. return jQuery.merge( [], parsed.childNodes );
  31. };
  32. return jQuery.parseHTML;
  33. } );