src/selector-native.js

Maintainability

65.57

Lines of code

124

Created with Raphaël 2.1.002550751002013-3-272013-3-27Maintainability: 65.57

Created with Raphaël 2.1.003162931242013-3-272013-3-27Lines of Code: 124

Difficulty

39.98

Estimated Errors

0.87

Function weight

By Complexity

Created with Raphaël 2.1.0selector_sortOrder10

By SLOC

Created with Raphaël 2.1.0selector_sortOrder31
1
/*
2
 * Optional (non-Sizzle) selector module for custom builds.
3
 *
4
 * Note that this DOES NOT SUPPORT many documented jQuery
5
 * features in exchange for its smaller size:
6
 *
7
 * Attribute not equal selector
8
 * Positional selectors (:first; :eq(n); :odd; etc.)
9
 * Type selectors (:input; :checkbox; :button; etc.)
10
 * State-based selectors (:animated; :visible; :hidden; etc.)
11
 * :has(selector)
12
 * :not(complex selector)
13
 * custom selectors via Sizzle extensions
14
 * Leading combinators (e.g., $collection.find("> *"))
15
 * Reliable functionality on XML fragments
16
 * Requiring all parts of a selector to match elements under context
17
 *   (e.g., $div.find("div > *") now matches children of $div)
18
 * Matching against non-elements
19
 * Reliable sorting of disconnected nodes
20
 * querySelectorAll bug fixes (e.g., unreliable :focus on WebKit)
21
 *
22
 * If any of these are unacceptable tradeoffs, either use Sizzle or
23
 * customize this stub for the project's specific needs.
24
 */
25
 
26
var selector_hasDuplicate,
27
    matches = docElem.webkitMatchesSelector ||
28
        docElem.mozMatchesSelector ||
29
        docElem.oMatchesSelector ||
30
        docElem.msMatchesSelector,
31
    selector_sortOrder = function( a, b ) {
32
        // Flag for duplicate removal
33
        if ( a === b ) {
34
            selector_hasDuplicate = true;
35
            return 0;
36
        }
37
 
38
        var compare = b.compareDocumentPosition && a.compareDocumentPosition && a.compareDocumentPosition( b );
39
 
40
        if ( compare ) {
41
            // Disconnected nodes
42
            if ( compare & 1 ) {
43
 
44
                // Choose the first element that is related to our document
45
                if ( a === document || jQuery.contains(document, a) ) {
46
                    return -1;
47
                }
48
                if ( b === document || jQuery.contains(document, b) ) {
49
                    return 1;
50
                }
51
 
52
                // Maintain original order
53
                return 0;
54
            }
55
 
56
            return compare & 4 ? -1 : 1;
57
        }
58
 
59
        // Not directly comparable, sort on existence of method
60
        return a.compareDocumentPosition ? -1 : 1;
61
    };
62
 
63
jQuery.extend({
64
    find: function( selector, context, results, seed ) {
65
        var elem,
66
            i = 0;
67
 
68
        results = results || [];
69
        context = context || document;
70
 
71
        if ( seed ) {
72
            while ( (elem = seed[i++]) ) {
73
                if ( jQuery.find.matchesSelector(elem, selector) ) {
74
                    results.push( elem );
75
                }
76
            }
77
        } else {
78
            jQuery.merge( results, context.querySelectorAll(selector) );
79
        }
80
 
81
        return results;
82
    },
83
    unique: function( results ) {
84
        var elem,
85
            duplicates = [],
86
            i = 0,
87
            j = 0;
88
 
89
        selector_hasDuplicate = false;
90
        results.sort( selector_sortOrder );
91
 
92
        if ( selector_hasDuplicate ) {
93
            while ( (elem = results[i++]) ) {
94
                if ( elem === results[ i ] ) {
95
                    j = duplicates.push( i );
96
                }
97
            }
98
            while ( j-- ) {
99
                results.splice( duplicates[ j ], 1 );
100
            }
101
        }
102
 
103
        return results;
104
    },
105
    text: function( elem ) {
106
        var node,
107
            ret = "",
108
            i = 0,
109
            nodeType = elem.nodeType;
110
 
111
        if ( !nodeType ) {
112
            // If no nodeType, this is expected to be an array
113
            while ( (node = elem[i++]) ) {
114
                // Do not traverse comment nodes
115
                ret += jQuery.text( node );
116
            }
117
        } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
118
            // Use textContent for elements
119
            return elem.textContent;
120
        } else if ( nodeType === 3 || nodeType === 4 ) {
121
            return elem.nodeValue;
122
        }
123
        // Do not include comment or processing instruction nodes
124
 
125
        return ret;
126
    },
127
    contains: function( a, b ) {
128
        var adown = a.nodeType === 9 ? a.documentElement : a,
129
            bup = b && b.parentNode;
130
        return a === bup || !!( bup && bup.nodeType === 1 && adown.contains(bup) );
131
    },
132
    isXMLDoc: function( elem ) {
133
        return (elem.ownerDocument || elem).documentElement.nodeName !== "HTML";
134
    },
135
    expr: {
136
        match: {
137
            needsContext: /^[\x20\t\r\n\f]*[>+~]/
138
        }
139
    }
140
});
141
 
142
jQuery.extend( jQuery.find, {
143
    matches: function( expr, elements ) {
144
        return jQuery.find( expr, null, null, elements );
145
    },
146
    matchesSelector: function( elem, expr ) {
147
        return matches.call( elem, expr );
148
    }
149
});