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