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
/
base.js
3859 lines (3402 loc) · 125 KB
/
base.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 Bootstrap for the Google JS Library (Closure).
*
* In uncompiled mode base.js will attempt to load Closure's deps file, unless
* the global <code>CLOSURE_NO_DEPS</code> is set to true. This allows projects
* to include their own deps file(s) from different locations.
*
* Avoid including base.js more than once. This is strictly discouraged and not
* supported. goog.require(...) won't work properly in that case.
*
* @provideGoog
*/
/**
* @define {boolean} Overridden to true by the compiler.
*/
var COMPILED = false;
/**
* Base namespace for the Closure library. Checks to see goog is already
* defined in the current scope before assigning to prevent clobbering if
* base.js is loaded more than once.
*
* @const
*/
var goog = goog || {};
/**
* Reference to the global object.
* https://www.ecma-international.org/ecma-262/9.0/index.html#sec-global-object
*
* More info on this implementation here:
* https://docs.google.com/document/d/1NAeW4Wk7I7FV0Y2tcUFvQdGMc89k2vdgSXInw8_nvCI/edit
*
* @const
* @suppress {undefinedVars} self won't be referenced unless `this` is falsy.
* @type {!Global}
*/
goog.global =
// Check `this` first for backwards compatibility.
// Valid unless running as an ES module or in a function wrapper called
// without setting `this` properly.
// Note that base.js can't usefully be imported as an ES module, but it may
// be compiled into bundles that are loadable as ES modules.
this ||
// https://developer.mozilla.org/en-US/docs/Web/API/Window/self
// For in-page browser environments and workers.
self;
/**
* A hook for overriding the define values in uncompiled mode.
*
* In uncompiled mode, `CLOSURE_UNCOMPILED_DEFINES` may be defined before
* loading base.js. If a key is defined in `CLOSURE_UNCOMPILED_DEFINES`,
* `goog.define` will use the value instead of the default value. This
* allows flags to be overwritten without compilation (this is normally
* accomplished with the compiler's "define" flag).
*
* Example:
* <pre>
* var CLOSURE_UNCOMPILED_DEFINES = {'goog.DEBUG': false};
* </pre>
*
* @type {Object<string, (string|number|boolean)>|undefined}
*/
goog.global.CLOSURE_UNCOMPILED_DEFINES;
/**
* A hook for overriding the define values in uncompiled or compiled mode,
* like CLOSURE_UNCOMPILED_DEFINES but effective in compiled code. In
* uncompiled code CLOSURE_UNCOMPILED_DEFINES takes precedence.
*
* Also unlike CLOSURE_UNCOMPILED_DEFINES the values must be number, boolean or
* string literals or the compiler will emit an error.
*
* While any @define value may be set, only those set with goog.define will be
* effective for uncompiled code.
*
* Example:
* <pre>
* var CLOSURE_DEFINES = {'goog.DEBUG': false} ;
* </pre>
*
* Currently the Closure Compiler will only recognize very simple definitions of
* this value when looking for values to apply to compiled code and ignore all
* other references. Specifically, it looks the value defined at the variable
* declaration, as with the example above.
*
* TODO(user): Improve the recognized definitions.
*
* @type {!Object<string, (string|number|boolean)>|null|undefined}
*/
goog.global.CLOSURE_DEFINES;
/**
* Builds an object structure for the provided namespace path, ensuring that
* names that already exist are not overwritten. For example:
* "a.b.c" -> a = {};a.b={};a.b.c={};
* Used by goog.provide and goog.exportSymbol.
* @param {string} name The name of the object that this file defines.
* @param {*=} object The object to expose at the end of the path.
* @param {boolean=} overwriteImplicit If object is set and a previous call
* implicitly constructed the namespace given by name, this parameter
* controls whether object should overwrite the implicitly constructed
* namespace or be merged into it. Defaults to false.
* @param {?Object=} objectToExportTo The object to add the path to; if this
* field is not specified, its value defaults to `goog.global`.
* @private
*/
goog.exportPath_ = function(name, object, overwriteImplicit, objectToExportTo) {
var parts = name.split('.');
var cur = objectToExportTo || goog.global;
// Internet Explorer exhibits strange behavior when throwing errors from
// methods externed in this manner. See the testExportSymbolExceptions in
// base_test.html for an example.
if (!(parts[0] in cur) && typeof cur.execScript != 'undefined') {
cur.execScript('var ' + parts[0]);
}
for (var part; parts.length && (part = parts.shift());) {
if (!parts.length && object !== undefined) {
if (!overwriteImplicit && goog.isObject(object) &&
goog.isObject(cur[part])) {
// Merge properties on object (the input parameter) with the existing
// implicitly defined namespace, so as to not clobber previously
// defined child namespaces.
for (var prop in object) {
if (object.hasOwnProperty(prop)) {
cur[part][prop] = object[prop];
}
}
} else {
// Either there is no existing implicit namespace, or overwriteImplicit
// is set to true, so directly assign object (the input parameter) to
// the namespace.
cur[part] = object;
}
} else if (cur[part] && cur[part] !== Object.prototype[part]) {
cur = cur[part];
} else {
cur = cur[part] = {};
}
}
};
/**
* Defines a named value. In uncompiled mode, the value is retrieved from
* CLOSURE_DEFINES or CLOSURE_UNCOMPILED_DEFINES if the object is defined and
* has the property specified, and otherwise used the defined defaultValue.
* When compiled the default can be overridden using the compiler options or the
* value set in the CLOSURE_DEFINES object. Returns the defined value so that it
* can be used safely in modules. Note that the value type MUST be either
* boolean, number, or string.
*
* @param {string} name The distinguished name to provide.
* @param {T} defaultValue
* @return {T} The defined value.
* @template T
*/
goog.define = function(name, defaultValue) {
var value = defaultValue;
if (!COMPILED) {
var uncompiledDefines = goog.global.CLOSURE_UNCOMPILED_DEFINES;
var defines = goog.global.CLOSURE_DEFINES;
if (uncompiledDefines &&
// Anti DOM-clobbering runtime check (b/37736576).
/** @type {?} */ (uncompiledDefines).nodeType === undefined &&
Object.prototype.hasOwnProperty.call(uncompiledDefines, name)) {
value = uncompiledDefines[name];
} else if (
defines &&
// Anti DOM-clobbering runtime check (b/37736576).
/** @type {?} */ (defines).nodeType === undefined &&
Object.prototype.hasOwnProperty.call(defines, name)) {
value = defines[name];
}
}
return value;
};
/**
* @define {number} Integer year indicating the set of browser features that are
* guaranteed to be present. This is defined to include exactly features that
* work correctly on all "modern" browsers that are stable on January 1 of the
* specified year. For example,
* ```js
* if (goog.FEATURESET_YEAR >= 2019) {
* // use APIs known to be available on all major stable browsers Jan 1, 2019
* } else {
* // polyfill for older browsers
* }
* ```
* This is intended to be the primary define for removing
* unnecessary browser compatibility code (such as ponyfills and workarounds),
* and should inform the default value for most other defines:
* ```js
* const ASSUME_NATIVE_PROMISE =
* goog.define('ASSUME_NATIVE_PROMISE', goog.FEATURESET_YEAR >= 2016);
* ```
*
* The default assumption is that IE9 is the lowest supported browser, which was
* first available Jan 1, 2012.
*
* TODO(user): Reference more thorough documentation when it's available.
*/
goog.FEATURESET_YEAR = goog.define('goog.FEATURESET_YEAR', 2012);
/**
* @define {boolean} DEBUG is provided as a convenience so that debugging code
* that should not be included in a production. It can be easily stripped
* by specifying --define goog.DEBUG=false to the Closure Compiler aka
* JSCompiler. For example, most toString() methods should be declared inside an
* "if (goog.DEBUG)" conditional because they are generally used for debugging
* purposes and it is difficult for the JSCompiler to statically determine
* whether they are used.
*/
goog.DEBUG = goog.define('goog.DEBUG', true);
/**
* @define {string} LOCALE defines the locale being used for compilation. It is
* used to select locale specific data to be compiled in js binary. BUILD rule
* can specify this value by "--define goog.LOCALE=<locale_name>" as a compiler
* option.
*
* Take into account that the locale code format is important. You should use
* the canonical Unicode format with hyphen as a delimiter. Language must be
* lowercase, Language Script - Capitalized, Region - UPPERCASE.
* There are few examples: pt-BR, en, en-US, sr-Latin-BO, zh-Hans-CN.
*
* See more info about locale codes here:
* http://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers
*
* For language codes you should use values defined by ISO 693-1. See it here
* http://www.w3.org/WAI/ER/IG/ert/iso639.htm. There is only one exception from
* this rule: the Hebrew language. For legacy reasons the old code (iw) should
* be used instead of the new code (he).
*
*/
goog.LOCALE = goog.define('goog.LOCALE', 'en'); // default to en
/**
* This method is intended to be used for bookkeeping purposes. We would
* like to distinguish uses of goog.LOCALE used for code stripping purposes
* and uses of goog.LOCALE for other uses (such as URL parameters).
*
* This allows us to ban direct uses of goog.LOCALE and to ensure that all
* code has been transformed to our new localization build scheme.
*
* @return {string}
*
*/
goog.getLocale = function() {
return goog.LOCALE;
};
/**
* @define {boolean} Whether this code is running on trusted sites.
*
* On untrusted sites, several native functions can be defined or overridden by
* external libraries like Prototype, Datejs, and JQuery and setting this flag
* to false forces closure to use its own implementations when possible.
*
* If your JavaScript can be loaded by a third party site and you are wary about
* relying on non-standard implementations, specify
* "--define goog.TRUSTED_SITE=false" to the compiler.
*/
goog.TRUSTED_SITE = goog.define('goog.TRUSTED_SITE', true);
/**
* @define {boolean} Whether code that calls {@link goog.setTestOnly} should
* be disallowed in the compilation unit.
*/
goog.DISALLOW_TEST_ONLY_CODE =
goog.define('goog.DISALLOW_TEST_ONLY_CODE', COMPILED && !goog.DEBUG);
/**
* @define {boolean} Whether to use a Chrome app CSP-compliant method for
* loading scripts via goog.require. @see appendScriptSrcNode_.
*/
goog.ENABLE_CHROME_APP_SAFE_SCRIPT_LOADING =
goog.define('goog.ENABLE_CHROME_APP_SAFE_SCRIPT_LOADING', false);
/**
* Defines a namespace in Closure.
*
* A namespace may only be defined once in a codebase. It may be defined using
* goog.provide() or goog.module().
*
* The presence of one or more goog.provide() calls in a file indicates
* that the file defines the given objects/namespaces.
* Provided symbols must not be null or undefined.
*
* In addition, goog.provide() creates the object stubs for a namespace
* (for example, goog.provide("goog.foo.bar") will create the object
* goog.foo.bar if it does not already exist).
*
* Build tools also scan for provide/require/module statements
* to discern dependencies, build dependency files (see deps.js), etc.
*
* @see goog.require
* @see goog.module
* @param {string} name Namespace provided by this file in the form
* "goog.package.part".
* deprecated Use goog.module (see b/159289405)
*/
goog.provide = function(name) {
if (goog.isInModuleLoader_()) {
throw new Error('goog.provide cannot be used within a module.');
}
if (!COMPILED) {
// Ensure that the same namespace isn't provided twice.
// A goog.module/goog.provide maps a goog.require to a specific file
if (goog.isProvided_(name)) {
throw new Error('Namespace "' + name + '" already declared.');
}
}
goog.constructNamespace_(name);
};
/**
* @param {string} name Namespace provided by this file in the form
* "goog.package.part".
* @param {?Object=} object The object to embed in the namespace.
* @param {boolean=} overwriteImplicit If object is set and a previous call
* implicitly constructed the namespace given by name, this parameter
* controls whether opt_obj should overwrite the implicitly constructed
* namespace or be merged into it. Defaults to false.
* @private
*/
goog.constructNamespace_ = function(name, object, overwriteImplicit) {
if (!COMPILED) {
delete goog.implicitNamespaces_[name];
var namespace = name;
while ((namespace = namespace.substring(0, namespace.lastIndexOf('.')))) {
if (goog.getObjectByName(namespace)) {
break;
}
goog.implicitNamespaces_[namespace] = true;
}
}
goog.exportPath_(name, object, overwriteImplicit);
};
/**
* According to the CSP3 spec a nonce must be a valid base64 string.
* @see https://www.w3.org/TR/CSP3/#grammardef-base64-value
* @private @const
*/
goog.NONCE_PATTERN_ = /^[\w+/_-]+[=]{0,2}$/;
/**
* Returns CSP nonce, if set for any script tag.
* @param {?Window=} opt_window The window context used to retrieve the nonce.
* Defaults to global context.
* @return {string} CSP nonce or empty string if no nonce is present.
* @private
*/
goog.getScriptNonce_ = function(opt_window) {
var doc = (opt_window || goog.global).document;
var script = doc.querySelector && doc.querySelector('script[nonce]');
if (script) {
// Try to get the nonce from the IDL property first, because browsers that
// implement additional nonce protection features (currently only Chrome) to
// prevent nonce stealing via CSS do not expose the nonce via attributes.
// See https://github.com/whatwg/html/issues/2369
var nonce = script['nonce'] || script.getAttribute('nonce');
if (nonce && goog.NONCE_PATTERN_.test(nonce)) {
return nonce;
}
}
return '';
};
/**
* Module identifier validation regexp.
* Note: This is a conservative check, it is very possible to be more lenient,
* the primary exclusion here is "/" and "\" and a leading ".", these
* restrictions are intended to leave the door open for using goog.require
* with relative file paths rather than module identifiers.
* @private
*/
goog.VALID_MODULE_RE_ = /^[a-zA-Z_$][a-zA-Z0-9._$]*$/;
/**
* Defines a module in Closure.
*
* Marks that this file must be loaded as a module and claims the namespace.
*
* A namespace may only be defined once in a codebase. It may be defined using
* goog.provide() or goog.module().
*
* goog.module() has three requirements:
* - goog.module may not be used in the same file as goog.provide.
* - goog.module must be the first statement in the file.
* - only one goog.module is allowed per file.
*
* When a goog.module annotated file is loaded, it is enclosed in
* a strict function closure. This means that:
* - any variables declared in a goog.module file are private to the file
* (not global), though the compiler is expected to inline the module.
* - The code must obey all the rules of "strict" JavaScript.
* - the file will be marked as "use strict"
*
* NOTE: unlike goog.provide, goog.module does not declare any symbols by
* itself. If declared symbols are desired, use
* goog.module.declareLegacyNamespace().
*
*
* See the public goog.module proposal: http://goo.gl/Va1hin
*
* @param {string} name Namespace provided by this file in the form
* "goog.package.part", is expected but not required.
* @return {void}
*/
goog.module = function(name) {
if (typeof name !== 'string' || !name ||
name.search(goog.VALID_MODULE_RE_) == -1) {
throw new Error('Invalid module identifier');
}
if (!goog.isInGoogModuleLoader_()) {
throw new Error(
'Module ' + name + ' has been loaded incorrectly. Note, ' +
'modules cannot be loaded as normal scripts. They require some kind of ' +
'pre-processing step. You\'re likely trying to load a module via a ' +
'script tag or as a part of a concatenated bundle without rewriting the ' +
'module. For more info see: ' +
'https://github.com/google/closure-library/wiki/goog.module:-an-ES6-module-like-alternative-to-goog.provide.');
}
if (goog.moduleLoaderState_.moduleName) {
throw new Error('goog.module may only be called once per module.');
}
// Store the module name for the loader.
goog.moduleLoaderState_.moduleName = name;
if (!COMPILED) {
// Ensure that the same namespace isn't provided twice.
// A goog.module/goog.provide maps a goog.require to a specific file
if (goog.isProvided_(name)) {
throw new Error('Namespace "' + name + '" already declared.');
}
delete goog.implicitNamespaces_[name];
}
};
/**
* @param {string} name The module identifier.
* @return {?} The module exports for an already loaded module or null.
*
* Note: This is not an alternative to goog.require, it does not
* indicate a hard dependency, instead it is used to indicate
* an optional dependency or to access the exports of a module
* that has already been loaded.
* @suppress {missingProvide}
*/
goog.module.get = function(name) {
return goog.module.getInternal_(name);
};
/**
* @param {string} name The module identifier.
* @return {?} The module exports for an already loaded module or null.
* @private
*/
goog.module.getInternal_ = function(name) {
if (!COMPILED) {
if (name in goog.loadedModules_) {
return goog.loadedModules_[name].exports;
} else if (!goog.implicitNamespaces_[name]) {
var ns = goog.getObjectByName(name);
return ns != null ? ns : null;
}
}
return null;
};
/**
* Types of modules the debug loader can load.
* @enum {string}
*/
goog.ModuleType = {
ES6: 'es6',
GOOG: 'goog'
};
/**
* @private {?{
* moduleName: (string|undefined),
* declareLegacyNamespace:boolean,
* type: ?goog.ModuleType
* }}
*/
goog.moduleLoaderState_ = null;
/**
* @private
* @return {boolean} Whether a goog.module or an es6 module is currently being
* initialized.
*/
goog.isInModuleLoader_ = function() {
return goog.isInGoogModuleLoader_() || goog.isInEs6ModuleLoader_();
};
/**
* @private
* @return {boolean} Whether a goog.module is currently being initialized.
*/
goog.isInGoogModuleLoader_ = function() {
return !!goog.moduleLoaderState_ &&
goog.moduleLoaderState_.type == goog.ModuleType.GOOG;
};
/**
* @private
* @return {boolean} Whether an es6 module is currently being initialized.
*/
goog.isInEs6ModuleLoader_ = function() {
var inLoader = !!goog.moduleLoaderState_ &&
goog.moduleLoaderState_.type == goog.ModuleType.ES6;
if (inLoader) {
return true;
}
var jscomp = goog.global['$jscomp'];
if (jscomp) {
// jscomp may not have getCurrentModulePath if this is a compiled bundle
// that has some of the runtime, but not all of it. This can happen if
// optimizations are turned on so the unused runtime is removed but renaming
// and Closure pass are off (so $jscomp is still named $jscomp and the
// goog.provide/require calls still exist).
if (typeof jscomp.getCurrentModulePath != 'function') {
return false;
}
// Bundled ES6 module.
return !!jscomp.getCurrentModulePath();
}
return false;
};
/**
* Provide the module's exports as a globally accessible object under the
* module's declared name. This is intended to ease migration to goog.module
* for files that have existing usages.
* @suppress {missingProvide}
*/
goog.module.declareLegacyNamespace = function() {
if (!COMPILED && !goog.isInGoogModuleLoader_()) {
throw new Error(
'goog.module.declareLegacyNamespace must be called from ' +
'within a goog.module');
}
if (!COMPILED && !goog.moduleLoaderState_.moduleName) {
throw new Error(
'goog.module must be called prior to ' +
'goog.module.declareLegacyNamespace.');
}
goog.moduleLoaderState_.declareLegacyNamespace = true;
};
/**
* Associates an ES6 module with a Closure module ID so that is available via
* goog.require. The associated ID acts like a goog.module ID - it does not
* create any global names, it is merely available via goog.require /
* goog.module.get / goog.forwardDeclare / goog.requireType. goog.require and
* goog.module.get will return the entire module as if it was import *'d. This
* allows Closure files to reference ES6 modules for the sake of migration.
*
* @param {string} namespace
* @suppress {missingProvide}
*/
goog.declareModuleId = function(namespace) {
if (!COMPILED) {
if (!goog.isInEs6ModuleLoader_()) {
throw new Error(
'goog.declareModuleId may only be called from ' +
'within an ES6 module');
}
if (goog.moduleLoaderState_ && goog.moduleLoaderState_.moduleName) {
throw new Error(
'goog.declareModuleId may only be called once per module.');
}
if (namespace in goog.loadedModules_) {
throw new Error(
'Module with namespace "' + namespace + '" already exists.');
}
}
if (goog.moduleLoaderState_) {
// Not bundled - debug loading.
goog.moduleLoaderState_.moduleName = namespace;
} else {
// Bundled - not debug loading, no module loader state.
var jscomp = goog.global['$jscomp'];
if (!jscomp || typeof jscomp.getCurrentModulePath != 'function') {
throw new Error(
'Module with namespace "' + namespace +
'" has been loaded incorrectly.');
}
var exports = jscomp.require(jscomp.getCurrentModulePath());
goog.loadedModules_[namespace] = {
exports: exports,
type: goog.ModuleType.ES6,
moduleId: namespace
};
}
};
/**
* Marks that the current file should only be used for testing, and never for
* live code in production.
*
* In the case of unit tests, the message may optionally be an exact namespace
* for the test (e.g. 'goog.stringTest'). The linter will then ignore the extra
* provide (if not explicitly defined in the code).
*
* @param {string=} opt_message Optional message to add to the error that's
* raised when used in production code.
*/
goog.setTestOnly = function(opt_message) {
if (goog.DISALLOW_TEST_ONLY_CODE) {
opt_message = opt_message || '';
throw new Error(
'Importing test-only code into non-debug environment' +
(opt_message ? ': ' + opt_message : '.'));
}
};
/**
* Forward declares a symbol. This is an indication to the compiler that the
* symbol may be used in the source yet is not required and may not be provided
* in compilation.
*
* The most common usage of forward declaration is code that takes a type as a
* function parameter but does not need to require it. By forward declaring
* instead of requiring, no hard dependency is made, and (if not required
* elsewhere) the namespace may never be required and thus, not be pulled
* into the JavaScript binary. If it is required elsewhere, it will be type
* checked as normal.
*
* Before using goog.forwardDeclare, please read the documentation at
* https://github.com/google/closure-compiler/wiki/Bad-Type-Annotation to
* understand the options and tradeoffs when working with forward declarations.
*
* @param {string} name The namespace to forward declare in the form of
* "goog.package.part".
* @deprecated See go/noforwarddeclaration, Use `goog.requireType` instead.
*/
goog.forwardDeclare = function(name) {};
/**
* Forward declare type information. Used to assign types to goog.global
* referenced object that would otherwise result in unknown type references
* and thus block property disambiguation.
*/
goog.forwardDeclare('Document');
goog.forwardDeclare('HTMLScriptElement');
goog.forwardDeclare('XMLHttpRequest');
if (!COMPILED) {
/**
* Check if the given name has been goog.provided. This will return false for
* names that are available only as implicit namespaces.
* @param {string} name name of the object to look for.
* @return {boolean} Whether the name has been provided.
* @private
*/
goog.isProvided_ = function(name) {
return (name in goog.loadedModules_) ||
(!goog.implicitNamespaces_[name] && goog.getObjectByName(name) != null);
};
/**
* Namespaces implicitly defined by goog.provide. For example,
* goog.provide('goog.events.Event') implicitly declares that 'goog' and
* 'goog.events' must be namespaces.
*
* @type {!Object<string, (boolean|undefined)>}
* @private
*/
goog.implicitNamespaces_ = {'goog.module': true};
// NOTE: We add goog.module as an implicit namespace as goog.module is defined
// here and because the existing module package has not been moved yet out of
// the goog.module namespace. This satisifies both the debug loader and
// ahead-of-time dependency management.
}
/**
* Returns an object based on its fully qualified external name. The object
* is not found if null or undefined. If you are using a compilation pass that
* renames property names beware that using this function will not find renamed
* properties.
*
* @param {string} name The fully qualified name.
* @param {Object=} opt_obj The object within which to look; default is
* |goog.global|.
* @return {?} The value (object or primitive) or, if not found, null.
*/
goog.getObjectByName = function(name, opt_obj) {
var parts = name.split('.');
var cur = opt_obj || goog.global;
for (var i = 0; i < parts.length; i++) {
cur = cur[parts[i]];
if (cur == null) {
return null;
}
}
return cur;
};
/**
* Adds a dependency from a file to the files it requires.
* @param {string} relPath The path to the js file.
* @param {!Array<string>} provides An array of strings with
* the names of the objects this file provides.
* @param {!Array<string>} requires An array of strings with
* the names of the objects this file requires.
* @param {boolean|!Object<string>=} opt_loadFlags Parameters indicating
* how the file must be loaded. The boolean 'true' is equivalent
* to {'module': 'goog'} for backwards-compatibility. Valid properties
* and values include {'module': 'goog'} and {'lang': 'es6'}.
*/
goog.addDependency = function(relPath, provides, requires, opt_loadFlags) {
if (!COMPILED && goog.DEPENDENCIES_ENABLED) {
goog.debugLoader_.addDependency(relPath, provides, requires, opt_loadFlags);
}
};
// NOTE(nnaze): The debug DOM loader was included in base.js as an original way
// to do "debug-mode" development. The dependency system can sometimes be
// confusing, as can the debug DOM loader's asynchronous nature.
//
// With the DOM loader, a call to goog.require() is not blocking -- the script
// will not load until some point after the current script. If a namespace is
// needed at runtime, it needs to be defined in a previous script, or loaded via
// require() with its registered dependencies.
//
// User-defined namespaces may need their own deps file. For a reference on
// creating a deps file, see:
// Externally: https://developers.google.com/closure/library/docs/depswriter
//
// Because of legacy clients, the DOM loader can't be easily removed from
// base.js. Work was done to make it disableable or replaceable for
// different environments (DOM-less JavaScript interpreters like Rhino or V8,
// for example). See bootstrap/ for more information.
/**
* @define {boolean} Whether to enable the debug loader.
*
* If enabled, a call to goog.require() will attempt to load the namespace by
* appending a script tag to the DOM (if the namespace has been registered).
*
* If disabled, goog.require() will simply assert that the namespace has been
* provided (and depend on the fact that some outside tool correctly ordered
* the script).
*/
goog.ENABLE_DEBUG_LOADER = goog.define('goog.ENABLE_DEBUG_LOADER', true);
/**
* @param {string} msg
* @private
*/
goog.logToConsole_ = function(msg) {
if (goog.global.console) {
goog.global.console['error'](msg);
}
};
/**
* Implements a system for the dynamic resolution of dependencies that works in
* parallel with the BUILD system.
*
* Note that all calls to goog.require will be stripped by the compiler.
*
* @see goog.provide
* @param {string} namespace Namespace (as was given in goog.provide,
* goog.module, or goog.declareModuleId) in the form
* "goog.package.part".
* @return {?} If called within a goog.module or ES6 module file, the associated
* namespace or module otherwise null.
*/
goog.require = function(namespace) {
if (!COMPILED) {
// Might need to lazy load on old IE.
if (goog.ENABLE_DEBUG_LOADER) {
goog.debugLoader_.requested(namespace);
}
// If the object already exists we do not need to do anything.
if (goog.isProvided_(namespace)) {
if (goog.isInModuleLoader_()) {
return goog.module.getInternal_(namespace);
}
} else if (goog.ENABLE_DEBUG_LOADER) {
var moduleLoaderState = goog.moduleLoaderState_;
goog.moduleLoaderState_ = null;
try {
goog.debugLoader_.load_(namespace);
} finally {
goog.moduleLoaderState_ = moduleLoaderState;
}
}
return null;
}
};
/**
* Requires a symbol for its type information. This is an indication to the
* compiler that the symbol may appear in type annotations, yet it is not
* referenced at runtime.
*
* When called within a goog.module or ES6 module file, the return value may be
* assigned to or destructured into a variable, but it may not be otherwise used
* in code outside of a type annotation.
*
* Note that all calls to goog.requireType will be stripped by the compiler.
*
* @param {string} namespace Namespace (as was given in goog.provide,
* goog.module, or goog.declareModuleId) in the form
* "goog.package.part".
* @return {?}
*/
goog.requireType = function(namespace) {
// Return an empty object so that single-level destructuring of the return
// value doesn't crash at runtime when using the debug loader. Multi-level
// destructuring isn't supported.
return {};
};
/**
* Path for included scripts.
* @type {string}
*/
goog.basePath = '';
/**
* A hook for overriding the base path.
* @type {string|undefined}
*/
goog.global.CLOSURE_BASE_PATH;
/**
* Whether to attempt to load Closure's deps file. By default, when uncompiled,
* deps files will attempt to be loaded.
* @type {boolean|undefined}
*/
goog.global.CLOSURE_NO_DEPS;
/**
* A function to import a single script. This is meant to be overridden when
* Closure is being run in non-HTML contexts, such as web workers. It's defined
* in the global scope so that it can be set before base.js is loaded, which
* allows deps.js to be imported properly.
*
* The first parameter the script source, which is a relative URI. The second,
* optional parameter is the script contents, in the event the script needed
* transformation. It should return true if the script was imported, false
* otherwise.
* @type {(function(string, string=): boolean)|undefined}
*/
goog.global.CLOSURE_IMPORT_SCRIPT;
/**
* Null function used for default values of callbacks, etc.
* @return {void} Nothing.
* @deprecated use '()=>{}' or 'function(){}' instead.
*/
goog.nullFunction = function() {};
/**
* When defining a class Foo with an abstract method bar(), you can do:
* Foo.prototype.bar = goog.abstractMethod
*
* Now if a subclass of Foo fails to override bar(), an error will be thrown
* when bar() is invoked.
*
* @type {!Function}
* @throws {Error} when invoked to indicate the method should be overridden.
* @deprecated Use "@abstract" annotation instead of goog.abstractMethod in new
* code. See
* https://github.com/google/closure-compiler/wiki/@abstract-classes-and-methods
*/
goog.abstractMethod = function() {
throw new Error('unimplemented abstract method');
};
/**
* Adds a `getInstance` static method that always returns the same
* instance object.
* @param {!Function} ctor The constructor for the class to add the static
* method to.
* @suppress {missingProperties} 'instance_' isn't a property on 'Function'
* but we don't have a better type to use here.
*/
goog.addSingletonGetter = function(ctor) {
// instance_ is immediately set to prevent issues with sealed constructors
// such as are encountered when a constructor is returned as the export object
// of a goog.module in unoptimized code.
// Delcare type to avoid conformance violations that ctor.instance_ is unknown
/** @type {undefined|!Object} @suppress {underscore} */
ctor.instance_ = undefined;
ctor.getInstance = function() {
if (ctor.instance_) {
return ctor.instance_;
}
if (goog.DEBUG) {
// NOTE: JSCompiler can't optimize away Array#push.
goog.instantiatedSingletons_[goog.instantiatedSingletons_.length] = ctor;
}
// Cast to avoid conformance violations that ctor.instance_ is unknown
return /** @type {!Object|undefined} */ (ctor.instance_) = new ctor;
};
};
/**
* All singleton classes that have been instantiated, for testing. Don't read
* it directly, use the `goog.testing.singleton` module. The compiler
* removes this variable if unused.
* @type {!Array<!Function>}
* @private
*/
goog.instantiatedSingletons_ = [];
/**
* @define {boolean} Whether to load goog.modules using `eval` when using
* the debug loader. This provides a better debugging experience as the
* source is unmodified and can be edited using Chrome Workspaces or similar.
* However in some environments the use of `eval` is banned
* so we provide an alternative.
*/
goog.LOAD_MODULE_USING_EVAL = goog.define('goog.LOAD_MODULE_USING_EVAL', true);
/**
* @define {boolean} Whether the exports of goog.modules should be sealed when
* possible.
*/
goog.SEAL_MODULE_EXPORTS = goog.define('goog.SEAL_MODULE_EXPORTS', goog.DEBUG);