init.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Initialize a jQuery object
  2. define( [
  3. "../core",
  4. "../var/document",
  5. "./var/rsingleTag",
  6. "../traversing/findFilter"
  7. ], function( jQuery, document, rsingleTag ) {
  8. "use strict";
  9. // A central reference to the root jQuery(document)
  10. var rootjQuery,
  11. // A simple way to check for HTML strings
  12. // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
  13. // Strict HTML recognition (#11290: must start with <)
  14. // Shortcut simple #id case for speed
  15. rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/,
  16. init = jQuery.fn.init = function( selector, context, root ) {
  17. var match, elem;
  18. // HANDLE: $(""), $(null), $(undefined), $(false)
  19. if ( !selector ) {
  20. return this;
  21. }
  22. // Method init() accepts an alternate rootjQuery
  23. // so migrate can support jQuery.sub (gh-2101)
  24. root = root || rootjQuery;
  25. // Handle HTML strings
  26. if ( typeof selector === "string" ) {
  27. if ( selector[ 0 ] === "<" &&
  28. selector[ selector.length - 1 ] === ">" &&
  29. selector.length >= 3 ) {
  30. // Assume that strings that start and end with <> are HTML and skip the regex check
  31. match = [ null, selector, null ];
  32. } else {
  33. match = rquickExpr.exec( selector );
  34. }
  35. // Match html or make sure no context is specified for #id
  36. if ( match && ( match[ 1 ] || !context ) ) {
  37. // HANDLE: $(html) -> $(array)
  38. if ( match[ 1 ] ) {
  39. context = context instanceof jQuery ? context[ 0 ] : context;
  40. // Option to run scripts is true for back-compat
  41. // Intentionally let the error be thrown if parseHTML is not present
  42. jQuery.merge( this, jQuery.parseHTML(
  43. match[ 1 ],
  44. context && context.nodeType ? context.ownerDocument || context : document,
  45. true
  46. ) );
  47. // HANDLE: $(html, props)
  48. if ( rsingleTag.test( match[ 1 ] ) && jQuery.isPlainObject( context ) ) {
  49. for ( match in context ) {
  50. // Properties of context are called as methods if possible
  51. if ( jQuery.isFunction( this[ match ] ) ) {
  52. this[ match ]( context[ match ] );
  53. // ...and otherwise set as attributes
  54. } else {
  55. this.attr( match, context[ match ] );
  56. }
  57. }
  58. }
  59. return this;
  60. // HANDLE: $(#id)
  61. } else {
  62. elem = document.getElementById( match[ 2 ] );
  63. if ( elem ) {
  64. // Inject the element directly into the jQuery object
  65. this[ 0 ] = elem;
  66. this.length = 1;
  67. }
  68. return this;
  69. }
  70. // HANDLE: $(expr, $(...))
  71. } else if ( !context || context.jquery ) {
  72. return ( context || root ).find( selector );
  73. // HANDLE: $(expr, context)
  74. // (which is just equivalent to: $(context).find(expr)
  75. } else {
  76. return this.constructor( context ).find( selector );
  77. }
  78. // HANDLE: $(DOMElement)
  79. } else if ( selector.nodeType ) {
  80. this[ 0 ] = selector;
  81. this.length = 1;
  82. return this;
  83. // HANDLE: $(function)
  84. // Shortcut for document ready
  85. } else if ( jQuery.isFunction( selector ) ) {
  86. return root.ready !== undefined ?
  87. root.ready( selector ) :
  88. // Execute immediately if ready is not present
  89. selector( jQuery );
  90. }
  91. return jQuery.makeArray( selector, this );
  92. };
  93. // Give the init function the jQuery prototype for later instantiation
  94. init.prototype = jQuery.fn;
  95. // Initialize central reference
  96. rootjQuery = jQuery( document );
  97. return init;
  98. } );