This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
dom.js
3490 lines (3050 loc) · 114 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
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Utilities for manipulating the browser's Document Object Model
* Inspiration taken *heavily* from mochikit (http://mochikit.com/).
*
* You can use {@link goog.dom.DomHelper} to create new dom helpers that refer
* to a different document object. This is useful if you are working with
* frames or multiple windows.
*
* @suppress {strictMissingProperties}
*/
// TODO(arv): Rename/refactor getTextContent and getRawTextContent. The problem
// is that getTextContent should mimic the DOM3 textContent. We should add a
// getInnerText (or getText) which tries to return the visible text, innerText.
goog.provide('goog.dom');
goog.provide('goog.dom.Appendable');
goog.provide('goog.dom.DomHelper');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.asserts.dom');
goog.require('goog.dom.BrowserFeature');
goog.require('goog.dom.NodeType');
goog.require('goog.dom.TagName');
goog.require('goog.dom.safe');
goog.require('goog.html.SafeHtml');
goog.require('goog.html.uncheckedconversions');
goog.require('goog.math.Coordinate');
goog.require('goog.math.Size');
goog.require('goog.object');
goog.require('goog.string');
goog.require('goog.string.Const');
goog.require('goog.string.Unicode');
goog.require('goog.userAgent');
/**
* @define {boolean} Whether we know at compile time that the browser is in
* quirks mode.
*/
goog.dom.ASSUME_QUIRKS_MODE = goog.define('goog.dom.ASSUME_QUIRKS_MODE', false);
/**
* @define {boolean} Whether we know at compile time that the browser is in
* standards compliance mode.
*/
goog.dom.ASSUME_STANDARDS_MODE =
goog.define('goog.dom.ASSUME_STANDARDS_MODE', false);
/**
* Whether we know the compatibility mode at compile time.
* @type {boolean}
* @private
*/
goog.dom.COMPAT_MODE_KNOWN_ =
goog.dom.ASSUME_QUIRKS_MODE || goog.dom.ASSUME_STANDARDS_MODE;
/**
* Gets the DomHelper object for the document where the element resides.
* @param {(Node|Window)=} opt_element If present, gets the DomHelper for this
* element.
* @return {!goog.dom.DomHelper} The DomHelper.
*/
goog.dom.getDomHelper = function(opt_element) {
'use strict';
return opt_element ?
new goog.dom.DomHelper(goog.dom.getOwnerDocument(opt_element)) :
(goog.dom.defaultDomHelper_ ||
(goog.dom.defaultDomHelper_ = new goog.dom.DomHelper()));
};
/**
* Cached default DOM helper.
* @type {!goog.dom.DomHelper|undefined}
* @private
*/
goog.dom.defaultDomHelper_;
/**
* Gets the document object being used by the dom library.
* @return {!Document} Document object.
*/
goog.dom.getDocument = function() {
'use strict';
return document;
};
/**
* Gets an element from the current document by element id.
*
* If an Element is passed in, it is returned.
*
* @param {string|Element} element Element ID or a DOM node.
* @return {Element} The element with the given ID, or the node passed in.
*/
goog.dom.getElement = function(element) {
'use strict';
return goog.dom.getElementHelper_(document, element);
};
/**
* Gets an HTML element from the current document by element id.
*
* @param {string} id
* @return {?HTMLElement} The element with the given ID or null if no such
* element exists.
*/
goog.dom.getHTMLElement = function(id) {
'use strict'
const element = goog.dom.getElement(id);
if (!element) {
return null;
}
return goog.asserts.dom.assertIsHtmlElement(element);
};
/**
* Gets an element by id from the given document (if present).
* If an element is given, it is returned.
* @param {!Document} doc
* @param {string|Element} element Element ID or a DOM node.
* @return {Element} The resulting element.
* @private
*/
goog.dom.getElementHelper_ = function(doc, element) {
'use strict';
return typeof element === 'string' ? doc.getElementById(element) : element;
};
/**
* Gets an element by id, asserting that the element is found.
*
* This is used when an element is expected to exist, and should fail with
* an assertion error if it does not (if assertions are enabled).
*
* @param {string} id Element ID.
* @return {!Element} The element with the given ID, if it exists.
*/
goog.dom.getRequiredElement = function(id) {
'use strict';
return goog.dom.getRequiredElementHelper_(document, id);
};
/**
* Gets an HTML element by id, asserting that the element is found.
*
* This is used when an element is expected to exist, and should fail with
* an assertion error if it does not (if assertions are enabled).
*
* @param {string} id Element ID.
* @return {!HTMLElement} The element with the given ID, if it exists.
*/
goog.dom.getRequiredHTMLElement = function(id) {
'use strict'
return goog.asserts.dom.assertIsHtmlElement(
goog.dom.getRequiredElementHelper_(document, id));
};
/**
* Helper function for getRequiredElementHelper functions, both static and
* on DomHelper. Asserts the element with the given id exists.
* @param {!Document} doc
* @param {string} id
* @return {!Element} The element with the given ID, if it exists.
* @private
*/
goog.dom.getRequiredElementHelper_ = function(doc, id) {
'use strict';
// To prevent users passing in Elements as is permitted in getElement().
goog.asserts.assertString(id);
var element = goog.dom.getElementHelper_(doc, id);
return goog.asserts.assert(element, 'No element found with id: ' + id);
};
/**
* Alias for getElement.
* @param {string|Element} element Element ID or a DOM node.
* @return {Element} The element with the given ID, or the node passed in.
* @deprecated Use {@link goog.dom.getElement} instead.
*/
goog.dom.$ = goog.dom.getElement;
/**
* Gets elements by tag name.
* @param {!goog.dom.TagName<T>} tagName
* @param {(!Document|!Element)=} opt_parent Parent element or document where to
* look for elements. Defaults to document.
* @return {!NodeList<R>} List of elements. The members of the list are
* {!Element} if tagName is not a member of goog.dom.TagName or more
* specific types if it is (e.g. {!HTMLAnchorElement} for
* goog.dom.TagName.A).
* @template T
* @template R := cond(isUnknown(T), 'Element', T) =:
*/
goog.dom.getElementsByTagName = function(tagName, opt_parent) {
'use strict';
var parent = opt_parent || document;
return parent.getElementsByTagName(String(tagName));
};
/**
* Looks up elements by both tag and class name, using browser native functions
* (`querySelectorAll`, `getElementsByTagName` or
* `getElementsByClassName`) where possible. This function
* is a useful, if limited, way of collecting a list of DOM elements
* with certain characteristics. `querySelectorAll` offers a
* more powerful and general solution which allows matching on CSS3
* selector expressions.
*
* Note that tag names are case sensitive in the SVG namespace, and this
* function converts opt_tag to uppercase for comparisons. For queries in the
* SVG namespace you should use querySelector or querySelectorAll instead.
* https://bugzilla.mozilla.org/show_bug.cgi?id=963870
* https://bugs.webkit.org/show_bug.cgi?id=83438
*
* @see {https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll}
*
* @param {(string|?goog.dom.TagName<T>)=} opt_tag Element tag name.
* @param {?string=} opt_class Optional class name.
* @param {(Document|Element)=} opt_el Optional element to look in.
* @return {!IArrayLike<R>} Array-like list of elements (only a length property
* and numerical indices are guaranteed to exist). The members of the array
* are {!Element} if opt_tag is not a member of goog.dom.TagName or more
* specific types if it is (e.g. {!HTMLAnchorElement} for
* goog.dom.TagName.A).
* @template T
* @template R := cond(isUnknown(T), 'Element', T) =:
*/
goog.dom.getElementsByTagNameAndClass = function(opt_tag, opt_class, opt_el) {
'use strict';
return goog.dom.getElementsByTagNameAndClass_(
document, opt_tag, opt_class, opt_el);
};
/**
* Gets the first element matching the tag and the class.
*
* @param {(string|?goog.dom.TagName<T>)=} opt_tag Element tag name.
* @param {?string=} opt_class Optional class name.
* @param {(Document|Element)=} opt_el Optional element to look in.
* @return {?R} Reference to a DOM node. The return type is {?Element} if
* tagName is a string or a more specific type if it is a member of
* goog.dom.TagName (e.g. {?HTMLAnchorElement} for goog.dom.TagName.A).
* @template T
* @template R := cond(isUnknown(T), 'Element', T) =:
*/
goog.dom.getElementByTagNameAndClass = function(opt_tag, opt_class, opt_el) {
'use strict';
return goog.dom.getElementByTagNameAndClass_(
document, opt_tag, opt_class, opt_el);
};
/**
* Returns a static, array-like list of the elements with the provided
* className.
*
* @param {string} className the name of the class to look for.
* @param {(Document|Element)=} opt_el Optional element to look in.
* @return {!IArrayLike<!Element>} The items found with the class name provided.
*/
goog.dom.getElementsByClass = function(className, opt_el) {
'use strict';
var parent = opt_el || document;
if (goog.dom.canUseQuerySelector_(parent)) {
return parent.querySelectorAll('.' + className);
}
return goog.dom.getElementsByTagNameAndClass_(
document, '*', className, opt_el);
};
/**
* Returns the first element with the provided className.
*
* @param {string} className the name of the class to look for.
* @param {Element|Document=} opt_el Optional element to look in.
* @return {Element} The first item with the class name provided.
*/
goog.dom.getElementByClass = function(className, opt_el) {
'use strict';
var parent = opt_el || document;
var retVal = null;
if (parent.getElementsByClassName) {
retVal = parent.getElementsByClassName(className)[0];
} else {
retVal =
goog.dom.getElementByTagNameAndClass_(document, '*', className, opt_el);
}
return retVal || null;
};
/**
* Returns the first element with the provided className and asserts that it is
* an HTML element.
*
* @param {string} className the name of the class to look for.
* @param {!Element|!Document=} opt_parent Optional element to look in.
* @return {?HTMLElement} The first item with the class name provided.
*/
goog.dom.getHTMLElementByClass = function(className, opt_parent) {
'use strict'
const element = goog.dom.getElementByClass(className, opt_parent);
if (!element) {
return null;
}
return goog.asserts.dom.assertIsHtmlElement(element);
};
/**
* Ensures an element with the given className exists, and then returns the
* first element with the provided className.
*
* @param {string} className the name of the class to look for.
* @param {!Element|!Document=} opt_root Optional element or document to look
* in.
* @return {!Element} The first item with the class name provided.
* @throws {goog.asserts.AssertionError} Thrown if no element is found.
*/
goog.dom.getRequiredElementByClass = function(className, opt_root) {
'use strict';
var retValue = goog.dom.getElementByClass(className, opt_root);
return goog.asserts.assert(
retValue, 'No element found with className: ' + className);
};
/**
* Ensures an element with the given className exists, and then returns the
* first element with the provided className after asserting that it is an
* HTML element.
*
* @param {string} className the name of the class to look for.
* @param {!Element|!Document=} opt_parent Optional element or document to look
* in.
* @return {!HTMLElement} The first item with the class name provided.
*/
goog.dom.getRequiredHTMLElementByClass = function(className, opt_parent) {
'use strict'
const retValue = goog.dom.getElementByClass(className, opt_parent);
goog.asserts.assert(
retValue, 'No HTMLElement found with className: ' + className);
return goog.asserts.dom.assertIsHtmlElement(retValue);
};
/**
* Prefer the standardized (http://www.w3.org/TR/selectors-api/), native and
* fast W3C Selectors API.
* @param {!(Element|Document)} parent The parent document object.
* @return {boolean} whether or not we can use parent.querySelector* APIs.
* @private
*/
goog.dom.canUseQuerySelector_ = function(parent) {
'use strict';
return !!(parent.querySelectorAll && parent.querySelector);
};
/**
* Helper for `getElementsByTagNameAndClass`.
* @param {!Document} doc The document to get the elements in.
* @param {(string|?goog.dom.TagName<T>)=} opt_tag Element tag name.
* @param {?string=} opt_class Optional class name.
* @param {(Document|Element)=} opt_el Optional element to look in.
* @return {!IArrayLike<R>} Array-like list of elements (only a length property
* and numerical indices are guaranteed to exist). The members of the array
* are {!Element} if opt_tag is not a member of goog.dom.TagName or more
* specific types if it is (e.g. {!HTMLAnchorElement} for
* goog.dom.TagName.A).
* @template T
* @template R := cond(isUnknown(T), 'Element', T) =:
* @private
*/
goog.dom.getElementsByTagNameAndClass_ = function(
doc, opt_tag, opt_class, opt_el) {
'use strict';
var parent = opt_el || doc;
var tagName =
(opt_tag && opt_tag != '*') ? String(opt_tag).toUpperCase() : '';
if (goog.dom.canUseQuerySelector_(parent) && (tagName || opt_class)) {
var query = tagName + (opt_class ? '.' + opt_class : '');
return parent.querySelectorAll(query);
}
// Use the native getElementsByClassName if available, under the assumption
// that even when the tag name is specified, there will be fewer elements to
// filter through when going by class than by tag name
if (opt_class && parent.getElementsByClassName) {
var els = parent.getElementsByClassName(opt_class);
if (tagName) {
var arrayLike = {};
var len = 0;
// Filter for specific tags if requested.
for (var i = 0, el; el = els[i]; i++) {
if (tagName == el.nodeName) {
arrayLike[len++] = el;
}
}
arrayLike.length = len;
return /** @type {!IArrayLike<!Element>} */ (arrayLike);
} else {
return els;
}
}
var els = parent.getElementsByTagName(tagName || '*');
if (opt_class) {
var arrayLike = {};
var len = 0;
for (var i = 0, el; el = els[i]; i++) {
var className = el.className;
// Check if className has a split function since SVG className does not.
if (typeof className.split == 'function' &&
goog.array.contains(className.split(/\s+/), opt_class)) {
arrayLike[len++] = el;
}
}
arrayLike.length = len;
return /** @type {!IArrayLike<!Element>} */ (arrayLike);
} else {
return els;
}
};
/**
* Helper for goog.dom.getElementByTagNameAndClass.
*
* @param {!Document} doc The document to get the elements in.
* @param {(string|?goog.dom.TagName<T>)=} opt_tag Element tag name.
* @param {?string=} opt_class Optional class name.
* @param {(Document|Element)=} opt_el Optional element to look in.
* @return {?R} Reference to a DOM node. The return type is {?Element} if
* tagName is a string or a more specific type if it is a member of
* goog.dom.TagName (e.g. {?HTMLAnchorElement} for goog.dom.TagName.A).
* @template T
* @template R := cond(isUnknown(T), 'Element', T) =:
* @private
*/
goog.dom.getElementByTagNameAndClass_ = function(
doc, opt_tag, opt_class, opt_el) {
'use strict';
var parent = opt_el || doc;
var tag = (opt_tag && opt_tag != '*') ? String(opt_tag).toUpperCase() : '';
if (goog.dom.canUseQuerySelector_(parent) && (tag || opt_class)) {
return parent.querySelector(tag + (opt_class ? '.' + opt_class : ''));
}
var elements =
goog.dom.getElementsByTagNameAndClass_(doc, opt_tag, opt_class, opt_el);
return elements[0] || null;
};
/**
* Alias for `getElementsByTagNameAndClass`.
* @param {(string|?goog.dom.TagName<T>)=} opt_tag Element tag name.
* @param {?string=} opt_class Optional class name.
* @param {Element=} opt_el Optional element to look in.
* @return {!IArrayLike<R>} Array-like list of elements (only a length property
* and numerical indices are guaranteed to exist). The members of the array
* are {!Element} if opt_tag is not a member of goog.dom.TagName or more
* specific types if it is (e.g. {!HTMLAnchorElement} for
* goog.dom.TagName.A).
* @template T
* @template R := cond(isUnknown(T), 'Element', T) =:
* @deprecated Use {@link goog.dom.getElementsByTagNameAndClass} instead.
*/
goog.dom.$$ = goog.dom.getElementsByTagNameAndClass;
/**
* Sets multiple properties, and sometimes attributes, on an element. Note that
* properties are simply object properties on the element instance, while
* attributes are visible in the DOM. Many properties map to attributes with the
* same names, some with different names, and there are also unmappable cases.
*
* This method sets properties by default (which means that custom attributes
* are not supported). These are the exeptions (some of which is legacy):
* - "style": Even though this is an attribute name, it is translated to a
* property, "style.cssText". Note that this property sanitizes and formats
* its value, unlike the attribute.
* - "class": This is an attribute name, it is translated to the "className"
* property.
* - "for": This is an attribute name, it is translated to the "htmlFor"
* property.
* - Entries in {@see goog.dom.DIRECT_ATTRIBUTE_MAP_} are set as attributes,
* this is probably due to browser quirks.
* - "aria-*", "data-*": Always set as attributes, they have no property
* counterparts.
*
* @param {Element} element DOM node to set properties on.
* @param {Object} properties Hash of property:value pairs.
* Property values can be strings or goog.string.TypedString values (such as
* goog.html.SafeUrl).
*/
goog.dom.setProperties = function(element, properties) {
'use strict';
goog.object.forEach(properties, function(val, key) {
'use strict';
if (val && typeof val == 'object' && val.implementsGoogStringTypedString) {
val = val.getTypedStringValue();
}
if (key == 'style') {
element.style.cssText = val;
} else if (key == 'class') {
element.className = val;
} else if (key == 'for') {
element.htmlFor = val;
} else if (goog.dom.DIRECT_ATTRIBUTE_MAP_.hasOwnProperty(key)) {
element.setAttribute(goog.dom.DIRECT_ATTRIBUTE_MAP_[key], val);
} else if (
goog.string.startsWith(key, 'aria-') ||
goog.string.startsWith(key, 'data-')) {
element.setAttribute(key, val);
} else {
element[key] = val;
}
});
};
/**
* Map of attributes that should be set using
* element.setAttribute(key, val) instead of element[key] = val. Used
* by goog.dom.setProperties.
*
* @private {!Object<string, string>}
* @const
*/
goog.dom.DIRECT_ATTRIBUTE_MAP_ = {
'cellpadding': 'cellPadding',
'cellspacing': 'cellSpacing',
'colspan': 'colSpan',
'frameborder': 'frameBorder',
'height': 'height',
'maxlength': 'maxLength',
'nonce': 'nonce',
'role': 'role',
'rowspan': 'rowSpan',
'type': 'type',
'usemap': 'useMap',
'valign': 'vAlign',
'width': 'width'
};
/**
* Gets the dimensions of the viewport.
*
* Gecko Standards mode:
* docEl.clientWidth Width of viewport excluding scrollbar.
* win.innerWidth Width of viewport including scrollbar.
* body.clientWidth Width of body element.
*
* docEl.clientHeight Height of viewport excluding scrollbar.
* win.innerHeight Height of viewport including scrollbar.
* body.clientHeight Height of document.
*
* Gecko Backwards compatible mode:
* docEl.clientWidth Width of viewport excluding scrollbar.
* win.innerWidth Width of viewport including scrollbar.
* body.clientWidth Width of viewport excluding scrollbar.
*
* docEl.clientHeight Height of document.
* win.innerHeight Height of viewport including scrollbar.
* body.clientHeight Height of viewport excluding scrollbar.
*
* IE6/7 Standards mode:
* docEl.clientWidth Width of viewport excluding scrollbar.
* win.innerWidth Undefined.
* body.clientWidth Width of body element.
*
* docEl.clientHeight Height of viewport excluding scrollbar.
* win.innerHeight Undefined.
* body.clientHeight Height of document element.
*
* IE5 + IE6/7 Backwards compatible mode:
* docEl.clientWidth 0.
* win.innerWidth Undefined.
* body.clientWidth Width of viewport excluding scrollbar.
*
* docEl.clientHeight 0.
* win.innerHeight Undefined.
* body.clientHeight Height of viewport excluding scrollbar.
*
* Opera 9 Standards and backwards compatible mode:
* docEl.clientWidth Width of viewport excluding scrollbar.
* win.innerWidth Width of viewport including scrollbar.
* body.clientWidth Width of viewport excluding scrollbar.
*
* docEl.clientHeight Height of document.
* win.innerHeight Height of viewport including scrollbar.
* body.clientHeight Height of viewport excluding scrollbar.
*
* WebKit:
* Safari 2
* docEl.clientHeight Same as scrollHeight.
* docEl.clientWidth Same as innerWidth.
* win.innerWidth Width of viewport excluding scrollbar.
* win.innerHeight Height of the viewport including scrollbar.
* frame.innerHeight Height of the viewport excluding scrollbar.
*
* Safari 3 (tested in 522)
*
* docEl.clientWidth Width of viewport excluding scrollbar.
* docEl.clientHeight Height of viewport excluding scrollbar in strict mode.
* body.clientHeight Height of viewport excluding scrollbar in quirks mode.
*
* @param {Window=} opt_window Optional window element to test.
* @return {!goog.math.Size} Object with values 'width' and 'height'.
*/
goog.dom.getViewportSize = function(opt_window) {
'use strict';
// TODO(arv): This should not take an argument
return goog.dom.getViewportSize_(opt_window || window);
};
/**
* Helper for `getViewportSize`.
* @param {Window} win The window to get the view port size for.
* @return {!goog.math.Size} Object with values 'width' and 'height'.
* @private
*/
goog.dom.getViewportSize_ = function(win) {
'use strict';
var doc = win.document;
var el = goog.dom.isCss1CompatMode_(doc) ? doc.documentElement : doc.body;
return new goog.math.Size(el.clientWidth, el.clientHeight);
};
/**
* Calculates the height of the document.
*
* @return {number} The height of the current document.
*/
goog.dom.getDocumentHeight = function() {
'use strict';
return goog.dom.getDocumentHeight_(window);
};
/**
* Calculates the height of the document of the given window.
*
* @param {!Window} win The window whose document height to retrieve.
* @return {number} The height of the document of the given window.
*/
goog.dom.getDocumentHeightForWindow = function(win) {
'use strict';
return goog.dom.getDocumentHeight_(win);
};
/**
* Calculates the height of the document of the given window.
*
* Function code copied from the opensocial gadget api:
* gadgets.window.adjustHeight(opt_height)
*
* @private
* @param {!Window} win The window whose document height to retrieve.
* @return {number} The height of the document of the given window.
*/
goog.dom.getDocumentHeight_ = function(win) {
'use strict';
// NOTE(eae): This method will return the window size rather than the document
// size in webkit quirks mode.
var doc = win.document;
var height = 0;
if (doc) {
// Calculating inner content height is hard and different between
// browsers rendering in Strict vs. Quirks mode. We use a combination of
// three properties within document.body and document.documentElement:
// - scrollHeight
// - offsetHeight
// - clientHeight
// These values differ significantly between browsers and rendering modes.
// But there are patterns. It just takes a lot of time and persistence
// to figure out.
var body = doc.body;
var docEl = /** @type {!HTMLElement} */ (doc.documentElement);
if (!(docEl && body)) {
return 0;
}
// Get the height of the viewport
var vh = goog.dom.getViewportSize_(win).height;
if (goog.dom.isCss1CompatMode_(doc) && docEl.scrollHeight) {
// In Strict mode:
// The inner content height is contained in either:
// document.documentElement.scrollHeight
// document.documentElement.offsetHeight
// Based on studying the values output by different browsers,
// use the value that's NOT equal to the viewport height found above.
height =
docEl.scrollHeight != vh ? docEl.scrollHeight : docEl.offsetHeight;
} else {
// In Quirks mode:
// documentElement.clientHeight is equal to documentElement.offsetHeight
// except in IE. In most browsers, document.documentElement can be used
// to calculate the inner content height.
// However, in other browsers (e.g. IE), document.body must be used
// instead. How do we know which one to use?
// If document.documentElement.clientHeight does NOT equal
// document.documentElement.offsetHeight, then use document.body.
var sh = docEl.scrollHeight;
var oh = docEl.offsetHeight;
if (docEl.clientHeight != oh) {
sh = body.scrollHeight;
oh = body.offsetHeight;
}
// Detect whether the inner content height is bigger or smaller
// than the bounding box (viewport). If bigger, take the larger
// value. If smaller, take the smaller value.
if (sh > vh) {
// Content is larger
height = sh > oh ? sh : oh;
} else {
// Content is smaller
height = sh < oh ? sh : oh;
}
}
}
return height;
};
/**
* Gets the page scroll distance as a coordinate object.
*
* @param {Window=} opt_window Optional window element to test.
* @return {!goog.math.Coordinate} Object with values 'x' and 'y'.
* @deprecated Use {@link goog.dom.getDocumentScroll} instead.
*/
goog.dom.getPageScroll = function(opt_window) {
'use strict';
var win = opt_window || goog.global || window;
return goog.dom.getDomHelper(win.document).getDocumentScroll();
};
/**
* Gets the document scroll distance as a coordinate object.
*
* @return {!goog.math.Coordinate} Object with values 'x' and 'y'.
*/
goog.dom.getDocumentScroll = function() {
'use strict';
return goog.dom.getDocumentScroll_(document);
};
/**
* Helper for `getDocumentScroll`.
*
* @param {!Document} doc The document to get the scroll for.
* @return {!goog.math.Coordinate} Object with values 'x' and 'y'.
* @private
*/
goog.dom.getDocumentScroll_ = function(doc) {
'use strict';
var el = goog.dom.getDocumentScrollElement_(doc);
var win = goog.dom.getWindow_(doc);
if (goog.userAgent.IE && win.pageYOffset != el.scrollTop) {
// The keyboard on IE10 touch devices shifts the page using the pageYOffset
// without modifying scrollTop. For this case, we want the body scroll
// offsets.
return new goog.math.Coordinate(el.scrollLeft, el.scrollTop);
}
return new goog.math.Coordinate(
win.pageXOffset || el.scrollLeft, win.pageYOffset || el.scrollTop);
};
/**
* Gets the document scroll element.
* @return {!Element} Scrolling element.
*/
goog.dom.getDocumentScrollElement = function() {
'use strict';
return goog.dom.getDocumentScrollElement_(document);
};
/**
* Helper for `getDocumentScrollElement`.
* @param {!Document} doc The document to get the scroll element for.
* @return {!Element} Scrolling element.
* @private
*/
goog.dom.getDocumentScrollElement_ = function(doc) {
'use strict';
// Old WebKit needs body.scrollLeft in both quirks mode and strict mode. We
// also default to the documentElement if the document does not have a body
// (e.g. a SVG document).
// Uses http://dev.w3.org/csswg/cssom-view/#dom-document-scrollingelement to
// avoid trying to guess about browser behavior from the UA string.
if (doc.scrollingElement) {
return doc.scrollingElement;
}
if (!goog.userAgent.WEBKIT && goog.dom.isCss1CompatMode_(doc)) {
return doc.documentElement;
}
return doc.body || doc.documentElement;
};
/**
* Gets the window object associated with the given document.
*
* @param {Document=} opt_doc Document object to get window for.
* @return {!Window} The window associated with the given document.
*/
goog.dom.getWindow = function(opt_doc) {
'use strict';
// TODO(arv): This should not take an argument.
return opt_doc ? goog.dom.getWindow_(opt_doc) : window;
};
/**
* Helper for `getWindow`.
*
* @param {!Document} doc Document object to get window for.
* @return {!Window} The window associated with the given document.
* @private
*/
goog.dom.getWindow_ = function(doc) {
'use strict';
return /** @type {!Window} */ (doc.parentWindow || doc.defaultView);
};
/**
* Returns a dom node with a set of attributes. This function accepts varargs
* for subsequent nodes to be added. Subsequent nodes will be added to the
* first node as childNodes.
*
* So:
* <code>createDom(goog.dom.TagName.DIV, null, createDom(goog.dom.TagName.P),
* createDom(goog.dom.TagName.P));</code> would return a div with two child
* paragraphs
*
* This function uses {@link goog.dom.setProperties} to set attributes: the
* `opt_attributes` parameter follows the same rules.
*
* @param {string|!goog.dom.TagName<T>} tagName Tag to create.
* @param {?Object|?Array<string>|string=} opt_attributes If object, then a map
* of name-value pairs for attributes. If a string, then this is the
* className of the new element. If an array, the elements will be joined
* together as the className of the new element.
* @param {...(Object|string|Array|NodeList|null|undefined)} var_args Further
* DOM nodes or strings for text nodes. If one of the var_args is an array
* or NodeList, its elements will be added as childNodes instead.
* @return {R} Reference to a DOM node. The return type is {!Element} if tagName
* is a string or a more specific type if it is a member of
* goog.dom.TagName (e.g. {!HTMLAnchorElement} for goog.dom.TagName.A).
* @template T
* @template R := cond(isUnknown(T), 'Element', T) =:
*/
goog.dom.createDom = function(tagName, opt_attributes, var_args) {
'use strict';
return goog.dom.createDom_(document, arguments);
};
/**
* Helper for `createDom`.
* @param {!Document} doc The document to create the DOM in.
* @param {!Arguments} args Argument object passed from the callers. See
* `goog.dom.createDom` for details.
* @return {!Element} Reference to a DOM node.
* @private
*/
goog.dom.createDom_ = function(doc, args) {
'use strict';
var tagName = String(args[0]);
var attributes = args[1];
var element = goog.dom.createElement_(doc, tagName);
if (attributes) {
if (typeof attributes === 'string') {
element.className = attributes;
} else if (Array.isArray(attributes)) {
element.className = attributes.join(' ');
} else {
goog.dom.setProperties(element, attributes);
}
}
if (args.length > 2) {
goog.dom.append_(doc, element, args, 2);
}
return element;
};
/**
* Appends a node with text or other nodes.
* @param {!Document} doc The document to create new nodes in.
* @param {!Node} parent The node to append nodes to.
* @param {!Arguments} args The values to add. See `goog.dom.append`.
* @param {number} startIndex The index of the array to start from.
* @private
*/
goog.dom.append_ = function(doc, parent, args, startIndex) {
'use strict';
function childHandler(child) {
// TODO(user): More coercion, ala MochiKit?
if (child) {
parent.appendChild(
typeof child === 'string' ? doc.createTextNode(child) : child);
}
}
for (var i = startIndex; i < args.length; i++) {
var arg = args[i];
// TODO(attila): Fix isArrayLike to return false for a text node.
if (goog.isArrayLike(arg) && !goog.dom.isNodeLike(arg)) {
// If the argument is a node list, not a real array, use a clone,
// because forEach can't be used to mutate a NodeList.
goog.array.forEach(
goog.dom.isNodeList(arg) ? goog.array.toArray(arg) : arg,
childHandler);
} else {
childHandler(arg);
}
}
};
/**
* Alias for `createDom`.
* @param {string|!goog.dom.TagName<T>} tagName Tag to create.
* @param {?Object|?Array<string>|string=} opt_attributes If object, then a map
* of name-value pairs for attributes. If a string, then this is the
* className of the new element. If an array, the elements will be joined
* together as the className of the new element.
* @param {...(Object|string|Array|NodeList|null|undefined)} var_args Further
* DOM nodes or strings for text nodes. If one of the var_args is an array,
* its children will be added as childNodes instead.
* @return {R} Reference to a DOM node. The return type is {!Element} if tagName
* is a string or a more specific type if it is a member of
* goog.dom.TagName (e.g. {!HTMLAnchorElement} for goog.dom.TagName.A).
* @template T
* @template R := cond(isUnknown(T), 'Element', T) =:
* @deprecated Use {@link goog.dom.createDom} instead.
*/
goog.dom.$dom = goog.dom.createDom;
/**
* Creates a new element.
* @param {string|!goog.dom.TagName<T>} name Tag to create.
* @return {R} The new element. The return type is {!Element} if name is
* a string or a more specific type if it is a member of goog.dom.TagName
* (e.g. {!HTMLAnchorElement} for goog.dom.TagName.A).
* @template T
* @template R := cond(isUnknown(T), 'Element', T) =:
*/
goog.dom.createElement = function(name) {