init.js 3.5 KB

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