/
dom.js
executable file
·3752 lines (3158 loc) · 105 KB
/
dom.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
/**
@name glow.dom
@namespace
@description Accessing and manipulating the DOM
@see <a href="../furtherinfo/creatingnodelists/">Creating NodeLists</a>
@see <a href="../furtherinfo/workingwithnodelists/">Working with NodeLists</a>
@see <a href="../furtherinfo/xmlnodelists/">XML NodeLists</a>
*/
(window.gloader || glow).module({
name: "glow.dom",
library: ["glow", "@VERSION@"],
depends: [],
builder: function(glow) {
//private
var env = glow.env,
lang = glow.lang,
/*
PrivateVar: cssRegex
For matching CSS selectors
*/
cssRegex = {
tagName: /^(\w+|\*)/,
combinator: /^\s*([>]?)\s*/,
//safari 1.3 is a bit dim when it comes to unicode stuff, only dot matches them (not even \S), so some negative lookalheads are needed
classNameOrId: (env.webkit < 417) ? new RegExp("^([\\.#])((?:(?![\\.#\\[:\\s\\\\]).|\\\\.)+)") : /^([\.#])((?:[^\.#\[:\\\s]+|\\.)+)/
},
//for escaping strings in regex
regexEscape = /([$^\\\/()|?+*\[\]{}.-])/g,
/*
PrivateVar: cssCache
Cache of arrays representing an execution path for css selectors
*/
cssCache = {},
/*
PrivateVar: dom0PropertyMapping
Mapping of HTML attribute names to DOM0 property names.
*/
dom0PropertyMapping = {
checked : "checked",
"class" : "className",
"disabled" : "disabled",
"for" : "htmlFor",
maxlength : "maxLength"
},
/*
PrivateVar: dom0BooleanAttribute
The HTML attributes names that should be converted from true/false
to ATTRIBUTENAME/undefined (i.e. boolean attributes like checked="checked").
*/
dom0BooleanAttribute = {
checked : true,
disabled : true
},
/*
PrivateVar: dom0AttributeMappings
Functions that map dom0 values to sane ones.
*/
dom0AttributeMappings = {
maxlength : function (val) { return val.toString() == "2147483647" ? undefined : val; }
},
/*
PrivateVar: ucheck
Used by unique(), increased by 1 on each use
*/
ucheck = 1,
/*
PrivateVar: ucheckPropName
This is the property name used by unique checks
*/
ucheckPropName = "_unique" + glow.UID,
/**
@name glow.dom-dataPropName
@private
@type String
@description The property name added to the DomElement by the NodeList#data method.
*/
dataPropName = "_uniqueData" + glow.UID,
/**
@name glow.dom-dataIndex
@private
@type String
@description The value of the dataPropName added by the NodeList#data method.
*/
dataIndex = 1, // must be a truthy value
/**
@name glow.dom-dataCache
@private
@type Object
@description Holds the data used by the NodeList#data method.
The structure is like:
[
{
myKey: "my data"
}
]
*/
dataCache = [],
/*
PrivateVar: htmlColorNames
Mapping of colour names to hex values
*/
htmlColorNames = {
black: 0,
silver: 0xc0c0c0,
gray: 0x808080,
white: 0xffffff,
maroon: 0x800000,
red: 0xff0000,
purple: 0x800080,
fuchsia: 0xff00ff,
green: 0x8000,
lime: 0xff00,
olive: 0x808000,
yellow: 0xffff00,
navy: 128,
blue: 255,
teal: 0x8080,
aqua: 0xffff,
orange: 0xffa500
},
/*
PrivateVar: usesYAxis
regex for detecting which css properties need to be calculated relative to the y axis
*/
usesYAxis = /height|top/,
colorRegex = /^rgb\(([\d\.]+)(%?),\s*([\d\.]+)(%?),\s*([\d\.]+)(%?)/i,
cssPropRegex = /^(?:(width|height)|(border-(top|bottom|left|right)-width))$/,
hasUnits = /width|height|top$|bottom$|left$|right$|spacing$|indent$|font-size/,
//append gets set to a function below
append,
//unique gets set to a function below
unique,
//we set this up at the end of the module
placeholderElm,
//getByTagName gets get to a function below
getByTagName,
win = window,
doc = document,
docBody,
docElm,
// true if properties of a dom node are cloned when the node is cloned (eg, true in IE)
nodePropertiesCloned,
// used to convert divs to strings
tmpDiv = doc.createElement("div"),
/*
PrivateVars: tableArray, elmFilter
Used in private function stringToNodes to capture any
elements that cannot be a childNode of <div>.
Each entry in JSON responds to an element.
First array value is how deep the element will be created in a node tree.
Second array value is the beginning of the node tree.
Third array value is the end of the node tree.
*/
tableArray = [1, '<table>', '</table>'],
emptyArray = [0, '', ''],
// webkit won't accept <link> elms to be the only child of an element,
// it steals them and hides them in the head for some reason. Using
// broken html fixes it for some reason
paddingElmArray = env.webkit < 526 ? [0, '', '</div>', true] : [1, 'b<div>', '</div>'],
trArray = [3, '<table><tbody><tr>', '</tr></tbody></table>'],
elmWraps = {
caption: tableArray,
thead: tableArray,
th: trArray,
colgroup: tableArray,
tbody: tableArray,
tr: [2, '<table><tbody>', '</tbody></table>'],
td: trArray,
tfoot: tableArray,
option: [1, '<select>', '</select>'],
legend: [1, '<fieldset>', '</fieldset>'],
link: paddingElmArray,
script: paddingElmArray,
style: paddingElmArray
};
// clean up IE's mess
if (env.ie) {
window.attachEvent("onunload", function() {
tmpDiv = null;
});
}
glow.ready(function() {
docBody = doc.body;
docElm = doc.documentElement;
});
// test for nodePropertiesCloned
(function() {
var div = doc.createElement("div");
div.a = 1;
nodePropertiesCloned = !!div.cloneNode(true).a;
})();
/**
@name glow.dom-getFirstChildElm
@private
@description Returns the first leaf of a NodeList
@param {NodeList}
*/
function getFirstChildElm(parent) {
for (var child = parent.firstChild; child; child = child.nextSibling) {
if (child.nodeType == 1) {
return child;
}
}
return null;
}
/*
PrivateMethod: removeClassRegex
Get a regex that can be used to remove a class from a space separated list of classes.
Arguments:
name - (string) the name of the class.
Returns:
The regex.
*/
function removeClassRegex (name) {
return new RegExp(["(^|\\s)", name.replace(regexEscape, "\\$1"), "($|\\s)"].join(""), "g");
}
/*
PrivateMethod: stringToNodes
Creates an array of nodes from a string
*/
function stringToNodes(str) {
var r = [],
tagName = (/^\s*<([^\s>]+)/.exec(str) || [,'div'])[1],
// This matches str content with potential elements that cannot
// be a child of <div>. elmFilter declared at top of page.
elmWrap = elmWraps[tagName] || emptyArray,
nodeDepth,
childElm,
rLen = 0;
// Create the new element using the node tree contents available in filteredElm.
tmpDiv.innerHTML = (elmWrap[1] + str + elmWrap[2]);
childElm = tmpDiv;
// Strip newElement down to just the required element and its parent
nodeDepth = elmWrap[0];
while(nodeDepth--) {
childElm = childElm.lastChild;
}
// pull nodes out of child
while (childElm.firstChild) {
r[rLen++] = childElm.removeChild(childElm.firstChild);
}
childElm = null;
return r;
}
/*
PrivateMethod: nodelistToArray
Converts a w3 NodeList to an array
*/
function nodelistToArray(nodelist) {
var r = [], i = 0;
for (; nodelist[i]; i++) {
r[i] = nodelist[i];
}
return r;
}
/*
PrivateMethod: setAttribute
Sets the attribute in the nodelist using the supplied function.
Arguments:
value - (String|Function) the value/value generator.
attributeSetter - (Function) a function that can be called to actually set the attribute.
Returns:
The <NodeList> object.
*/
// is marginal having this separated out as it is only used twice and call is almost as big
// leaving it separate for now for once and only once, as well as in case attr does some more mutating stuff
// could be merged back with attr later
function setAttribute (value, attributeSetter) {
for (var that = this, i = 0, length = that.length; i < length; i++) {
attributeSetter.call(
that[i],
value.call ?
value.call(that[i], i) :
value
);
}
return that;
}
/*
PrivateMethod: append
append the nodes in "b" to the array / list "a"
*/
//return different function for IE & Opera to deal with their stupid bloody expandos. Pah.
if (document.all) {
append = function(a, b) {
var i = 0,
ai = a.length,
length = b.length;
if (typeof b.length == "number") {
for (; i < length; i++) {
a[ai++] = b[i];
}
} else {
for (; b[i]; i++) {
a[ai++] = b[i];
}
}
};
} else {
append = function(a, b) {
var i = 0, ai = a.length;
for (; b[i]; i++) {
a[ai++] = b[i];
}
};
}
/*
PrivateMethod: isXml
Is this node an XML Document node or within an XML Document node
Arguments:
node
Returns:
Bool
*/
function isXml(node) {
//test for nodes within xml element
return (node.ownerDocument && !node.ownerDocument.body) ||
//test for xml document elements
(node.documentElement && !node.documentElement.body);
}
/*
PrivateMethod: unique
Get an array of nodes without duplicate nodes from an array of nodes.
Arguments:
aNodes - (Array|<NodeList>)
Returns:
An array of nodes without duplicates.
*/
//worth checking if it's an XML document?
if (env.ie) {
unique = function(aNodes) {
if (aNodes.length == 1) { return aNodes; }
//remove duplicates
var r = [],
ri = 0,
i = 0;
for (; aNodes[i]; i++) {
if (aNodes[i].getAttribute(ucheckPropName) != ucheck && aNodes[i].nodeType == 1) {
r[ri++] = aNodes[i];
}
aNodes[i].setAttribute(ucheckPropName, ucheck);
}
for (i=0; aNodes[i]; i++) {
aNodes[i].removeAttribute(ucheckPropName);
}
ucheck++;
return r;
}
} else {
unique = function(aNodes) {
if (aNodes.length == 1) { return aNodes; }
//remove duplicates
var r = [],
ri = 0,
i = 0;
for (; aNodes[i]; i++) {
if (aNodes[i][ucheckPropName] != ucheck && aNodes[i].nodeType == 1) {
r[ri++] = aNodes[i];
}
aNodes[i][ucheckPropName] = ucheck;
}
ucheck++;
return r;
}
}
/*
PrivateMethod: getElementsByTag
Get elements by a specified tag name from a set of context objects. If multiple
context objects are passed, then the resulting array may contain duplicates. See
<unique> to remove duplicate nodes.
Arguments:
tag - (string) Tag name. "*" for all.
contexts - (array) DOM Documents and/or DOM Elements to search in.
Returns:
An array(like) collection of elements with the specified tag name.
*/
if (document.all) { //go the long way around for IE (and Opera)
getByTagName = function(tag, context) {
var r = [], i = 0;
for (; context[i]; i++) {
//need to check .all incase data is XML
//TODO: Drop IE5.5
if (tag == "*" && context[i].all && !isXml(context[i])) { // IE 5.5 doesn't support getElementsByTagName("*")
append(r, context[i].all);
} else {
append(r, context[i].getElementsByTagName(tag));
}
}
return r;
};
} else {
getByTagName = function(tag, context) {
var r = [], i = 0, len = context.length;
for (; i < len; i++) {
append(r, context[i].getElementsByTagName(tag));
}
return r;
};
}
/*
Get the child elements for an html node
*/
function getChildElms(node) {
var r = [],
childNodes = node.childNodes,
i = 0,
ri = 0;
for (; childNodes[i]; i++) {
if (childNodes[i].nodeType == 1 && childNodes[i].nodeName != "!") {
r[ri++] = childNodes[i];
}
}
return r;
}
var horizontalBorderPadding = [
'border-left-width',
'border-right-width',
'padding-left',
'padding-right'
],
verticalBorderPadding = [
'border-top-width',
'border-bottom-width',
'padding-top',
'padding-bottom'
];
/*
PrivateMethod: getElmDimension
Gets the size of an element as an integer, not including padding or border
*/
function getElmDimension(elm, cssProp /* (width|height) */) {
var r, // val to return
docElmOrBody = env.standardsMode ? docElm : docBody,
isWidth = (cssProp == "width"),
cssPropCaps = isWidth ? "Width" : "Height",
cssBorderPadding;
if (elm.window) { // is window
r = env.webkit < 522.11 ? (isWidth ? elm.innerWidth : elm.innerHeight) :
env.webkit ? (isWidth ? docBody.clientWidth : elm.innerHeight) :
env.opera < 9.5 ? (isWidth ? docBody.clientWidth : docBody.clientHeight) :
/* else */ (isWidth ? docElmOrBody.clientWidth : docElmOrBody.clientHeight);
}
else if (elm.getElementById) { // is document
r = Math.max(
docBody["scroll" + cssPropCaps],
docBody["offset" + cssPropCaps],
docElm["client" + cssPropCaps],
docElm["offset" + cssPropCaps],
docElm["scroll" + cssPropCaps]
)
}
else {
// get an array of css borders & padding
cssBorderPadding = isWidth ? horizontalBorderPadding : verticalBorderPadding;
r = elm['offset' + cssPropCaps] - parseInt( getCssValue(elm, cssBorderPadding) );
}
return r;
}
/*
PrivateMethod: getBodyElm
Gets the body elm for a given element. Gets around IE5.5 nonsense. do getBodyElm(elm).parentNode to get documentElement
*/
function getBodyElm(elm) {
if (env.ie < 6) {
return elm.document.body;
} else {
return elm.ownerDocument.body;
}
}
/*
PrivateMethod: setElmsSize
Set element's size
Arguments:
elms - (<NodeList>) Elements
val - (Mixed) Set element height / width. In px unless stated
type - (String) "height" or "width"
Returns:
Nowt.
*/
function setElmsSize(elms, val, type) {
if (typeof val == "number" || /\d$/.test(val)) {
val += "px";
}
for (var i = 0, len = elms.length; i < len; i++) {
elms[i].style[type] = val;
}
}
/*
PrivateMethod: toStyleProp
Converts a css property name into its javascript name, such as "background-color" to "backgroundColor".
Arguments:
prop - (String) CSS Property name
Returns:
String, javascript style property name
*/
function toStyleProp(prop) {
if (prop == "float") {
return env.ie ? "styleFloat" : "cssFloat";
}
return lang.replace(prop, /-(\w)/g, function(match, p1) {
return p1.toUpperCase();
});
}
/*
PrivateMethod: tempBlock
Gives an element display:block (but keeps it hidden) and runs a function, then sets the element back how it was
Arguments:
elm - element
func - function to run
Returns:
Return value of the function
*/
function tempBlock(elm, func) {
//TODO: rather than recording individual style properties, just cache cssText? This was faster for getting the element size
var r,
elmStyle = elm.style,
oldDisp = elmStyle.display,
oldVis = elmStyle.visibility,
oldPos = elmStyle.position;
elmStyle.visibility = "hidden";
elmStyle.position = "absolute";
elmStyle.display = "block";
if (!isVisible(elm)) {
elmStyle.position = oldPos;
r = tempBlock(elm.parentNode, func);
elmStyle.display = oldDisp;
elmStyle.visibility = oldVis;
} else {
r = func();
elmStyle.display = oldDisp;
elmStyle.position = oldPos;
elmStyle.visibility = oldVis;
}
return r;
}
/*
PrivateMethod: isVisible
Is the element visible?
*/
function isVisible(elm) {
//this is a bit of a guess, if there's a better way to do this I'm interested!
return elm.offsetWidth ||
elm.offsetHeight;
}
/*
PrivateMethod: getCssValue
Get a computed css property
Arguments:
elm - element
prop - css property or array of properties to add together
Returns:
String, value
*/
function getCssValue(elm, prop) {
var r, //return value
total = 0,
i = 0,
propLen = prop.length,
compStyle = doc.defaultView && (doc.defaultView.getComputedStyle(elm, null) || doc.defaultView.getComputedStyle),
elmCurrentStyle = elm.currentStyle,
oldDisplay,
match,
propTest = prop.push || cssPropRegex.exec(prop) || [];
if (prop.push) { //multiple properties, add them up
for (; i < propLen; i++) {
total += parseInt( getCssValue(elm, prop[i]), 10 ) || 0;
}
return total + "px";
}
if (propTest[1]) { // is width / height
if (!isVisible(elm)) { //element may be display: none
return tempBlock(elm, function() {
return getElmDimension(elm, propTest[1]) + "px";
});
}
return getElmDimension(elm, propTest[1]) + "px";
}
else if (propTest[2] //is border-*-width
&& glow.env.ie
&& getCssValue(elm, "border-" + propTest[3] + "-style") == "none"
) {
return "0";
}
else if (compStyle) { //W3 Method
//this returns computed values
if (typeof compStyle == "function") {
//safari returns null for compStyle when element is display:none
oldDisplay = elm.style.display;
r = tempBlock(elm, function() {
if (prop == "display") { //get true value for display, since we've just fudged it
elm.style.display = oldDisplay;
if (!doc.defaultView.getComputedStyle(elm, null)) {
return "none";
}
elm.style.display = "block";
}
return getCssValue(elm, prop);
});
} else {
// assume equal horizontal margins in safari 3
// http://bugs.webkit.org/show_bug.cgi?id=13343
// The above bug doesn't appear to be closed, but it works fine in Safari 4
if (env.webkit > 500 && env.webkit < 526 && prop == 'margin-right' && compStyle.getPropertyValue('position') != 'absolute') {
prop = 'margin-left';
}
r = compStyle.getPropertyValue(prop);
}
} else if (elmCurrentStyle) { //IE method
if (prop == "opacity") {
match = /alpha\(opacity=([^\)]+)\)/.exec(elmCurrentStyle.filter);
return match ? String(parseInt(match[1], 10) / 100) : "1";
}
//this returns cascaded values so needs fixing
r = String(elmCurrentStyle[toStyleProp(prop)]);
if (/^-?[\d\.]+(?!px)[%a-z]+$/i.test(r) && prop != "font-size") {
r = getPixelValue(elm, r, usesYAxis.test(prop)) + "px";
}
}
//some results need post processing
if (prop.indexOf("color") != -1) { //deal with colour values
r = normaliseCssColor(r).toString();
} else if (r.indexOf("url") == 0) { //some browsers put quotes around the url, get rid
r = r.replace(/\"/g,"");
}
return r;
}
/*
PrivateMethod: getPixelValue
Converts a relative value into an absolute pixel value. Only works in IE with Dimension value (not stuff like relative font-size).
Based on some Dean Edwards' code
Arguments:
element - element used to calculate relative values
value - (string) relative value
useYAxis - (string) calulate relative values to the y axis rather than x
Returns:
Number
*/
function getPixelValue(element, value, useYAxis) {
// Remember the original values
var axisPos = useYAxis ? "top" : "left",
axisPosUpper = useYAxis ? "Top" : "Left",
elmStyle = element.style,
positionVal = elmStyle[axisPos],
runtimePositionVal = element.runtimeStyle[axisPos],
r;
// copy to the runtime type to prevent changes to the display
element.runtimeStyle[axisPos] = element.currentStyle[axisPos];
// set value to left / top
elmStyle[axisPos] = value;
// get the pixel value
r = elmStyle["pixel" + axisPosUpper];
// revert values
elmStyle[axisPos] = positionVal;
element.runtimeStyle[axisPos] = runtimePositionVal;
return r;
}
/*
PrivateMethod: normaliseCssColor
Converts a CSS colour into "rgb(255, 255, 255)" or "transparent" format
*/
function normaliseCssColor(val) {
if (/^(transparent|rgba\(0, ?0, ?0, ?0\))$/.test(val)) { return 'transparent'; }
var match, //tmp regex match holder
r, g, b, //final colour vals
hex, //tmp hex holder
mathRound = Math.round,
parseIntFunc = parseInt,
parseFloatFunc = parseFloat;
if (match = colorRegex.exec(val)) { //rgb() format, cater for percentages
r = match[2] ? mathRound(((parseFloatFunc(match[1]) / 100) * 255)) : parseIntFunc(match[1]);
g = match[4] ? mathRound(((parseFloatFunc(match[3]) / 100) * 255)) : parseIntFunc(match[3]);
b = match[6] ? mathRound(((parseFloatFunc(match[5]) / 100) * 255)) : parseIntFunc(match[5]);
} else {
if (typeof val == "number") {
hex = val;
} else if (val.charAt(0) == "#") {
if (val.length == "4") { //deal with #fff shortcut
val = "#" + val.charAt(1) + val.charAt(1) + val.charAt(2) + val.charAt(2) + val.charAt(3) + val.charAt(3);
}
hex = parseIntFunc(val.slice(1), 16);
} else {
hex = htmlColorNames[val];
}
r = (hex) >> 16;
g = (hex & 0x00ff00) >> 8;
b = (hex & 0x0000ff);
}
val = new String("rgb(" + r + ", " + g + ", " + b + ")");
val.r = r;
val.g = g;
val.b = b;
return val;
}
/*
PrivateMethod: getTextNodeConcat
Take an element and returns a string of its text nodes combined. This is useful when a browser (Safari 2) goes mad and doesn't return anything for innerText or textContent
*/
function getTextNodeConcat(elm) {
var r = "",
nodes = elm.childNodes,
i = 0,
len = nodes.length;
for (; i < len; i++) {
if (nodes[i].nodeType == 3) { //text
//this function is used in safari only, string concatination is faster
r += nodes[i].nodeValue;
} else if (nodes[i].nodeType == 1) { //element
r += getTextNodeConcat(nodes[i]);
}
}
return r;
}
/*
PrivateMethod: getNextOrPrev
This gets the next / previous sibling element of each node in a nodeset
and returns the new nodeset.
*/
function getNextOrPrev(nodelist, dir /* "next" or "previous" */) {
var ret = [],
ri = 0,
nextTmp,
i = 0,
length = nodelist.length;
for (; i < length; i++) {
nextTmp = nodelist[i];
while (nextTmp = nextTmp[dir + "Sibling"]) {
if (nextTmp.nodeType == 1 && nextTmp.nodeName != "!") {
ret[ri++] = nextTmp;
break;
}
}
}
return r.get(ret);
}
/*
Get the 'real' positioned parent for an element, otherwise return null.
*/
function getPositionedParent(elm) {
var offsetParent = elm.offsetParent;
// get the real positioned parent
// IE places elements with hasLayout in the offsetParent chain even if they're position:static
// Also, <body> and <html> can appear in the offsetParent chain, but we don't want to return them if they're position:static
while (offsetParent && r.get(offsetParent).css("position") == "static") {
offsetParent = offsetParent.offsetParent;
}
// sometimes the <html> element doesn't appear in the offsetParent chain, even if it has position:relative
if (!offsetParent && r.get(docElm).css("position") != "static") {
offsetParent = docElm;
}
return offsetParent || null;
}
/**
@name glow.dom-getScrollOffset
@private
@description Get the scrollTop / scrollLeft of a particular element
@param {Element} elm Element (or window object) to get the scroll position of
@param {Boolean} isLeft True if we're dealing with left scrolling, otherwise top
*/
function getScrollOffset(elm, isLeft) {
var r,
scrollProp = 'scroll' + (isLeft ? "Left" : "Top");
// are we dealing with the window object or the document object?
if (elm.window) {
// get the scroll of the documentElement or the pageX/Yoffset
// - some browsers use one but not the other
r = elm.document.documentElement[scrollProp]
|| (isLeft ? elm.pageXOffset : elm.pageYOffset)
|| 0;
}
else {
r = elm[scrollProp];
}
return r;
}
/**
@name glow.dom-setScrollOffset
@private
@description Set the scrollTop / scrollLeft of a particular element
@param {Element} elm Element (or window object) to get the scroll position of
@param {Boolean} isLeft True if we're dealing with left scrolling, otherwise top
@param {Number} newVal New scroll value
*/
function setScrollOffset(elm, isLeft, newVal) {
// are we dealing with the window object or the document object?
if (elm.window) {
// we need to get whichever value we're not setting
elm.scrollTo(
isLeft ? newVal : getScrollOffset(elm, true),
!isLeft ? newVal : getScrollOffset(elm, false)
);
}
else {
elm['scroll' + (isLeft ? "Left" : "Top")] = newVal;
}
}
/**
@name glow.dom-scrollOffset
@private
@description Set/get the scrollTop / scrollLeft of a NodeList
@param {glow.dom.NodeList} nodeList Elements to get / set the position of
@param {Boolean} isLeft True if we're dealing with left scrolling, otherwise top
@param {Number} [val] Val to set (if not provided, we'll get the value)
@returns NodeList for sets, Number for gets
*/
function scrollOffset(nodeList, isLeft, val) {
var i = nodeList.length;
if (val !== undefined) {
while (i--) {
setScrollOffset(nodeList[i], isLeft, val);
}
return nodeList;
} else {
return getScrollOffset(nodeList[0], isLeft);
}
}
//public
var r = {}; //object to be returned
/**
@name glow.dom.get
@function
@description Returns a {@link glow.dom.NodeList NodeList} from CSS selectors and/or Elements.
@param {String | String[] | Element | Element[] | glow.dom.NodeList} nodespec+ One or more CSS selector strings, Elements or {@link glow.dom.NodeList NodeLists}.
Will also accept arrays of these types, or any combinations thereof.
Supported CSS selectors:
<ul>
<li>Universal selector "*".</li>
<li>Type selector "div"</li>
<li>Class selector ".myClass"</li>
<li>ID selector "#myDiv"</li>
<li>Child selector "ul > li"</li>
<li>Grouping "div, p"</li>
</ul>
@returns {glow.dom.NodeList}
@example
// Nodelist with all links in element with id "nav"
var myNodeList = glow.dom.get("#nav a");
@example
// NodeList containing the nodes passed in
var myNodeList = glow.dom.get(someNode, anotherNode);
@example
// NodeList containing elements in the first form
var myNodeList = glow.dom.get(document.forms[0].elements);
*/
r.get = function() {
var r = new glow.dom.NodeList(),
i = 0,
args = arguments,
argsLen = args.length;
for (; i < argsLen; i++) {
if (typeof args[i] == "string") {
r.push(new glow.dom.NodeList().push(doc).get(args[i]));
} else {
r.push(args[i]);
}
}
return r;
};
/**
@name glow.dom.create
@function
@description Returns a {@link glow.dom.NodeList NodeList} from an HTML fragment.
@param {String} html An HTML string.
All top-level nodes must be elements (i.e. text content in the
HTML must be wrapped in HTML tags).
@param {Object} [opts] An optional options object
@param {Object} [opts.interpolate] Data for a call to {@link glow.lang.interpolate}
If this option is set, the String html parameter will be passed through glow.lang.interpolate with this as the data and no options
If glow.lang.interpolates options are required, an explicit call must be made
@param {Boolean} [opts.escapeHtml] Escape HTML in the interpolate data object.
See {@link glow.lang.interpolate}
@returns {glow.dom.NodeList}
@example
// NodeList of two elements
var myNodeList = glow.dom.create("<div>Hello</div><div>World</div>");
@example
// Nodelist of one list item
var listItem = glow.dom.create('<li>{content}</li>', {
interpolate: {content: textFromUser},
escapeHtml: true
});
// if testFromUser contains HTML, it will be correctly escaped
// before being inserted into the li
*/
r.create = function(sHtml, opts) {
var ret = [],
i = 0,
rLen = 0,