Mercurial > nebulaweb3
comparison default/node_modules/shoestring/src/shoestring.js @ 0:1d038bc9b3d2 default tip
Up:default
author | Liny <dev@neowd.com> |
---|---|
date | Sat, 31 May 2025 09:21:51 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1d038bc9b3d2 |
---|---|
1 //>>excludeStart("exclude", pragmas.exclude); | |
2 define([], function(){ | |
3 //>>excludeEnd("exclude"); | |
4 | |
5 /** | |
6 * The shoestring object constructor. | |
7 * | |
8 * @param {string,object} prim The selector to find or element to wrap. | |
9 * @param {object} sec The context in which to match the `prim` selector. | |
10 * @returns shoestring | |
11 * @this window | |
12 */ | |
13 function shoestring( prim, sec ){ | |
14 var pType = typeof( prim ), | |
15 ret = [], | |
16 sel; | |
17 | |
18 // return an empty shoestring object | |
19 if( !prim ){ | |
20 return new Shoestring( ret ); | |
21 } | |
22 | |
23 // ready calls | |
24 if( prim.call ){ | |
25 return shoestring.ready( prim ); | |
26 } | |
27 | |
28 // handle re-wrapping shoestring objects | |
29 if( prim.constructor === Shoestring && !sec ){ | |
30 return prim; | |
31 } | |
32 | |
33 // if string starting with <, make html | |
34 if( pType === "string" && prim.indexOf( "<" ) === 0 ){ | |
35 var dfrag = doc.createElement( "div" ); | |
36 | |
37 dfrag.innerHTML = prim; | |
38 | |
39 // TODO depends on children (circular) | |
40 return shoestring( dfrag ).children().each(function(){ | |
41 dfrag.removeChild( this ); | |
42 }); | |
43 } | |
44 | |
45 // if string, it's a selector, use qsa | |
46 if( pType === "string" ){ | |
47 if( sec ){ | |
48 return shoestring( sec ).find( prim ); | |
49 } | |
50 | |
51 //>>includeStart("development", pragmas.development); | |
52 try { | |
53 //>>includeEnd("development"); | |
54 sel = doc.querySelectorAll( prim ); | |
55 //>>includeStart("development", pragmas.development); | |
56 } catch( e ) { | |
57 shoestring.error( 'queryselector', prim ); | |
58 } | |
59 //>>includeEnd("development"); | |
60 | |
61 return new Shoestring( sel, prim ); | |
62 } | |
63 | |
64 // array like objects or node lists | |
65 if( Object.prototype.toString.call( pType ) === '[object Array]' || | |
66 (win.NodeList && prim instanceof win.NodeList) ){ | |
67 | |
68 return new Shoestring( prim, prim ); | |
69 } | |
70 | |
71 // if it's an array, use all the elements | |
72 if( prim.constructor === Array ){ | |
73 return new Shoestring( prim, prim ); | |
74 } | |
75 | |
76 // otherwise assume it's an object the we want at an index | |
77 return new Shoestring( [prim], prim ); | |
78 } | |
79 | |
80 var Shoestring = function( ret, prim ) { | |
81 this.length = 0; | |
82 this.selector = prim; | |
83 shoestring.merge(this, ret); | |
84 }; | |
85 | |
86 // TODO only required for tests | |
87 Shoestring.prototype.reverse = [].reverse; | |
88 | |
89 // For adding element set methods | |
90 shoestring.fn = Shoestring.prototype; | |
91 | |
92 shoestring.Shoestring = Shoestring; | |
93 | |
94 // For extending objects | |
95 // TODO move to separate module when we use prototypes | |
96 shoestring.extend = function( first, second ){ | |
97 for( var i in second ){ | |
98 if( second.hasOwnProperty( i ) ){ | |
99 first[ i ] = second[ i ]; | |
100 } | |
101 } | |
102 | |
103 return first; | |
104 }; | |
105 | |
106 // taken directly from jQuery | |
107 shoestring.merge = function( first, second ) { | |
108 var len, j, i; | |
109 | |
110 len = +second.length, | |
111 j = 0, | |
112 i = first.length; | |
113 | |
114 for ( ; j < len; j++ ) { | |
115 first[ i++ ] = second[ j ]; | |
116 } | |
117 | |
118 first.length = i; | |
119 | |
120 return first; | |
121 }; | |
122 | |
123 // expose | |
124 win.shoestring = shoestring; | |
125 | |
126 //>>excludeStart("exclude", pragmas.exclude); | |
127 }); | |
128 //>>excludeEnd("exclude"); |