lib/goog/base.js

1// Copyright 2006 The Closure Library Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS-IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/**
16 * @fileoverview Bootstrap for the Google JS Library (Closure).
17 *
18 * In uncompiled mode base.js will write out Closure's deps file, unless the
19 * global <code>CLOSURE_NO_DEPS</code> is set to true. This allows projects to
20 * include their own deps file(s) from different locations.
21 *
22 * @author arv@google.com (Erik Arvidsson)
23 *
24 * @provideGoog
25 */
26
27
28/**
29 * @define {boolean} Overridden to true by the compiler when --closure_pass
30 * or --mark_as_compiled is specified.
31 */
32var COMPILED = false;
33
34
35/**
36 * Base namespace for the Closure library. Checks to see goog is already
37 * defined in the current scope before assigning to prevent clobbering if
38 * base.js is loaded more than once.
39 *
40 * @const
41 */
42var goog = goog || {};
43
44
45/**
46 * Reference to the global context. In most cases this will be 'window'.
47 */
48goog.global = this;
49
50
51/**
52 * A hook for overriding the define values in uncompiled mode.
53 *
54 * In uncompiled mode, {@code CLOSURE_UNCOMPILED_DEFINES} may be defined before
55 * loading base.js. If a key is defined in {@code CLOSURE_UNCOMPILED_DEFINES},
56 * {@code goog.define} will use the value instead of the default value. This
57 * allows flags to be overwritten without compilation (this is normally
58 * accomplished with the compiler's "define" flag).
59 *
60 * Example:
61 * <pre>
62 * var CLOSURE_UNCOMPILED_DEFINES = {'goog.DEBUG': false};
63 * </pre>
64 *
65 * @type {Object<string, (string|number|boolean)>|undefined}
66 */
67goog.global.CLOSURE_UNCOMPILED_DEFINES;
68
69
70/**
71 * A hook for overriding the define values in uncompiled or compiled mode,
72 * like CLOSURE_UNCOMPILED_DEFINES but effective in compiled code. In
73 * uncompiled code CLOSURE_UNCOMPILED_DEFINES takes precedence.
74 *
75 * Also unlike CLOSURE_UNCOMPILED_DEFINES the values must be number, boolean or
76 * string literals or the compiler will emit an error.
77 *
78 * While any @define value may be set, only those set with goog.define will be
79 * effective for uncompiled code.
80 *
81 * Example:
82 * <pre>
83 * var CLOSURE_DEFINES = {'goog.DEBUG': false} ;
84 * </pre>
85 *
86 * @type {Object<string, (string|number|boolean)>|undefined}
87 */
88goog.global.CLOSURE_DEFINES;
89
90
91/**
92 * Returns true if the specified value is not undefined.
93 * WARNING: Do not use this to test if an object has a property. Use the in
94 * operator instead.
95 *
96 * @param {?} val Variable to test.
97 * @return {boolean} Whether variable is defined.
98 */
99goog.isDef = function(val) {
100 // void 0 always evaluates to undefined and hence we do not need to depend on
101 // the definition of the global variable named 'undefined'.
102 return val !== void 0;
103};
104
105
106/**
107 * Builds an object structure for the provided namespace path, ensuring that
108 * names that already exist are not overwritten. For example:
109 * "a.b.c" -> a = {};a.b={};a.b.c={};
110 * Used by goog.provide and goog.exportSymbol.
111 * @param {string} name name of the object that this file defines.
112 * @param {*=} opt_object the object to expose at the end of the path.
113 * @param {Object=} opt_objectToExportTo The object to add the path to; default
114 * is |goog.global|.
115 * @private
116 */
117goog.exportPath_ = function(name, opt_object, opt_objectToExportTo) {
118 var parts = name.split('.');
119 var cur = opt_objectToExportTo || goog.global;
120
121 // Internet Explorer exhibits strange behavior when throwing errors from
122 // methods externed in this manner. See the testExportSymbolExceptions in
123 // base_test.html for an example.
124 if (!(parts[0] in cur) && cur.execScript) {
125 cur.execScript('var ' + parts[0]);
126 }
127
128 // Certain browsers cannot parse code in the form for((a in b); c;);
129 // This pattern is produced by the JSCompiler when it collapses the
130 // statement above into the conditional loop below. To prevent this from
131 // happening, use a for-loop and reserve the init logic as below.
132
133 // Parentheses added to eliminate strict JS warning in Firefox.
134 for (var part; parts.length && (part = parts.shift());) {
135 if (!parts.length && goog.isDef(opt_object)) {
136 // last part and we have an object; use it
137 cur[part] = opt_object;
138 } else if (cur[part]) {
139 cur = cur[part];
140 } else {
141 cur = cur[part] = {};
142 }
143 }
144};
145
146
147/**
148 * Defines a named value. In uncompiled mode, the value is retrieved from
149 * CLOSURE_DEFINES or CLOSURE_UNCOMPILED_DEFINES if the object is defined and
150 * has the property specified, and otherwise used the defined defaultValue.
151 * When compiled the default can be overridden using the compiler
152 * options or the value set in the CLOSURE_DEFINES object.
153 *
154 * @param {string} name The distinguished name to provide.
155 * @param {string|number|boolean} defaultValue
156 */
157goog.define = function(name, defaultValue) {
158 var value = defaultValue;
159 if (!COMPILED) {
160 if (goog.global.CLOSURE_UNCOMPILED_DEFINES &&
161 Object.prototype.hasOwnProperty.call(
162 goog.global.CLOSURE_UNCOMPILED_DEFINES, name)) {
163 value = goog.global.CLOSURE_UNCOMPILED_DEFINES[name];
164 } else if (goog.global.CLOSURE_DEFINES &&
165 Object.prototype.hasOwnProperty.call(
166 goog.global.CLOSURE_DEFINES, name)) {
167 value = goog.global.CLOSURE_DEFINES[name];
168 }
169 }
170 goog.exportPath_(name, value);
171};
172
173
174/**
175 * @define {boolean} DEBUG is provided as a convenience so that debugging code
176 * that should not be included in a production js_binary can be easily stripped
177 * by specifying --define goog.DEBUG=false to the JSCompiler. For example, most
178 * toString() methods should be declared inside an "if (goog.DEBUG)" conditional
179 * because they are generally used for debugging purposes and it is difficult
180 * for the JSCompiler to statically determine whether they are used.
181 */
182goog.define('goog.DEBUG', true);
183
184
185/**
186 * @define {string} LOCALE defines the locale being used for compilation. It is
187 * used to select locale specific data to be compiled in js binary. BUILD rule
188 * can specify this value by "--define goog.LOCALE=<locale_name>" as JSCompiler
189 * option.
190 *
191 * Take into account that the locale code format is important. You should use
192 * the canonical Unicode format with hyphen as a delimiter. Language must be
193 * lowercase, Language Script - Capitalized, Region - UPPERCASE.
194 * There are few examples: pt-BR, en, en-US, sr-Latin-BO, zh-Hans-CN.
195 *
196 * See more info about locale codes here:
197 * http://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers
198 *
199 * For language codes you should use values defined by ISO 693-1. See it here
200 * http://www.w3.org/WAI/ER/IG/ert/iso639.htm. There is only one exception from
201 * this rule: the Hebrew language. For legacy reasons the old code (iw) should
202 * be used instead of the new code (he), see http://wiki/Main/IIISynonyms.
203 */
204goog.define('goog.LOCALE', 'en'); // default to en
205
206
207/**
208 * @define {boolean} Whether this code is running on trusted sites.
209 *
210 * On untrusted sites, several native functions can be defined or overridden by
211 * external libraries like Prototype, Datejs, and JQuery and setting this flag
212 * to false forces closure to use its own implementations when possible.
213 *
214 * If your JavaScript can be loaded by a third party site and you are wary about
215 * relying on non-standard implementations, specify
216 * "--define goog.TRUSTED_SITE=false" to the JSCompiler.
217 */
218goog.define('goog.TRUSTED_SITE', true);
219
220
221/**
222 * @define {boolean} Whether a project is expected to be running in strict mode.
223 *
224 * This define can be used to trigger alternate implementations compatible with
225 * running in EcmaScript Strict mode or warn about unavailable functionality.
226 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode
227 *
228 */
229goog.define('goog.STRICT_MODE_COMPATIBLE', false);
230
231
232/**
233 * @define {boolean} Whether code that calls {@link goog.setTestOnly} should
234 * be disallowed in the compilation unit.
235 */
236goog.define('goog.DISALLOW_TEST_ONLY_CODE', COMPILED && !goog.DEBUG);
237
238
239/**
240 * @define {boolean} Whether to use a Chrome app CSP-compliant method for
241 * loading scripts via goog.require. @see appendScriptSrcNode_.
242 */
243goog.define('goog.ENABLE_CHROME_APP_SAFE_SCRIPT_LOADING', false);
244
245
246/**
247 * Defines a namespace in Closure.
248 *
249 * A namespace may only be defined once in a codebase. It may be defined using
250 * goog.provide() or goog.module().
251 *
252 * The presence of one or more goog.provide() calls in a file indicates
253 * that the file defines the given objects/namespaces.
254 * Provided symbols must not be null or undefined.
255 *
256 * In addition, goog.provide() creates the object stubs for a namespace
257 * (for example, goog.provide("goog.foo.bar") will create the object
258 * goog.foo.bar if it does not already exist).
259 *
260 * Build tools also scan for provide/require/module statements
261 * to discern dependencies, build dependency files (see deps.js), etc.
262 *
263 * @see goog.require
264 * @see goog.module
265 * @param {string} name Namespace provided by this file in the form
266 * "goog.package.part".
267 */
268goog.provide = function(name) {
269 if (!COMPILED) {
270 // Ensure that the same namespace isn't provided twice.
271 // A goog.module/goog.provide maps a goog.require to a specific file
272 if (goog.isProvided_(name)) {
273 throw Error('Namespace "' + name + '" already declared.');
274 }
275 }
276
277 goog.constructNamespace_(name);
278};
279
280
281/**
282 * @param {string} name Namespace provided by this file in the form
283 * "goog.package.part".
284 * @param {Object=} opt_obj The object to embed in the namespace.
285 * @private
286 */
287goog.constructNamespace_ = function(name, opt_obj) {
288 if (!COMPILED) {
289 delete goog.implicitNamespaces_[name];
290
291 var namespace = name;
292 while ((namespace = namespace.substring(0, namespace.lastIndexOf('.')))) {
293 if (goog.getObjectByName(namespace)) {
294 break;
295 }
296 goog.implicitNamespaces_[namespace] = true;
297 }
298 }
299
300 goog.exportPath_(name, opt_obj);
301};
302
303
304/**
305 * Module identifier validation regexp.
306 * Note: This is a conservative check, it is very possible to be more lenient,
307 * the primary exclusion here is "/" and "\" and a leading ".", these
308 * restrictions are intended to leave the door open for using goog.require
309 * with relative file paths rather than module identifiers.
310 * @private
311 */
312goog.VALID_MODULE_RE_ = /^[a-zA-Z_$][a-zA-Z0-9._$]*$/;
313
314
315/**
316 * Defines a module in Closure.
317 *
318 * Marks that this file must be loaded as a module and claims the namespace.
319 *
320 * A namespace may only be defined once in a codebase. It may be defined using
321 * goog.provide() or goog.module().
322 *
323 * goog.module() has three requirements:
324 * - goog.module may not be used in the same file as goog.provide.
325 * - goog.module must be the first statement in the file.
326 * - only one goog.module is allowed per file.
327 *
328 * When a goog.module annotated file is loaded, it is enclosed in
329 * a strict function closure. This means that:
330 * - any variables declared in a goog.module file are private to the file
331 * (not global), though the compiler is expected to inline the module.
332 * - The code must obey all the rules of "strict" JavaScript.
333 * - the file will be marked as "use strict"
334 *
335 * NOTE: unlike goog.provide, goog.module does not declare any symbols by
336 * itself. If declared symbols are desired, use
337 * goog.module.declareLegacyNamespace().
338 *
339 *
340 * See the public goog.module proposal: http://goo.gl/Va1hin
341 *
342 * @param {string} name Namespace provided by this file in the form
343 * "goog.package.part", is expected but not required.
344 */
345goog.module = function(name) {
346 if (!goog.isString(name) ||
347 !name ||
348 name.search(goog.VALID_MODULE_RE_) == -1) {
349 throw Error('Invalid module identifier');
350 }
351 if (!goog.isInModuleLoader_()) {
352 throw Error('Module ' + name + ' has been loaded incorrectly.');
353 }
354 if (goog.moduleLoaderState_.moduleName) {
355 throw Error('goog.module may only be called once per module.');
356 }
357
358 // Store the module name for the loader.
359 goog.moduleLoaderState_.moduleName = name;
360 if (!COMPILED) {
361 // Ensure that the same namespace isn't provided twice.
362 // A goog.module/goog.provide maps a goog.require to a specific file
363 if (goog.isProvided_(name)) {
364 throw Error('Namespace "' + name + '" already declared.');
365 }
366 delete goog.implicitNamespaces_[name];
367 }
368};
369
370
371/**
372 * @param {string} name The module identifier.
373 * @return {?} The module exports for an already loaded module or null.
374 *
375 * Note: This is not an alternative to goog.require, it does not
376 * indicate a hard dependency, instead it is used to indicate
377 * an optional dependency or to access the exports of a module
378 * that has already been loaded.
379 * @suppress {missingProvide}
380 */
381goog.module.get = function(name) {
382 return goog.module.getInternal_(name);
383};
384
385
386/**
387 * @param {string} name The module identifier.
388 * @return {?} The module exports for an already loaded module or null.
389 * @private
390 */
391goog.module.getInternal_ = function(name) {
392 if (!COMPILED) {
393 if (goog.isProvided_(name)) {
394 // goog.require only return a value with-in goog.module files.
395 return name in goog.loadedModules_ ?
396 goog.loadedModules_[name] :
397 goog.getObjectByName(name);
398 } else {
399 return null;
400 }
401 }
402};
403
404
405/**
406 * @private {?{
407 * moduleName: (string|undefined),
408 * declareTestMethods: boolean
409 * }}
410 */
411goog.moduleLoaderState_ = null;
412
413
414/**
415 * @private
416 * @return {boolean} Whether a goog.module is currently being initialized.
417 */
418goog.isInModuleLoader_ = function() {
419 return goog.moduleLoaderState_ != null;
420};
421
422
423/**
424 * Indicate that a module's exports that are known test methods should
425 * be copied to the global object. This makes the test methods visible to
426 * test runners that inspect the global object.
427 *
428 * TODO(johnlenz): Make the test framework aware of goog.module so
429 * that this isn't necessary. Alternately combine this with goog.setTestOnly
430 * to minimize boiler plate.
431 * @suppress {missingProvide}
432 * @deprecated This approach does not translate to ES6 module syntax, instead
433 * use goog.testing.testSuite to declare the test methods.
434 */
435goog.module.declareTestMethods = function() {
436 if (!goog.isInModuleLoader_()) {
437 throw new Error('goog.module.declareTestMethods must be called from ' +
438 'within a goog.module');
439 }
440 goog.moduleLoaderState_.declareTestMethods = true;
441};
442
443
444/**
445 * Provide the module's exports as a globally accessible object under the
446 * module's declared name. This is intended to ease migration to goog.module
447 * for files that have existing usages.
448 * @suppress {missingProvide}
449 */
450goog.module.declareLegacyNamespace = function() {
451 if (!COMPILED && !goog.isInModuleLoader_()) {
452 throw new Error('goog.module.declareLegacyNamespace must be called from ' +
453 'within a goog.module');
454 }
455 if (!COMPILED && !goog.moduleLoaderState_.moduleName) {
456 throw Error('goog.module must be called prior to ' +
457 'goog.module.declareLegacyNamespace.');
458 }
459 goog.moduleLoaderState_.declareLegacyNamespace = true;
460};
461
462
463/**
464 * Marks that the current file should only be used for testing, and never for
465 * live code in production.
466 *
467 * In the case of unit tests, the message may optionally be an exact namespace
468 * for the test (e.g. 'goog.stringTest'). The linter will then ignore the extra
469 * provide (if not explicitly defined in the code).
470 *
471 * @param {string=} opt_message Optional message to add to the error that's
472 * raised when used in production code.
473 */
474goog.setTestOnly = function(opt_message) {
475 if (goog.DISALLOW_TEST_ONLY_CODE) {
476 opt_message = opt_message || '';
477 throw Error('Importing test-only code into non-debug environment' +
478 (opt_message ? ': ' + opt_message : '.'));
479 }
480};
481
482
483/**
484 * Forward declares a symbol. This is an indication to the compiler that the
485 * symbol may be used in the source yet is not required and may not be provided
486 * in compilation.
487 *
488 * The most common usage of forward declaration is code that takes a type as a
489 * function parameter but does not need to require it. By forward declaring
490 * instead of requiring, no hard dependency is made, and (if not required
491 * elsewhere) the namespace may never be required and thus, not be pulled
492 * into the JavaScript binary. If it is required elsewhere, it will be type
493 * checked as normal.
494 *
495 *
496 * @param {string} name The namespace to forward declare in the form of
497 * "goog.package.part".
498 */
499goog.forwardDeclare = function(name) {};
500
501
502if (!COMPILED) {
503
504 /**
505 * Check if the given name has been goog.provided. This will return false for
506 * names that are available only as implicit namespaces.
507 * @param {string} name name of the object to look for.
508 * @return {boolean} Whether the name has been provided.
509 * @private
510 */
511 goog.isProvided_ = function(name) {
512 return (name in goog.loadedModules_) ||
513 (!goog.implicitNamespaces_[name] &&
514 goog.isDefAndNotNull(goog.getObjectByName(name)));
515 };
516
517 /**
518 * Namespaces implicitly defined by goog.provide. For example,
519 * goog.provide('goog.events.Event') implicitly declares that 'goog' and
520 * 'goog.events' must be namespaces.
521 *
522 * @type {!Object<string, (boolean|undefined)>}
523 * @private
524 */
525 goog.implicitNamespaces_ = {'goog.module': true};
526
527 // NOTE: We add goog.module as an implicit namespace as goog.module is defined
528 // here and because the existing module package has not been moved yet out of
529 // the goog.module namespace. This satisifies both the debug loader and
530 // ahead-of-time dependency management.
531}
532
533
534/**
535 * Returns an object based on its fully qualified external name. The object
536 * is not found if null or undefined. If you are using a compilation pass that
537 * renames property names beware that using this function will not find renamed
538 * properties.
539 *
540 * @param {string} name The fully qualified name.
541 * @param {Object=} opt_obj The object within which to look; default is
542 * |goog.global|.
543 * @return {?} The value (object or primitive) or, if not found, null.
544 */
545goog.getObjectByName = function(name, opt_obj) {
546 var parts = name.split('.');
547 var cur = opt_obj || goog.global;
548 for (var part; part = parts.shift(); ) {
549 if (goog.isDefAndNotNull(cur[part])) {
550 cur = cur[part];
551 } else {
552 return null;
553 }
554 }
555 return cur;
556};
557
558
559/**
560 * Globalizes a whole namespace, such as goog or goog.lang.
561 *
562 * @param {!Object} obj The namespace to globalize.
563 * @param {Object=} opt_global The object to add the properties to.
564 * @deprecated Properties may be explicitly exported to the global scope, but
565 * this should no longer be done in bulk.
566 */
567goog.globalize = function(obj, opt_global) {
568 var global = opt_global || goog.global;
569 for (var x in obj) {
570 global[x] = obj[x];
571 }
572};
573
574
575/**
576 * Adds a dependency from a file to the files it requires.
577 * @param {string} relPath The path to the js file.
578 * @param {!Array<string>} provides An array of strings with
579 * the names of the objects this file provides.
580 * @param {!Array<string>} requires An array of strings with
581 * the names of the objects this file requires.
582 * @param {boolean=} opt_isModule Whether this dependency must be loaded as
583 * a module as declared by goog.module.
584 */
585goog.addDependency = function(relPath, provides, requires, opt_isModule) {
586 if (goog.DEPENDENCIES_ENABLED) {
587 var provide, require;
588 var path = relPath.replace(/\\/g, '/');
589 var deps = goog.dependencies_;
590 for (var i = 0; provide = provides[i]; i++) {
591 deps.nameToPath[provide] = path;
592 deps.pathIsModule[path] = !!opt_isModule;
593 }
594 for (var j = 0; require = requires[j]; j++) {
595 if (!(path in deps.requires)) {
596 deps.requires[path] = {};
597 }
598 deps.requires[path][require] = true;
599 }
600 }
601};
602
603
604
605
606// NOTE(nnaze): The debug DOM loader was included in base.js as an original way
607// to do "debug-mode" development. The dependency system can sometimes be
608// confusing, as can the debug DOM loader's asynchronous nature.
609//
610// With the DOM loader, a call to goog.require() is not blocking -- the script
611// will not load until some point after the current script. If a namespace is
612// needed at runtime, it needs to be defined in a previous script, or loaded via
613// require() with its registered dependencies.
614// User-defined namespaces may need their own deps file. See http://go/js_deps,
615// http://go/genjsdeps, or, externally, DepsWriter.
616// https://developers.google.com/closure/library/docs/depswriter
617//
618// Because of legacy clients, the DOM loader can't be easily removed from
619// base.js. Work is being done to make it disableable or replaceable for
620// different environments (DOM-less JavaScript interpreters like Rhino or V8,
621// for example). See bootstrap/ for more information.
622
623
624/**
625 * @define {boolean} Whether to enable the debug loader.
626 *
627 * If enabled, a call to goog.require() will attempt to load the namespace by
628 * appending a script tag to the DOM (if the namespace has been registered).
629 *
630 * If disabled, goog.require() will simply assert that the namespace has been
631 * provided (and depend on the fact that some outside tool correctly ordered
632 * the script).
633 */
634goog.define('goog.ENABLE_DEBUG_LOADER', true);
635
636
637/**
638 * @param {string} msg
639 * @private
640 */
641goog.logToConsole_ = function(msg) {
642 if (goog.global.console) {
643 goog.global.console['error'](msg);
644 }
645};
646
647
648/**
649 * Implements a system for the dynamic resolution of dependencies that works in
650 * parallel with the BUILD system. Note that all calls to goog.require will be
651 * stripped by the JSCompiler when the --closure_pass option is used.
652 * @see goog.provide
653 * @param {string} name Namespace to include (as was given in goog.provide()) in
654 * the form "goog.package.part".
655 * @return {?} If called within a goog.module file, the associated namespace or
656 * module otherwise null.
657 */
658goog.require = function(name) {
659
660 // If the object already exists we do not need do do anything.
661 if (!COMPILED) {
662 if (goog.ENABLE_DEBUG_LOADER && goog.IS_OLD_IE_) {
663 goog.maybeProcessDeferredDep_(name);
664 }
665
666 if (goog.isProvided_(name)) {
667 if (goog.isInModuleLoader_()) {
668 return goog.module.getInternal_(name);
669 } else {
670 return null;
671 }
672 }
673
674 if (goog.ENABLE_DEBUG_LOADER) {
675 var path = goog.getPathFromDeps_(name);
676 if (path) {
677 goog.included_[path] = true;
678 goog.writeScripts_();
679 return null;
680 }
681 }
682
683 var errorMessage = 'goog.require could not find: ' + name;
684 goog.logToConsole_(errorMessage);
685
686 throw Error(errorMessage);
687 }
688};
689
690
691/**
692 * Path for included scripts.
693 * @type {string}
694 */
695goog.basePath = '';
696
697
698/**
699 * A hook for overriding the base path.
700 * @type {string|undefined}
701 */
702goog.global.CLOSURE_BASE_PATH;
703
704
705/**
706 * Whether to write out Closure's deps file. By default, the deps are written.
707 * @type {boolean|undefined}
708 */
709goog.global.CLOSURE_NO_DEPS;
710
711
712/**
713 * A function to import a single script. This is meant to be overridden when
714 * Closure is being run in non-HTML contexts, such as web workers. It's defined
715 * in the global scope so that it can be set before base.js is loaded, which
716 * allows deps.js to be imported properly.
717 *
718 * The function is passed the script source, which is a relative URI. It should
719 * return true if the script was imported, false otherwise.
720 * @type {(function(string): boolean)|undefined}
721 */
722goog.global.CLOSURE_IMPORT_SCRIPT;
723
724
725/**
726 * Null function used for default values of callbacks, etc.
727 * @return {void} Nothing.
728 */
729goog.nullFunction = function() {};
730
731
732
733/**
734 * When defining a class Foo with an abstract method bar(), you can do:
735 * Foo.prototype.bar = goog.abstractMethod
736 *
737 * Now if a subclass of Foo fails to override bar(), an error will be thrown
738 * when bar() is invoked.
739 *
740 * Note: This does not take the name of the function to override as an argument
741 * because that would make it more difficult to obfuscate our JavaScript code.
742 *
743 * @type {!Function}
744 * @throws {Error} when invoked to indicate the method should be overridden.
745 */
746goog.abstractMethod = function() {
747 throw Error('unimplemented abstract method');
748};
749
750
751/**
752 * Adds a {@code getInstance} static method that always returns the same
753 * instance object.
754 * @param {!Function} ctor The constructor for the class to add the static
755 * method to.
756 */
757goog.addSingletonGetter = function(ctor) {
758 ctor.getInstance = function() {
759 if (ctor.instance_) {
760 return ctor.instance_;
761 }
762 if (goog.DEBUG) {
763 // NOTE: JSCompiler can't optimize away Array#push.
764 goog.instantiatedSingletons_[goog.instantiatedSingletons_.length] = ctor;
765 }
766 return ctor.instance_ = new ctor;
767 };
768};
769
770
771/**
772 * All singleton classes that have been instantiated, for testing. Don't read
773 * it directly, use the {@code goog.testing.singleton} module. The compiler
774 * removes this variable if unused.
775 * @type {!Array<!Function>}
776 * @private
777 */
778goog.instantiatedSingletons_ = [];
779
780
781/**
782 * @define {boolean} Whether to load goog.modules using {@code eval} when using
783 * the debug loader. This provides a better debugging experience as the
784 * source is unmodified and can be edited using Chrome Workspaces or similar.
785 * However in some environments the use of {@code eval} is banned
786 * so we provide an alternative.
787 */
788goog.define('goog.LOAD_MODULE_USING_EVAL', true);
789
790
791/**
792 * @define {boolean} Whether the exports of goog.modules should be sealed when
793 * possible.
794 */
795goog.define('goog.SEAL_MODULE_EXPORTS', goog.DEBUG);
796
797
798/**
799 * The registry of initialized modules:
800 * the module identifier to module exports map.
801 * @private @const {!Object<string, ?>}
802 */
803goog.loadedModules_ = {};
804
805
806/**
807 * True if goog.dependencies_ is available.
808 * @const {boolean}
809 */
810goog.DEPENDENCIES_ENABLED = !COMPILED && goog.ENABLE_DEBUG_LOADER;
811
812
813if (goog.DEPENDENCIES_ENABLED) {
814 /**
815 * Object used to keep track of urls that have already been added. This record
816 * allows the prevention of circular dependencies.
817 * @private {!Object<string, boolean>}
818 */
819 goog.included_ = {};
820
821
822 /**
823 * This object is used to keep track of dependencies and other data that is
824 * used for loading scripts.
825 * @private
826 * @type {{
827 * pathIsModule: !Object<string, boolean>,
828 * nameToPath: !Object<string, string>,
829 * requires: !Object<string, !Object<string, boolean>>,
830 * visited: !Object<string, boolean>,
831 * written: !Object<string, boolean>,
832 * deferred: !Object<string, string>
833 * }}
834 */
835 goog.dependencies_ = {
836 pathIsModule: {}, // 1 to 1
837
838 nameToPath: {}, // 1 to 1
839
840 requires: {}, // 1 to many
841
842 // Used when resolving dependencies to prevent us from visiting file twice.
843 visited: {},
844
845 written: {}, // Used to keep track of script files we have written.
846
847 deferred: {} // Used to track deferred module evaluations in old IEs
848 };
849
850
851 /**
852 * Tries to detect whether is in the context of an HTML document.
853 * @return {boolean} True if it looks like HTML document.
854 * @private
855 */
856 goog.inHtmlDocument_ = function() {
857 var doc = goog.global.document;
858 return typeof doc != 'undefined' &&
859 'write' in doc; // XULDocument misses write.
860 };
861
862
863 /**
864 * Tries to detect the base path of base.js script that bootstraps Closure.
865 * @private
866 */
867 goog.findBasePath_ = function() {
868 if (goog.global.CLOSURE_BASE_PATH) {
869 goog.basePath = goog.global.CLOSURE_BASE_PATH;
870 return;
871 } else if (!goog.inHtmlDocument_()) {
872 return;
873 }
874 var doc = goog.global.document;
875 var scripts = doc.getElementsByTagName('SCRIPT');
876 // Search backwards since the current script is in almost all cases the one
877 // that has base.js.
878 for (var i = scripts.length - 1; i >= 0; --i) {
879 var script = /** @type {!HTMLScriptElement} */ (scripts[i]);
880 var src = script.src;
881 var qmark = src.lastIndexOf('?');
882 var l = qmark == -1 ? src.length : qmark;
883 if (src.substr(l - 7, 7) == 'base.js') {
884 goog.basePath = src.substr(0, l - 7);
885 return;
886 }
887 }
888 };
889
890
891 /**
892 * Imports a script if, and only if, that script hasn't already been imported.
893 * (Must be called at execution time)
894 * @param {string} src Script source.
895 * @param {string=} opt_sourceText The optionally source text to evaluate
896 * @private
897 */
898 goog.importScript_ = function(src, opt_sourceText) {
899 var importScript = goog.global.CLOSURE_IMPORT_SCRIPT ||
900 goog.writeScriptTag_;
901 if (importScript(src, opt_sourceText)) {
902 goog.dependencies_.written[src] = true;
903 }
904 };
905
906
907 /** @const @private {boolean} */
908 goog.IS_OLD_IE_ = !goog.global.atob && goog.global.document &&
909 goog.global.document.all;
910
911
912 /**
913 * Given a URL initiate retrieval and execution of the module.
914 * @param {string} src Script source URL.
915 * @private
916 */
917 goog.importModule_ = function(src) {
918 // In an attempt to keep browsers from timing out loading scripts using
919 // synchronous XHRs, put each load in its own script block.
920 var bootstrap = 'goog.retrieveAndExecModule_("' + src + '");';
921
922 if (goog.importScript_('', bootstrap)) {
923 goog.dependencies_.written[src] = true;
924 }
925 };
926
927
928 /** @private {!Array<string>} */
929 goog.queuedModules_ = [];
930
931
932 /**
933 * Return an appropriate module text. Suitable to insert into
934 * a script tag (that is unescaped).
935 * @param {string} srcUrl
936 * @param {string} scriptText
937 * @return {string}
938 * @private
939 */
940 goog.wrapModule_ = function(srcUrl, scriptText) {
941 if (!goog.LOAD_MODULE_USING_EVAL || !goog.isDef(goog.global.JSON)) {
942 return '' +
943 'goog.loadModule(function(exports) {' +
944 '"use strict";' +
945 scriptText +
946 '\n' + // terminate any trailing single line comment.
947 ';return exports' +
948 '});' +
949 '\n//# sourceURL=' + srcUrl + '\n';
950 } else {
951 return '' +
952 'goog.loadModule(' +
953 goog.global.JSON.stringify(
954 scriptText + '\n//# sourceURL=' + srcUrl + '\n') +
955 ');';
956 }
957 };
958
959 // On IE9 and earlier, it is necessary to handle
960 // deferred module loads. In later browsers, the
961 // code to be evaluated is simply inserted as a script
962 // block in the correct order. To eval deferred
963 // code at the right time, we piggy back on goog.require to call
964 // goog.maybeProcessDeferredDep_.
965 //
966 // The goog.requires are used both to bootstrap
967 // the loading process (when no deps are available) and
968 // declare that they should be available.
969 //
970 // Here we eval the sources, if all the deps are available
971 // either already eval'd or goog.require'd. This will
972 // be the case when all the dependencies have already
973 // been loaded, and the dependent module is loaded.
974 //
975 // But this alone isn't sufficient because it is also
976 // necessary to handle the case where there is no root
977 // that is not deferred. For that there we register for an event
978 // and trigger goog.loadQueuedModules_ handle any remaining deferred
979 // evaluations.
980
981 /**
982 * Handle any remaining deferred goog.module evals.
983 * @private
984 */
985 goog.loadQueuedModules_ = function() {
986 var count = goog.queuedModules_.length;
987 if (count > 0) {
988 var queue = goog.queuedModules_;
989 goog.queuedModules_ = [];
990 for (var i = 0; i < count; i++) {
991 var path = queue[i];
992 goog.maybeProcessDeferredPath_(path);
993 }
994 }
995 };
996
997
998 /**
999 * Eval the named module if its dependencies are
1000 * available.
1001 * @param {string} name The module to load.
1002 * @private
1003 */
1004 goog.maybeProcessDeferredDep_ = function(name) {
1005 if (goog.isDeferredModule_(name) &&
1006 goog.allDepsAreAvailable_(name)) {
1007 var path = goog.getPathFromDeps_(name);
1008 goog.maybeProcessDeferredPath_(goog.basePath + path);
1009 }
1010 };
1011
1012 /**
1013 * @param {string} name The module to check.
1014 * @return {boolean} Whether the name represents a
1015 * module whose evaluation has been deferred.
1016 * @private
1017 */
1018 goog.isDeferredModule_ = function(name) {
1019 var path = goog.getPathFromDeps_(name);
1020 if (path && goog.dependencies_.pathIsModule[path]) {
1021 var abspath = goog.basePath + path;
1022 return (abspath) in goog.dependencies_.deferred;
1023 }
1024 return false;
1025 };
1026
1027 /**
1028 * @param {string} name The module to check.
1029 * @return {boolean} Whether the name represents a
1030 * module whose declared dependencies have all been loaded
1031 * (eval'd or a deferred module load)
1032 * @private
1033 */
1034 goog.allDepsAreAvailable_ = function(name) {
1035 var path = goog.getPathFromDeps_(name);
1036 if (path && (path in goog.dependencies_.requires)) {
1037 for (var requireName in goog.dependencies_.requires[path]) {
1038 if (!goog.isProvided_(requireName) &&
1039 !goog.isDeferredModule_(requireName)) {
1040 return false;
1041 }
1042 }
1043 }
1044 return true;
1045 };
1046
1047
1048 /**
1049 * @param {string} abspath
1050 * @private
1051 */
1052 goog.maybeProcessDeferredPath_ = function(abspath) {
1053 if (abspath in goog.dependencies_.deferred) {
1054 var src = goog.dependencies_.deferred[abspath];
1055 delete goog.dependencies_.deferred[abspath];
1056 goog.globalEval(src);
1057 }
1058 };
1059
1060
1061 /**
1062 * @param {function(?):?|string} moduleDef The module definition.
1063 */
1064 goog.loadModule = function(moduleDef) {
1065 // NOTE: we allow function definitions to be either in the from
1066 // of a string to eval (which keeps the original source intact) or
1067 // in a eval forbidden environment (CSP) we allow a function definition
1068 // which in its body must call {@code goog.module}, and return the exports
1069 // of the module.
1070 var previousState = goog.moduleLoaderState_;
1071 try {
1072 goog.moduleLoaderState_ = {
1073 moduleName: undefined, declareTestMethods: false};
1074 var exports;
1075 if (goog.isFunction(moduleDef)) {
1076 exports = moduleDef.call(goog.global, {});
1077 } else if (goog.isString(moduleDef)) {
1078 exports = goog.loadModuleFromSource_.call(goog.global, moduleDef);
1079 } else {
1080 throw Error('Invalid module definition');
1081 }
1082
1083 var moduleName = goog.moduleLoaderState_.moduleName;
1084 if (!goog.isString(moduleName) || !moduleName) {
1085 throw Error('Invalid module name \"' + moduleName + '\"');
1086 }
1087
1088 // Don't seal legacy namespaces as they may be uses as a parent of
1089 // another namespace
1090 if (goog.moduleLoaderState_.declareLegacyNamespace) {
1091 goog.constructNamespace_(moduleName, exports);
1092 } else if (goog.SEAL_MODULE_EXPORTS && Object.seal) {
1093 Object.seal(exports);
1094 }
1095
1096 goog.loadedModules_[moduleName] = exports;
1097 if (goog.moduleLoaderState_.declareTestMethods) {
1098 for (var entry in exports) {
1099 if (entry.indexOf('test', 0) === 0 ||
1100 entry == 'tearDown' ||
1101 entry == 'setUp' ||
1102 entry == 'setUpPage' ||
1103 entry == 'tearDownPage') {
1104 goog.global[entry] = exports[entry];
1105 }
1106 }
1107 }
1108 } finally {
1109 goog.moduleLoaderState_ = previousState;
1110 }
1111 };
1112
1113
1114 /**
1115 * @param {string} source
1116 * @return {!Object}
1117 * @private
1118 */
1119 goog.loadModuleFromSource_ = function(source) {
1120 // NOTE: we avoid declaring parameters or local variables here to avoid
1121 // masking globals or leaking values into the module definition.
1122 'use strict';
1123 var exports = {};
1124 eval(arguments[0]);
1125 return exports;
1126 };
1127
1128
1129 /**
1130 * Writes a new script pointing to {@code src} directly into the DOM.
1131 *
1132 * NOTE: This method is not CSP-compliant. @see goog.appendScriptSrcNode_ for
1133 * the fallback mechanism.
1134 *
1135 * @param {string} src The script URL.
1136 * @private
1137 */
1138 goog.writeScriptSrcNode_ = function(src) {
1139 goog.global.document.write(
1140 '<script type="text/javascript" src="' + src + '"></' + 'script>');
1141 };
1142
1143
1144 /**
1145 * Appends a new script node to the DOM using a CSP-compliant mechanism. This
1146 * method exists as a fallback for document.write (which is not allowed in a
1147 * strict CSP context, e.g., Chrome apps).
1148 *
1149 * NOTE: This method is not analogous to using document.write to insert a
1150 * <script> tag; specifically, the user agent will execute a script added by
1151 * document.write immediately after the current script block finishes
1152 * executing, whereas the DOM-appended script node will not be executed until
1153 * the entire document is parsed and executed. That is to say, this script is
1154 * added to the end of the script execution queue.
1155 *
1156 * The page must not attempt to call goog.required entities until after the
1157 * document has loaded, e.g., in or after the window.onload callback.
1158 *
1159 * @param {string} src The script URL.
1160 * @private
1161 */
1162 goog.appendScriptSrcNode_ = function(src) {
1163 var doc = goog.global.document;
1164 var scriptEl = doc.createElement('script');
1165 scriptEl.type = 'text/javascript';
1166 scriptEl.src = src;
1167 scriptEl.defer = false;
1168 scriptEl.async = false;
1169 doc.head.appendChild(scriptEl);
1170 };
1171
1172
1173 /**
1174 * The default implementation of the import function. Writes a script tag to
1175 * import the script.
1176 *
1177 * @param {string} src The script url.
1178 * @param {string=} opt_sourceText The optionally source text to evaluate
1179 * @return {boolean} True if the script was imported, false otherwise.
1180 * @private
1181 */
1182 goog.writeScriptTag_ = function(src, opt_sourceText) {
1183 if (goog.inHtmlDocument_()) {
1184 var doc = goog.global.document;
1185
1186 // If the user tries to require a new symbol after document load,
1187 // something has gone terribly wrong. Doing a document.write would
1188 // wipe out the page. This does not apply to the CSP-compliant method
1189 // of writing script tags.
1190 if (!goog.ENABLE_CHROME_APP_SAFE_SCRIPT_LOADING &&
1191 doc.readyState == 'complete') {
1192 // Certain test frameworks load base.js multiple times, which tries
1193 // to write deps.js each time. If that happens, just fail silently.
1194 // These frameworks wipe the page between each load of base.js, so this
1195 // is OK.
1196 var isDeps = /\bdeps.js$/.test(src);
1197 if (isDeps) {
1198 return false;
1199 } else {
1200 throw Error('Cannot write "' + src + '" after document load');
1201 }
1202 }
1203
1204 var isOldIE = goog.IS_OLD_IE_;
1205
1206 if (opt_sourceText === undefined) {
1207 if (!isOldIE) {
1208 if (goog.ENABLE_CHROME_APP_SAFE_SCRIPT_LOADING) {
1209 goog.appendScriptSrcNode_(src);
1210 } else {
1211 goog.writeScriptSrcNode_(src);
1212 }
1213 } else {
1214 var state = " onreadystatechange='goog.onScriptLoad_(this, " +
1215 ++goog.lastNonModuleScriptIndex_ + ")' ";
1216 doc.write(
1217 '<script type="text/javascript" src="' +
1218 src + '"' + state + '></' + 'script>');
1219 }
1220 } else {
1221 doc.write(
1222 '<script type="text/javascript">' +
1223 opt_sourceText +
1224 '</' + 'script>');
1225 }
1226 return true;
1227 } else {
1228 return false;
1229 }
1230 };
1231
1232
1233 /** @private {number} */
1234 goog.lastNonModuleScriptIndex_ = 0;
1235
1236
1237 /**
1238 * A readystatechange handler for legacy IE
1239 * @param {!HTMLScriptElement} script
1240 * @param {number} scriptIndex
1241 * @return {boolean}
1242 * @private
1243 */
1244 goog.onScriptLoad_ = function(script, scriptIndex) {
1245 // for now load the modules when we reach the last script,
1246 // later allow more inter-mingling.
1247 if (script.readyState == 'complete' &&
1248 goog.lastNonModuleScriptIndex_ == scriptIndex) {
1249 goog.loadQueuedModules_();
1250 }
1251 return true;
1252 };
1253
1254 /**
1255 * Resolves dependencies based on the dependencies added using addDependency
1256 * and calls importScript_ in the correct order.
1257 * @private
1258 */
1259 goog.writeScripts_ = function() {
1260 /** @type {!Array<string>} The scripts we need to write this time. */
1261 var scripts = [];
1262 var seenScript = {};
1263 var deps = goog.dependencies_;
1264
1265 /** @param {string} path */
1266 function visitNode(path) {
1267 if (path in deps.written) {
1268 return;
1269 }
1270
1271 // We have already visited this one. We can get here if we have cyclic
1272 // dependencies.
1273 if (path in deps.visited) {
1274 if (!(path in seenScript)) {
1275 seenScript[path] = true;
1276 scripts.push(path);
1277 }
1278 return;
1279 }
1280
1281 deps.visited[path] = true;
1282
1283 if (path in deps.requires) {
1284 for (var requireName in deps.requires[path]) {
1285 // If the required name is defined, we assume that it was already
1286 // bootstrapped by other means.
1287 if (!goog.isProvided_(requireName)) {
1288 if (requireName in deps.nameToPath) {
1289 visitNode(deps.nameToPath[requireName]);
1290 } else {
1291 throw Error('Undefined nameToPath for ' + requireName);
1292 }
1293 }
1294 }
1295 }
1296
1297 if (!(path in seenScript)) {
1298 seenScript[path] = true;
1299 scripts.push(path);
1300 }
1301 }
1302
1303 for (var path in goog.included_) {
1304 if (!deps.written[path]) {
1305 visitNode(path);
1306 }
1307 }
1308
1309 // record that we are going to load all these scripts.
1310 for (var i = 0; i < scripts.length; i++) {
1311 var path = scripts[i];
1312 goog.dependencies_.written[path] = true;
1313 }
1314
1315 // If a module is loaded synchronously then we need to
1316 // clear the current inModuleLoader value, and restore it when we are
1317 // done loading the current "requires".
1318 var moduleState = goog.moduleLoaderState_;
1319 goog.moduleLoaderState_ = null;
1320
1321 var loadingModule = false;
1322 for (var i = 0; i < scripts.length; i++) {
1323 var path = scripts[i];
1324 if (path) {
1325 if (!deps.pathIsModule[path]) {
1326 goog.importScript_(goog.basePath + path);
1327 } else {
1328 loadingModule = true;
1329 goog.importModule_(goog.basePath + path);
1330 }
1331 } else {
1332 goog.moduleLoaderState_ = moduleState;
1333 throw Error('Undefined script input');
1334 }
1335 }
1336
1337 // restore the current "module loading state"
1338 goog.moduleLoaderState_ = moduleState;
1339 };
1340
1341
1342 /**
1343 * Looks at the dependency rules and tries to determine the script file that
1344 * fulfills a particular rule.
1345 * @param {string} rule In the form goog.namespace.Class or project.script.
1346 * @return {?string} Url corresponding to the rule, or null.
1347 * @private
1348 */
1349 goog.getPathFromDeps_ = function(rule) {
1350 if (rule in goog.dependencies_.nameToPath) {
1351 return goog.dependencies_.nameToPath[rule];
1352 } else {
1353 return null;
1354 }
1355 };
1356
1357 goog.findBasePath_();
1358
1359 // Allow projects to manage the deps files themselves.
1360 if (!goog.global.CLOSURE_NO_DEPS) {
1361 goog.importScript_(goog.basePath + 'deps.js');
1362 }
1363}
1364
1365
1366/**
1367 * Normalize a file path by removing redundant ".." and extraneous "." file
1368 * path components.
1369 * @param {string} path
1370 * @return {string}
1371 * @private
1372 */
1373goog.normalizePath_ = function(path) {
1374 var components = path.split('/');
1375 var i = 0;
1376 while (i < components.length) {
1377 if (components[i] == '.') {
1378 components.splice(i, 1);
1379 } else if (i && components[i] == '..' &&
1380 components[i - 1] && components[i - 1] != '..') {
1381 components.splice(--i, 2);
1382 } else {
1383 i++;
1384 }
1385 }
1386 return components.join('/');
1387};
1388
1389
1390/**
1391 * Loads file by synchronous XHR. Should not be used in production environments.
1392 * @param {string} src Source URL.
1393 * @return {string} File contents.
1394 * @private
1395 */
1396goog.loadFileSync_ = function(src) {
1397 if (goog.global.CLOSURE_LOAD_FILE_SYNC) {
1398 return goog.global.CLOSURE_LOAD_FILE_SYNC(src);
1399 } else {
1400 var xhr = new goog.global['XMLHttpRequest']();
1401 xhr.open('get', src, false);
1402 xhr.send();
1403 return xhr.responseText;
1404 }
1405};
1406
1407
1408/**
1409 * Retrieve and execute a module.
1410 * @param {string} src Script source URL.
1411 * @private
1412 */
1413goog.retrieveAndExecModule_ = function(src) {
1414 if (!COMPILED) {
1415 // The full but non-canonicalized URL for later use.
1416 var originalPath = src;
1417 // Canonicalize the path, removing any /./ or /../ since Chrome's debugging
1418 // console doesn't auto-canonicalize XHR loads as it does <script> srcs.
1419 src = goog.normalizePath_(src);
1420
1421 var importScript = goog.global.CLOSURE_IMPORT_SCRIPT ||
1422 goog.writeScriptTag_;
1423
1424 var scriptText = goog.loadFileSync_(src);
1425
1426 if (scriptText != null) {
1427 var execModuleScript = goog.wrapModule_(src, scriptText);
1428 var isOldIE = goog.IS_OLD_IE_;
1429 if (isOldIE) {
1430 goog.dependencies_.deferred[originalPath] = execModuleScript;
1431 goog.queuedModules_.push(originalPath);
1432 } else {
1433 importScript(src, execModuleScript);
1434 }
1435 } else {
1436 throw new Error('load of ' + src + 'failed');
1437 }
1438 }
1439};
1440
1441
1442//==============================================================================
1443// Language Enhancements
1444//==============================================================================
1445
1446
1447/**
1448 * This is a "fixed" version of the typeof operator. It differs from the typeof
1449 * operator in such a way that null returns 'null' and arrays return 'array'.
1450 * @param {*} value The value to get the type of.
1451 * @return {string} The name of the type.
1452 */
1453goog.typeOf = function(value) {
1454 var s = typeof value;
1455 if (s == 'object') {
1456 if (value) {
1457 // Check these first, so we can avoid calling Object.prototype.toString if
1458 // possible.
1459 //
1460 // IE improperly marshals tyepof across execution contexts, but a
1461 // cross-context object will still return false for "instanceof Object".
1462 if (value instanceof Array) {
1463 return 'array';
1464 } else if (value instanceof Object) {
1465 return s;
1466 }
1467
1468 // HACK: In order to use an Object prototype method on the arbitrary
1469 // value, the compiler requires the value be cast to type Object,
1470 // even though the ECMA spec explicitly allows it.
1471 var className = Object.prototype.toString.call(
1472 /** @type {Object} */ (value));
1473 // In Firefox 3.6, attempting to access iframe window objects' length
1474 // property throws an NS_ERROR_FAILURE, so we need to special-case it
1475 // here.
1476 if (className == '[object Window]') {
1477 return 'object';
1478 }
1479
1480 // We cannot always use constructor == Array or instanceof Array because
1481 // different frames have different Array objects. In IE6, if the iframe
1482 // where the array was created is destroyed, the array loses its
1483 // prototype. Then dereferencing val.splice here throws an exception, so
1484 // we can't use goog.isFunction. Calling typeof directly returns 'unknown'
1485 // so that will work. In this case, this function will return false and
1486 // most array functions will still work because the array is still
1487 // array-like (supports length and []) even though it has lost its
1488 // prototype.
1489 // Mark Miller noticed that Object.prototype.toString
1490 // allows access to the unforgeable [[Class]] property.
1491 // 15.2.4.2 Object.prototype.toString ( )
1492 // When the toString method is called, the following steps are taken:
1493 // 1. Get the [[Class]] property of this object.
1494 // 2. Compute a string value by concatenating the three strings
1495 // "[object ", Result(1), and "]".
1496 // 3. Return Result(2).
1497 // and this behavior survives the destruction of the execution context.
1498 if ((className == '[object Array]' ||
1499 // In IE all non value types are wrapped as objects across window
1500 // boundaries (not iframe though) so we have to do object detection
1501 // for this edge case.
1502 typeof value.length == 'number' &&
1503 typeof value.splice != 'undefined' &&
1504 typeof value.propertyIsEnumerable != 'undefined' &&
1505 !value.propertyIsEnumerable('splice')
1506
1507 )) {
1508 return 'array';
1509 }
1510 // HACK: There is still an array case that fails.
1511 // function ArrayImpostor() {}
1512 // ArrayImpostor.prototype = [];
1513 // var impostor = new ArrayImpostor;
1514 // this can be fixed by getting rid of the fast path
1515 // (value instanceof Array) and solely relying on
1516 // (value && Object.prototype.toString.vall(value) === '[object Array]')
1517 // but that would require many more function calls and is not warranted
1518 // unless closure code is receiving objects from untrusted sources.
1519
1520 // IE in cross-window calls does not correctly marshal the function type
1521 // (it appears just as an object) so we cannot use just typeof val ==
1522 // 'function'. However, if the object has a call property, it is a
1523 // function.
1524 if ((className == '[object Function]' ||
1525 typeof value.call != 'undefined' &&
1526 typeof value.propertyIsEnumerable != 'undefined' &&
1527 !value.propertyIsEnumerable('call'))) {
1528 return 'function';
1529 }
1530
1531 } else {
1532 return 'null';
1533 }
1534
1535 } else if (s == 'function' && typeof value.call == 'undefined') {
1536 // In Safari typeof nodeList returns 'function', and on Firefox typeof
1537 // behaves similarly for HTML{Applet,Embed,Object}, Elements and RegExps. We
1538 // would like to return object for those and we can detect an invalid
1539 // function by making sure that the function object has a call method.
1540 return 'object';
1541 }
1542 return s;
1543};
1544
1545
1546/**
1547 * Returns true if the specified value is null.
1548 * @param {?} val Variable to test.
1549 * @return {boolean} Whether variable is null.
1550 */
1551goog.isNull = function(val) {
1552 return val === null;
1553};
1554
1555
1556/**
1557 * Returns true if the specified value is defined and not null.
1558 * @param {?} val Variable to test.
1559 * @return {boolean} Whether variable is defined and not null.
1560 */
1561goog.isDefAndNotNull = function(val) {
1562 // Note that undefined == null.
1563 return val != null;
1564};
1565
1566
1567/**
1568 * Returns true if the specified value is an array.
1569 * @param {?} val Variable to test.
1570 * @return {boolean} Whether variable is an array.
1571 */
1572goog.isArray = function(val) {
1573 return goog.typeOf(val) == 'array';
1574};
1575
1576
1577/**
1578 * Returns true if the object looks like an array. To qualify as array like
1579 * the value needs to be either a NodeList or an object with a Number length
1580 * property. As a special case, a function value is not array like, because its
1581 * length property is fixed to correspond to the number of expected arguments.
1582 * @param {?} val Variable to test.
1583 * @return {boolean} Whether variable is an array.
1584 */
1585goog.isArrayLike = function(val) {
1586 var type = goog.typeOf(val);
1587 // We do not use goog.isObject here in order to exclude function values.
1588 return type == 'array' || type == 'object' && typeof val.length == 'number';
1589};
1590
1591
1592/**
1593 * Returns true if the object looks like a Date. To qualify as Date-like the
1594 * value needs to be an object and have a getFullYear() function.
1595 * @param {?} val Variable to test.
1596 * @return {boolean} Whether variable is a like a Date.
1597 */
1598goog.isDateLike = function(val) {
1599 return goog.isObject(val) && typeof val.getFullYear == 'function';
1600};
1601
1602
1603/**
1604 * Returns true if the specified value is a string.
1605 * @param {?} val Variable to test.
1606 * @return {boolean} Whether variable is a string.
1607 */
1608goog.isString = function(val) {
1609 return typeof val == 'string';
1610};
1611
1612
1613/**
1614 * Returns true if the specified value is a boolean.
1615 * @param {?} val Variable to test.
1616 * @return {boolean} Whether variable is boolean.
1617 */
1618goog.isBoolean = function(val) {
1619 return typeof val == 'boolean';
1620};
1621
1622
1623/**
1624 * Returns true if the specified value is a number.
1625 * @param {?} val Variable to test.
1626 * @return {boolean} Whether variable is a number.
1627 */
1628goog.isNumber = function(val) {
1629 return typeof val == 'number';
1630};
1631
1632
1633/**
1634 * Returns true if the specified value is a function.
1635 * @param {?} val Variable to test.
1636 * @return {boolean} Whether variable is a function.
1637 */
1638goog.isFunction = function(val) {
1639 return goog.typeOf(val) == 'function';
1640};
1641
1642
1643/**
1644 * Returns true if the specified value is an object. This includes arrays and
1645 * functions.
1646 * @param {?} val Variable to test.
1647 * @return {boolean} Whether variable is an object.
1648 */
1649goog.isObject = function(val) {
1650 var type = typeof val;
1651 return type == 'object' && val != null || type == 'function';
1652 // return Object(val) === val also works, but is slower, especially if val is
1653 // not an object.
1654};
1655
1656
1657/**
1658 * Gets a unique ID for an object. This mutates the object so that further calls
1659 * with the same object as a parameter returns the same value. The unique ID is
1660 * guaranteed to be unique across the current session amongst objects that are
1661 * passed into {@code getUid}. There is no guarantee that the ID is unique or
1662 * consistent across sessions. It is unsafe to generate unique ID for function
1663 * prototypes.
1664 *
1665 * @param {Object} obj The object to get the unique ID for.
1666 * @return {number} The unique ID for the object.
1667 */
1668goog.getUid = function(obj) {
1669 // TODO(arv): Make the type stricter, do not accept null.
1670
1671 // In Opera window.hasOwnProperty exists but always returns false so we avoid
1672 // using it. As a consequence the unique ID generated for BaseClass.prototype
1673 // and SubClass.prototype will be the same.
1674 return obj[goog.UID_PROPERTY_] ||
1675 (obj[goog.UID_PROPERTY_] = ++goog.uidCounter_);
1676};
1677
1678
1679/**
1680 * Whether the given object is already assigned a unique ID.
1681 *
1682 * This does not modify the object.
1683 *
1684 * @param {!Object} obj The object to check.
1685 * @return {boolean} Whether there is an assigned unique id for the object.
1686 */
1687goog.hasUid = function(obj) {
1688 return !!obj[goog.UID_PROPERTY_];
1689};
1690
1691
1692/**
1693 * Removes the unique ID from an object. This is useful if the object was
1694 * previously mutated using {@code goog.getUid} in which case the mutation is
1695 * undone.
1696 * @param {Object} obj The object to remove the unique ID field from.
1697 */
1698goog.removeUid = function(obj) {
1699 // TODO(arv): Make the type stricter, do not accept null.
1700
1701 // In IE, DOM nodes are not instances of Object and throw an exception if we
1702 // try to delete. Instead we try to use removeAttribute.
1703 if ('removeAttribute' in obj) {
1704 obj.removeAttribute(goog.UID_PROPERTY_);
1705 }
1706 /** @preserveTry */
1707 try {
1708 delete obj[goog.UID_PROPERTY_];
1709 } catch (ex) {
1710 }
1711};
1712
1713
1714/**
1715 * Name for unique ID property. Initialized in a way to help avoid collisions
1716 * with other closure JavaScript on the same page.
1717 * @type {string}
1718 * @private
1719 */
1720goog.UID_PROPERTY_ = 'closure_uid_' + ((Math.random() * 1e9) >>> 0);
1721
1722
1723/**
1724 * Counter for UID.
1725 * @type {number}
1726 * @private
1727 */
1728goog.uidCounter_ = 0;
1729
1730
1731/**
1732 * Adds a hash code field to an object. The hash code is unique for the
1733 * given object.
1734 * @param {Object} obj The object to get the hash code for.
1735 * @return {number} The hash code for the object.
1736 * @deprecated Use goog.getUid instead.
1737 */
1738goog.getHashCode = goog.getUid;
1739
1740
1741/**
1742 * Removes the hash code field from an object.
1743 * @param {Object} obj The object to remove the field from.
1744 * @deprecated Use goog.removeUid instead.
1745 */
1746goog.removeHashCode = goog.removeUid;
1747
1748
1749/**
1750 * Clones a value. The input may be an Object, Array, or basic type. Objects and
1751 * arrays will be cloned recursively.
1752 *
1753 * WARNINGS:
1754 * <code>goog.cloneObject</code> does not detect reference loops. Objects that
1755 * refer to themselves will cause infinite recursion.
1756 *
1757 * <code>goog.cloneObject</code> is unaware of unique identifiers, and copies
1758 * UIDs created by <code>getUid</code> into cloned results.
1759 *
1760 * @param {*} obj The value to clone.
1761 * @return {*} A clone of the input value.
1762 * @deprecated goog.cloneObject is unsafe. Prefer the goog.object methods.
1763 */
1764goog.cloneObject = function(obj) {
1765 var type = goog.typeOf(obj);
1766 if (type == 'object' || type == 'array') {
1767 if (obj.clone) {
1768 return obj.clone();
1769 }
1770 var clone = type == 'array' ? [] : {};
1771 for (var key in obj) {
1772 clone[key] = goog.cloneObject(obj[key]);
1773 }
1774 return clone;
1775 }
1776
1777 return obj;
1778};
1779
1780
1781/**
1782 * A native implementation of goog.bind.
1783 * @param {Function} fn A function to partially apply.
1784 * @param {Object|undefined} selfObj Specifies the object which this should
1785 * point to when the function is run.
1786 * @param {...*} var_args Additional arguments that are partially applied to the
1787 * function.
1788 * @return {!Function} A partially-applied form of the function bind() was
1789 * invoked as a method of.
1790 * @private
1791 * @suppress {deprecated} The compiler thinks that Function.prototype.bind is
1792 * deprecated because some people have declared a pure-JS version.
1793 * Only the pure-JS version is truly deprecated.
1794 */
1795goog.bindNative_ = function(fn, selfObj, var_args) {
1796 return /** @type {!Function} */ (fn.call.apply(fn.bind, arguments));
1797};
1798
1799
1800/**
1801 * A pure-JS implementation of goog.bind.
1802 * @param {Function} fn A function to partially apply.
1803 * @param {Object|undefined} selfObj Specifies the object which this should
1804 * point to when the function is run.
1805 * @param {...*} var_args Additional arguments that are partially applied to the
1806 * function.
1807 * @return {!Function} A partially-applied form of the function bind() was
1808 * invoked as a method of.
1809 * @private
1810 */
1811goog.bindJs_ = function(fn, selfObj, var_args) {
1812 if (!fn) {
1813 throw new Error();
1814 }
1815
1816 if (arguments.length > 2) {
1817 var boundArgs = Array.prototype.slice.call(arguments, 2);
1818 return function() {
1819 // Prepend the bound arguments to the current arguments.
1820 var newArgs = Array.prototype.slice.call(arguments);
1821 Array.prototype.unshift.apply(newArgs, boundArgs);
1822 return fn.apply(selfObj, newArgs);
1823 };
1824
1825 } else {
1826 return function() {
1827 return fn.apply(selfObj, arguments);
1828 };
1829 }
1830};
1831
1832
1833/**
1834 * Partially applies this function to a particular 'this object' and zero or
1835 * more arguments. The result is a new function with some arguments of the first
1836 * function pre-filled and the value of this 'pre-specified'.
1837 *
1838 * Remaining arguments specified at call-time are appended to the pre-specified
1839 * ones.
1840 *
1841 * Also see: {@link #partial}.
1842 *
1843 * Usage:
1844 * <pre>var barMethBound = bind(myFunction, myObj, 'arg1', 'arg2');
1845 * barMethBound('arg3', 'arg4');</pre>
1846 *
1847 * @param {?function(this:T, ...)} fn A function to partially apply.
1848 * @param {T} selfObj Specifies the object which this should point to when the
1849 * function is run.
1850 * @param {...*} var_args Additional arguments that are partially applied to the
1851 * function.
1852 * @return {!Function} A partially-applied form of the function bind() was
1853 * invoked as a method of.
1854 * @template T
1855 * @suppress {deprecated} See above.
1856 */
1857goog.bind = function(fn, selfObj, var_args) {
1858 // TODO(nicksantos): narrow the type signature.
1859 if (Function.prototype.bind &&
1860 // NOTE(nicksantos): Somebody pulled base.js into the default Chrome
1861 // extension environment. This means that for Chrome extensions, they get
1862 // the implementation of Function.prototype.bind that calls goog.bind
1863 // instead of the native one. Even worse, we don't want to introduce a
1864 // circular dependency between goog.bind and Function.prototype.bind, so
1865 // we have to hack this to make sure it works correctly.
1866 Function.prototype.bind.toString().indexOf('native code') != -1) {
1867 goog.bind = goog.bindNative_;
1868 } else {
1869 goog.bind = goog.bindJs_;
1870 }
1871 return goog.bind.apply(null, arguments);
1872};
1873
1874
1875/**
1876 * Like bind(), except that a 'this object' is not required. Useful when the
1877 * target function is already bound.
1878 *
1879 * Usage:
1880 * var g = partial(f, arg1, arg2);
1881 * g(arg3, arg4);
1882 *
1883 * @param {Function} fn A function to partially apply.
1884 * @param {...*} var_args Additional arguments that are partially applied to fn.
1885 * @return {!Function} A partially-applied form of the function bind() was
1886 * invoked as a method of.
1887 */
1888goog.partial = function(fn, var_args) {
1889 var args = Array.prototype.slice.call(arguments, 1);
1890 return function() {
1891 // Clone the array (with slice()) and append additional arguments
1892 // to the existing arguments.
1893 var newArgs = args.slice();
1894 newArgs.push.apply(newArgs, arguments);
1895 return fn.apply(this, newArgs);
1896 };
1897};
1898
1899
1900/**
1901 * Copies all the members of a source object to a target object. This method
1902 * does not work on all browsers for all objects that contain keys such as
1903 * toString or hasOwnProperty. Use goog.object.extend for this purpose.
1904 * @param {Object} target Target.
1905 * @param {Object} source Source.
1906 */
1907goog.mixin = function(target, source) {
1908 for (var x in source) {
1909 target[x] = source[x];
1910 }
1911
1912 // For IE7 or lower, the for-in-loop does not contain any properties that are
1913 // not enumerable on the prototype object (for example, isPrototypeOf from
1914 // Object.prototype) but also it will not include 'replace' on objects that
1915 // extend String and change 'replace' (not that it is common for anyone to
1916 // extend anything except Object).
1917};
1918
1919
1920/**
1921 * @return {number} An integer value representing the number of milliseconds
1922 * between midnight, January 1, 1970 and the current time.
1923 */
1924goog.now = (goog.TRUSTED_SITE && Date.now) || (function() {
1925 // Unary plus operator converts its operand to a number which in the case of
1926 // a date is done by calling getTime().
1927 return +new Date();
1928});
1929
1930
1931/**
1932 * Evals JavaScript in the global scope. In IE this uses execScript, other
1933 * browsers use goog.global.eval. If goog.global.eval does not evaluate in the
1934 * global scope (for example, in Safari), appends a script tag instead.
1935 * Throws an exception if neither execScript or eval is defined.
1936 * @param {string} script JavaScript string.
1937 */
1938goog.globalEval = function(script) {
1939 if (goog.global.execScript) {
1940 goog.global.execScript(script, 'JavaScript');
1941 } else if (goog.global.eval) {
1942 // Test to see if eval works
1943 if (goog.evalWorksForGlobals_ == null) {
1944 goog.global.eval('var _et_ = 1;');
1945 if (typeof goog.global['_et_'] != 'undefined') {
1946 delete goog.global['_et_'];
1947 goog.evalWorksForGlobals_ = true;
1948 } else {
1949 goog.evalWorksForGlobals_ = false;
1950 }
1951 }
1952
1953 if (goog.evalWorksForGlobals_) {
1954 goog.global.eval(script);
1955 } else {
1956 var doc = goog.global.document;
1957 var scriptElt = doc.createElement('SCRIPT');
1958 scriptElt.type = 'text/javascript';
1959 scriptElt.defer = false;
1960 // Note(user): can't use .innerHTML since "t('<test>')" will fail and
1961 // .text doesn't work in Safari 2. Therefore we append a text node.
1962 scriptElt.appendChild(doc.createTextNode(script));
1963 doc.body.appendChild(scriptElt);
1964 doc.body.removeChild(scriptElt);
1965 }
1966 } else {
1967 throw Error('goog.globalEval not available');
1968 }
1969};
1970
1971
1972/**
1973 * Indicates whether or not we can call 'eval' directly to eval code in the
1974 * global scope. Set to a Boolean by the first call to goog.globalEval (which
1975 * empirically tests whether eval works for globals). @see goog.globalEval
1976 * @type {?boolean}
1977 * @private
1978 */
1979goog.evalWorksForGlobals_ = null;
1980
1981
1982/**
1983 * Optional map of CSS class names to obfuscated names used with
1984 * goog.getCssName().
1985 * @private {!Object<string, string>|undefined}
1986 * @see goog.setCssNameMapping
1987 */
1988goog.cssNameMapping_;
1989
1990
1991/**
1992 * Optional obfuscation style for CSS class names. Should be set to either
1993 * 'BY_WHOLE' or 'BY_PART' if defined.
1994 * @type {string|undefined}
1995 * @private
1996 * @see goog.setCssNameMapping
1997 */
1998goog.cssNameMappingStyle_;
1999
2000
2001/**
2002 * Handles strings that are intended to be used as CSS class names.
2003 *
2004 * This function works in tandem with @see goog.setCssNameMapping.
2005 *
2006 * Without any mapping set, the arguments are simple joined with a hyphen and
2007 * passed through unaltered.
2008 *
2009 * When there is a mapping, there are two possible styles in which these
2010 * mappings are used. In the BY_PART style, each part (i.e. in between hyphens)
2011 * of the passed in css name is rewritten according to the map. In the BY_WHOLE
2012 * style, the full css name is looked up in the map directly. If a rewrite is
2013 * not specified by the map, the compiler will output a warning.
2014 *
2015 * When the mapping is passed to the compiler, it will replace calls to
2016 * goog.getCssName with the strings from the mapping, e.g.
2017 * var x = goog.getCssName('foo');
2018 * var y = goog.getCssName(this.baseClass, 'active');
2019 * becomes:
2020 * var x= 'foo';
2021 * var y = this.baseClass + '-active';
2022 *
2023 * If one argument is passed it will be processed, if two are passed only the
2024 * modifier will be processed, as it is assumed the first argument was generated
2025 * as a result of calling goog.getCssName.
2026 *
2027 * @param {string} className The class name.
2028 * @param {string=} opt_modifier A modifier to be appended to the class name.
2029 * @return {string} The class name or the concatenation of the class name and
2030 * the modifier.
2031 */
2032goog.getCssName = function(className, opt_modifier) {
2033 var getMapping = function(cssName) {
2034 return goog.cssNameMapping_[cssName] || cssName;
2035 };
2036
2037 var renameByParts = function(cssName) {
2038 // Remap all the parts individually.
2039 var parts = cssName.split('-');
2040 var mapped = [];
2041 for (var i = 0; i < parts.length; i++) {
2042 mapped.push(getMapping(parts[i]));
2043 }
2044 return mapped.join('-');
2045 };
2046
2047 var rename;
2048 if (goog.cssNameMapping_) {
2049 rename = goog.cssNameMappingStyle_ == 'BY_WHOLE' ?
2050 getMapping : renameByParts;
2051 } else {
2052 rename = function(a) {
2053 return a;
2054 };
2055 }
2056
2057 if (opt_modifier) {
2058 return className + '-' + rename(opt_modifier);
2059 } else {
2060 return rename(className);
2061 }
2062};
2063
2064
2065/**
2066 * Sets the map to check when returning a value from goog.getCssName(). Example:
2067 * <pre>
2068 * goog.setCssNameMapping({
2069 * "goog": "a",
2070 * "disabled": "b",
2071 * });
2072 *
2073 * var x = goog.getCssName('goog');
2074 * // The following evaluates to: "a a-b".
2075 * goog.getCssName('goog') + ' ' + goog.getCssName(x, 'disabled')
2076 * </pre>
2077 * When declared as a map of string literals to string literals, the JSCompiler
2078 * will replace all calls to goog.getCssName() using the supplied map if the
2079 * --closure_pass flag is set.
2080 *
2081 * @param {!Object} mapping A map of strings to strings where keys are possible
2082 * arguments to goog.getCssName() and values are the corresponding values
2083 * that should be returned.
2084 * @param {string=} opt_style The style of css name mapping. There are two valid
2085 * options: 'BY_PART', and 'BY_WHOLE'.
2086 * @see goog.getCssName for a description.
2087 */
2088goog.setCssNameMapping = function(mapping, opt_style) {
2089 goog.cssNameMapping_ = mapping;
2090 goog.cssNameMappingStyle_ = opt_style;
2091};
2092
2093
2094/**
2095 * To use CSS renaming in compiled mode, one of the input files should have a
2096 * call to goog.setCssNameMapping() with an object literal that the JSCompiler
2097 * can extract and use to replace all calls to goog.getCssName(). In uncompiled
2098 * mode, JavaScript code should be loaded before this base.js file that declares
2099 * a global variable, CLOSURE_CSS_NAME_MAPPING, which is used below. This is
2100 * to ensure that the mapping is loaded before any calls to goog.getCssName()
2101 * are made in uncompiled mode.
2102 *
2103 * A hook for overriding the CSS name mapping.
2104 * @type {!Object<string, string>|undefined}
2105 */
2106goog.global.CLOSURE_CSS_NAME_MAPPING;
2107
2108
2109if (!COMPILED && goog.global.CLOSURE_CSS_NAME_MAPPING) {
2110 // This does not call goog.setCssNameMapping() because the JSCompiler
2111 // requires that goog.setCssNameMapping() be called with an object literal.
2112 goog.cssNameMapping_ = goog.global.CLOSURE_CSS_NAME_MAPPING;
2113}
2114
2115
2116/**
2117 * Gets a localized message.
2118 *
2119 * This function is a compiler primitive. If you give the compiler a localized
2120 * message bundle, it will replace the string at compile-time with a localized
2121 * version, and expand goog.getMsg call to a concatenated string.
2122 *
2123 * Messages must be initialized in the form:
2124 * <code>
2125 * var MSG_NAME = goog.getMsg('Hello {$placeholder}', {'placeholder': 'world'});
2126 * </code>
2127 *
2128 * @param {string} str Translatable string, places holders in the form {$foo}.
2129 * @param {Object<string, string>=} opt_values Maps place holder name to value.
2130 * @return {string} message with placeholders filled.
2131 */
2132goog.getMsg = function(str, opt_values) {
2133 if (opt_values) {
2134 str = str.replace(/\{\$([^}]+)}/g, function(match, key) {
2135 return key in opt_values ? opt_values[key] : match;
2136 });
2137 }
2138 return str;
2139};
2140
2141
2142/**
2143 * Gets a localized message. If the message does not have a translation, gives a
2144 * fallback message.
2145 *
2146 * This is useful when introducing a new message that has not yet been
2147 * translated into all languages.
2148 *
2149 * This function is a compiler primitive. Must be used in the form:
2150 * <code>var x = goog.getMsgWithFallback(MSG_A, MSG_B);</code>
2151 * where MSG_A and MSG_B were initialized with goog.getMsg.
2152 *
2153 * @param {string} a The preferred message.
2154 * @param {string} b The fallback message.
2155 * @return {string} The best translated message.
2156 */
2157goog.getMsgWithFallback = function(a, b) {
2158 return a;
2159};
2160
2161
2162/**
2163 * Exposes an unobfuscated global namespace path for the given object.
2164 * Note that fields of the exported object *will* be obfuscated, unless they are
2165 * exported in turn via this function or goog.exportProperty.
2166 *
2167 * Also handy for making public items that are defined in anonymous closures.
2168 *
2169 * ex. goog.exportSymbol('public.path.Foo', Foo);
2170 *
2171 * ex. goog.exportSymbol('public.path.Foo.staticFunction', Foo.staticFunction);
2172 * public.path.Foo.staticFunction();
2173 *
2174 * ex. goog.exportSymbol('public.path.Foo.prototype.myMethod',
2175 * Foo.prototype.myMethod);
2176 * new public.path.Foo().myMethod();
2177 *
2178 * @param {string} publicPath Unobfuscated name to export.
2179 * @param {*} object Object the name should point to.
2180 * @param {Object=} opt_objectToExportTo The object to add the path to; default
2181 * is goog.global.
2182 */
2183goog.exportSymbol = function(publicPath, object, opt_objectToExportTo) {
2184 goog.exportPath_(publicPath, object, opt_objectToExportTo);
2185};
2186
2187
2188/**
2189 * Exports a property unobfuscated into the object's namespace.
2190 * ex. goog.exportProperty(Foo, 'staticFunction', Foo.staticFunction);
2191 * ex. goog.exportProperty(Foo.prototype, 'myMethod', Foo.prototype.myMethod);
2192 * @param {Object} object Object whose static property is being exported.
2193 * @param {string} publicName Unobfuscated name to export.
2194 * @param {*} symbol Object the name should point to.
2195 */
2196goog.exportProperty = function(object, publicName, symbol) {
2197 object[publicName] = symbol;
2198};
2199
2200
2201/**
2202 * Inherit the prototype methods from one constructor into another.
2203 *
2204 * Usage:
2205 * <pre>
2206 * function ParentClass(a, b) { }
2207 * ParentClass.prototype.foo = function(a) { };
2208 *
2209 * function ChildClass(a, b, c) {
2210 * ChildClass.base(this, 'constructor', a, b);
2211 * }
2212 * goog.inherits(ChildClass, ParentClass);
2213 *
2214 * var child = new ChildClass('a', 'b', 'see');
2215 * child.foo(); // This works.
2216 * </pre>
2217 *
2218 * @param {Function} childCtor Child class.
2219 * @param {Function} parentCtor Parent class.
2220 */
2221goog.inherits = function(childCtor, parentCtor) {
2222 /** @constructor */
2223 function tempCtor() {};
2224 tempCtor.prototype = parentCtor.prototype;
2225 childCtor.superClass_ = parentCtor.prototype;
2226 childCtor.prototype = new tempCtor();
2227 /** @override */
2228 childCtor.prototype.constructor = childCtor;
2229
2230 /**
2231 * Calls superclass constructor/method.
2232 *
2233 * This function is only available if you use goog.inherits to
2234 * express inheritance relationships between classes.
2235 *
2236 * NOTE: This is a replacement for goog.base and for superClass_
2237 * property defined in childCtor.
2238 *
2239 * @param {!Object} me Should always be "this".
2240 * @param {string} methodName The method name to call. Calling
2241 * superclass constructor can be done with the special string
2242 * 'constructor'.
2243 * @param {...*} var_args The arguments to pass to superclass
2244 * method/constructor.
2245 * @return {*} The return value of the superclass method/constructor.
2246 */
2247 childCtor.base = function(me, methodName, var_args) {
2248 // Copying using loop to avoid deop due to passing arguments object to
2249 // function. This is faster in many JS engines as of late 2014.
2250 var args = new Array(arguments.length - 2);
2251 for (var i = 2; i < arguments.length; i++) {
2252 args[i - 2] = arguments[i];
2253 }
2254 return parentCtor.prototype[methodName].apply(me, args);
2255 };
2256};
2257
2258
2259/**
2260 * Call up to the superclass.
2261 *
2262 * If this is called from a constructor, then this calls the superclass
2263 * constructor with arguments 1-N.
2264 *
2265 * If this is called from a prototype method, then you must pass the name of the
2266 * method as the second argument to this function. If you do not, you will get a
2267 * runtime error. This calls the superclass' method with arguments 2-N.
2268 *
2269 * This function only works if you use goog.inherits to express inheritance
2270 * relationships between your classes.
2271 *
2272 * This function is a compiler primitive. At compile-time, the compiler will do
2273 * macro expansion to remove a lot of the extra overhead that this function
2274 * introduces. The compiler will also enforce a lot of the assumptions that this
2275 * function makes, and treat it as a compiler error if you break them.
2276 *
2277 * @param {!Object} me Should always be "this".
2278 * @param {*=} opt_methodName The method name if calling a super method.
2279 * @param {...*} var_args The rest of the arguments.
2280 * @return {*} The return value of the superclass method.
2281 * @suppress {es5Strict} This method can not be used in strict mode, but
2282 * all Closure Library consumers must depend on this file.
2283 */
2284goog.base = function(me, opt_methodName, var_args) {
2285 var caller = arguments.callee.caller;
2286
2287 if (goog.STRICT_MODE_COMPATIBLE || (goog.DEBUG && !caller)) {
2288 throw Error('arguments.caller not defined. goog.base() cannot be used ' +
2289 'with strict mode code. See ' +
2290 'http://www.ecma-international.org/ecma-262/5.1/#sec-C');
2291 }
2292
2293 if (caller.superClass_) {
2294 // Copying using loop to avoid deop due to passing arguments object to
2295 // function. This is faster in many JS engines as of late 2014.
2296 var ctorArgs = new Array(arguments.length - 1);
2297 for (var i = 1; i < arguments.length; i++) {
2298 ctorArgs[i - 1] = arguments[i];
2299 }
2300 // This is a constructor. Call the superclass constructor.
2301 return caller.superClass_.constructor.apply(me, ctorArgs);
2302 }
2303
2304 // Copying using loop to avoid deop due to passing arguments object to
2305 // function. This is faster in many JS engines as of late 2014.
2306 var args = new Array(arguments.length - 2);
2307 for (var i = 2; i < arguments.length; i++) {
2308 args[i - 2] = arguments[i];
2309 }
2310 var foundCaller = false;
2311 for (var ctor = me.constructor;
2312 ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
2313 if (ctor.prototype[opt_methodName] === caller) {
2314 foundCaller = true;
2315 } else if (foundCaller) {
2316 return ctor.prototype[opt_methodName].apply(me, args);
2317 }
2318 }
2319
2320 // If we did not find the caller in the prototype chain, then one of two
2321 // things happened:
2322 // 1) The caller is an instance method.
2323 // 2) This method was not called by the right caller.
2324 if (me[opt_methodName] === caller) {
2325 return me.constructor.prototype[opt_methodName].apply(me, args);
2326 } else {
2327 throw Error(
2328 'goog.base called from a method of one name ' +
2329 'to a method of a different name');
2330 }
2331};
2332
2333
2334/**
2335 * Allow for aliasing within scope functions. This function exists for
2336 * uncompiled code - in compiled code the calls will be inlined and the aliases
2337 * applied. In uncompiled code the function is simply run since the aliases as
2338 * written are valid JavaScript.
2339 *
2340 *
2341 * @param {function()} fn Function to call. This function can contain aliases
2342 * to namespaces (e.g. "var dom = goog.dom") or classes
2343 * (e.g. "var Timer = goog.Timer").
2344 */
2345goog.scope = function(fn) {
2346 fn.call(goog.global);
2347};
2348
2349
2350/*
2351 * To support uncompiled, strict mode bundles that use eval to divide source
2352 * like so:
2353 * eval('someSource;//# sourceUrl sourcefile.js');
2354 * We need to export the globally defined symbols "goog" and "COMPILED".
2355 * Exporting "goog" breaks the compiler optimizations, so we required that
2356 * be defined externally.
2357 * NOTE: We don't use goog.exportSymbol here because we don't want to trigger
2358 * extern generation when that compiler option is enabled.
2359 */
2360if (!COMPILED) {
2361 goog.global['COMPILED'] = COMPILED;
2362}
2363
2364
2365
2366//==============================================================================
2367// goog.defineClass implementation
2368//==============================================================================
2369
2370
2371/**
2372 * Creates a restricted form of a Closure "class":
2373 * - from the compiler's perspective, the instance returned from the
2374 * constructor is sealed (no new properties may be added). This enables
2375 * better checks.
2376 * - the compiler will rewrite this definition to a form that is optimal
2377 * for type checking and optimization (initially this will be a more
2378 * traditional form).
2379 *
2380 * @param {Function} superClass The superclass, Object or null.
2381 * @param {goog.defineClass.ClassDescriptor} def
2382 * An object literal describing
2383 * the class. It may have the following properties:
2384 * "constructor": the constructor function
2385 * "statics": an object literal containing methods to add to the constructor
2386 * as "static" methods or a function that will receive the constructor
2387 * function as its only parameter to which static properties can
2388 * be added.
2389 * all other properties are added to the prototype.
2390 * @return {!Function} The class constructor.
2391 */
2392goog.defineClass = function(superClass, def) {
2393 // TODO(johnlenz): consider making the superClass an optional parameter.
2394 var constructor = def.constructor;
2395 var statics = def.statics;
2396 // Wrap the constructor prior to setting up the prototype and static methods.
2397 if (!constructor || constructor == Object.prototype.constructor) {
2398 constructor = function() {
2399 throw Error('cannot instantiate an interface (no constructor defined).');
2400 };
2401 }
2402
2403 var cls = goog.defineClass.createSealingConstructor_(constructor, superClass);
2404 if (superClass) {
2405 goog.inherits(cls, superClass);
2406 }
2407
2408 // Remove all the properties that should not be copied to the prototype.
2409 delete def.constructor;
2410 delete def.statics;
2411
2412 goog.defineClass.applyProperties_(cls.prototype, def);
2413 if (statics != null) {
2414 if (statics instanceof Function) {
2415 statics(cls);
2416 } else {
2417 goog.defineClass.applyProperties_(cls, statics);
2418 }
2419 }
2420
2421 return cls;
2422};
2423
2424
2425/**
2426 * @typedef {
2427 * !Object|
2428 * {constructor:!Function}|
2429 * {constructor:!Function, statics:(Object|function(Function):void)}}
2430 * @suppress {missingProvide}
2431 */
2432goog.defineClass.ClassDescriptor;
2433
2434
2435/**
2436 * @define {boolean} Whether the instances returned by
2437 * goog.defineClass should be sealed when possible.
2438 */
2439goog.define('goog.defineClass.SEAL_CLASS_INSTANCES', goog.DEBUG);
2440
2441
2442/**
2443 * If goog.defineClass.SEAL_CLASS_INSTANCES is enabled and Object.seal is
2444 * defined, this function will wrap the constructor in a function that seals the
2445 * results of the provided constructor function.
2446 *
2447 * @param {!Function} ctr The constructor whose results maybe be sealed.
2448 * @param {Function} superClass The superclass constructor.
2449 * @return {!Function} The replacement constructor.
2450 * @private
2451 */
2452goog.defineClass.createSealingConstructor_ = function(ctr, superClass) {
2453 if (goog.defineClass.SEAL_CLASS_INSTANCES &&
2454 Object.seal instanceof Function) {
2455 // Don't seal subclasses of unsealable-tagged legacy classes.
2456 if (superClass && superClass.prototype &&
2457 superClass.prototype[goog.UNSEALABLE_CONSTRUCTOR_PROPERTY_]) {
2458 return ctr;
2459 }
2460 /**
2461 * @this {Object}
2462 * @return {?}
2463 */
2464 var wrappedCtr = function() {
2465 // Don't seal an instance of a subclass when it calls the constructor of
2466 // its super class as there is most likely still setup to do.
2467 var instance = ctr.apply(this, arguments) || this;
2468 instance[goog.UID_PROPERTY_] = instance[goog.UID_PROPERTY_];
2469 if (this.constructor === wrappedCtr) {
2470 Object.seal(instance);
2471 }
2472 return instance;
2473 };
2474 return wrappedCtr;
2475 }
2476 return ctr;
2477};
2478
2479
2480// TODO(johnlenz): share these values with the goog.object
2481/**
2482 * The names of the fields that are defined on Object.prototype.
2483 * @type {!Array<string>}
2484 * @private
2485 * @const
2486 */
2487goog.defineClass.OBJECT_PROTOTYPE_FIELDS_ = [
2488 'constructor',
2489 'hasOwnProperty',
2490 'isPrototypeOf',
2491 'propertyIsEnumerable',
2492 'toLocaleString',
2493 'toString',
2494 'valueOf'
2495];
2496
2497
2498// TODO(johnlenz): share this function with the goog.object
2499/**
2500 * @param {!Object} target The object to add properties to.
2501 * @param {!Object} source The object to copy properties from.
2502 * @private
2503 */
2504goog.defineClass.applyProperties_ = function(target, source) {
2505 // TODO(johnlenz): update this to support ES5 getters/setters
2506
2507 var key;
2508 for (key in source) {
2509 if (Object.prototype.hasOwnProperty.call(source, key)) {
2510 target[key] = source[key];
2511 }
2512 }
2513
2514 // For IE the for-in-loop does not contain any properties that are not
2515 // enumerable on the prototype object (for example isPrototypeOf from
2516 // Object.prototype) and it will also not include 'replace' on objects that
2517 // extend String and change 'replace' (not that it is common for anyone to
2518 // extend anything except Object).
2519 for (var i = 0; i < goog.defineClass.OBJECT_PROTOTYPE_FIELDS_.length; i++) {
2520 key = goog.defineClass.OBJECT_PROTOTYPE_FIELDS_[i];
2521 if (Object.prototype.hasOwnProperty.call(source, key)) {
2522 target[key] = source[key];
2523 }
2524 }
2525};
2526
2527
2528/**
2529 * Sealing classes breaks the older idiom of assigning properties on the
2530 * prototype rather than in the constructor. As such, goog.defineClass
2531 * must not seal subclasses of these old-style classes until they are fixed.
2532 * Until then, this marks a class as "broken", instructing defineClass
2533 * not to seal subclasses.
2534 * @param {!Function} ctr The legacy constructor to tag as unsealable.
2535 */
2536goog.tagUnsealableClass = function(ctr) {
2537 if (!COMPILED && goog.defineClass.SEAL_CLASS_INSTANCES) {
2538 ctr.prototype[goog.UNSEALABLE_CONSTRUCTOR_PROPERTY_] = true;
2539 }
2540};
2541
2542
2543/**
2544 * Name for unsealable tag property.
2545 * @const @private {string}
2546 */
2547goog.UNSEALABLE_CONSTRUCTOR_PROPERTY_ = 'goog_defineClass_legacy_unsealable';