1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 * @fileoverview Bootstrap for the Google JS Library (Closure).
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.
22 * @author pupius@google.com (Dan Pupius)
23 * @author arv@google.com (Erik Arvidsson)
30 * @define {boolean} Overridden to true by the compiler when --closure_pass
31 * or --mark_as_compiled is specified.
37 * Base namespace for the Closure library. Checks to see goog is
38 * already defined in the current scope before assigning to prevent
39 * clobbering if base.js is loaded more than once.
43 var goog = goog || {};
47 * Reference to the global context. In most cases this will be 'window'.
53 * A hook for overriding the define values in uncompiled mode.
55 * In uncompiled mode, {@code CLOSURE_DEFINES} may be defined before loading
56 * base.js. If a key is defined in {@code CLOSURE_DEFINES}, {@code goog.define}
57 * will use the value instead of the default value. This allows flags to be
58 * overwritten without compilation (this is normally accomplished with the
59 * compiler's "define" flag).
63 * var CLOSURE_DEFINES = {'goog.DEBUG', false};
66 * @type {Object.<string, (string|number|boolean)>|undefined}
68 goog.global.CLOSURE_DEFINES;
72 * Builds an object structure for the provided namespace path,
73 * ensuring that names that already exist are not overwritten. For
75 * "a.b.c" -> a = {};a.b={};a.b.c={};
76 * Used by goog.provide and goog.exportSymbol.
77 * @param {string} name name of the object that this file defines.
78 * @param {*=} opt_object the object to expose at the end of the path.
79 * @param {Object=} opt_objectToExportTo The object to add the path to; default
83 goog.exportPath_ = function(name, opt_object, opt_objectToExportTo) {
84 var parts = name.split('.');
85 var cur = opt_objectToExportTo || goog.global;
87 // Internet Explorer exhibits strange behavior when throwing errors from
88 // methods externed in this manner. See the testExportSymbolExceptions in
89 // base_test.html for an example.
90 if (!(parts[0] in cur) && cur.execScript) {
91 cur.execScript('var ' + parts[0]);
94 // Certain browsers cannot parse code in the form for((a in b); c;);
95 // This pattern is produced by the JSCompiler when it collapses the
96 // statement above into the conditional loop below. To prevent this from
97 // happening, use a for-loop and reserve the init logic as below.
99 // Parentheses added to eliminate strict JS warning in Firefox.
100 for (var part; parts.length && (part = parts.shift());) {
101 if (!parts.length && opt_object !== undefined) {
102 // last part and we have an object; use it
103 cur[part] = opt_object;
104 } else if (cur[part]) {
107 cur = cur[part] = {};
114 * Defines a named value. In uncompiled mode, the value is retreived from
115 * CLOSURE_DEFINES if the object is defined and has the property specified,
116 * and otherwise used the defined defaultValue. When compiled, the default
117 * can be overridden using compiler command-line options.
119 * @param {string} name The distinguished name to provide.
120 * @param {string|number|boolean} defaultValue
122 goog.define = function(name, defaultValue) {
123 var value = defaultValue;
125 if (goog.global.CLOSURE_DEFINES && Object.prototype.hasOwnProperty.call(
126 goog.global.CLOSURE_DEFINES, name)) {
127 value = goog.global.CLOSURE_DEFINES[name];
130 goog.exportPath_(name, value);
135 * @define {boolean} DEBUG is provided as a convenience so that debugging code
136 * that should not be included in a production js_binary can be easily stripped
137 * by specifying --define goog.DEBUG=false to the JSCompiler. For example, most
138 * toString() methods should be declared inside an "if (goog.DEBUG)" conditional
139 * because they are generally used for debugging purposes and it is difficult
140 * for the JSCompiler to statically determine whether they are used.
146 * @define {string} LOCALE defines the locale being used for compilation. It is
147 * used to select locale specific data to be compiled in js binary. BUILD rule
148 * can specify this value by "--define goog.LOCALE=<locale_name>" as JSCompiler
151 * Take into account that the locale code format is important. You should use
152 * the canonical Unicode format with hyphen as a delimiter. Language must be
153 * lowercase, Language Script - Capitalized, Region - UPPERCASE.
154 * There are few examples: pt-BR, en, en-US, sr-Latin-BO, zh-Hans-CN.
156 * See more info about locale codes here:
157 * http://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers
159 * For language codes you should use values defined by ISO 693-1. See it here
160 * http://www.w3.org/WAI/ER/IG/ert/iso639.htm. There is only one exception from
161 * this rule: the Hebrew language. For legacy reasons the old code (iw) should
162 * be used instead of the new code (he), see http://wiki/Main/IIISynonyms.
164 goog.define('goog.LOCALE', 'en'); // default to en
168 * @define {boolean} Whether this code is running on trusted sites.
170 * On untrusted sites, several native functions can be defined or overridden by
171 * external libraries like Prototype, Datejs, and JQuery and setting this flag
172 * to false forces closure to use its own implementations when possible.
174 * If your javascript can be loaded by a third party site and you are wary about
175 * relying on non-standard implementations, specify
176 * "--define goog.TRUSTED_SITE=false" to the JSCompiler.
178 goog.define('goog.TRUSTED_SITE', true);
182 * Creates object stubs for a namespace. The presence of one or more
183 * goog.provide() calls indicate that the file defines the given
184 * objects/namespaces. Build tools also scan for provide/require statements
185 * to discern dependencies, build dependency files (see deps.js), etc.
187 * @param {string} name Namespace provided by this file in the form
188 * "goog.package.part".
190 goog.provide = function(name) {
192 // Ensure that the same namespace isn't provided twice. This is intended
193 // to teach new developers that 'goog.provide' is effectively a variable
194 // declaration. And when JSCompiler transforms goog.provide into a real
195 // variable declaration, the compiled JS should work the same as the raw
196 // JS--even when the raw JS uses goog.provide incorrectly.
197 if (goog.isProvided_(name)) {
198 throw Error('Namespace "' + name + '" already declared.');
200 delete goog.implicitNamespaces_[name];
202 var namespace = name;
203 while ((namespace = namespace.substring(0, namespace.lastIndexOf('.')))) {
204 if (goog.getObjectByName(namespace)) {
207 goog.implicitNamespaces_[namespace] = true;
211 goog.exportPath_(name);
216 * Marks that the current file should only be used for testing, and never for
217 * live code in production.
219 * In the case of unit tests, the message may optionally be an exact
220 * namespace for the test (e.g. 'goog.stringTest'). The linter will then
221 * ignore the extra provide (if not explicitly defined in the code).
223 * @param {string=} opt_message Optional message to add to the error that's
224 * raised when used in production code.
226 goog.setTestOnly = function(opt_message) {
227 if (COMPILED && !goog.DEBUG) {
228 opt_message = opt_message || '';
229 throw Error('Importing test-only code into non-debug environment' +
230 opt_message ? ': ' + opt_message : '.');
238 * Check if the given name has been goog.provided. This will return false for
239 * names that are available only as implicit namespaces.
240 * @param {string} name name of the object to look for.
241 * @return {boolean} Whether the name has been provided.
244 goog.isProvided_ = function(name) {
245 return !goog.implicitNamespaces_[name] && !!goog.getObjectByName(name);
249 * Namespaces implicitly defined by goog.provide. For example,
250 * goog.provide('goog.events.Event') implicitly declares
251 * that 'goog' and 'goog.events' must be namespaces.
256 goog.implicitNamespaces_ = {};
261 * Returns an object based on its fully qualified external name. If you are
262 * using a compilation pass that renames property names beware that using this
263 * function will not find renamed properties.
265 * @param {string} name The fully qualified name.
266 * @param {Object=} opt_obj The object within which to look; default is
268 * @return {?} The value (object or primitive) or, if not found, null.
270 goog.getObjectByName = function(name, opt_obj) {
271 var parts = name.split('.');
272 var cur = opt_obj || goog.global;
273 for (var part; part = parts.shift(); ) {
274 if (goog.isDefAndNotNull(cur[part])) {
285 * Globalizes a whole namespace, such as goog or goog.lang.
287 * @param {Object} obj The namespace to globalize.
288 * @param {Object=} opt_global The object to add the properties to.
289 * @deprecated Properties may be explicitly exported to the global scope, but
290 * this should no longer be done in bulk.
292 goog.globalize = function(obj, opt_global) {
293 var global = opt_global || goog.global;
301 * Adds a dependency from a file to the files it requires.
302 * @param {string} relPath The path to the js file.
303 * @param {Array} provides An array of strings with the names of the objects
304 * this file provides.
305 * @param {Array} requires An array of strings with the names of the objects
306 * this file requires.
308 goog.addDependency = function(relPath, provides, requires) {
309 if (goog.DEPENDENCIES_ENABLED) {
310 var provide, require;
311 var path = relPath.replace(/\\/g, '/');
312 var deps = goog.dependencies_;
313 for (var i = 0; provide = provides[i]; i++) {
314 deps.nameToPath[provide] = path;
315 if (!(path in deps.pathToNames)) {
316 deps.pathToNames[path] = {};
318 deps.pathToNames[path][provide] = true;
320 for (var j = 0; require = requires[j]; j++) {
321 if (!(path in deps.requires)) {
322 deps.requires[path] = {};
324 deps.requires[path][require] = true;
332 * Whether goog.require should throw an exception if it fails.
335 goog.useStrictRequires = false;
341 // NOTE(nnaze): The debug DOM loader was included in base.js as an orignal
342 // way to do "debug-mode" development. The dependency system can sometimes
343 // be confusing, as can the debug DOM loader's asyncronous nature.
345 // With the DOM loader, a call to goog.require() is not blocking -- the
346 // script will not load until some point after the current script. If a
347 // namespace is needed at runtime, it needs to be defined in a previous
348 // script, or loaded via require() with its registered dependencies.
349 // User-defined namespaces may need their own deps file. See http://go/js_deps,
350 // http://go/genjsdeps, or, externally, DepsWriter.
351 // http://code.google.com/closure/library/docs/depswriter.html
353 // Because of legacy clients, the DOM loader can't be easily removed from
354 // base.js. Work is being done to make it disableable or replaceable for
355 // different environments (DOM-less JavaScript interpreters like Rhino or V8,
356 // for example). See bootstrap/ for more information.
360 * @define {boolean} Whether to enable the debug loader.
362 * If enabled, a call to goog.require() will attempt to load the namespace by
363 * appending a script tag to the DOM (if the namespace has been registered).
365 * If disabled, goog.require() will simply assert that the namespace has been
366 * provided (and depend on the fact that some outside tool correctly ordered
369 goog.define('goog.ENABLE_DEBUG_LOADER', true);
373 * Implements a system for the dynamic resolution of dependencies
374 * that works in parallel with the BUILD system. Note that all calls
375 * to goog.require will be stripped by the JSCompiler when the
376 * --closure_pass option is used.
378 * @param {string} name Namespace to include (as was given in goog.provide())
379 * in the form "goog.package.part".
381 goog.require = function(name) {
383 // if the object already exists we do not need do do anything
384 // TODO(arv): If we start to support require based on file name this has
386 // TODO(arv): If we allow goog.foo.* this has to change
387 // TODO(arv): If we implement dynamic load after page load we should probably
388 // not remove this code for the compiled output
390 if (goog.isProvided_(name)) {
394 if (goog.ENABLE_DEBUG_LOADER) {
395 var path = goog.getPathFromDeps_(name);
397 goog.included_[path] = true;
398 goog.writeScripts_();
403 var errorMessage = 'goog.require could not find: ' + name;
404 if (goog.global.console) {
405 goog.global.console['error'](errorMessage);
410 // NOTE(nicksantos): We could always throw an error, but this would break
411 // legacy users that depended on this failing silently. Instead, the
412 // compiler should warn us when there are invalid goog.require calls.
413 // For now, we simply give clients a way to turn strict mode on.
414 if (goog.useStrictRequires) {
417 throw Error(errorMessage);
427 * Path for included scripts
434 * A hook for overriding the base path.
435 * @type {string|undefined}
437 goog.global.CLOSURE_BASE_PATH;
441 * Whether to write out Closure's deps file. By default,
442 * the deps are written.
443 * @type {boolean|undefined}
445 goog.global.CLOSURE_NO_DEPS;
449 * A function to import a single script. This is meant to be overridden when
450 * Closure is being run in non-HTML contexts, such as web workers. It's defined
451 * in the global scope so that it can be set before base.js is loaded, which
452 * allows deps.js to be imported properly.
454 * The function is passed the script source, which is a relative URI. It should
455 * return true if the script was imported, false otherwise.
457 goog.global.CLOSURE_IMPORT_SCRIPT;
461 * Null function used for default values of callbacks, etc.
462 * @return {void} Nothing.
464 goog.nullFunction = function() {};
468 * The identity function. Returns its first argument.
470 * @param {*=} opt_returnValue The single value that will be returned.
471 * @param {...*} var_args Optional trailing arguments. These are ignored.
472 * @return {?} The first argument. We can't know the type -- just pass it along
474 * @deprecated Use goog.functions.identity instead.
476 goog.identityFunction = function(opt_returnValue, var_args) {
477 return opt_returnValue;
482 * When defining a class Foo with an abstract method bar(), you can do:
484 * Foo.prototype.bar = goog.abstractMethod
486 * Now if a subclass of Foo fails to override bar(), an error
487 * will be thrown when bar() is invoked.
489 * Note: This does not take the name of the function to override as
490 * an argument because that would make it more difficult to obfuscate
491 * our JavaScript code.
494 * @throws {Error} when invoked to indicate the method should be
497 goog.abstractMethod = function() {
498 throw Error('unimplemented abstract method');
503 * Adds a {@code getInstance} static method that always return the same instance
505 * @param {!Function} ctor The constructor for the class to add the static
508 goog.addSingletonGetter = function(ctor) {
509 ctor.getInstance = function() {
510 if (ctor.instance_) {
511 return ctor.instance_;
514 // NOTE: JSCompiler can't optimize away Array#push.
515 goog.instantiatedSingletons_[goog.instantiatedSingletons_.length] = ctor;
517 return ctor.instance_ = new ctor;
523 * All singleton classes that have been instantiated, for testing. Don't read
524 * it directly, use the {@code goog.testing.singleton} module. The compiler
525 * removes this variable if unused.
526 * @type {!Array.<!Function>}
529 goog.instantiatedSingletons_ = [];
533 * True if goog.dependencies_ is available.
536 goog.DEPENDENCIES_ENABLED = !COMPILED && goog.ENABLE_DEBUG_LOADER;
539 if (goog.DEPENDENCIES_ENABLED) {
541 * Object used to keep track of urls that have already been added. This
542 * record allows the prevention of circular dependencies.
550 * This object is used to keep track of dependencies and other data that is
551 * used for loading scripts
555 goog.dependencies_ = {
556 pathToNames: {}, // 1 to many
557 nameToPath: {}, // 1 to 1
558 requires: {}, // 1 to many
559 // used when resolving dependencies to prevent us from
560 // visiting the file twice
562 written: {} // used to keep track of script files we have written
567 * Tries to detect whether is in the context of an HTML document.
568 * @return {boolean} True if it looks like HTML document.
571 goog.inHtmlDocument_ = function() {
572 var doc = goog.global.document;
573 return typeof doc != 'undefined' &&
574 'write' in doc; // XULDocument misses write.
579 * Tries to detect the base path of the base.js script that bootstraps Closure
582 goog.findBasePath_ = function() {
583 if (goog.global.CLOSURE_BASE_PATH) {
584 goog.basePath = goog.global.CLOSURE_BASE_PATH;
586 } else if (!goog.inHtmlDocument_()) {
589 var doc = goog.global.document;
590 var scripts = doc.getElementsByTagName('script');
591 // Search backwards since the current script is in almost all cases the one
593 for (var i = scripts.length - 1; i >= 0; --i) {
594 var src = scripts[i].src;
595 var qmark = src.lastIndexOf('?');
596 var l = qmark == -1 ? src.length : qmark;
597 if (src.substr(l - 7, 7) == 'base.js') {
598 goog.basePath = src.substr(0, l - 7);
606 * Imports a script if, and only if, that script hasn't already been imported.
607 * (Must be called at execution time)
608 * @param {string} src Script source.
611 goog.importScript_ = function(src) {
612 var importScript = goog.global.CLOSURE_IMPORT_SCRIPT ||
613 goog.writeScriptTag_;
614 if (!goog.dependencies_.written[src] && importScript(src)) {
615 goog.dependencies_.written[src] = true;
621 * The default implementation of the import function. Writes a script tag to
624 * @param {string} src The script source.
625 * @return {boolean} True if the script was imported, false otherwise.
628 goog.writeScriptTag_ = function(src) {
629 if (goog.inHtmlDocument_()) {
630 var doc = goog.global.document;
632 // If the user tries to require a new symbol after document load,
633 // something has gone terribly wrong. Doing a document.write would
634 // wipe out the page.
635 if (doc.readyState == 'complete') {
636 // Certain test frameworks load base.js multiple times, which tries
637 // to write deps.js each time. If that happens, just fail silently.
638 // These frameworks wipe the page between each load of base.js, so this
640 var isDeps = /\bdeps.js$/.test(src);
644 throw Error('Cannot write "' + src + '" after document load');
649 '<script type="text/javascript" src="' + src + '"></' + 'script>');
658 * Resolves dependencies based on the dependencies added using addDependency
659 * and calls importScript_ in the correct order.
662 goog.writeScripts_ = function() {
663 // the scripts we need to write this time
666 var deps = goog.dependencies_;
668 function visitNode(path) {
669 if (path in deps.written) {
673 // we have already visited this one. We can get here if we have cyclic
675 if (path in deps.visited) {
676 if (!(path in seenScript)) {
677 seenScript[path] = true;
683 deps.visited[path] = true;
685 if (path in deps.requires) {
686 for (var requireName in deps.requires[path]) {
687 // If the required name is defined, we assume that it was already
688 // bootstrapped by other means.
689 if (!goog.isProvided_(requireName)) {
690 if (requireName in deps.nameToPath) {
691 visitNode(deps.nameToPath[requireName]);
693 throw Error('Undefined nameToPath for ' + requireName);
699 if (!(path in seenScript)) {
700 seenScript[path] = true;
705 for (var path in goog.included_) {
706 if (!deps.written[path]) {
711 for (var i = 0; i < scripts.length; i++) {
713 goog.importScript_(goog.basePath + scripts[i]);
715 throw Error('Undefined script input');
722 * Looks at the dependency rules and tries to determine the script file that
723 * fulfills a particular rule.
724 * @param {string} rule In the form goog.namespace.Class or project.script.
725 * @return {?string} Url corresponding to the rule, or null.
728 goog.getPathFromDeps_ = function(rule) {
729 if (rule in goog.dependencies_.nameToPath) {
730 return goog.dependencies_.nameToPath[rule];
736 goog.findBasePath_();
738 // Allow projects to manage the deps files themselves.
739 if (!goog.global.CLOSURE_NO_DEPS) {
740 goog.importScript_(goog.basePath + 'deps.js');
746 //==============================================================================
747 // Language Enhancements
748 //==============================================================================
752 * This is a "fixed" version of the typeof operator. It differs from the typeof
753 * operator in such a way that null returns 'null' and arrays return 'array'.
754 * @param {*} value The value to get the type of.
755 * @return {string} The name of the type.
757 goog.typeOf = function(value) {
758 var s = typeof value;
761 // Check these first, so we can avoid calling Object.prototype.toString if
764 // IE improperly marshals tyepof across execution contexts, but a
765 // cross-context object will still return false for "instanceof Object".
766 if (value instanceof Array) {
768 } else if (value instanceof Object) {
772 // HACK: In order to use an Object prototype method on the arbitrary
773 // value, the compiler requires the value be cast to type Object,
774 // even though the ECMA spec explicitly allows it.
775 var className = Object.prototype.toString.call(
776 /** @type {Object} */ (value));
777 // In Firefox 3.6, attempting to access iframe window objects' length
778 // property throws an NS_ERROR_FAILURE, so we need to special-case it
780 if (className == '[object Window]') {
784 // We cannot always use constructor == Array or instanceof Array because
785 // different frames have different Array objects. In IE6, if the iframe
786 // where the array was created is destroyed, the array loses its
787 // prototype. Then dereferencing val.splice here throws an exception, so
788 // we can't use goog.isFunction. Calling typeof directly returns 'unknown'
789 // so that will work. In this case, this function will return false and
790 // most array functions will still work because the array is still
791 // array-like (supports length and []) even though it has lost its
793 // Mark Miller noticed that Object.prototype.toString
794 // allows access to the unforgeable [[Class]] property.
795 // 15.2.4.2 Object.prototype.toString ( )
796 // When the toString method is called, the following steps are taken:
797 // 1. Get the [[Class]] property of this object.
798 // 2. Compute a string value by concatenating the three strings
799 // "[object ", Result(1), and "]".
800 // 3. Return Result(2).
801 // and this behavior survives the destruction of the execution context.
802 if ((className == '[object Array]' ||
803 // In IE all non value types are wrapped as objects across window
804 // boundaries (not iframe though) so we have to do object detection
805 // for this edge case
806 typeof value.length == 'number' &&
807 typeof value.splice != 'undefined' &&
808 typeof value.propertyIsEnumerable != 'undefined' &&
809 !value.propertyIsEnumerable('splice')
814 // HACK: There is still an array case that fails.
815 // function ArrayImpostor() {}
816 // ArrayImpostor.prototype = [];
817 // var impostor = new ArrayImpostor;
818 // this can be fixed by getting rid of the fast path
819 // (value instanceof Array) and solely relying on
820 // (value && Object.prototype.toString.vall(value) === '[object Array]')
821 // but that would require many more function calls and is not warranted
822 // unless closure code is receiving objects from untrusted sources.
824 // IE in cross-window calls does not correctly marshal the function type
825 // (it appears just as an object) so we cannot use just typeof val ==
826 // 'function'. However, if the object has a call property, it is a
828 if ((className == '[object Function]' ||
829 typeof value.call != 'undefined' &&
830 typeof value.propertyIsEnumerable != 'undefined' &&
831 !value.propertyIsEnumerable('call'))) {
840 } else if (s == 'function' && typeof value.call == 'undefined') {
841 // In Safari typeof nodeList returns 'function', and on Firefox
842 // typeof behaves similarly for HTML{Applet,Embed,Object}Elements
843 // and RegExps. We would like to return object for those and we can
844 // detect an invalid function by making sure that the function
845 // object has a call method.
853 * Returns true if the specified value is not |undefined|.
854 * WARNING: Do not use this to test if an object has a property. Use the in
855 * operator instead. Additionally, this function assumes that the global
856 * undefined variable has not been redefined.
857 * @param {*} val Variable to test.
858 * @return {boolean} Whether variable is defined.
860 goog.isDef = function(val) {
861 return val !== undefined;
866 * Returns true if the specified value is |null|
867 * @param {*} val Variable to test.
868 * @return {boolean} Whether variable is null.
870 goog.isNull = function(val) {
876 * Returns true if the specified value is defined and not null
877 * @param {*} val Variable to test.
878 * @return {boolean} Whether variable is defined and not null.
880 goog.isDefAndNotNull = function(val) {
881 // Note that undefined == null.
887 * Returns true if the specified value is an array
888 * @param {*} val Variable to test.
889 * @return {boolean} Whether variable is an array.
891 goog.isArray = function(val) {
892 return goog.typeOf(val) == 'array';
897 * Returns true if the object looks like an array. To qualify as array like
898 * the value needs to be either a NodeList or an object with a Number length
900 * @param {*} val Variable to test.
901 * @return {boolean} Whether variable is an array.
903 goog.isArrayLike = function(val) {
904 var type = goog.typeOf(val);
905 return type == 'array' || type == 'object' && typeof val.length == 'number';
910 * Returns true if the object looks like a Date. To qualify as Date-like
911 * the value needs to be an object and have a getFullYear() function.
912 * @param {*} val Variable to test.
913 * @return {boolean} Whether variable is a like a Date.
915 goog.isDateLike = function(val) {
916 return goog.isObject(val) && typeof val.getFullYear == 'function';
921 * Returns true if the specified value is a string
922 * @param {*} val Variable to test.
923 * @return {boolean} Whether variable is a string.
925 goog.isString = function(val) {
926 return typeof val == 'string';
931 * Returns true if the specified value is a boolean
932 * @param {*} val Variable to test.
933 * @return {boolean} Whether variable is boolean.
935 goog.isBoolean = function(val) {
936 return typeof val == 'boolean';
941 * Returns true if the specified value is a number
942 * @param {*} val Variable to test.
943 * @return {boolean} Whether variable is a number.
945 goog.isNumber = function(val) {
946 return typeof val == 'number';
951 * Returns true if the specified value is a function
952 * @param {*} val Variable to test.
953 * @return {boolean} Whether variable is a function.
955 goog.isFunction = function(val) {
956 return goog.typeOf(val) == 'function';
961 * Returns true if the specified value is an object. This includes arrays
963 * @param {*} val Variable to test.
964 * @return {boolean} Whether variable is an object.
966 goog.isObject = function(val) {
967 var type = typeof val;
968 return type == 'object' && val != null || type == 'function';
969 // return Object(val) === val also works, but is slower, especially if val is
975 * Gets a unique ID for an object. This mutates the object so that further
976 * calls with the same object as a parameter returns the same value. The unique
977 * ID is guaranteed to be unique across the current session amongst objects that
978 * are passed into {@code getUid}. There is no guarantee that the ID is unique
979 * or consistent across sessions. It is unsafe to generate unique ID for
980 * function prototypes.
982 * @param {Object} obj The object to get the unique ID for.
983 * @return {number} The unique ID for the object.
985 goog.getUid = function(obj) {
986 // TODO(arv): Make the type stricter, do not accept null.
988 // In Opera window.hasOwnProperty exists but always returns false so we avoid
989 // using it. As a consequence the unique ID generated for BaseClass.prototype
990 // and SubClass.prototype will be the same.
991 return obj[goog.UID_PROPERTY_] ||
992 (obj[goog.UID_PROPERTY_] = ++goog.uidCounter_);
997 * Removes the unique ID from an object. This is useful if the object was
998 * previously mutated using {@code goog.getUid} in which case the mutation is
1000 * @param {Object} obj The object to remove the unique ID field from.
1002 goog.removeUid = function(obj) {
1003 // TODO(arv): Make the type stricter, do not accept null.
1005 // DOM nodes in IE are not instance of Object and throws exception
1006 // for delete. Instead we try to use removeAttribute
1007 if ('removeAttribute' in obj) {
1008 obj.removeAttribute(goog.UID_PROPERTY_);
1012 delete obj[goog.UID_PROPERTY_];
1019 * Name for unique ID property. Initialized in a way to help avoid collisions
1020 * with other closure javascript on the same page.
1024 goog.UID_PROPERTY_ = 'closure_uid_' + ((Math.random() * 1e9) >>> 0);
1032 goog.uidCounter_ = 0;
1036 * Adds a hash code field to an object. The hash code is unique for the
1038 * @param {Object} obj The object to get the hash code for.
1039 * @return {number} The hash code for the object.
1040 * @deprecated Use goog.getUid instead.
1042 goog.getHashCode = goog.getUid;
1046 * Removes the hash code field from an object.
1047 * @param {Object} obj The object to remove the field from.
1048 * @deprecated Use goog.removeUid instead.
1050 goog.removeHashCode = goog.removeUid;
1054 * Clones a value. The input may be an Object, Array, or basic type. Objects and
1055 * arrays will be cloned recursively.
1058 * <code>goog.cloneObject</code> does not detect reference loops. Objects that
1059 * refer to themselves will cause infinite recursion.
1061 * <code>goog.cloneObject</code> is unaware of unique identifiers, and copies
1062 * UIDs created by <code>getUid</code> into cloned results.
1064 * @param {*} obj The value to clone.
1065 * @return {*} A clone of the input value.
1066 * @deprecated goog.cloneObject is unsafe. Prefer the goog.object methods.
1068 goog.cloneObject = function(obj) {
1069 var type = goog.typeOf(obj);
1070 if (type == 'object' || type == 'array') {
1074 var clone = type == 'array' ? [] : {};
1075 for (var key in obj) {
1076 clone[key] = goog.cloneObject(obj[key]);
1086 * A native implementation of goog.bind.
1087 * @param {Function} fn A function to partially apply.
1088 * @param {Object|undefined} selfObj Specifies the object which |this| should
1089 * point to when the function is run.
1090 * @param {...*} var_args Additional arguments that are partially
1091 * applied to the function.
1092 * @return {!Function} A partially-applied form of the function bind() was
1093 * invoked as a method of.
1095 * @suppress {deprecated} The compiler thinks that Function.prototype.bind
1096 * is deprecated because some people have declared a pure-JS version.
1097 * Only the pure-JS version is truly deprecated.
1099 goog.bindNative_ = function(fn, selfObj, var_args) {
1100 return /** @type {!Function} */ (fn.call.apply(fn.bind, arguments));
1105 * A pure-JS implementation of goog.bind.
1106 * @param {Function} fn A function to partially apply.
1107 * @param {Object|undefined} selfObj Specifies the object which |this| should
1108 * point to when the function is run.
1109 * @param {...*} var_args Additional arguments that are partially
1110 * applied to the function.
1111 * @return {!Function} A partially-applied form of the function bind() was
1112 * invoked as a method of.
1115 goog.bindJs_ = function(fn, selfObj, var_args) {
1120 if (arguments.length > 2) {
1121 var boundArgs = Array.prototype.slice.call(arguments, 2);
1123 // Prepend the bound arguments to the current arguments.
1124 var newArgs = Array.prototype.slice.call(arguments);
1125 Array.prototype.unshift.apply(newArgs, boundArgs);
1126 return fn.apply(selfObj, newArgs);
1131 return fn.apply(selfObj, arguments);
1138 * Partially applies this function to a particular 'this object' and zero or
1139 * more arguments. The result is a new function with some arguments of the first
1140 * function pre-filled and the value of |this| 'pre-specified'.<br><br>
1142 * Remaining arguments specified at call-time are appended to the pre-
1143 * specified ones.<br><br>
1145 * Also see: {@link #partial}.<br><br>
1148 * <pre>var barMethBound = bind(myFunction, myObj, 'arg1', 'arg2');
1149 * barMethBound('arg3', 'arg4');</pre>
1151 * @param {?function(this:T, ...)} fn A function to partially apply.
1152 * @param {T} selfObj Specifies the object which |this| should
1153 * point to when the function is run.
1154 * @param {...*} var_args Additional arguments that are partially
1155 * applied to the function.
1156 * @return {!Function} A partially-applied form of the function bind() was
1157 * invoked as a method of.
1159 * @suppress {deprecated} See above.
1161 goog.bind = function(fn, selfObj, var_args) {
1162 // TODO(nicksantos): narrow the type signature.
1163 if (Function.prototype.bind &&
1164 // NOTE(nicksantos): Somebody pulled base.js into the default
1165 // Chrome extension environment. This means that for Chrome extensions,
1166 // they get the implementation of Function.prototype.bind that
1167 // calls goog.bind instead of the native one. Even worse, we don't want
1168 // to introduce a circular dependency between goog.bind and
1169 // Function.prototype.bind, so we have to hack this to make sure it
1171 Function.prototype.bind.toString().indexOf('native code') != -1) {
1172 goog.bind = goog.bindNative_;
1174 goog.bind = goog.bindJs_;
1176 return goog.bind.apply(null, arguments);
1181 * Like bind(), except that a 'this object' is not required. Useful when the
1182 * target function is already bound.
1185 * var g = partial(f, arg1, arg2);
1188 * @param {Function} fn A function to partially apply.
1189 * @param {...*} var_args Additional arguments that are partially
1191 * @return {!Function} A partially-applied form of the function bind() was
1192 * invoked as a method of.
1194 goog.partial = function(fn, var_args) {
1195 var args = Array.prototype.slice.call(arguments, 1);
1197 // Prepend the bound arguments to the current arguments.
1198 var newArgs = Array.prototype.slice.call(arguments);
1199 newArgs.unshift.apply(newArgs, args);
1200 return fn.apply(this, newArgs);
1206 * Copies all the members of a source object to a target object. This method
1207 * does not work on all browsers for all objects that contain keys such as
1208 * toString or hasOwnProperty. Use goog.object.extend for this purpose.
1209 * @param {Object} target Target.
1210 * @param {Object} source Source.
1212 goog.mixin = function(target, source) {
1213 for (var x in source) {
1214 target[x] = source[x];
1217 // For IE7 or lower, the for-in-loop does not contain any properties that are
1218 // not enumerable on the prototype object (for example, isPrototypeOf from
1219 // Object.prototype) but also it will not include 'replace' on objects that
1220 // extend String and change 'replace' (not that it is common for anyone to
1221 // extend anything except Object).
1226 * @return {number} An integer value representing the number of milliseconds
1227 * between midnight, January 1, 1970 and the current time.
1229 goog.now = (goog.TRUSTED_SITE && Date.now) || (function() {
1230 // Unary plus operator converts its operand to a number which in the case of
1231 // a date is done by calling getTime().
1237 * Evals javascript in the global scope. In IE this uses execScript, other
1238 * browsers use goog.global.eval. If goog.global.eval does not evaluate in the
1239 * global scope (for example, in Safari), appends a script tag instead.
1240 * Throws an exception if neither execScript or eval is defined.
1241 * @param {string} script JavaScript string.
1243 goog.globalEval = function(script) {
1244 if (goog.global.execScript) {
1245 goog.global.execScript(script, 'JavaScript');
1246 } else if (goog.global.eval) {
1247 // Test to see if eval works
1248 if (goog.evalWorksForGlobals_ == null) {
1249 goog.global.eval('var _et_ = 1;');
1250 if (typeof goog.global['_et_'] != 'undefined') {
1251 delete goog.global['_et_'];
1252 goog.evalWorksForGlobals_ = true;
1254 goog.evalWorksForGlobals_ = false;
1258 if (goog.evalWorksForGlobals_) {
1259 goog.global.eval(script);
1261 var doc = goog.global.document;
1262 var scriptElt = doc.createElement('script');
1263 scriptElt.type = 'text/javascript';
1264 scriptElt.defer = false;
1265 // Note(pupius): can't use .innerHTML since "t('<test>')" will fail and
1266 // .text doesn't work in Safari 2. Therefore we append a text node.
1267 scriptElt.appendChild(doc.createTextNode(script));
1268 doc.body.appendChild(scriptElt);
1269 doc.body.removeChild(scriptElt);
1272 throw Error('goog.globalEval not available');
1278 * Indicates whether or not we can call 'eval' directly to eval code in the
1279 * global scope. Set to a Boolean by the first call to goog.globalEval (which
1280 * empirically tests whether eval works for globals). @see goog.globalEval
1284 goog.evalWorksForGlobals_ = null;
1288 * Optional map of CSS class names to obfuscated names used with
1289 * goog.getCssName().
1290 * @type {Object|undefined}
1292 * @see goog.setCssNameMapping
1294 goog.cssNameMapping_;
1298 * Optional obfuscation style for CSS class names. Should be set to either
1299 * 'BY_WHOLE' or 'BY_PART' if defined.
1300 * @type {string|undefined}
1302 * @see goog.setCssNameMapping
1304 goog.cssNameMappingStyle_;
1308 * Handles strings that are intended to be used as CSS class names.
1310 * This function works in tandem with @see goog.setCssNameMapping.
1312 * Without any mapping set, the arguments are simple joined with a
1313 * hyphen and passed through unaltered.
1315 * When there is a mapping, there are two possible styles in which
1316 * these mappings are used. In the BY_PART style, each part (i.e. in
1317 * between hyphens) of the passed in css name is rewritten according
1318 * to the map. In the BY_WHOLE style, the full css name is looked up in
1319 * the map directly. If a rewrite is not specified by the map, the
1320 * compiler will output a warning.
1322 * When the mapping is passed to the compiler, it will replace calls
1323 * to goog.getCssName with the strings from the mapping, e.g.
1324 * var x = goog.getCssName('foo');
1325 * var y = goog.getCssName(this.baseClass, 'active');
1328 * var y = this.baseClass + '-active';
1330 * If one argument is passed it will be processed, if two are passed
1331 * only the modifier will be processed, as it is assumed the first
1332 * argument was generated as a result of calling goog.getCssName.
1334 * @param {string} className The class name.
1335 * @param {string=} opt_modifier A modifier to be appended to the class name.
1336 * @return {string} The class name or the concatenation of the class name and
1339 goog.getCssName = function(className, opt_modifier) {
1340 var getMapping = function(cssName) {
1341 return goog.cssNameMapping_[cssName] || cssName;
1344 var renameByParts = function(cssName) {
1345 // Remap all the parts individually.
1346 var parts = cssName.split('-');
1348 for (var i = 0; i < parts.length; i++) {
1349 mapped.push(getMapping(parts[i]));
1351 return mapped.join('-');
1355 if (goog.cssNameMapping_) {
1356 rename = goog.cssNameMappingStyle_ == 'BY_WHOLE' ?
1357 getMapping : renameByParts;
1359 rename = function(a) {
1365 return className + '-' + rename(opt_modifier);
1367 return rename(className);
1373 * Sets the map to check when returning a value from goog.getCssName(). Example:
1375 * goog.setCssNameMapping({
1380 * var x = goog.getCssName('goog');
1381 * // The following evaluates to: "a a-b".
1382 * goog.getCssName('goog') + ' ' + goog.getCssName(x, 'disabled')
1384 * When declared as a map of string literals to string literals, the JSCompiler
1385 * will replace all calls to goog.getCssName() using the supplied map if the
1386 * --closure_pass flag is set.
1388 * @param {!Object} mapping A map of strings to strings where keys are possible
1389 * arguments to goog.getCssName() and values are the corresponding values
1390 * that should be returned.
1391 * @param {string=} opt_style The style of css name mapping. There are two valid
1392 * options: 'BY_PART', and 'BY_WHOLE'.
1393 * @see goog.getCssName for a description.
1395 goog.setCssNameMapping = function(mapping, opt_style) {
1396 goog.cssNameMapping_ = mapping;
1397 goog.cssNameMappingStyle_ = opt_style;
1402 * To use CSS renaming in compiled mode, one of the input files should have a
1403 * call to goog.setCssNameMapping() with an object literal that the JSCompiler
1404 * can extract and use to replace all calls to goog.getCssName(). In uncompiled
1405 * mode, JavaScript code should be loaded before this base.js file that declares
1406 * a global variable, CLOSURE_CSS_NAME_MAPPING, which is used below. This is
1407 * to ensure that the mapping is loaded before any calls to goog.getCssName()
1408 * are made in uncompiled mode.
1410 * A hook for overriding the CSS name mapping.
1411 * @type {Object|undefined}
1413 goog.global.CLOSURE_CSS_NAME_MAPPING;
1416 if (!COMPILED && goog.global.CLOSURE_CSS_NAME_MAPPING) {
1417 // This does not call goog.setCssNameMapping() because the JSCompiler
1418 // requires that goog.setCssNameMapping() be called with an object literal.
1419 goog.cssNameMapping_ = goog.global.CLOSURE_CSS_NAME_MAPPING;
1424 * Gets a localized message.
1426 * This function is a compiler primitive. If you give the compiler a localized
1427 * message bundle, it will replace the string at compile-time with a localized
1428 * version, and expand goog.getMsg call to a concatenated string.
1430 * Messages must be initialized in the form:
1432 * var MSG_NAME = goog.getMsg('Hello {$placeholder}', {'placeholder': 'world'});
1435 * @param {string} str Translatable string, places holders in the form {$foo}.
1436 * @param {Object=} opt_values Map of place holder name to value.
1437 * @return {string} message with placeholders filled.
1439 goog.getMsg = function(str, opt_values) {
1440 var values = opt_values || {};
1441 for (var key in values) {
1442 var value = ('' + values[key]).replace(/\$/g, '$$$$');
1443 str = str.replace(new RegExp('\\{\\$' + key + '\\}', 'gi'), value);
1450 * Gets a localized message. If the message does not have a translation, gives a
1453 * This is useful when introducing a new message that has not yet been
1454 * translated into all languages.
1456 * This function is a compiler primtive. Must be used in the form:
1457 * <code>var x = goog.getMsgWithFallback(MSG_A, MSG_B);</code>
1458 * where MSG_A and MSG_B were initialized with goog.getMsg.
1460 * @param {string} a The preferred message.
1461 * @param {string} b The fallback message.
1462 * @return {string} The best translated message.
1464 goog.getMsgWithFallback = function(a, b) {
1470 * Exposes an unobfuscated global namespace path for the given object.
1471 * Note that fields of the exported object *will* be obfuscated,
1472 * unless they are exported in turn via this function or
1473 * goog.exportProperty
1475 * <p>Also handy for making public items that are defined in anonymous
1478 * ex. goog.exportSymbol('public.path.Foo', Foo);
1480 * ex. goog.exportSymbol('public.path.Foo.staticFunction',
1481 * Foo.staticFunction);
1482 * public.path.Foo.staticFunction();
1484 * ex. goog.exportSymbol('public.path.Foo.prototype.myMethod',
1485 * Foo.prototype.myMethod);
1486 * new public.path.Foo().myMethod();
1488 * @param {string} publicPath Unobfuscated name to export.
1489 * @param {*} object Object the name should point to.
1490 * @param {Object=} opt_objectToExportTo The object to add the path to; default
1493 goog.exportSymbol = function(publicPath, object, opt_objectToExportTo) {
1494 goog.exportPath_(publicPath, object, opt_objectToExportTo);
1499 * Exports a property unobfuscated into the object's namespace.
1500 * ex. goog.exportProperty(Foo, 'staticFunction', Foo.staticFunction);
1501 * ex. goog.exportProperty(Foo.prototype, 'myMethod', Foo.prototype.myMethod);
1502 * @param {Object} object Object whose static property is being exported.
1503 * @param {string} publicName Unobfuscated name to export.
1504 * @param {*} symbol Object the name should point to.
1506 goog.exportProperty = function(object, publicName, symbol) {
1507 object[publicName] = symbol;
1512 * Inherit the prototype methods from one constructor into another.
1516 * function ParentClass(a, b) { }
1517 * ParentClass.prototype.foo = function(a) { }
1519 * function ChildClass(a, b, c) {
1520 * goog.base(this, a, b);
1522 * goog.inherits(ChildClass, ParentClass);
1524 * var child = new ChildClass('a', 'b', 'see');
1525 * child.foo(); // works
1528 * In addition, a superclass' implementation of a method can be invoked
1532 * ChildClass.prototype.foo = function(a) {
1533 * ChildClass.superClass_.foo.call(this, a);
1538 * @param {Function} childCtor Child class.
1539 * @param {Function} parentCtor Parent class.
1541 goog.inherits = function(childCtor, parentCtor) {
1543 function tempCtor() {};
1544 tempCtor.prototype = parentCtor.prototype;
1545 childCtor.superClass_ = parentCtor.prototype;
1546 childCtor.prototype = new tempCtor();
1548 childCtor.prototype.constructor = childCtor;
1553 * Call up to the superclass.
1555 * If this is called from a constructor, then this calls the superclass
1556 * contructor with arguments 1-N.
1558 * If this is called from a prototype method, then you must pass
1559 * the name of the method as the second argument to this function. If
1560 * you do not, you will get a runtime error. This calls the superclass'
1561 * method with arguments 2-N.
1563 * This function only works if you use goog.inherits to express
1564 * inheritance relationships between your classes.
1566 * This function is a compiler primitive. At compile-time, the
1567 * compiler will do macro expansion to remove a lot of
1568 * the extra overhead that this function introduces. The compiler
1569 * will also enforce a lot of the assumptions that this function
1570 * makes, and treat it as a compiler error if you break them.
1572 * @param {!Object} me Should always be "this".
1573 * @param {*=} opt_methodName The method name if calling a super method.
1574 * @param {...*} var_args The rest of the arguments.
1575 * @return {*} The return value of the superclass method.
1577 goog.base = function(me, opt_methodName, var_args) {
1578 var caller = arguments.callee.caller;
1582 throw Error('arguments.caller not defined. goog.base() expects not ' +
1583 'to be running in strict mode. See ' +
1584 'http://www.ecma-international.org/ecma-262/5.1/#sec-C');
1588 if (caller.superClass_) {
1589 // This is a constructor. Call the superclass constructor.
1590 return caller.superClass_.constructor.apply(
1591 me, Array.prototype.slice.call(arguments, 1));
1594 var args = Array.prototype.slice.call(arguments, 2);
1595 var foundCaller = false;
1596 for (var ctor = me.constructor;
1597 ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
1598 if (ctor.prototype[opt_methodName] === caller) {
1600 } else if (foundCaller) {
1601 return ctor.prototype[opt_methodName].apply(me, args);
1605 // If we did not find the caller in the prototype chain,
1606 // then one of two things happened:
1607 // 1) The caller is an instance method.
1608 // 2) This method was not called by the right caller.
1609 if (me[opt_methodName] === caller) {
1610 return me.constructor.prototype[opt_methodName].apply(me, args);
1613 'goog.base called from a method of one name ' +
1614 'to a method of a different name');
1620 * Allow for aliasing within scope functions. This function exists for
1621 * uncompiled code - in compiled code the calls will be inlined and the
1622 * aliases applied. In uncompiled code the function is simply run since the
1623 * aliases as written are valid JavaScript.
1624 * @param {function()} fn Function to call. This function can contain aliases
1625 * to namespaces (e.g. "var dom = goog.dom") or classes
1626 * (e.g. "var Timer = goog.Timer").
1628 goog.scope = function(fn) {
1629 fn.call(goog.global);
1635 // The section between this token and the end token below will be stripped
1636 // automatically by the open source release scripts. Please leave in place.
1638 //==============================================================================
1639 // Extending Function
1640 //==============================================================================
1644 * @define {boolean} Whether to extend Function.prototype.
1645 * Use --define='goog.MODIFY_FUNCTION_PROTOTYPES=false' to change.
1647 goog.define('goog.MODIFY_FUNCTION_PROTOTYPES', true);
1649 if (goog.MODIFY_FUNCTION_PROTOTYPES) {
1651 * An alias to the {@link goog.bind()} global function.
1654 * var g = f.bind(obj, arg1, arg2);
1657 * @param {Object} selfObj Specifies the object to which |this| should point
1658 * when the function is run. If the value is null or undefined, it will
1659 * default to the global object.
1660 * @param {...*} var_args Additional arguments that are partially
1662 * @return {!Function} A partially-applied form of the Function on which
1663 * bind() was invoked as a method.
1664 * @deprecated Use the static function goog.bind instead.
1665 * @suppress {duplicate}
1667 Function.prototype.bind =
1668 Function.prototype.bind || function(selfObj, var_args) {
1669 if (arguments.length > 1) {
1670 var args = Array.prototype.slice.call(arguments, 1);
1671 args.unshift(this, selfObj);
1672 return goog.bind.apply(null, args);
1674 return goog.bind(this, selfObj);
1680 * An alias to the {@link goog.partial()} static function.
1683 * var g = f.partial(arg1, arg2);
1686 * @param {...*} var_args Additional arguments that are partially
1688 * @return {!Function} A partially-applied form of the function partial() was
1689 * invoked as a method of.
1690 * @deprecated Use the static function goog.partial instead.
1692 Function.prototype.partial = function(var_args) {
1693 var args = Array.prototype.slice.call(arguments);
1694 args.unshift(this, null);
1695 return goog.bind.apply(null, args);
1700 * Inherit the prototype methods from one constructor into another.
1701 * @param {Function} parentCtor Parent class.
1702 * @see goog.inherits
1703 * @deprecated Use the static function goog.inherits instead.
1705 Function.prototype.inherits = function(parentCtor) {
1706 goog.inherits(this, parentCtor);
1711 * Mixes in an object's properties and methods into the callee's prototype.
1712 * Basically mixin based inheritance, thus providing an alternative method for
1713 * adding properties and methods to a class' prototype.
1721 * doit: function() { return this.one + this.two + this.three; }
1725 * Y.mixin(X.prototype);
1726 * Y.prototype.four = 15;
1727 * Y.prototype.doit2 = function() { return this.doit() + this.four; }
1737 * doit2: function() { return this.doit() + this.four; }
1741 * @param {Object} source from which to copy properties.
1743 * @deprecated Use the static function goog.object.extend instead.
1745 Function.prototype.mixin = function(source) {
1746 goog.mixin(this.prototype, source);