-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsb.js
3317 lines (2707 loc) · 78.9 KB
/
sb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
@Description: All of these array prototypes are part of Javascript 1.5 and are included by defaut in sureert for browsers that do not have them (IE and Opera). They are built in by defualt in Firefox(mozilla) and Safari (webkit)
*/
if(!Array.prototype.forEach){
/**
@Name: Array.prototype.forEach
@Description: Runs a function on every value in an array
@Param: Function func An anonymous function or a reference to a function. Array data is passed to the function for each vlaue in the array. Values passed are v,k,a which stand for value, key and array. v is the current value as it loops through the array, k is the current key as it loops through tthe array and a is the entire array.
@Example:
function addOne(val,key,arr){
val = val+1;
}
var myArray=[1,2,3];
myArray.forEach(addOne);
//afterwards myArray = [2,3,4]
*/
Array.prototype.forEach = function(func){
var k;
if(typeof func === 'function'){
var len = this.length;
for(k=0;k<len;k++){
func(this[k], k, this);
}
}
};
/**
@Name: Array.prototype.filter
@Description: Filters values out of an array that do not return true from the test function.
@Param: Function func An anonymous function or a reference to a function. Array data is passed to the function for each vlaue in the array. Values passed are v,k,a which stand for value, key and array. v is the current value as it loops through the array, k is the current key as it loops through tthe array and a is the entire array.
@Return: Array The new array contains only the values which were true.
@Example:
function over10(val, key, arr) {
if(val > 10){return true;}
}
var myArray = [5, 10, 15];
var newArray = myArray.filter(over10);
//returns the array 10,15 because those two values are >=10
*/
Array.prototype.filter = function(func){
var n=[];
if(typeof func === 'function'){
this.forEach(function(v,k,arr){
if(func(arr[k], k, arr) === true){
n.push(v);
}
});
}
return n;
};
/**
@Name: Array.prototype.every
@Description: Checks to see if every value in an array returns true from the function provided
@Param: Function func An anonymous function or a reference to a function. Array data is passed to the function for each vlaue in the array. Values passed are v,k,a which stand for value, key and array. v is the current value as it loops through the array, k is the current key as it loops through tthe array and a is the entire array.
@Return: Boolean True or False
@Example:
function over10(val, key, arr) {
if(val > 10){return true;}
}
var myArray = [5, 10, 15];
myArray.every(over10);
//returns false because not every number in the array is over 10
*/
Array.prototype.every = function(func){
var k;
if(typeof func === 'function'){
for(k=0;k<this.length;k++){
if(func(this[k], k, this) !== true){
return false;
}
}
return true;
}
};
/**
@Name: Array.prototype.indexOf
@Description: Finds the index of the value given within the array. Return the position of the first matching value. Rememeber that array start at 0.
@Param: Object/String/Number val The value you want to search for in the array.
@Return: Integer
@Example:
var myArray = [1,2,3,'a','b'];
var answer = myArray.indexOf('a');
//answer is 3
*/
Array.prototype.indexOf = function(val){
var k=0;
for(k;k<this.length;k++){
if(this[k] === val){
return k;
}
}
return -1;
};
/**
@Name: Array.prototype.lastIndexOf
@Description: Finds the last index of the value given within the array.Rememeber that array start at 0.
@Param: Object/String/Number val The value you want to search for in the array.
@Return: Integer
@Example:
var myArray = [1,2,3,2];
var answer = myArray.lastIndexOf(2);
//answer is 3
*/
Array.prototype.lastIndexOf = function(val){
var p=-1,k;
for(k=0;k<this.length;k++){
if(this[k] === val){
p=k;
}
}
return p;
};
/**
@Name: Array.prototype.map
@Description: Runs a function on every item in the array and returns the results in an array.
@Param: Function func The function you want applied run on every value in the array. It is automatically passed the current (value, key, and array) as arguments on eqach loop through the array. The function can be either a reference to a global function or an inline anonymouse function.
@Return: Array A new array with each value mapping to the result of the original arrays value after is is passed through the function specified.
@Example:
function addTen(val, key, array) {
return val+10;
}
var myArray = [5, 10, 15];
var answer = myArray.map(addTen);
//answer = [15, 20, 25];
*/
Array.prototype.map = function(func){
var n=[];
if(typeof func === 'function'){
this.forEach(function(v,k,a){
n.push(func(v,k,a));
});
}
return n;
};
/**
@Name: Array.prototype.some
@Description: Similar to sb.arrays.every - if some of the function results are true then some returns true
@Param: Function func A function that every value of the array is passed to. The function is passed (val, key, arr) on every pass of the loop.
@Return: Boolean Returns true if some of the values return true when run through the function provided
@Example:
function isAboveFive(val, key, arr){
if(val >5) {return true;}
}
var myArray = [5, 10, 15];
var answer = myArray.some(isAboveFive);
//answer = true //because some values return true when passed through the isAboveFive function
*/
Array.prototype.some = function(func){
var k;
if(typeof func === 'function'){
for(k=0;k<this.length;k++){
if(func(this[k], k, this) === true){
return true;
}
}
return false;
}
};
};
if (!Object.keys) {
/**
@Name: Object.keys
@Description: Implements Object.keys in browsers that don't have it
taken from https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys
*/
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) result.push(prop);
}
if (hasDontEnumBug) {
for (var i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
}
}
return result;
}
})()
};
/**
@Description: add console global for browsers that don't have it, so that using it won't throw errors
*/
if(typeof console === 'undefined'){
console = {
log : function(){}
};
};
/**
@Author: Paul Visco of http://paul.estrip.org
@Package: surebert
*/
var sb = {
/**
@Name: sb.base
@Description: Used Internally to find required files
*/
base : (typeof window.sbBase !== 'undefined') ? window.sbBase : '/surebert',
/**
@Name: sb.colors
@Description: Used Internally. Methods used to calculate and manipulate color values, see also /colors direcory
*/
colors : {},
/**
@Name: sb.date
@Description: Used Internally.
*/
date : {},
/**
@Name: sb.consol
@Description: Used Internally. Used as placeholder for sb.developer functions
*/
consol : {
log : function(){},
write : function(){},
error : function(){}
},
/**
@Name: sb.css
@Description: Used Internally. Used as placeholder for sb.css functions
*/
css : {},
/**
@Name: sb.included
@Description: An array of all modules that are included, updated live and can be used for debugging and making compressed libraries before putting into production
@Example:
alert(sb.included);
*/
included : [],
/**
@Name: sb.include
@Description: Includes another surebert module. Make sure you surebert files are in /surebert or that you have set sb.base before using this.
@Param: module You can include multiple modules by separating with a comma
@Example:
sb.include('String.prototype.nl2br');
//or multiple modules
sb.include('cookies,date');
*/
include : function(module, onload){
onload = typeof onload === 'function' ? onload : function(){};
if(module.match(',')){
var modules = module.split(',');
modules.forEach(function(v){
sb.include(v);
});
return true;
}
var mods = module.split('.');
var path ='', file, unit=sb,m;
if(mods[0] === 'String' || mods[0] === 'Element' || mods[0] === 'Array'){
unit = window;
}
for(m=0;m<mods.length;m++){
if(m !==0 && m < mods.length && mods.length >1){
path +='.';
}
path +=mods[m];
try{
unit = unit[mods[m]];
} catch(e){}
if(typeof unit === 'undefined'){
this.included.push(path);
if(sb.base === '/surebert'){
file = sb.base+'/'+path.replace(/\./g, "/");
} else {
file = sb.base+path;
}
if(sb.base.match(/^http/)){
var s = new sb.script({
src : file
});
if(path === module){
s.onload = onload;
}
s.load();
} else {
sb.load(file);
if(path === module){
onload();
}
}
} else if(path === module){
onload();
}
}
},
/**
@Name: sb.load
@Description: Used to load external javascript from the same server synchronously and on demand.
@Return: Returns 0 upon eval success or 1 if not
@Example:
sb.load('/surebert/surebert.effects.js');
if(sb.load('../js/myJavascript.js')){
//run function from myJavascript.js
}
*/
load : function(url){
var evaled = 0;
(function(){
var load = new sb.ajax({
url : url,
async : 0,
method : 'get',
format : 'javascript',
debug : sb.loadDebug ? 1 : 0,
onResponse: function(r){
//#######look into this
try{
evaled=1;
}catch(e){
evaled=0;
delete e.stack;
sb.consol.error(sb.messages[13]+"\nURL: "+url+"\n"+sb.objects.dump(e));
}
load=null;
}
}).fetch();
}());
return evaled;
},
/**
@Name: sb.script
@Description: creates a script tag for loading
@Return: DOM node A script tag
@Example:
var script = new sb.script({
src : 'http://webservicesdev.roswellpark.org/test/script',
onload : function(){
alert($('head').innerHTML);
this.remove();
alert($('head').innerHTML);
}
});
script.load();
*/
script : function(o){
var script = document.createElement("script");
script.type = o.type || 'text/javascript';
script.charset = o.charset || 'utf-8';
script.src = o.src;
script.onload = typeof o.onload === 'function' ? o.onload : function(){};
script.load = function(){
document.getElementsByTagName('head')[0].appendChild(this);
};
script.remove = function(){
if(this.clearAttributes){
this.clearAttributes();
}
this.parentNode.removeChild(this);
this.onload = this.onreadystatechange = null;
this.remove = null;
};
if(script.readyState){
script.onreadystatechange = function(){
//IE does not fire regular onloaded
if (this.readyState && this.readyState !== "loaded") {
return;
}
script.onload();
};
}
return script;
},
/**
@Name: sb.math
@Description: Used Internally. A placeholder for sb.math
*/
math : {},
/**
@Name: sb.messages
@Description: a placeholder used internally for holding error messages which are defined in sb.developer. This array just keeps errors from occuring when referencing messages if sb.developer is not included.
*/
messages : [],
/**
@Name: sb.onbodyload
@Description: an array of functions that run once the DOM loads, they are fired in order, funcitons can be function references or inline anonymous functions
@Example:
sb.onbodyload.push({myFunction});
*/
onbodyload : [],
/**
@Name: sb.onleavepage
@Description: an array of functions that run once when leaving the page, they are fired in order
@Example:
sb.onleavepage.push({myFunction});
*/
onleavepage : [],
/**
@Name: sb.get
@Description: a shortcut for sending data via ajax with get and fetch it automatically
@Param: string url The address to send to
@Param: object data The data to send as object or string
@Param: function onResponse The callback function or #id of node to replace innerHTML of
@Param: object params Any additional properties for the ajax object e.g. format, target, etc
@Return: sb.ajax The sb.ajax instance created
@Example:
sb.get('/some/url', {a: 'b'}, function(r){alert(r);});
or
sb.get('/some/url', function(r){alert(r);});
or
sb.get('/some/url', {a: 'b'}, '#myDiv');
or
sb.get('/some/url', '#myDiv');
*/
get : function(url, data, onResponse, params){
if(typeof data === 'function'){
params = onResponse;
onResponse = data;
data=null;
} else if(typeof data === 'string'){
onResponse = data;
data = null;
}
params = params || {};
params.method = 'get';
return sb.ajax.shortcut(url, data, onResponse, params);
},
/**
@Name: sb.post
@Description: a shortcut for sending data via ajax with post and fetch it automatically
@Param: string url The address to send to
@Param: object data The data to send as object or string
@Param: function onResponse The callback function or #id of node to replace innerHTML of
@Param: object params Any additional properties for the ajax object e.g. format, target, etc
@Return: sb.ajax The sb.ajax instance created
@Example:
sb.post('/some/url', {a: 'b'}, function(r){alert(r);});
or
sb.post('/some/url', function(r){alert(r);});
or
sb.post('/some/url', {a: 'b'}, '#myDiv');
or
sb.post('/some/url', '#myDiv');
*/
post : function(url, data, onResponse, params){
if(typeof data === 'function'){
params = onResponse;
onResponse = data;
data=null;
} else if(typeof data === 'string'){
onResponse = data;
data = null;
}
params = params || {};
params.method = 'post';
return sb.ajax.shortcut(url, data, onResponse, params);
},
/**
@Name: sb.toArray
@Description: converts other types of iterable objects into an array e.g. an arguments list or an element sb.nodeList returned from getElementsByTagName.
@Param: Object Iterable non-array
@Return: Array A normal iteratable array with all the properties of an array and the values of the iterable object it was passed.
@Example:
var images = document.getElementsByTagName('img');
images = sb.toArray(images);
images.forEach(function(image,key,arr){
alert(image.src);
});
*/
toArray : function(o){
var a=[],x=0;
var len=o.length;
for(x;x<len;x++){
a.push(o[x]);
}
return a;
},
/**
@Name: sb.typeOf
@Description: returns the type of the object it is passed
@Param: object o Any type of javascript object, string, array, function, number, etc
@Return: String 'function', 'array', 'string', 'object', 'textnode', 'element', 'boolean', 'float', 'number', or returns value of object's custom typeOf() if it exists, 'null'
@Example:
var obj = {name : 'joe'}
sb.typeOf(obj); //return 'object'
*/
typeOf : function(o){
var type='';
if(o === null){
return 'null';
} else if (o instanceof Function) {
type = 'function';
} else if (o instanceof Array) {
type = 'array';
} else if(typeof o === 'number'){
type = 'number';
if(String(o).match(/\./)){
type = 'float';
}
} else if(typeof o === 'string'){
type = 'string';
} else if(o === true || o === false){
type='boolean';
} else {
type = (typeof o).toLowerCase();
}
if(typeof o === 'object' ){
if(typeof o.typeOf === 'function'){
type = o.typeOf();
} else if (o.nodeType){
if (o.nodeType === 3) {
type = 'textnode';
} else if (o.nodeType === 1) {
type = 'element';
}
} else if(typeof o.length !=='undefined' && type !=='array'){
type = 'sb.nodeList';
}
}
return type;
},
/**
@Name: sb.uid
@Description: a placeholder used internally when creating unqiue IDs for DOM elements
*/
uid : 0,
/**
@Name: sb.uniqueID
@Description: produces a unique id, ideal for DOM element which are created on the fly but require unique ids
@Return: String a unique id string for a dom elements id string e.g. 'uid_5'
@Example:
var myUniqueId = sb.uniqueID();
//myUniqueId = 'uid_5' //<--just an example return would be unique each time it is called on a page
*/
uniqueID : function(){
return 'uid_'+(sb.uid +=1);
},
/**
@Name: sb.unixTime
@Description: calculates the current time as a unix timestamp
@Return: Number A unix timestamp
@Example:
var unixtime = sb.unixTime();
//unixtime = 1170091311//<- just a possible example - would return current time
*/
unixTime : function(){
return parseInt(String(new Date().getTime()).substring(0,10), 10);
},
/**
@Name: sb.functions
@Description: Used Internally. A placeholder for sb.functions
*/
functions : {},
/**
@Name: sb.utils
@Description: Used Internally. A placeholder for sb.utils
*/
utils : {},
/**
@Name: sb.widget
@Description: Used Internally. A placeholder for sb.widgets
*/
widget : {},
/**
@Name: sb.ui
@Description: Used Internally. A placeholder for sb.ui elements
*/
ui : {},
/**
@Name: sb.forms
@Description: Used Internally. A placeholder for sb.forms
*/
forms : {}
};
/**
@Name: sb.$
@Type: function
@Param: String Use CSS selectors to return the elements desired
@Description: One of the most important parts of the surebert library. Can reference DOM elements in many way using CSS selectors. The simplest use of it is to reference DOM elements by their id property.
@Example:
e.g.'#myForm' An element id. When passed an element ID it returns a reference to the element with that id'
e.g.'body' An tag name. When passed a tag name it returns an array of all the tags that match that tag name. If the tag is found in sb.singleTags e.g. body, head, title then only one element is returned instead of an array
e.g. '#myDiv' returns node with the id 'myDiv'
e.g. '.myClass' returns all nodes with the class 'myClass', see also [class="myClass"] below
e.g. '*' returns all nodes
e.g. '#myDiv p' returns all the p tags that are decendents of #myDiv
e.g. '#myDiv > p' returns all the p tags that are direct decendents of #myDiv
e.g. 'p + b' returns all the b tags that are direct adjacent siblings of p tags
e.g. 'div ~ p' a p element preceded by an div element
e.g 'p:first-child' returns all the p tags that are the first child of their parent, be careful its not the first child of each p tag
e.g. 'p:last-child' returns all the p tags that are the last child of their parent
e.g. 'p:empty' returns all the p tags that are empty
e.g '#myDiv *:not(p)' returns all nodes that are not p tags within #myDiv
e.g 'input[name="choosen"]' returns all the input nodes with the name 'choosen'
e.g. 'a[href="http://www.surebert.com"] return all the a tags that have the href http://www.surebert.com
e.g. 'a[href$="google.com"] return all the a tags that end in google.com
e.g. 'a[href^="http"] return all the a tags that start with http
e.g. 'a[href*="surebert"] return all the a tags that have the substring "surebert" in them
e.g. a[hreflang|="en"] returns all a tags whose "hreflang" attribute has a hyphen-separated list of values beginning (from the left) with "en"
e.g. 'p[class~="bob"] returns an array of all p tags whose "class" attribute value is a list of space-separated values, one of which is exactly equal to "bob"
e.g. 'p, b, #wrapper' Commas allow you to make multiple selections at once.This example returns all b nodes, all p nodes and node with the id 'wrapper'
e.g '*:not(p)' LIMITED SUPPORT - returns all nodes that are not p tags
*/
sb.$$ = function(selector, root){
return sb.$(selector, root, true);
};
sb.$ = function(selector, root, asNodeList) {
root = root || document;
if(selector === ''){
return new sb.nodeList();
}
//return items that are already objects
if(typeof selector !== 'string'){
if(Element.emulated === true && typeof selector === 'object' && selector !== null){
if(selector.nodeType && selector.nodeType === 1){
sb.$.copyElementPrototypes(selector);
} else if (typeof selector.getElementPrototypes === 'function'){
selector.getElementPrototypes();
}
}
return selector;
}
var nodeList = new sb.nodeList();
nodeList.setSelector(selector);
if(root.querySelectorAll){
nodeList.add(root.querySelectorAll(selector));
} else {
sb.$.parseSelectors(nodeList, root);
}
if(asNodeList){
return nodeList;
}
if(nodeList.length() === 0 && nodeList.selector.match(/^\#[\w\-]+$/) ){
return null;
} else if(nodeList.length() === 1 && (nodeList.selector.match(/^\#[\w\-]+$/) || sb.nodeList.singleTags.some(function(v){
return v === nodeList.selector;
}))){
return nodeList.nodes[0];
} else {
return nodeList;
}
};
sb.$.copyElementPrototypes = function(node){
var ep = Element.prototype,prop;
for(prop in ep){
if(ep.hasOwnProperty(prop)){
node[prop] = ep[prop];
}
}
};
/**
@Name: sb.nodeList.parseInheritors
@Param: inheritor
@Param: within
@Description: Used Internally
*/
sb.$.parseSelectors = function(nodes, within){
within = within || document;
var root = [within], s=0, selectors = nodes.selector.split(",");
var len = selectors.length;
for(s=0;s<len;s++){
root = [within];
selectors[s].split(" ").forEach(function(selector,k,a){
if(selector.indexOf(">")+1){
root = sb.$.getElementsByParent(selector);
if(k+1 === a.length){
nodes.add(root);
}
return true;
} else if(selector.indexOf('[')+1){
///look for attribute's by searching for sqaure brackets //
root = sb.$.getElementsByAttributes(root, selector);
if(k+1 === a.length){
nodes.add(root);
}
return true;
} else if(selector.indexOf("~")+1){
root = sb.$.getElementsBySiblingCombinator(root, selector);
if(k+1 === a.length){
nodes.add(root);
}
return true;
} else if(selector.indexOf("+")+1){
root = sb.$.getElementsByAdjacentSibling(root, selector);
if(k+1 === a.length){
nodes.add(root);
}
return true;
} else if(selector.indexOf(":")+1){
//look for pseudo selectors
root = sb.$.parsePseudoSelectors(root, selector);
if(k+1 === a.length){
nodes.add(root);
}
return true;
} else if((selector.indexOf("#") === 0 && selector.match(/^\#[\w\-]+$/)) || selector.match(/\w+\#[\w\-]+/)) {
var element = sb.$.getElementById(selector);
if(element){
root = (element instanceof Array) ? element : [element];
if(k+1 === a.length){
nodes.add(root);
}
}
return true;
} else if (selector.indexOf(".") !== false){
var period_pos = selector.indexOf(".");
var left_bracket_pos = selector.indexOf("[");
var right_bracket_pos = selector.indexOf("]");
if(period_pos+1 && !(period_pos > left_bracket_pos && period_pos < right_bracket_pos)) {
root = sb.$.getElementsByClassName(selector, root[0]);
if(k+1 === a.length){
nodes.add(root);
}
return true;
}
}
//Tag selectors - no class or id specified.
root = sb.$.getElementsByTagName(root, selector);
if(k+1 === a.length){
nodes.add(root);
}
return true;
});
}
return nodes;
};
/**
@Name: sb.$.getElementById
@Description: Used Internally
*/
sb.$.getElementById = function(selector){
var parts = selector.split("#");
var element = document.getElementById(parts[1]);
return element;
};
/**
@Name: sb.$.getElementsByClassName
@Param: string Selector The selector e.g. .myclass or div.myclass
@Param: element The root to search within e.g. document, div
@Description: Used Internally
*/
sb.$.getElementsByClassName = function(selector, root){
var nodes,elements = [],x=0;
if(root.getElementsByClassName && selector.charAt(0) === '.'){
nodes = root.getElementsByClassName(selector.replace(/\./, ''));
for(x=0;x<nodes.length;x++){
elements.push(nodes[x]);
}
return elements;
}
var parts = selector.split('.');
nodes = root.getElementsByTagName(parts[0] || '*');
var className = parts[1], node, cur_class_name,len = nodes.length;
x=0;
var rg = RegExp("\\b"+className+"\\b");
if(nodes.length > 0){
do{
node = nodes[x];
cur_class_name = node.className;
if (cur_class_name.length && (cur_class_name === className || rg.test(cur_class_name))){
elements.push(node);
}
x++;
} while(x<len);
}
return elements;
};
/**
@Name: sb.$.getElementsByTagName
@Description: Used Internally
*/
sb.$.getElementsByTagName = function(root, tag) {
root = (root instanceof Array) ? root : [root];
var matches = [],len1 = root.length,len2,x=0,i=0,nodes,elements;
for(x=0;x<len1;x++){
nodes = root[x].getElementsByTagName(tag || '*');
elements = [];
len2 = nodes.length;
for(i=0;i<len2;i++){
elements.push(nodes[i]);
}
matches = matches.concat(elements);
}
return matches;
};