-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
base.js
945 lines (847 loc) · 31.7 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
/**
* @license
* Copyright The Closure Library Authors.
* Copyright The Closure Compiler Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Bootstrap for the Closure Compiler.
*
* Avoid including base.js more than once. This is strictly discouraged and not
* supported. goog.require(...) won't work properly in that case.
*
* Historically, this file originated from the Closure Library that has since
* been deprecated: https://github.com/google/closure-library/issues/1214
*
* This is a supported version released with the Closure Compiler.
*
* @suppress {deprecated} Users cannot remove deprecated uses here.
* @provideGoog
*/
/**
* @define {boolean} Overridden to true by the compiler.
*/
var COMPILED = false;
/**
* 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 || {};
/**
* A hook for overriding the define values in uncompiled or compiled mode.
*
* 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.
*
* @type {!Object<string, (string|number|boolean)>|null|undefined}
*/
globalThis.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 `globalThis`.
* @private
*/
goog.exportPath_ = function(name, object, overwriteImplicit, objectToExportTo) {
var parts = name.split('.');
var cur = objectToExportTo || globalThis;
// 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 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 defines = globalThis.CLOSURE_DEFINES;
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.
*/
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
/**
* Defines a global namespace in Closure.
*
* NOTE: goog.module() is STRONGLY preferred over goog.provide().
*
* 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".
*/
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);
};
/**
* 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.isInModuleLoader_()) {
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];
} else if (!goog.implicitNamespaces_[name]) {
var ns = goog.getObjectByName_(name);
return ns != null ? ns : null;
}
}
return null;
};
/**
* @private {?{
* moduleName: (string|undefined),
* declareLegacyNamespace:boolean,
* }}
*/
goog.moduleLoaderState_ = null;
/**
* @private
* @return {boolean} Whether a goog.module is currently being initialized.
*/
goog.isInModuleLoader_ = function() {
return !!goog.moduleLoaderState_;
};
/**
* 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.isInModuleLoader_()) {
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;
};
/**
* 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) {};
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 satisfies 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
* globalThis.
* @return {?} The value (object or primitive) or, if not found, null.
* @private
*/
goog.getObjectByName_ = function(name, opt_obj) {
var parts = name.split('.');
var cur = opt_obj || globalThis;
for (var i = 0; i < parts.length; i++) {
cur = cur[parts[i]];
if (cur == null) {
return null;
}
}
return cur;
};
/**
* 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 or
* goog.module) in the form "goog.package.part".
* @return {?} The associated namespace or module for use in goog.module or ES6
* module source files. NOT for use in goog.provide source files where using
* this returned value will result in undefined behavior when compiled.
*/
goog.require = function(namespace) {
if (!COMPILED) {
// If the object already exists we do not need to do anything.
if (goog.isProvided_(namespace)) {
return goog.module.getInternal_(namespace);
}
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 or
* goog.module 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 {};
};
/**
* @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);
/**
* The registry of initialized modules:
* The module identifier or path to module exports map.
* @private @const {!Object<string, ?>}
*/
goog.loadedModules_ = {};
/**
* @param {function(?):?} moduleDef The module definition.
*/
goog.loadModule = function(moduleDef) {
var previousState = goog.moduleLoaderState_;
try {
goog.moduleLoaderState_ = {
moduleName: '',
declareLegacyNamespace: false,
};
var origExports = {};
var exports = origExports;
if (typeof moduleDef === 'function') {
exports = moduleDef.call(undefined, exports);
} else {
throw new Error('Invalid module definition');
}
var moduleName = goog.moduleLoaderState_.moduleName;
if (typeof moduleName === 'string' && moduleName) {
// Don't seal legacy namespaces as they may be used as a parent of
// another namespace
if (goog.moduleLoaderState_.declareLegacyNamespace) {
// Whether exports was overwritten via default export assignment.
// This is important for legacy namespaces as it dictates whether a
// previously loaded implicit namespace should be clobbered or not.
var isDefaultExport = origExports !== exports;
goog.constructNamespace_(moduleName, exports, isDefaultExport);
} else if (
goog.SEAL_MODULE_EXPORTS && Object.seal &&
typeof exports == 'object' && exports != null) {
Object.seal(exports);
}
goog.loadedModules_[moduleName] = exports;
} else {
throw new Error('Invalid module name \"' + moduleName + '\"');
}
} finally {
goog.moduleLoaderState_ = previousState;
}
};
/**
* Returns true if the specified value is an object. This includes arrays and
* functions.
* @param {?} val Variable to test.
* @return {boolean} Whether variable is an object.
* @private
*/
goog.isObject_ = function(val) {
var type = typeof val;
return type == 'object' && val != null || type == 'function';
// return Object(val) === val also works, but is slower, especially if val is
// not an object.
};
/**
* Optional map of CSS class names to obfuscated names used with
* goog.getCssName().
* @private {!Object<string, string>|undefined}
* @see goog.setCssNameMapping
*/
goog.cssNameMapping_;
/**
* Optional obfuscation style for CSS class names. Should be set to either
* 'BY_WHOLE' or 'BY_PART' if defined.
* @type {string|undefined}
* @private
* @see goog.setCssNameMapping
*/
goog.cssNameMappingStyle_;
/**
* A hook for modifying the default behavior goog.getCssName. The function
* if present, will receive the standard output of the goog.getCssName as
* its input.
*
* @type {(function(string):string)|undefined}
*/
globalThis.CLOSURE_CSS_NAME_MAP_FN;
/**
* Handles strings that are intended to be used as CSS class names.
*
* This function works in tandem with @see goog.setCssNameMapping.
*
* Without any mapping set, the arguments are simple joined with a hyphen and
* passed through unaltered.
*
* When there is a mapping, there are two possible styles in which these
* mappings are used. In the BY_PART style, each part (i.e. in between hyphens)
* of the passed in css name is rewritten according to the map. In the BY_WHOLE
* style, the full css name is looked up in the map directly. If a rewrite is
* not specified by the map, the compiler will output a warning.
*
* When the mapping is passed to the compiler, it will replace calls to
* goog.getCssName with the strings from the mapping, e.g.
* var x = goog.getCssName('foo');
* var y = goog.getCssName(this.baseClass, 'active');
* becomes:
* var x = 'foo';
* var y = this.baseClass + '-active';
*
* If one argument is passed it will be processed, if two are passed only the
* modifier will be processed, as it is assumed the first argument was generated
* as a result of calling goog.getCssName.
*
* @param {string} className The class name.
* @param {string=} opt_modifier A modifier to be appended to the class name.
* @return {string} The class name or the concatenation of the class name and
* the modifier.
*/
goog.getCssName = function(className, opt_modifier) {
// String() is used for compatibility with compiled soy where the passed
// className can be non-string objects.
if (String(className).charAt(0) == '.') {
throw new Error(
'className passed in goog.getCssName must not start with ".".' +
' You passed: ' + className);
}
var getMapping = function(cssName) {
return goog.cssNameMapping_[cssName] || cssName;
};
var renameByParts = function(cssName) {
// Remap all the parts individually.
var parts = cssName.split('-');
var mapped = [];
for (var i = 0; i < parts.length; i++) {
mapped.push(getMapping(parts[i]));
}
return mapped.join('-');
};
var rename;
if (goog.cssNameMapping_) {
rename =
goog.cssNameMappingStyle_ == 'BY_WHOLE' ? getMapping : renameByParts;
} else {
rename = function(a) {
return a;
};
}
var result =
opt_modifier ? className + '-' + rename(opt_modifier) : rename(className);
// The special CLOSURE_CSS_NAME_MAP_FN allows users to specify further
// processing of the class name.
if (globalThis.CLOSURE_CSS_NAME_MAP_FN) {
return globalThis.CLOSURE_CSS_NAME_MAP_FN(result);
}
return result;
};
/**
* Sets the map to check when returning a value from goog.getCssName(). Example:
* <pre>
* goog.setCssNameMapping({
* "goog": "a",
* "disabled": "b",
* });
*
* var x = goog.getCssName('goog');
* // The following evaluates to: "a a-b".
* goog.getCssName('goog') + ' ' + goog.getCssName(x, 'disabled')
* </pre>
* When declared as a map of string literals to string literals, the JSCompiler
* will replace all calls to goog.getCssName() using the supplied map if the
* --process_closure_primitives flag is set.
*
* @param {!Object} mapping A map of strings to strings where keys are possible
* arguments to goog.getCssName() and values are the corresponding values
* that should be returned.
* @param {string=} opt_style The style of css name mapping. There are two valid
* options: 'BY_PART', and 'BY_WHOLE'.
* @see goog.getCssName for a description.
*/
goog.setCssNameMapping = function(mapping, opt_style) {
goog.cssNameMapping_ = mapping;
goog.cssNameMappingStyle_ = opt_style;
};
/**
* To use CSS renaming in compiled mode, one of the input files should have a
* call to goog.setCssNameMapping() with an object literal that the JSCompiler
* can extract and use to replace all calls to goog.getCssName(). In uncompiled
* mode, JavaScript code should be loaded before this base.js file that declares
* a global variable, CLOSURE_CSS_NAME_MAPPING, which is used below. This is
* to ensure that the mapping is loaded before any calls to goog.getCssName()
* are made in uncompiled mode.
*
* A hook for overriding the CSS name mapping.
* @type {!Object<string, string>|undefined}
*/
globalThis.CLOSURE_CSS_NAME_MAPPING;
if (!COMPILED && globalThis.CLOSURE_CSS_NAME_MAPPING) {
// This does not call goog.setCssNameMapping() because the JSCompiler
// requires that goog.setCssNameMapping() be called with an object literal.
goog.cssNameMapping_ = globalThis.CLOSURE_CSS_NAME_MAPPING;
}
/**
* Options bag type for `goog.getMsg()` third argument.
*
* It is important to note that these options need to be known at compile time,
* so they must always be provided to `goog.getMsg()` as an actual object
* literal in the function call. Otherwise, closure-compiler will report an
* error.
* @record
*/
goog.GetMsgOptions = function() {};
/**
* If `true`, escape '<' in the message string to '<'.
*
* Used by Closure Templates where the generated code size and performance is
* critical which is why {@link goog.html.SafeHtmlFormatter} is not used.
* The value must be literal `true` or `false`.
* @type {boolean|undefined}
*/
goog.GetMsgOptions.prototype.html;
/**
* If `true`, unescape common html entities: >, <, ', " and
* &.
*
* Used for messages not in HTML context, such as with the `textContent`
* property.
* The value must be literal `true` or `false`.
* @type {boolean|undefined}
*/
goog.GetMsgOptions.prototype.unescapeHtmlEntities;
/**
* Associates placeholder names with strings showing how their values are
* obtained.
*
* This field is intended for use in automatically generated JS code.
* Human-written code should use meaningful placeholder names instead.
*
* closure-compiler uses this as the contents of the `<ph>` tag in the
* XMB file it generates or defaults to `-` for historical reasons.
*
* Must be an object literal.
* Ignored at runtime.
* Keys are placeholder names.
* Values are string literals indicating how the value is obtained.
* Typically this is a snippet of source code.
* @type {!Object<string, string>|undefined}
*/
goog.GetMsgOptions.prototype.original_code;
/**
* Associates placeholder names with example values.
*
* closure-compiler uses this as the contents of the `<ex>` tag in the
* XMB file it generates or defaults to `-` for historical reasons.
*
* Must be an object literal.
* Ignored at runtime.
* Keys are placeholder names.
* Values are string literals containing example placeholder values.
* (e.g. "George McFly" for a name placeholder)
* @type {!Object<string, string>|undefined}
*/
goog.GetMsgOptions.prototype.example;
/**
* Gets a localized message.
*
* This function is a compiler primitive. If you give the compiler a localized
* message bundle, it will replace the string at compile-time with a localized
* version, and expand goog.getMsg call to a concatenated string.
*
* Messages must be initialized in the form:
* <code>
* var MSG_NAME = goog.getMsg('Hello {$placeholder}', {'placeholder': 'world'});
* </code>
*
* This function produces a string which should be treated as plain text. Use
* {@link goog.html.SafeHtmlFormatter} in conjunction with goog.getMsg to
* produce SafeHtml.
*
* @param {string} str Translatable string, places holders in the form {$foo}.
* @param {!Object<string, string>=} opt_values Maps place holder name to value.
* @param {!goog.GetMsgOptions=} opt_options see `goog.GetMsgOptions`
* @return {string} message with placeholders filled.
*/
goog.getMsg = function(str, opt_values, opt_options) {
if (opt_options && opt_options.html) {
// Note that '&' is not replaced because the translation can contain HTML
// entities.
str = str.replace(/</g, '<');
}
if (opt_options && opt_options.unescapeHtmlEntities) {
// Note that "&" must be the last to avoid "creating" new entities.
str = str.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/'/g, '\'')
.replace(/"/g, '"')
.replace(/&/g, '&');
}
if (opt_values) {
str = str.replace(/\{\$([^}]+)}/g, function(match, key) {
return (opt_values != null && key in opt_values) ? opt_values[key] :
match;
});
}
return str;
};
/**
* Gets a localized message. If the message does not have a translation, gives a
* fallback message.
*
* This is useful when introducing a new message that has not yet been
* translated into all languages.
*
* This function is a compiler primitive. Must be used in the form:
* <code>var x = goog.getMsgWithFallback(MSG_A, MSG_B);</code>
* where MSG_A and MSG_B were initialized with goog.getMsg.
*
* @param {string} a The preferred message.
* @param {string} b The fallback message.
* @return {string} The best translated message.
*/
goog.getMsgWithFallback = function(a, b) {
return a;
};
/**
* Exposes an unobfuscated global namespace path for the given object.
* Note that fields of the exported object *will* be obfuscated, unless they are
* exported in turn via this function or goog.exportProperty.
*
* Also handy for making public items that are defined in anonymous closures.
*
* ex. goog.exportSymbol('public.path.Foo', Foo);
*
* ex. goog.exportSymbol('public.path.Foo.staticFunction', Foo.staticFunction);
* public.path.Foo.staticFunction();
*
* ex. goog.exportSymbol('public.path.Foo.prototype.myMethod',
* Foo.prototype.myMethod);
* new public.path.Foo().myMethod();
*
* @param {string} publicPath Unobfuscated name to export.
* @param {*} object Object the name should point to.
* @param {?Object=} objectToExportTo The object to add the path to; default
* is globalThis.
*/
goog.exportSymbol = function(publicPath, object, objectToExportTo) {
goog.exportPath_(
publicPath, object, /* overwriteImplicit= */ true, objectToExportTo);
};
/**
* Exports a property unobfuscated into the object's namespace.
* ex. goog.exportProperty(Foo, 'staticFunction', Foo.staticFunction);
* ex. goog.exportProperty(Foo.prototype, 'myMethod', Foo.prototype.myMethod);
* @param {!Object} object Object whose static property is being exported.
* @param {string} publicName Unobfuscated name to export.
* @param {*} symbol Object the name should point to.
*/
goog.exportProperty = function(object, publicName, symbol) {
object[publicName] = symbol;
};