init.js 3.3 KB

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