Skip to content

Commit 49d9559

Browse files
gkdncopybara-github
authored andcommitted
Switch goog.global to original implementation to maintain existing behavior.
Note to users: this behavior may revert back again in the future so try to avoid depending on it. PiperOrigin-RevId: 724437584
1 parent 6c807c6 commit 49d9559

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

lib/base.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ var goog = goog || {};
4848
* @type {!Global}
4949
* @deprecated Use `globalThis` directly; this version will be deleted.
5050
*/
51-
goog.global = globalThis;
51+
goog.global =
52+
// Check `this` first for backwards compatibility.
53+
// Valid unless running as an ES module or in a function wrapper called
54+
// without setting `this` properly.
55+
// Note that base.js can't usefully be imported as an ES module, but it may
56+
// be compiled into bundles that are loadable as ES modules.
57+
this ||
58+
// https://developer.mozilla.org/en-US/docs/Web/API/Window/self
59+
// For in-page browser environments and workers.
60+
self;
5261

5362

5463
/**
@@ -72,7 +81,7 @@ goog.global = globalThis;
7281
*
7382
* @type {!Object<string, (string|number|boolean)>|null|undefined}
7483
*/
75-
globalThis.CLOSURE_DEFINES;
84+
goog.global.CLOSURE_DEFINES;
7685

7786

7887
/**
@@ -87,12 +96,12 @@ globalThis.CLOSURE_DEFINES;
8796
* controls whether object should overwrite the implicitly constructed
8897
* namespace or be merged into it. Defaults to false.
8998
* @param {?Object=} objectToExportTo The object to add the path to; if this
90-
* field is not specified, its value defaults to `globalThis`.
99+
* field is not specified, its value defaults to `goog.global`.
91100
* @private
92101
*/
93102
goog.exportPath_ = function(name, object, overwriteImplicit, objectToExportTo) {
94103
var parts = name.split('.');
95-
var cur = objectToExportTo || globalThis;
104+
var cur = objectToExportTo || goog.global;
96105

97106
// Internet Explorer exhibits strange behavior when throwing errors from
98107
// methods externed in this manner. See the testExportSymbolExceptions in
@@ -144,7 +153,7 @@ goog.exportPath_ = function(name, object, overwriteImplicit, objectToExportTo) {
144153
goog.define = function(name, defaultValue) {
145154
var value = defaultValue;
146155
if (!COMPILED) {
147-
var defines = globalThis.CLOSURE_DEFINES;
156+
var defines = goog.global.CLOSURE_DEFINES;
148157
if (defines &&
149158
// Anti DOM-clobbering runtime check (b/37736576).
150159
/** @type {?} */ (defines).nodeType === undefined &&
@@ -535,13 +544,13 @@ if (!COMPILED) {
535544
*
536545
* @param {string} name The fully qualified name.
537546
* @param {!Object=} opt_obj The object within which to look; default is
538-
* globalThis.
547+
* goog.global.
539548
* @return {?} The value (object or primitive) or, if not found, null.
540549
* @deprecated Prefer non-reflective access.
541550
*/
542551
goog.getObjectByName = function(name, opt_obj) {
543552
var parts = name.split('.');
544-
var cur = opt_obj || globalThis;
553+
var cur = opt_obj || goog.global;
545554
for (var i = 0; i < parts.length; i++) {
546555
cur = cur[parts[i]];
547556
if (cur == null) {
@@ -701,7 +710,7 @@ goog.cssNameMappingStyle_;
701710
*
702711
* @type {(function(string):string)|undefined}
703712
*/
704-
globalThis.CLOSURE_CSS_NAME_MAP_FN;
713+
goog.global.CLOSURE_CSS_NAME_MAP_FN;
705714

706715

707716
/**
@@ -773,8 +782,8 @@ goog.getCssName = function(className, opt_modifier) {
773782

774783
// The special CLOSURE_CSS_NAME_MAP_FN allows users to specify further
775784
// processing of the class name.
776-
if (globalThis.CLOSURE_CSS_NAME_MAP_FN) {
777-
return globalThis.CLOSURE_CSS_NAME_MAP_FN(result);
785+
if (goog.global.CLOSURE_CSS_NAME_MAP_FN) {
786+
return goog.global.CLOSURE_CSS_NAME_MAP_FN(result);
778787
}
779788

780789
return result;
@@ -822,12 +831,12 @@ goog.setCssNameMapping = function(mapping, opt_style) {
822831
* A hook for overriding the CSS name mapping.
823832
* @type {!Object<string, string>|undefined}
824833
*/
825-
globalThis.CLOSURE_CSS_NAME_MAPPING;
834+
goog.global.CLOSURE_CSS_NAME_MAPPING;
826835

827-
if (!COMPILED && globalThis.CLOSURE_CSS_NAME_MAPPING) {
836+
if (!COMPILED && goog.global.CLOSURE_CSS_NAME_MAPPING) {
828837
// This does not call goog.setCssNameMapping() because the JSCompiler
829838
// requires that goog.setCssNameMapping() be called with an object literal.
830-
goog.cssNameMapping_ = globalThis.CLOSURE_CSS_NAME_MAPPING;
839+
goog.cssNameMapping_ = goog.global.CLOSURE_CSS_NAME_MAPPING;
831840
}
832841

833842

@@ -982,7 +991,7 @@ goog.getMsgWithFallback = function(a, b) {
982991
* @param {string} publicPath Unobfuscated name to export.
983992
* @param {*} object Object the name should point to.
984993
* @param {?Object=} objectToExportTo The object to add the path to; default
985-
* is globalThis.
994+
* is goog.global.
986995
*/
987996
goog.exportSymbol = function(publicPath, object, objectToExportTo) {
988997
goog.exportPath_(

0 commit comments

Comments
 (0)