src/core/init.js

Maintainability

49.37

Lines of code

127

Created with Raphaël 2.1.002550751002016-7-102016-6-102016-5-112016-4-112016-3-122016-2-112016-1-12

2016-8-9
Maintainability: 49.37

Created with Raphaël 2.1.00501001502002016-7-102016-6-102016-5-112016-4-112016-3-122016-2-112016-1-12

2016-8-9
Lines of Code: 127

Difficulty

32.98

Estimated Errors

0.58

Function weight

By Complexity

Created with Raphaël 2.1.0<anonymous>25

By SLOC

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