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 pupius (a] google.com (Dan Pupius) 23 * @author arv (a] google.com (Erik Arvidsson) 24 * 25 * @provideGoog 26 */ 27 28 29 /** 30 * @define {boolean} Overridden to true by the compiler when --closure_pass 31 * or --mark_as_compiled is specified. 32 */ 33 var COMPILED = false; 34 35 36 /** 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. 40 * 41 * @const 42 */ 43 var goog = goog || {}; 44 45 46 /** 47 * Reference to the global context. In most cases this will be 'window'. 48 */ 49 goog.global = this; 50 51 52 /** 53 * A hook for overriding the define values in uncompiled mode. 54 * 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). 60 * 61 * Example: 62 * <pre> 63 * var CLOSURE_DEFINES = {'goog.DEBUG', false}; 64 * </pre> 65 * 66 * @type {Object.<string, (string|number|boolean)>|undefined} 67 */ 68 goog.global.CLOSURE_DEFINES; 69 70 71 /** 72 * Builds an object structure for the provided namespace path, 73 * ensuring that names that already exist are not overwritten. For 74 * example: 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 80 * is |goog.global|. 81 * @private 82 */ 83 goog.exportPath_ = function(name, opt_object, opt_objectToExportTo) { 84 var parts = name.split('.'); 85 var cur = opt_objectToExportTo || goog.global; 86 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]); 92 } 93 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. 98 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]) { 105 cur = cur[part]; 106 } else { 107 cur = cur[part] = {}; 108 } 109 } 110 }; 111 112 113 /** 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. 118 * 119 * @param {string} name The distinguished name to provide. 120 * @param {string|number|boolean} defaultValue 121 */ 122 goog.define = function(name, defaultValue) { 123 var value = defaultValue; 124 if (!COMPILED) { 125 if (goog.global.CLOSURE_DEFINES && Object.prototype.hasOwnProperty.call( 126 goog.global.CLOSURE_DEFINES, name)) { 127 value = goog.global.CLOSURE_DEFINES[name]; 128 } 129 } 130 goog.exportPath_(name, value); 131 }; 132 133 134 /** 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. 141 */ 142 goog.DEBUG = true; 143 144 145 /** 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 149 * option. 150 * 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. 155 * 156 * See more info about locale codes here: 157 * http://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers 158 * 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. 163 */ 164 goog.define('goog.LOCALE', 'en'); // default to en 165 166 167 /** 168 * @define {boolean} Whether this code is running on trusted sites. 169 * 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. 173 * 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. 177 */ 178 goog.define('goog.TRUSTED_SITE', true); 179 180 181 /** 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. 186 * @see goog.require 187 * @param {string} name Namespace provided by this file in the form 188 * "goog.package.part". 189 */ 190 goog.provide = function(name) { 191 if (!COMPILED) { 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.'); 199 } 200 delete goog.implicitNamespaces_[name]; 201 202 var namespace = name; 203 while ((namespace = namespace.substring(0, namespace.lastIndexOf('.')))) { 204 if (goog.getObjectByName(namespace)) { 205 break; 206 } 207 goog.implicitNamespaces_[namespace] = true; 208 } 209 } 210 211 goog.exportPath_(name); 212 }; 213 214 215 /** 216 * Marks that the current file should only be used for testing, and never for 217 * live code in production. 218 * 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). 222 * 223 * @param {string=} opt_message Optional message to add to the error that's 224 * raised when used in production code. 225 */ 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 : '.'); 231 } 232 }; 233 234 235 if (!COMPILED) { 236 237 /** 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. 242 * @private 243 */ 244 goog.isProvided_ = function(name) { 245 return !goog.implicitNamespaces_[name] && !!goog.getObjectByName(name); 246 }; 247 248 /** 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. 252 * 253 * @type {Object} 254 * @private 255 */ 256 goog.implicitNamespaces_ = {}; 257 } 258 259 260 /** 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. 264 * 265 * @param {string} name The fully qualified name. 266 * @param {Object=} opt_obj The object within which to look; default is 267 * |goog.global|. 268 * @return {?} The value (object or primitive) or, if not found, null. 269 */ 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])) { 275 cur = cur[part]; 276 } else { 277 return null; 278 } 279 } 280 return cur; 281 }; 282 283 284 /** 285 * Globalizes a whole namespace, such as goog or goog.lang. 286 * 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. 291 */ 292 goog.globalize = function(obj, opt_global) { 293 var global = opt_global || goog.global; 294 for (var x in obj) { 295 global[x] = obj[x]; 296 } 297 }; 298 299 300 /** 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. 307 */ 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] = {}; 317 } 318 deps.pathToNames[path][provide] = true; 319 } 320 for (var j = 0; require = requires[j]; j++) { 321 if (!(path in deps.requires)) { 322 deps.requires[path] = {}; 323 } 324 deps.requires[path][require] = true; 325 } 326 } 327 }; 328 329 330 // MOE:begin_strip 331 /** 332 * Whether goog.require should throw an exception if it fails. 333 * @type {boolean} 334 */ 335 goog.useStrictRequires = false; 336 337 338 // MOE:end_strip 339 340 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. 344 // 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 352 // 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. 357 358 359 /** 360 * @define {boolean} Whether to enable the debug loader. 361 * 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). 364 * 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 367 * the script). 368 */ 369 goog.define('goog.ENABLE_DEBUG_LOADER', true); 370 371 372 /** 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. 377 * @see goog.provide 378 * @param {string} name Namespace to include (as was given in goog.provide()) 379 * in the form "goog.package.part". 380 */ 381 goog.require = function(name) { 382 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 385 // to change 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 389 if (!COMPILED) { 390 if (goog.isProvided_(name)) { 391 return; 392 } 393 394 if (goog.ENABLE_DEBUG_LOADER) { 395 var path = goog.getPathFromDeps_(name); 396 if (path) { 397 goog.included_[path] = true; 398 goog.writeScripts_(); 399 return; 400 } 401 } 402 403 var errorMessage = 'goog.require could not find: ' + name; 404 if (goog.global.console) { 405 goog.global.console['error'](errorMessage); 406 } 407 408 // MOE:begin_strip 409 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) { 415 // MOE:end_strip 416 417 throw Error(errorMessage); 418 419 // MOE:begin_strip 420 } 421 // MOE:end_strip 422 } 423 }; 424 425 426 /** 427 * Path for included scripts 428 * @type {string} 429 */ 430 goog.basePath = ''; 431 432 433 /** 434 * A hook for overriding the base path. 435 * @type {string|undefined} 436 */ 437 goog.global.CLOSURE_BASE_PATH; 438 439 440 /** 441 * Whether to write out Closure's deps file. By default, 442 * the deps are written. 443 * @type {boolean|undefined} 444 */ 445 goog.global.CLOSURE_NO_DEPS; 446 447 448 /** 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. 453 * 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. 456 */ 457 goog.global.CLOSURE_IMPORT_SCRIPT; 458 459 460 /** 461 * Null function used for default values of callbacks, etc. 462 * @return {void} Nothing. 463 */ 464 goog.nullFunction = function() {}; 465 466 467 /** 468 * The identity function. Returns its first argument. 469 * 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 473 * without type. 474 * @deprecated Use goog.functions.identity instead. 475 */ 476 goog.identityFunction = function(opt_returnValue, var_args) { 477 return opt_returnValue; 478 }; 479 480 481 /** 482 * When defining a class Foo with an abstract method bar(), you can do: 483 * 484 * Foo.prototype.bar = goog.abstractMethod 485 * 486 * Now if a subclass of Foo fails to override bar(), an error 487 * will be thrown when bar() is invoked. 488 * 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. 492 * 493 * @type {!Function} 494 * @throws {Error} when invoked to indicate the method should be 495 * overridden. 496 */ 497 goog.abstractMethod = function() { 498 throw Error('unimplemented abstract method'); 499 }; 500 501 502 /** 503 * Adds a {@code getInstance} static method that always return the same instance 504 * object. 505 * @param {!Function} ctor The constructor for the class to add the static 506 * method to. 507 */ 508 goog.addSingletonGetter = function(ctor) { 509 ctor.getInstance = function() { 510 if (ctor.instance_) { 511 return ctor.instance_; 512 } 513 if (goog.DEBUG) { 514 // NOTE: JSCompiler can't optimize away Array#push. 515 goog.instantiatedSingletons_[goog.instantiatedSingletons_.length] = ctor; 516 } 517 return ctor.instance_ = new ctor; 518 }; 519 }; 520 521 522 /** 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>} 527 * @private 528 */ 529 goog.instantiatedSingletons_ = []; 530 531 532 /** 533 * True if goog.dependencies_ is available. 534 * @const {boolean} 535 */ 536 goog.DEPENDENCIES_ENABLED = !COMPILED && goog.ENABLE_DEBUG_LOADER; 537 538 539 if (goog.DEPENDENCIES_ENABLED) { 540 /** 541 * Object used to keep track of urls that have already been added. This 542 * record allows the prevention of circular dependencies. 543 * @type {Object} 544 * @private 545 */ 546 goog.included_ = {}; 547 548 549 /** 550 * This object is used to keep track of dependencies and other data that is 551 * used for loading scripts 552 * @private 553 * @type {Object} 554 */ 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 561 visited: {}, 562 written: {} // used to keep track of script files we have written 563 }; 564 565 566 /** 567 * Tries to detect whether is in the context of an HTML document. 568 * @return {boolean} True if it looks like HTML document. 569 * @private 570 */ 571 goog.inHtmlDocument_ = function() { 572 var doc = goog.global.document; 573 return typeof doc != 'undefined' && 574 'write' in doc; // XULDocument misses write. 575 }; 576 577 578 /** 579 * Tries to detect the base path of the base.js script that bootstraps Closure 580 * @private 581 */ 582 goog.findBasePath_ = function() { 583 if (goog.global.CLOSURE_BASE_PATH) { 584 goog.basePath = goog.global.CLOSURE_BASE_PATH; 585 return; 586 } else if (!goog.inHtmlDocument_()) { 587 return; 588 } 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 592 // that has base.js. 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); 599 return; 600 } 601 } 602 }; 603 604 605 /** 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. 609 * @private 610 */ 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; 616 } 617 }; 618 619 620 /** 621 * The default implementation of the import function. Writes a script tag to 622 * import the script. 623 * 624 * @param {string} src The script source. 625 * @return {boolean} True if the script was imported, false otherwise. 626 * @private 627 */ 628 goog.writeScriptTag_ = function(src) { 629 if (goog.inHtmlDocument_()) { 630 var doc = goog.global.document; 631 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 639 // is OK. 640 var isDeps = /\bdeps.js$/.test(src); 641 if (isDeps) { 642 return false; 643 } else { 644 throw Error('Cannot write "' + src + '" after document load'); 645 } 646 } 647 648 doc.write( 649 '<script type="text/javascript" src="' + src + '"></' + 'script>'); 650 return true; 651 } else { 652 return false; 653 } 654 }; 655 656 657 /** 658 * Resolves dependencies based on the dependencies added using addDependency 659 * and calls importScript_ in the correct order. 660 * @private 661 */ 662 goog.writeScripts_ = function() { 663 // the scripts we need to write this time 664 var scripts = []; 665 var seenScript = {}; 666 var deps = goog.dependencies_; 667 668 function visitNode(path) { 669 if (path in deps.written) { 670 return; 671 } 672 673 // we have already visited this one. We can get here if we have cyclic 674 // dependencies 675 if (path in deps.visited) { 676 if (!(path in seenScript)) { 677 seenScript[path] = true; 678 scripts.push(path); 679 } 680 return; 681 } 682 683 deps.visited[path] = true; 684 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]); 692 } else { 693 throw Error('Undefined nameToPath for ' + requireName); 694 } 695 } 696 } 697 } 698 699 if (!(path in seenScript)) { 700 seenScript[path] = true; 701 scripts.push(path); 702 } 703 } 704 705 for (var path in goog.included_) { 706 if (!deps.written[path]) { 707 visitNode(path); 708 } 709 } 710 711 for (var i = 0; i < scripts.length; i++) { 712 if (scripts[i]) { 713 goog.importScript_(goog.basePath + scripts[i]); 714 } else { 715 throw Error('Undefined script input'); 716 } 717 } 718 }; 719 720 721 /** 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. 726 * @private 727 */ 728 goog.getPathFromDeps_ = function(rule) { 729 if (rule in goog.dependencies_.nameToPath) { 730 return goog.dependencies_.nameToPath[rule]; 731 } else { 732 return null; 733 } 734 }; 735 736 goog.findBasePath_(); 737 738 // Allow projects to manage the deps files themselves. 739 if (!goog.global.CLOSURE_NO_DEPS) { 740 goog.importScript_(goog.basePath + 'deps.js'); 741 } 742 } 743 744 745 746 //============================================================================== 747 // Language Enhancements 748 //============================================================================== 749 750 751 /** 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. 756 */ 757 goog.typeOf = function(value) { 758 var s = typeof value; 759 if (s == 'object') { 760 if (value) { 761 // Check these first, so we can avoid calling Object.prototype.toString if 762 // possible. 763 // 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) { 767 return 'array'; 768 } else if (value instanceof Object) { 769 return s; 770 } 771 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 779 // here. 780 if (className == '[object Window]') { 781 return 'object'; 782 } 783 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 792 // prototype. 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') 810 811 )) { 812 return 'array'; 813 } 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. 823 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 827 // function. 828 if ((className == '[object Function]' || 829 typeof value.call != 'undefined' && 830 typeof value.propertyIsEnumerable != 'undefined' && 831 !value.propertyIsEnumerable('call'))) { 832 return 'function'; 833 } 834 835 836 } else { 837 return 'null'; 838 } 839 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. 846 return 'object'; 847 } 848 return s; 849 }; 850 851 852 /** 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. 859 */ 860 goog.isDef = function(val) { 861 return val !== undefined; 862 }; 863 864 865 /** 866 * Returns true if the specified value is |null| 867 * @param {*} val Variable to test. 868 * @return {boolean} Whether variable is null. 869 */ 870 goog.isNull = function(val) { 871 return val === null; 872 }; 873 874 875 /** 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. 879 */ 880 goog.isDefAndNotNull = function(val) { 881 // Note that undefined == null. 882 return val != null; 883 }; 884 885 886 /** 887 * Returns true if the specified value is an array 888 * @param {*} val Variable to test. 889 * @return {boolean} Whether variable is an array. 890 */ 891 goog.isArray = function(val) { 892 return goog.typeOf(val) == 'array'; 893 }; 894 895 896 /** 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 899 * property. 900 * @param {*} val Variable to test. 901 * @return {boolean} Whether variable is an array. 902 */ 903 goog.isArrayLike = function(val) { 904 var type = goog.typeOf(val); 905 return type == 'array' || type == 'object' && typeof val.length == 'number'; 906 }; 907 908 909 /** 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. 914 */ 915 goog.isDateLike = function(val) { 916 return goog.isObject(val) && typeof val.getFullYear == 'function'; 917 }; 918 919 920 /** 921 * Returns true if the specified value is a string 922 * @param {*} val Variable to test. 923 * @return {boolean} Whether variable is a string. 924 */ 925 goog.isString = function(val) { 926 return typeof val == 'string'; 927 }; 928 929 930 /** 931 * Returns true if the specified value is a boolean 932 * @param {*} val Variable to test. 933 * @return {boolean} Whether variable is boolean. 934 */ 935 goog.isBoolean = function(val) { 936 return typeof val == 'boolean'; 937 }; 938 939 940 /** 941 * Returns true if the specified value is a number 942 * @param {*} val Variable to test. 943 * @return {boolean} Whether variable is a number. 944 */ 945 goog.isNumber = function(val) { 946 return typeof val == 'number'; 947 }; 948 949 950 /** 951 * Returns true if the specified value is a function 952 * @param {*} val Variable to test. 953 * @return {boolean} Whether variable is a function. 954 */ 955 goog.isFunction = function(val) { 956 return goog.typeOf(val) == 'function'; 957 }; 958 959 960 /** 961 * Returns true if the specified value is an object. This includes arrays 962 * and functions. 963 * @param {*} val Variable to test. 964 * @return {boolean} Whether variable is an object. 965 */ 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 970 // not an object. 971 }; 972 973 974 /** 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. 981 * 982 * @param {Object} obj The object to get the unique ID for. 983 * @return {number} The unique ID for the object. 984 */ 985 goog.getUid = function(obj) { 986 // TODO(arv): Make the type stricter, do not accept null. 987 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_); 993 }; 994 995 996 /** 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 999 * undone. 1000 * @param {Object} obj The object to remove the unique ID field from. 1001 */ 1002 goog.removeUid = function(obj) { 1003 // TODO(arv): Make the type stricter, do not accept null. 1004 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_); 1009 } 1010 /** @preserveTry */ 1011 try { 1012 delete obj[goog.UID_PROPERTY_]; 1013 } catch (ex) { 1014 } 1015 }; 1016 1017 1018 /** 1019 * Name for unique ID property. Initialized in a way to help avoid collisions 1020 * with other closure javascript on the same page. 1021 * @type {string} 1022 * @private 1023 */ 1024 goog.UID_PROPERTY_ = 'closure_uid_' + ((Math.random() * 1e9) >>> 0); 1025 1026 1027 /** 1028 * Counter for UID. 1029 * @type {number} 1030 * @private 1031 */ 1032 goog.uidCounter_ = 0; 1033 1034 1035 /** 1036 * Adds a hash code field to an object. The hash code is unique for the 1037 * given object. 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. 1041 */ 1042 goog.getHashCode = goog.getUid; 1043 1044 1045 /** 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. 1049 */ 1050 goog.removeHashCode = goog.removeUid; 1051 1052 1053 /** 1054 * Clones a value. The input may be an Object, Array, or basic type. Objects and 1055 * arrays will be cloned recursively. 1056 * 1057 * WARNINGS: 1058 * <code>goog.cloneObject</code> does not detect reference loops. Objects that 1059 * refer to themselves will cause infinite recursion. 1060 * 1061 * <code>goog.cloneObject</code> is unaware of unique identifiers, and copies 1062 * UIDs created by <code>getUid</code> into cloned results. 1063 * 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. 1067 */ 1068 goog.cloneObject = function(obj) { 1069 var type = goog.typeOf(obj); 1070 if (type == 'object' || type == 'array') { 1071 if (obj.clone) { 1072 return obj.clone(); 1073 } 1074 var clone = type == 'array' ? [] : {}; 1075 for (var key in obj) { 1076 clone[key] = goog.cloneObject(obj[key]); 1077 } 1078 return clone; 1079 } 1080 1081 return obj; 1082 }; 1083 1084 1085 /** 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. 1094 * @private 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. 1098 */ 1099 goog.bindNative_ = function(fn, selfObj, var_args) { 1100 return /** @type {!Function} */ (fn.call.apply(fn.bind, arguments)); 1101 }; 1102 1103 1104 /** 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. 1113 * @private 1114 */ 1115 goog.bindJs_ = function(fn, selfObj, var_args) { 1116 if (!fn) { 1117 throw new Error(); 1118 } 1119 1120 if (arguments.length > 2) { 1121 var boundArgs = Array.prototype.slice.call(arguments, 2); 1122 return function() { 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); 1127 }; 1128 1129 } else { 1130 return function() { 1131 return fn.apply(selfObj, arguments); 1132 }; 1133 } 1134 }; 1135 1136 1137 /** 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> 1141 * 1142 * Remaining arguments specified at call-time are appended to the pre- 1143 * specified ones.<br><br> 1144 * 1145 * Also see: {@link #partial}.<br><br> 1146 * 1147 * Usage: 1148 * <pre>var barMethBound = bind(myFunction, myObj, 'arg1', 'arg2'); 1149 * barMethBound('arg3', 'arg4');</pre> 1150 * 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. 1158 * @template T 1159 * @suppress {deprecated} See above. 1160 */ 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 1170 // works correctly. 1171 Function.prototype.bind.toString().indexOf('native code') != -1) { 1172 goog.bind = goog.bindNative_; 1173 } else { 1174 goog.bind = goog.bindJs_; 1175 } 1176 return goog.bind.apply(null, arguments); 1177 }; 1178 1179 1180 /** 1181 * Like bind(), except that a 'this object' is not required. Useful when the 1182 * target function is already bound. 1183 * 1184 * Usage: 1185 * var g = partial(f, arg1, arg2); 1186 * g(arg3, arg4); 1187 * 1188 * @param {Function} fn A function to partially apply. 1189 * @param {...*} var_args Additional arguments that are partially 1190 * applied to fn. 1191 * @return {!Function} A partially-applied form of the function bind() was 1192 * invoked as a method of. 1193 */ 1194 goog.partial = function(fn, var_args) { 1195 var args = Array.prototype.slice.call(arguments, 1); 1196 return function() { 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); 1201 }; 1202 }; 1203 1204 1205 /** 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. 1211 */ 1212 goog.mixin = function(target, source) { 1213 for (var x in source) { 1214 target[x] = source[x]; 1215 } 1216 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). 1222 }; 1223 1224 1225 /** 1226 * @return {number} An integer value representing the number of milliseconds 1227 * between midnight, January 1, 1970 and the current time. 1228 */ 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(). 1232 return +new Date(); 1233 }); 1234 1235 1236 /** 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. 1242 */ 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; 1253 } else { 1254 goog.evalWorksForGlobals_ = false; 1255 } 1256 } 1257 1258 if (goog.evalWorksForGlobals_) { 1259 goog.global.eval(script); 1260 } else { 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); 1270 } 1271 } else { 1272 throw Error('goog.globalEval not available'); 1273 } 1274 }; 1275 1276 1277 /** 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 1281 * @type {?boolean} 1282 * @private 1283 */ 1284 goog.evalWorksForGlobals_ = null; 1285 1286 1287 /** 1288 * Optional map of CSS class names to obfuscated names used with 1289 * goog.getCssName(). 1290 * @type {Object|undefined} 1291 * @private 1292 * @see goog.setCssNameMapping 1293 */ 1294 goog.cssNameMapping_; 1295 1296 1297 /** 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} 1301 * @private 1302 * @see goog.setCssNameMapping 1303 */ 1304 goog.cssNameMappingStyle_; 1305 1306 1307 /** 1308 * Handles strings that are intended to be used as CSS class names. 1309 * 1310 * This function works in tandem with @see goog.setCssNameMapping. 1311 * 1312 * Without any mapping set, the arguments are simple joined with a 1313 * hyphen and passed through unaltered. 1314 * 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. 1321 * 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'); 1326 * becomes: 1327 * var x= 'foo'; 1328 * var y = this.baseClass + '-active'; 1329 * 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. 1333 * 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 1337 * the modifier. 1338 */ 1339 goog.getCssName = function(className, opt_modifier) { 1340 var getMapping = function(cssName) { 1341 return goog.cssNameMapping_[cssName] || cssName; 1342 }; 1343 1344 var renameByParts = function(cssName) { 1345 // Remap all the parts individually. 1346 var parts = cssName.split('-'); 1347 var mapped = []; 1348 for (var i = 0; i < parts.length; i++) { 1349 mapped.push(getMapping(parts[i])); 1350 } 1351 return mapped.join('-'); 1352 }; 1353 1354 var rename; 1355 if (goog.cssNameMapping_) { 1356 rename = goog.cssNameMappingStyle_ == 'BY_WHOLE' ? 1357 getMapping : renameByParts; 1358 } else { 1359 rename = function(a) { 1360 return a; 1361 }; 1362 } 1363 1364 if (opt_modifier) { 1365 return className + '-' + rename(opt_modifier); 1366 } else { 1367 return rename(className); 1368 } 1369 }; 1370 1371 1372 /** 1373 * Sets the map to check when returning a value from goog.getCssName(). Example: 1374 * <pre> 1375 * goog.setCssNameMapping({ 1376 * "goog": "a", 1377 * "disabled": "b", 1378 * }); 1379 * 1380 * var x = goog.getCssName('goog'); 1381 * // The following evaluates to: "a a-b". 1382 * goog.getCssName('goog') + ' ' + goog.getCssName(x, 'disabled') 1383 * </pre> 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. 1387 * 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. 1394 */ 1395 goog.setCssNameMapping = function(mapping, opt_style) { 1396 goog.cssNameMapping_ = mapping; 1397 goog.cssNameMappingStyle_ = opt_style; 1398 }; 1399 1400 1401 /** 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. 1409 * 1410 * A hook for overriding the CSS name mapping. 1411 * @type {Object|undefined} 1412 */ 1413 goog.global.CLOSURE_CSS_NAME_MAPPING; 1414 1415 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; 1420 } 1421 1422 1423 /** 1424 * Gets a localized message. 1425 * 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. 1429 * 1430 * Messages must be initialized in the form: 1431 * <code> 1432 * var MSG_NAME = goog.getMsg('Hello {$placeholder}', {'placeholder': 'world'}); 1433 * </code> 1434 * 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. 1438 */ 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); 1444 } 1445 return str; 1446 }; 1447 1448 1449 /** 1450 * Gets a localized message. If the message does not have a translation, gives a 1451 * fallback message. 1452 * 1453 * This is useful when introducing a new message that has not yet been 1454 * translated into all languages. 1455 * 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. 1459 * 1460 * @param {string} a The preferred message. 1461 * @param {string} b The fallback message. 1462 * @return {string} The best translated message. 1463 */ 1464 goog.getMsgWithFallback = function(a, b) { 1465 return a; 1466 }; 1467 1468 1469 /** 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 1474 * 1475 * <p>Also handy for making public items that are defined in anonymous 1476 * closures. 1477 * 1478 * ex. goog.exportSymbol('public.path.Foo', Foo); 1479 * 1480 * ex. goog.exportSymbol('public.path.Foo.staticFunction', 1481 * Foo.staticFunction); 1482 * public.path.Foo.staticFunction(); 1483 * 1484 * ex. goog.exportSymbol('public.path.Foo.prototype.myMethod', 1485 * Foo.prototype.myMethod); 1486 * new public.path.Foo().myMethod(); 1487 * 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 1491 * is |goog.global|. 1492 */ 1493 goog.exportSymbol = function(publicPath, object, opt_objectToExportTo) { 1494 goog.exportPath_(publicPath, object, opt_objectToExportTo); 1495 }; 1496 1497 1498 /** 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. 1505 */ 1506 goog.exportProperty = function(object, publicName, symbol) { 1507 object[publicName] = symbol; 1508 }; 1509 1510 1511 /** 1512 * Inherit the prototype methods from one constructor into another. 1513 * 1514 * Usage: 1515 * <pre> 1516 * function ParentClass(a, b) { } 1517 * ParentClass.prototype.foo = function(a) { } 1518 * 1519 * function ChildClass(a, b, c) { 1520 * goog.base(this, a, b); 1521 * } 1522 * goog.inherits(ChildClass, ParentClass); 1523 * 1524 * var child = new ChildClass('a', 'b', 'see'); 1525 * child.foo(); // works 1526 * </pre> 1527 * 1528 * In addition, a superclass' implementation of a method can be invoked 1529 * as follows: 1530 * 1531 * <pre> 1532 * ChildClass.prototype.foo = function(a) { 1533 * ChildClass.superClass_.foo.call(this, a); 1534 * // other code 1535 * }; 1536 * </pre> 1537 * 1538 * @param {Function} childCtor Child class. 1539 * @param {Function} parentCtor Parent class. 1540 */ 1541 goog.inherits = function(childCtor, parentCtor) { 1542 /** @constructor */ 1543 function tempCtor() {}; 1544 tempCtor.prototype = parentCtor.prototype; 1545 childCtor.superClass_ = parentCtor.prototype; 1546 childCtor.prototype = new tempCtor(); 1547 /** @override */ 1548 childCtor.prototype.constructor = childCtor; 1549 }; 1550 1551 1552 /** 1553 * Call up to the superclass. 1554 * 1555 * If this is called from a constructor, then this calls the superclass 1556 * contructor with arguments 1-N. 1557 * 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. 1562 * 1563 * This function only works if you use goog.inherits to express 1564 * inheritance relationships between your classes. 1565 * 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. 1571 * 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. 1576 */ 1577 goog.base = function(me, opt_methodName, var_args) { 1578 var caller = arguments.callee.caller; 1579 1580 if (goog.DEBUG) { 1581 if (!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'); 1585 } 1586 } 1587 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)); 1592 } 1593 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) { 1599 foundCaller = true; 1600 } else if (foundCaller) { 1601 return ctor.prototype[opt_methodName].apply(me, args); 1602 } 1603 } 1604 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); 1611 } else { 1612 throw Error( 1613 'goog.base called from a method of one name ' + 1614 'to a method of a different name'); 1615 } 1616 }; 1617 1618 1619 /** 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"). 1627 */ 1628 goog.scope = function(fn) { 1629 fn.call(goog.global); 1630 }; 1631 1632 1633 // MOE:begin_strip 1634 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. 1637 1638 //============================================================================== 1639 // Extending Function 1640 //============================================================================== 1641 1642 1643 /** 1644 * @define {boolean} Whether to extend Function.prototype. 1645 * Use --define='goog.MODIFY_FUNCTION_PROTOTYPES=false' to change. 1646 */ 1647 goog.define('goog.MODIFY_FUNCTION_PROTOTYPES', true); 1648 1649 if (goog.MODIFY_FUNCTION_PROTOTYPES) { 1650 /** 1651 * An alias to the {@link goog.bind()} global function. 1652 * 1653 * Usage: 1654 * var g = f.bind(obj, arg1, arg2); 1655 * g(arg3, arg4); 1656 * 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 1661 * applied to fn. 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} 1666 */ 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); 1673 } else { 1674 return goog.bind(this, selfObj); 1675 } 1676 }; 1677 1678 1679 /** 1680 * An alias to the {@link goog.partial()} static function. 1681 * 1682 * Usage: 1683 * var g = f.partial(arg1, arg2); 1684 * g(arg3, arg4); 1685 * 1686 * @param {...*} var_args Additional arguments that are partially 1687 * applied to fn. 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. 1691 */ 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); 1696 }; 1697 1698 1699 /** 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. 1704 */ 1705 Function.prototype.inherits = function(parentCtor) { 1706 goog.inherits(this, parentCtor); 1707 }; 1708 1709 1710 /** 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. 1714 * 1715 * <pre> 1716 * function X() {} 1717 * X.mixin({ 1718 * one: 1, 1719 * two: 2, 1720 * three: 3, 1721 * doit: function() { return this.one + this.two + this.three; } 1722 * }); 1723 * 1724 * function Y() { } 1725 * Y.mixin(X.prototype); 1726 * Y.prototype.four = 15; 1727 * Y.prototype.doit2 = function() { return this.doit() + this.four; } 1728 * }); 1729 * 1730 * // or 1731 * 1732 * function Y() { } 1733 * Y.inherits(X); 1734 * Y.mixin({ 1735 * one: 10, 1736 * four: 15, 1737 * doit2: function() { return this.doit() + this.four; } 1738 * }); 1739 * </pre> 1740 * 1741 * @param {Object} source from which to copy properties. 1742 * @see goog.mixin 1743 * @deprecated Use the static function goog.object.extend instead. 1744 */ 1745 Function.prototype.mixin = function(source) { 1746 goog.mixin(this.prototype, source); 1747 }; 1748 } 1749 1750 // MOE:end_strip 1751