base.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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. * @fileoverview Bootstrap for the Google JS Library (Closure).
  16. *
  17. * In uncompiled mode base.js will write out Closure's deps file, unless the
  18. * global <code>CLOSURE_NO_DEPS</code> is set to true. This allows projects to
  19. * include their own deps file(s) from different locations.
  20. *
  21. *
  22. * @provideGoog
  23. */
  24. /**
  25. * @define {boolean} Overridden to true by the compiler when --closure_pass
  26. * or --mark_as_compiled is specified.
  27. */
  28. var COMPILED = false;
  29. /**
  30. * Base namespace for the Closure library. Checks to see goog is already
  31. * defined in the current scope before assigning to prevent clobbering if
  32. * base.js is loaded more than once.
  33. *
  34. * @const
  35. */
  36. var goog = goog || {};
  37. /**
  38. * Reference to the global context. In most cases this will be 'window'.
  39. */
  40. goog.global = this;
  41. /**
  42. * A hook for overriding the define values in uncompiled mode.
  43. *
  44. * In uncompiled mode, {@code CLOSURE_DEFINES} may be defined before loading
  45. * base.js. If a key is defined in {@code CLOSURE_DEFINES}, {@code goog.define}
  46. * will use the value instead of the default value. This allows flags to be
  47. * overwritten without compilation (this is normally accomplished with the
  48. * compiler's "define" flag).
  49. *
  50. * Example:
  51. * <pre>
  52. * var CLOSURE_DEFINES = {'goog.DEBUG', false};
  53. * </pre>
  54. *
  55. * @type {Object.<string, (string|number|boolean)>|undefined}
  56. */
  57. goog.global.CLOSURE_DEFINES;
  58. /**
  59. * Builds an object structure for the provided namespace path, ensuring that
  60. * names that already exist are not overwritten. For example:
  61. * "a.b.c" -> a = {};a.b={};a.b.c={};
  62. * Used by goog.provide and goog.exportSymbol.
  63. * @param {string} name name of the object that this file defines.
  64. * @param {*=} opt_object the object to expose at the end of the path.
  65. * @param {Object=} opt_objectToExportTo The object to add the path to; default
  66. * is |goog.global|.
  67. * @private
  68. */
  69. goog.exportPath_ = function(name, opt_object, opt_objectToExportTo) {
  70. var parts = name.split('.');
  71. var cur = opt_objectToExportTo || goog.global;
  72. // Internet Explorer exhibits strange behavior when throwing errors from
  73. // methods externed in this manner. See the testExportSymbolExceptions in
  74. // base_test.html for an example.
  75. if (!(parts[0] in cur) && cur.execScript) {
  76. cur.execScript('var ' + parts[0]);
  77. }
  78. // Certain browsers cannot parse code in the form for((a in b); c;);
  79. // This pattern is produced by the JSCompiler when it collapses the
  80. // statement above into the conditional loop below. To prevent this from
  81. // happening, use a for-loop and reserve the init logic as below.
  82. // Parentheses added to eliminate strict JS warning in Firefox.
  83. for (var part; parts.length && (part = parts.shift());) {
  84. if (!parts.length && opt_object !== undefined) {
  85. // last part and we have an object; use it
  86. cur[part] = opt_object;
  87. } else if (cur[part]) {
  88. cur = cur[part];
  89. } else {
  90. cur = cur[part] = {};
  91. }
  92. }
  93. };
  94. /**
  95. * Defines a named value. In uncompiled mode, the value is retreived from
  96. * CLOSURE_DEFINES if the object is defined and has the property specified,
  97. * and otherwise used the defined defaultValue. When compiled, the default
  98. * can be overridden using compiler command-line options.
  99. *
  100. * @param {string} name The distinguished name to provide.
  101. * @param {string|number|boolean} defaultValue
  102. */
  103. goog.define = function(name, defaultValue) {
  104. var value = defaultValue;
  105. if (!COMPILED) {
  106. if (goog.global.CLOSURE_DEFINES && Object.prototype.hasOwnProperty.call(
  107. goog.global.CLOSURE_DEFINES, name)) {
  108. value = goog.global.CLOSURE_DEFINES[name];
  109. }
  110. }
  111. goog.exportPath_(name, value);
  112. };
  113. /**
  114. * @define {boolean} DEBUG is provided as a convenience so that debugging code
  115. * that should not be included in a production js_binary can be easily stripped
  116. * by specifying --define goog.DEBUG=false to the JSCompiler. For example, most
  117. * toString() methods should be declared inside an "if (goog.DEBUG)" conditional
  118. * because they are generally used for debugging purposes and it is difficult
  119. * for the JSCompiler to statically determine whether they are used.
  120. */
  121. goog.DEBUG = true;
  122. /**
  123. * @define {string} LOCALE defines the locale being used for compilation. It is
  124. * used to select locale specific data to be compiled in js binary. BUILD rule
  125. * can specify this value by "--define goog.LOCALE=<locale_name>" as JSCompiler
  126. * option.
  127. *
  128. * Take into account that the locale code format is important. You should use
  129. * the canonical Unicode format with hyphen as a delimiter. Language must be
  130. * lowercase, Language Script - Capitalized, Region - UPPERCASE.
  131. * There are few examples: pt-BR, en, en-US, sr-Latin-BO, zh-Hans-CN.
  132. *
  133. * See more info about locale codes here:
  134. * http://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers
  135. *
  136. * For language codes you should use values defined by ISO 693-1. See it here
  137. * http://www.w3.org/WAI/ER/IG/ert/iso639.htm. There is only one exception from
  138. * this rule: the Hebrew language. For legacy reasons the old code (iw) should
  139. * be used instead of the new code (he), see http://wiki/Main/IIISynonyms.
  140. */
  141. goog.define('goog.LOCALE', 'en'); // default to en
  142. /**
  143. * @define {boolean} Whether this code is running on trusted sites.
  144. *
  145. * On untrusted sites, several native functions can be defined or overridden by
  146. * external libraries like Prototype, Datejs, and JQuery and setting this flag
  147. * to false forces closure to use its own implementations when possible.
  148. *
  149. * If your JavaScript can be loaded by a third party site and you are wary about
  150. * relying on non-standard implementations, specify
  151. * "--define goog.TRUSTED_SITE=false" to the JSCompiler.
  152. */
  153. goog.define('goog.TRUSTED_SITE', true);
  154. /**
  155. * Creates object stubs for a namespace. The presence of one or more
  156. * goog.provide() calls indicate that the file defines the given
  157. * objects/namespaces. Build tools also scan for provide/require statements
  158. * to discern dependencies, build dependency files (see deps.js), etc.
  159. * @see goog.require
  160. * @param {string} name Namespace provided by this file in the form
  161. * "goog.package.part".
  162. */
  163. goog.provide = function(name) {
  164. if (!COMPILED) {
  165. // Ensure that the same namespace isn't provided twice. This is intended
  166. // to teach new developers that 'goog.provide' is effectively a variable
  167. // declaration. And when JSCompiler transforms goog.provide into a real
  168. // variable declaration, the compiled JS should work the same as the raw
  169. // JS--even when the raw JS uses goog.provide incorrectly.
  170. if (goog.isProvided_(name)) {
  171. throw Error('Namespace "' + name + '" already declared.');
  172. }
  173. delete goog.implicitNamespaces_[name];
  174. var namespace = name;
  175. while ((namespace = namespace.substring(0, namespace.lastIndexOf('.')))) {
  176. if (goog.getObjectByName(namespace)) {
  177. break;
  178. }
  179. goog.implicitNamespaces_[namespace] = true;
  180. }
  181. }
  182. goog.exportPath_(name);
  183. };
  184. /**
  185. * Marks that the current file should only be used for testing, and never for
  186. * live code in production.
  187. *
  188. * In the case of unit tests, the message may optionally be an exact namespace
  189. * for the test (e.g. 'goog.stringTest'). The linter will then ignore the extra
  190. * provide (if not explicitly defined in the code).
  191. *
  192. * @param {string=} opt_message Optional message to add to the error that's
  193. * raised when used in production code.
  194. */
  195. goog.setTestOnly = function(opt_message) {
  196. if (COMPILED && !goog.DEBUG) {
  197. opt_message = opt_message || '';
  198. throw Error('Importing test-only code into non-debug environment' +
  199. opt_message ? ': ' + opt_message : '.');
  200. }
  201. };
  202. if (!COMPILED) {
  203. /**
  204. * Check if the given name has been goog.provided. This will return false for
  205. * names that are available only as implicit namespaces.
  206. * @param {string} name name of the object to look for.
  207. * @return {boolean} Whether the name has been provided.
  208. * @private
  209. */
  210. goog.isProvided_ = function(name) {
  211. return !goog.implicitNamespaces_[name] && !!goog.getObjectByName(name);
  212. };
  213. /**
  214. * Namespaces implicitly defined by goog.provide. For example,
  215. * goog.provide('goog.events.Event') implicitly declares that 'goog' and
  216. * 'goog.events' must be namespaces.
  217. *
  218. * @type {Object}
  219. * @private
  220. */
  221. goog.implicitNamespaces_ = {};
  222. }
  223. /**
  224. * Returns an object based on its fully qualified external name. If you are
  225. * using a compilation pass that renames property names beware that using this
  226. * function will not find renamed properties.
  227. *
  228. * @param {string} name The fully qualified name.
  229. * @param {Object=} opt_obj The object within which to look; default is
  230. * |goog.global|.
  231. * @return {?} The value (object or primitive) or, if not found, null.
  232. */
  233. goog.getObjectByName = function(name, opt_obj) {
  234. var parts = name.split('.');
  235. var cur = opt_obj || goog.global;
  236. for (var part; part = parts.shift(); ) {
  237. if (goog.isDefAndNotNull(cur[part])) {
  238. cur = cur[part];
  239. } else {
  240. return null;
  241. }
  242. }
  243. return cur;
  244. };
  245. /**
  246. * Globalizes a whole namespace, such as goog or goog.lang.
  247. *
  248. * @param {Object} obj The namespace to globalize.
  249. * @param {Object=} opt_global The object to add the properties to.
  250. * @deprecated Properties may be explicitly exported to the global scope, but
  251. * this should no longer be done in bulk.
  252. */
  253. goog.globalize = function(obj, opt_global) {
  254. var global = opt_global || goog.global;
  255. for (var x in obj) {
  256. global[x] = obj[x];
  257. }
  258. };
  259. /**
  260. * Adds a dependency from a file to the files it requires.
  261. * @param {string} relPath The path to the js file.
  262. * @param {Array} provides An array of strings with the names of the objects
  263. * this file provides.
  264. * @param {Array} requires An array of strings with the names of the objects
  265. * this file requires.
  266. */
  267. goog.addDependency = function(relPath, provides, requires) {
  268. if (goog.DEPENDENCIES_ENABLED) {
  269. var provide, require;
  270. var path = relPath.replace(/\\/g, '/');
  271. var deps = goog.dependencies_;
  272. for (var i = 0; provide = provides[i]; i++) {
  273. deps.nameToPath[provide] = path;
  274. if (!(path in deps.pathToNames)) {
  275. deps.pathToNames[path] = {};
  276. }
  277. deps.pathToNames[path][provide] = true;
  278. }
  279. for (var j = 0; require = requires[j]; j++) {
  280. if (!(path in deps.requires)) {
  281. deps.requires[path] = {};
  282. }
  283. deps.requires[path][require] = true;
  284. }
  285. }
  286. };
  287. // NOTE(nnaze): The debug DOM loader was included in base.js as an original way
  288. // to do "debug-mode" development. The dependency system can sometimes be
  289. // confusing, as can the debug DOM loader's asynchronous nature.
  290. //
  291. // With the DOM loader, a call to goog.require() is not blocking -- the script
  292. // will not load until some point after the current script. If a namespace is
  293. // needed at runtime, it needs to be defined in a previous script, or loaded via
  294. // require() with its registered dependencies.
  295. // User-defined namespaces may need their own deps file. See http://go/js_deps,
  296. // http://go/genjsdeps, or, externally, DepsWriter.
  297. // http://code.google.com/closure/library/docs/depswriter.html
  298. //
  299. // Because of legacy clients, the DOM loader can't be easily removed from
  300. // base.js. Work is being done to make it disableable or replaceable for
  301. // different environments (DOM-less JavaScript interpreters like Rhino or V8,
  302. // for example). See bootstrap/ for more information.
  303. /**
  304. * @define {boolean} Whether to enable the debug loader.
  305. *
  306. * If enabled, a call to goog.require() will attempt to load the namespace by
  307. * appending a script tag to the DOM (if the namespace has been registered).
  308. *
  309. * If disabled, goog.require() will simply assert that the namespace has been
  310. * provided (and depend on the fact that some outside tool correctly ordered
  311. * the script).
  312. */
  313. goog.define('goog.ENABLE_DEBUG_LOADER', true);
  314. /**
  315. * Implements a system for the dynamic resolution of dependencies that works in
  316. * parallel with the BUILD system. Note that all calls to goog.require will be
  317. * stripped by the JSCompiler when the --closure_pass option is used.
  318. * @see goog.provide
  319. * @param {string} name Namespace to include (as was given in goog.provide()) in
  320. * the form "goog.package.part".
  321. */
  322. goog.require = function(name) {
  323. // If the object already exists we do not need do do anything.
  324. // TODO(arv): If we start to support require based on file name this has to
  325. // change.
  326. // TODO(arv): If we allow goog.foo.* this has to change.
  327. // TODO(arv): If we implement dynamic load after page load we should probably
  328. // not remove this code for the compiled output.
  329. if (!COMPILED) {
  330. if (goog.isProvided_(name)) {
  331. return;
  332. }
  333. if (goog.ENABLE_DEBUG_LOADER) {
  334. var path = goog.getPathFromDeps_(name);
  335. if (path) {
  336. goog.included_[path] = true;
  337. goog.writeScripts_();
  338. return;
  339. }
  340. }
  341. var errorMessage = 'goog.require could not find: ' + name;
  342. if (goog.global.console) {
  343. goog.global.console['error'](errorMessage);
  344. }
  345. throw Error(errorMessage);
  346. }
  347. };
  348. /**
  349. * Path for included scripts.
  350. * @type {string}
  351. */
  352. goog.basePath = '';
  353. /**
  354. * A hook for overriding the base path.
  355. * @type {string|undefined}
  356. */
  357. goog.global.CLOSURE_BASE_PATH;
  358. /**
  359. * Whether to write out Closure's deps file. By default, the deps are written.
  360. * @type {boolean|undefined}
  361. */
  362. goog.global.CLOSURE_NO_DEPS;
  363. /**
  364. * A function to import a single script. This is meant to be overridden when
  365. * Closure is being run in non-HTML contexts, such as web workers. It's defined
  366. * in the global scope so that it can be set before base.js is loaded, which
  367. * allows deps.js to be imported properly.
  368. *
  369. * The function is passed the script source, which is a relative URI. It should
  370. * return true if the script was imported, false otherwise.
  371. */
  372. goog.global.CLOSURE_IMPORT_SCRIPT;
  373. /**
  374. * Null function used for default values of callbacks, etc.
  375. * @return {void} Nothing.
  376. */
  377. goog.nullFunction = function() {};
  378. /**
  379. * The identity function. Returns its first argument.
  380. *
  381. * @param {*=} opt_returnValue The single value that will be returned.
  382. * @param {...*} var_args Optional trailing arguments. These are ignored.
  383. * @return {?} The first argument. We can't know the type -- just pass it along
  384. * without type.
  385. * @deprecated Use goog.functions.identity instead.
  386. */
  387. goog.identityFunction = function(opt_returnValue, var_args) {
  388. return opt_returnValue;
  389. };
  390. /**
  391. * When defining a class Foo with an abstract method bar(), you can do:
  392. * Foo.prototype.bar = goog.abstractMethod
  393. *
  394. * Now if a subclass of Foo fails to override bar(), an error will be thrown
  395. * when bar() is invoked.
  396. *
  397. * Note: This does not take the name of the function to override as an argument
  398. * because that would make it more difficult to obfuscate our JavaScript code.
  399. *
  400. * @type {!Function}
  401. * @throws {Error} when invoked to indicate the method should be overridden.
  402. */
  403. goog.abstractMethod = function() {
  404. throw Error('unimplemented abstract method');
  405. };
  406. /**
  407. * Adds a {@code getInstance} static method that always returns the same
  408. * instance object.
  409. * @param {!Function} ctor The constructor for the class to add the static
  410. * method to.
  411. */
  412. goog.addSingletonGetter = function(ctor) {
  413. ctor.getInstance = function() {
  414. if (ctor.instance_) {
  415. return ctor.instance_;
  416. }
  417. if (goog.DEBUG) {
  418. // NOTE: JSCompiler can't optimize away Array#push.
  419. goog.instantiatedSingletons_[goog.instantiatedSingletons_.length] = ctor;
  420. }
  421. return ctor.instance_ = new ctor;
  422. };
  423. };
  424. /**
  425. * All singleton classes that have been instantiated, for testing. Don't read
  426. * it directly, use the {@code goog.testing.singleton} module. The compiler
  427. * removes this variable if unused.
  428. * @type {!Array.<!Function>}
  429. * @private
  430. */
  431. goog.instantiatedSingletons_ = [];
  432. /**
  433. * True if goog.dependencies_ is available.
  434. * @const {boolean}
  435. */
  436. goog.DEPENDENCIES_ENABLED = !COMPILED && goog.ENABLE_DEBUG_LOADER;
  437. if (goog.DEPENDENCIES_ENABLED) {
  438. /**
  439. * Object used to keep track of urls that have already been added. This record
  440. * allows the prevention of circular dependencies.
  441. * @type {Object}
  442. * @private
  443. */
  444. goog.included_ = {};
  445. /**
  446. * This object is used to keep track of dependencies and other data that is
  447. * used for loading scripts.
  448. * @private
  449. * @type {Object}
  450. */
  451. goog.dependencies_ = {
  452. pathToNames: {}, // 1 to many
  453. nameToPath: {}, // 1 to 1
  454. requires: {}, // 1 to many
  455. // Used when resolving dependencies to prevent us from visiting file twice.
  456. visited: {},
  457. written: {} // Used to keep track of script files we have written.
  458. };
  459. /**
  460. * Tries to detect whether is in the context of an HTML document.
  461. * @return {boolean} True if it looks like HTML document.
  462. * @private
  463. */
  464. goog.inHtmlDocument_ = function() {
  465. var doc = goog.global.document;
  466. return typeof doc != 'undefined' &&
  467. 'write' in doc; // XULDocument misses write.
  468. };
  469. /**
  470. * Tries to detect the base path of base.js script that bootstraps Closure.
  471. * @private
  472. */
  473. goog.findBasePath_ = function() {
  474. if (goog.global.CLOSURE_BASE_PATH) {
  475. goog.basePath = goog.global.CLOSURE_BASE_PATH;
  476. return;
  477. } else if (!goog.inHtmlDocument_()) {
  478. return;
  479. }
  480. var doc = goog.global.document;
  481. var scripts = doc.getElementsByTagName('script');
  482. // Search backwards since the current script is in almost all cases the one
  483. // that has base.js.
  484. for (var i = scripts.length - 1; i >= 0; --i) {
  485. var src = scripts[i].src;
  486. var qmark = src.lastIndexOf('?');
  487. var l = qmark == -1 ? src.length : qmark;
  488. if (src.substr(l - 7, 7) == 'base.js') {
  489. goog.basePath = src.substr(0, l - 7);
  490. return;
  491. }
  492. }
  493. };
  494. /**
  495. * Imports a script if, and only if, that script hasn't already been imported.
  496. * (Must be called at execution time)
  497. * @param {string} src Script source.
  498. */
  499. goog.importScript_ = function(src) {
  500. var importScript = goog.global.CLOSURE_IMPORT_SCRIPT ||
  501. goog.writeScriptTag_;
  502. if (!goog.dependencies_.written[src] && importScript(src)) {
  503. goog.dependencies_.written[src] = true;
  504. }
  505. };
  506. /**
  507. * The default implementation of the import function. Writes a script tag to
  508. * import the script.
  509. *
  510. * @param {string} src The script source.
  511. * @return {boolean} True if the script was imported, false otherwise.
  512. * @private
  513. */
  514. goog.writeScriptTag_ = function(src) {
  515. if (goog.inHtmlDocument_()) {
  516. var doc = goog.global.document;
  517. // If the user tries to require a new symbol after document load,
  518. // something has gone terribly wrong. Doing a document.write would
  519. // wipe out the page.
  520. if (doc.readyState == 'complete') {
  521. // Certain test frameworks load base.js multiple times, which tries
  522. // to write deps.js each time. If that happens, just fail silently.
  523. // These frameworks wipe the page between each load of base.js, so this
  524. // is OK.
  525. var isDeps = /\bdeps.js$/.test(src);
  526. if (isDeps) {
  527. return false;
  528. } else {
  529. throw Error('Cannot write "' + src + '" after document load');
  530. }
  531. }
  532. doc.write(
  533. '<script type="text/javascript" src="' + src + '"></' + 'script>');
  534. return true;
  535. } else {
  536. return false;
  537. }
  538. };
  539. /**
  540. * Resolves dependencies based on the dependencies added using addDependency
  541. * and calls importScript_ in the correct order.
  542. * @private
  543. */
  544. goog.writeScripts_ = function() {
  545. // The scripts we need to write this time.
  546. var scripts = [];
  547. var seenScript = {};
  548. var deps = goog.dependencies_;
  549. function visitNode(path) {
  550. if (path in deps.written) {
  551. return;
  552. }
  553. // We have already visited this one. We can get here if we have cyclic
  554. // dependencies.
  555. if (path in deps.visited) {
  556. if (!(path in seenScript)) {
  557. seenScript[path] = true;
  558. scripts.push(path);
  559. }
  560. return;
  561. }
  562. deps.visited[path] = true;
  563. if (path in deps.requires) {
  564. for (var requireName in deps.requires[path]) {
  565. // If the required name is defined, we assume that it was already
  566. // bootstrapped by other means.
  567. if (!goog.isProvided_(requireName)) {
  568. if (requireName in deps.nameToPath) {
  569. visitNode(deps.nameToPath[requireName]);
  570. } else {
  571. throw Error('Undefined nameToPath for ' + requireName);
  572. }
  573. }
  574. }
  575. }
  576. if (!(path in seenScript)) {
  577. seenScript[path] = true;
  578. scripts.push(path);
  579. }
  580. }
  581. for (var path in goog.included_) {
  582. if (!deps.written[path]) {
  583. visitNode(path);
  584. }
  585. }
  586. for (var i = 0; i < scripts.length; i++) {
  587. if (scripts[i]) {
  588. goog.importScript_(goog.basePath + scripts[i]);
  589. } else {
  590. throw Error('Undefined script input');
  591. }
  592. }
  593. };
  594. /**
  595. * Looks at the dependency rules and tries to determine the script file that
  596. * fulfills a particular rule.
  597. * @param {string} rule In the form goog.namespace.Class or project.script.
  598. * @return {?string} Url corresponding to the rule, or null.
  599. * @private
  600. */
  601. goog.getPathFromDeps_ = function(rule) {
  602. if (rule in goog.dependencies_.nameToPath) {
  603. return goog.dependencies_.nameToPath[rule];
  604. } else {
  605. return null;
  606. }
  607. };
  608. goog.findBasePath_();
  609. // Allow projects to manage the deps files themselves.
  610. if (!goog.global.CLOSURE_NO_DEPS) {
  611. goog.importScript_(goog.basePath + 'deps.js');
  612. }
  613. }
  614. //==============================================================================
  615. // Language Enhancements
  616. //==============================================================================
  617. /**
  618. * This is a "fixed" version of the typeof operator. It differs from the typeof
  619. * operator in such a way that null returns 'null' and arrays return 'array'.
  620. * @param {*} value The value to get the type of.
  621. * @return {string} The name of the type.
  622. */
  623. goog.typeOf = function(value) {
  624. var s = typeof value;
  625. if (s == 'object') {
  626. if (value) {
  627. // Check these first, so we can avoid calling Object.prototype.toString if
  628. // possible.
  629. //
  630. // IE improperly marshals tyepof across execution contexts, but a
  631. // cross-context object will still return false for "instanceof Object".
  632. if (value instanceof Array) {
  633. return 'array';
  634. } else if (value instanceof Object) {
  635. return s;
  636. }
  637. // HACK: In order to use an Object prototype method on the arbitrary
  638. // value, the compiler requires the value be cast to type Object,
  639. // even though the ECMA spec explicitly allows it.
  640. var className = Object.prototype.toString.call(
  641. /** @type {Object} */ (value));
  642. // In Firefox 3.6, attempting to access iframe window objects' length
  643. // property throws an NS_ERROR_FAILURE, so we need to special-case it
  644. // here.
  645. if (className == '[object Window]') {
  646. return 'object';
  647. }
  648. // We cannot always use constructor == Array or instanceof Array because
  649. // different frames have different Array objects. In IE6, if the iframe
  650. // where the array was created is destroyed, the array loses its
  651. // prototype. Then dereferencing val.splice here throws an exception, so
  652. // we can't use goog.isFunction. Calling typeof directly returns 'unknown'
  653. // so that will work. In this case, this function will return false and
  654. // most array functions will still work because the array is still
  655. // array-like (supports length and []) even though it has lost its
  656. // prototype.
  657. // Mark Miller noticed that Object.prototype.toString
  658. // allows access to the unforgeable [[Class]] property.
  659. // 15.2.4.2 Object.prototype.toString ( )
  660. // When the toString method is called, the following steps are taken:
  661. // 1. Get the [[Class]] property of this object.
  662. // 2. Compute a string value by concatenating the three strings
  663. // "[object ", Result(1), and "]".
  664. // 3. Return Result(2).
  665. // and this behavior survives the destruction of the execution context.
  666. if ((className == '[object Array]' ||
  667. // In IE all non value types are wrapped as objects across window
  668. // boundaries (not iframe though) so we have to do object detection
  669. // for this edge case.
  670. typeof value.length == 'number' &&
  671. typeof value.splice != 'undefined' &&
  672. typeof value.propertyIsEnumerable != 'undefined' &&
  673. !value.propertyIsEnumerable('splice')
  674. )) {
  675. return 'array';
  676. }
  677. // HACK: There is still an array case that fails.
  678. // function ArrayImpostor() {}
  679. // ArrayImpostor.prototype = [];
  680. // var impostor = new ArrayImpostor;
  681. // this can be fixed by getting rid of the fast path
  682. // (value instanceof Array) and solely relying on
  683. // (value && Object.prototype.toString.vall(value) === '[object Array]')
  684. // but that would require many more function calls and is not warranted
  685. // unless closure code is receiving objects from untrusted sources.
  686. // IE in cross-window calls does not correctly marshal the function type
  687. // (it appears just as an object) so we cannot use just typeof val ==
  688. // 'function'. However, if the object has a call property, it is a
  689. // function.
  690. if ((className == '[object Function]' ||
  691. typeof value.call != 'undefined' &&
  692. typeof value.propertyIsEnumerable != 'undefined' &&
  693. !value.propertyIsEnumerable('call'))) {
  694. return 'function';
  695. }
  696. } else {
  697. return 'null';
  698. }
  699. } else if (s == 'function' && typeof value.call == 'undefined') {
  700. // In Safari typeof nodeList returns 'function', and on Firefox typeof
  701. // behaves similarly for HTML{Applet,Embed,Object}, Elements and RegExps. We
  702. // would like to return object for those and we can detect an invalid
  703. // function by making sure that the function object has a call method.
  704. return 'object';
  705. }
  706. return s;
  707. };
  708. /**
  709. * Returns true if the specified value is not undefined.
  710. * WARNING: Do not use this to test if an object has a property. Use the in
  711. * operator instead. Additionally, this function assumes that the global
  712. * undefined variable has not been redefined.
  713. * @param {*} val Variable to test.
  714. * @return {boolean} Whether variable is defined.
  715. */
  716. goog.isDef = function(val) {
  717. return val !== undefined;
  718. };
  719. /**
  720. * Returns true if the specified value is null.
  721. * @param {*} val Variable to test.
  722. * @return {boolean} Whether variable is null.
  723. */
  724. goog.isNull = function(val) {
  725. return val === null;
  726. };
  727. /**
  728. * Returns true if the specified value is defined and not null.
  729. * @param {*} val Variable to test.
  730. * @return {boolean} Whether variable is defined and not null.
  731. */
  732. goog.isDefAndNotNull = function(val) {
  733. // Note that undefined == null.
  734. return val != null;
  735. };
  736. /**
  737. * Returns true if the specified value is an array.
  738. * @param {*} val Variable to test.
  739. * @return {boolean} Whether variable is an array.
  740. */
  741. goog.isArray = function(val) {
  742. return goog.typeOf(val) == 'array';
  743. };
  744. /**
  745. * Returns true if the object looks like an array. To qualify as array like
  746. * the value needs to be either a NodeList or an object with a Number length
  747. * property.
  748. * @param {*} val Variable to test.
  749. * @return {boolean} Whether variable is an array.
  750. */
  751. goog.isArrayLike = function(val) {
  752. var type = goog.typeOf(val);
  753. return type == 'array' || type == 'object' && typeof val.length == 'number';
  754. };
  755. /**
  756. * Returns true if the object looks like a Date. To qualify as Date-like the
  757. * value needs to be an object and have a getFullYear() function.
  758. * @param {*} val Variable to test.
  759. * @return {boolean} Whether variable is a like a Date.
  760. */
  761. goog.isDateLike = function(val) {
  762. return goog.isObject(val) && typeof val.getFullYear == 'function';
  763. };
  764. /**
  765. * Returns true if the specified value is a string.
  766. * @param {*} val Variable to test.
  767. * @return {boolean} Whether variable is a string.
  768. */
  769. goog.isString = function(val) {
  770. return typeof val == 'string';
  771. };
  772. /**
  773. * Returns true if the specified value is a boolean.
  774. * @param {*} val Variable to test.
  775. * @return {boolean} Whether variable is boolean.
  776. */
  777. goog.isBoolean = function(val) {
  778. return typeof val == 'boolean';
  779. };
  780. /**
  781. * Returns true if the specified value is a number.
  782. * @param {*} val Variable to test.
  783. * @return {boolean} Whether variable is a number.
  784. */
  785. goog.isNumber = function(val) {
  786. return typeof val == 'number';
  787. };
  788. /**
  789. * Returns true if the specified value is a function.
  790. * @param {*} val Variable to test.
  791. * @return {boolean} Whether variable is a function.
  792. */
  793. goog.isFunction = function(val) {
  794. return goog.typeOf(val) == 'function';
  795. };
  796. /**
  797. * Returns true if the specified value is an object. This includes arrays and
  798. * functions.
  799. * @param {*} val Variable to test.
  800. * @return {boolean} Whether variable is an object.
  801. */
  802. goog.isObject = function(val) {
  803. var type = typeof val;
  804. return type == 'object' && val != null || type == 'function';
  805. // return Object(val) === val also works, but is slower, especially if val is
  806. // not an object.
  807. };
  808. /**
  809. * Gets a unique ID for an object. This mutates the object so that further calls
  810. * with the same object as a parameter returns the same value. The unique ID is
  811. * guaranteed to be unique across the current session amongst objects that are
  812. * passed into {@code getUid}. There is no guarantee that the ID is unique or
  813. * consistent across sessions. It is unsafe to generate unique ID for function
  814. * prototypes.
  815. *
  816. * @param {Object} obj The object to get the unique ID for.
  817. * @return {number} The unique ID for the object.
  818. */
  819. goog.getUid = function(obj) {
  820. // TODO(arv): Make the type stricter, do not accept null.
  821. // In Opera window.hasOwnProperty exists but always returns false so we avoid
  822. // using it. As a consequence the unique ID generated for BaseClass.prototype
  823. // and SubClass.prototype will be the same.
  824. return obj[goog.UID_PROPERTY_] ||
  825. (obj[goog.UID_PROPERTY_] = ++goog.uidCounter_);
  826. };
  827. /**
  828. * Removes the unique ID from an object. This is useful if the object was
  829. * previously mutated using {@code goog.getUid} in which case the mutation is
  830. * undone.
  831. * @param {Object} obj The object to remove the unique ID field from.
  832. */
  833. goog.removeUid = function(obj) {
  834. // TODO(arv): Make the type stricter, do not accept null.
  835. // In IE, DOM nodes are not instances of Object and throw an exception if we
  836. // try to delete. Instead we try to use removeAttribute.
  837. if ('removeAttribute' in obj) {
  838. obj.removeAttribute(goog.UID_PROPERTY_);
  839. }
  840. /** @preserveTry */
  841. try {
  842. delete obj[goog.UID_PROPERTY_];
  843. } catch (ex) {
  844. }
  845. };
  846. /**
  847. * Name for unique ID property. Initialized in a way to help avoid collisions
  848. * with other closure JavaScript on the same page.
  849. * @type {string}
  850. * @private
  851. */
  852. goog.UID_PROPERTY_ = 'closure_uid_' + ((Math.random() * 1e9) >>> 0);
  853. /**
  854. * Counter for UID.
  855. * @type {number}
  856. * @private
  857. */
  858. goog.uidCounter_ = 0;
  859. /**
  860. * Adds a hash code field to an object. The hash code is unique for the
  861. * given object.
  862. * @param {Object} obj The object to get the hash code for.
  863. * @return {number} The hash code for the object.
  864. * @deprecated Use goog.getUid instead.
  865. */
  866. goog.getHashCode = goog.getUid;
  867. /**
  868. * Removes the hash code field from an object.
  869. * @param {Object} obj The object to remove the field from.
  870. * @deprecated Use goog.removeUid instead.
  871. */
  872. goog.removeHashCode = goog.removeUid;
  873. /**
  874. * Clones a value. The input may be an Object, Array, or basic type. Objects and
  875. * arrays will be cloned recursively.
  876. *
  877. * WARNINGS:
  878. * <code>goog.cloneObject</code> does not detect reference loops. Objects that
  879. * refer to themselves will cause infinite recursion.
  880. *
  881. * <code>goog.cloneObject</code> is unaware of unique identifiers, and copies
  882. * UIDs created by <code>getUid</code> into cloned results.
  883. *
  884. * @param {*} obj The value to clone.
  885. * @return {*} A clone of the input value.
  886. * @deprecated goog.cloneObject is unsafe. Prefer the goog.object methods.
  887. */
  888. goog.cloneObject = function(obj) {
  889. var type = goog.typeOf(obj);
  890. if (type == 'object' || type == 'array') {
  891. if (obj.clone) {
  892. return obj.clone();
  893. }
  894. var clone = type == 'array' ? [] : {};
  895. for (var key in obj) {
  896. clone[key] = goog.cloneObject(obj[key]);
  897. }
  898. return clone;
  899. }
  900. return obj;
  901. };
  902. /**
  903. * A native implementation of goog.bind.
  904. * @param {Function} fn A function to partially apply.
  905. * @param {Object|undefined} selfObj Specifies the object which this should
  906. * point to when the function is run.
  907. * @param {...*} var_args Additional arguments that are partially applied to the
  908. * function.
  909. * @return {!Function} A partially-applied form of the function bind() was
  910. * invoked as a method of.
  911. * @private
  912. * @suppress {deprecated} The compiler thinks that Function.prototype.bind is
  913. * deprecated because some people have declared a pure-JS version.
  914. * Only the pure-JS version is truly deprecated.
  915. */
  916. goog.bindNative_ = function(fn, selfObj, var_args) {
  917. return /** @type {!Function} */ (fn.call.apply(fn.bind, arguments));
  918. };
  919. /**
  920. * A pure-JS implementation of goog.bind.
  921. * @param {Function} fn A function to partially apply.
  922. * @param {Object|undefined} selfObj Specifies the object which this should
  923. * point to when the function is run.
  924. * @param {...*} var_args Additional arguments that are partially applied to the
  925. * function.
  926. * @return {!Function} A partially-applied form of the function bind() was
  927. * invoked as a method of.
  928. * @private
  929. */
  930. goog.bindJs_ = function(fn, selfObj, var_args) {
  931. if (!fn) {
  932. throw new Error();
  933. }
  934. if (arguments.length > 2) {
  935. var boundArgs = Array.prototype.slice.call(arguments, 2);
  936. return function() {
  937. // Prepend the bound arguments to the current arguments.
  938. var newArgs = Array.prototype.slice.call(arguments);
  939. Array.prototype.unshift.apply(newArgs, boundArgs);
  940. return fn.apply(selfObj, newArgs);
  941. };
  942. } else {
  943. return function() {
  944. return fn.apply(selfObj, arguments);
  945. };
  946. }
  947. };
  948. /**
  949. * Partially applies this function to a particular 'this object' and zero or
  950. * more arguments. The result is a new function with some arguments of the first
  951. * function pre-filled and the value of this 'pre-specified'.
  952. *
  953. * Remaining arguments specified at call-time are appended to the pre-specified
  954. * ones.
  955. *
  956. * Also see: {@link #partial}.
  957. *
  958. * Usage:
  959. * <pre>var barMethBound = bind(myFunction, myObj, 'arg1', 'arg2');
  960. * barMethBound('arg3', 'arg4');</pre>
  961. *
  962. * @param {?function(this:T, ...)} fn A function to partially apply.
  963. * @param {T} selfObj Specifies the object which this should point to when the
  964. * function is run.
  965. * @param {...*} var_args Additional arguments that are partially applied to the
  966. * function.
  967. * @return {!Function} A partially-applied form of the function bind() was
  968. * invoked as a method of.
  969. * @template T
  970. * @suppress {deprecated} See above.
  971. */
  972. goog.bind = function(fn, selfObj, var_args) {
  973. // TODO(nicksantos): narrow the type signature.
  974. if (Function.prototype.bind &&
  975. // NOTE(nicksantos): Somebody pulled base.js into the default Chrome
  976. // extension environment. This means that for Chrome extensions, they get
  977. // the implementation of Function.prototype.bind that calls goog.bind
  978. // instead of the native one. Even worse, we don't want to introduce a
  979. // circular dependency between goog.bind and Function.prototype.bind, so
  980. // we have to hack this to make sure it works correctly.
  981. Function.prototype.bind.toString().indexOf('native code') != -1) {
  982. goog.bind = goog.bindNative_;
  983. } else {
  984. goog.bind = goog.bindJs_;
  985. }
  986. return goog.bind.apply(null, arguments);
  987. };
  988. /**
  989. * Like bind(), except that a 'this object' is not required. Useful when the
  990. * target function is already bound.
  991. *
  992. * Usage:
  993. * var g = partial(f, arg1, arg2);
  994. * g(arg3, arg4);
  995. *
  996. * @param {Function} fn A function to partially apply.
  997. * @param {...*} var_args Additional arguments that are partially applied to fn.
  998. * @return {!Function} A partially-applied form of the function bind() was
  999. * invoked as a method of.
  1000. */
  1001. goog.partial = function(fn, var_args) {
  1002. var args = Array.prototype.slice.call(arguments, 1);
  1003. return function() {
  1004. // Prepend the bound arguments to the current arguments.
  1005. var newArgs = Array.prototype.slice.call(arguments);
  1006. newArgs.unshift.apply(newArgs, args);
  1007. return fn.apply(this, newArgs);
  1008. };
  1009. };
  1010. /**
  1011. * Copies all the members of a source object to a target object. This method
  1012. * does not work on all browsers for all objects that contain keys such as
  1013. * toString or hasOwnProperty. Use goog.object.extend for this purpose.
  1014. * @param {Object} target Target.
  1015. * @param {Object} source Source.
  1016. */
  1017. goog.mixin = function(target, source) {
  1018. for (var x in source) {
  1019. target[x] = source[x];
  1020. }
  1021. // For IE7 or lower, the for-in-loop does not contain any properties that are
  1022. // not enumerable on the prototype object (for example, isPrototypeOf from
  1023. // Object.prototype) but also it will not include 'replace' on objects that
  1024. // extend String and change 'replace' (not that it is common for anyone to
  1025. // extend anything except Object).
  1026. };
  1027. /**
  1028. * @return {number} An integer value representing the number of milliseconds
  1029. * between midnight, January 1, 1970 and the current time.
  1030. */
  1031. goog.now = (goog.TRUSTED_SITE && Date.now) || (function() {
  1032. // Unary plus operator converts its operand to a number which in the case of
  1033. // a date is done by calling getTime().
  1034. return +new Date();
  1035. });
  1036. /**
  1037. * Evals JavaScript in the global scope. In IE this uses execScript, other
  1038. * browsers use goog.global.eval. If goog.global.eval does not evaluate in the
  1039. * global scope (for example, in Safari), appends a script tag instead.
  1040. * Throws an exception if neither execScript or eval is defined.
  1041. * @param {string} script JavaScript string.
  1042. */
  1043. goog.globalEval = function(script) {
  1044. if (goog.global.execScript) {
  1045. goog.global.execScript(script, 'JavaScript');
  1046. } else if (goog.global.eval) {
  1047. // Test to see if eval works
  1048. if (goog.evalWorksForGlobals_ == null) {
  1049. goog.global.eval('var _et_ = 1;');
  1050. if (typeof goog.global['_et_'] != 'undefined') {
  1051. delete goog.global['_et_'];
  1052. goog.evalWorksForGlobals_ = true;
  1053. } else {
  1054. goog.evalWorksForGlobals_ = false;
  1055. }
  1056. }
  1057. if (goog.evalWorksForGlobals_) {
  1058. goog.global.eval(script);
  1059. } else {
  1060. var doc = goog.global.document;
  1061. var scriptElt = doc.createElement('script');
  1062. scriptElt.type = 'text/javascript';
  1063. scriptElt.defer = false;
  1064. // Note(user): can't use .innerHTML since "t('<test>')" will fail and
  1065. // .text doesn't work in Safari 2. Therefore we append a text node.
  1066. scriptElt.appendChild(doc.createTextNode(script));
  1067. doc.body.appendChild(scriptElt);
  1068. doc.body.removeChild(scriptElt);
  1069. }
  1070. } else {
  1071. throw Error('goog.globalEval not available');
  1072. }
  1073. };
  1074. /**
  1075. * Indicates whether or not we can call 'eval' directly to eval code in the
  1076. * global scope. Set to a Boolean by the first call to goog.globalEval (which
  1077. * empirically tests whether eval works for globals). @see goog.globalEval
  1078. * @type {?boolean}
  1079. * @private
  1080. */
  1081. goog.evalWorksForGlobals_ = null;
  1082. /**
  1083. * Optional map of CSS class names to obfuscated names used with
  1084. * goog.getCssName().
  1085. * @type {Object|undefined}
  1086. * @private
  1087. * @see goog.setCssNameMapping
  1088. */
  1089. goog.cssNameMapping_;
  1090. /**
  1091. * Optional obfuscation style for CSS class names. Should be set to either
  1092. * 'BY_WHOLE' or 'BY_PART' if defined.
  1093. * @type {string|undefined}
  1094. * @private
  1095. * @see goog.setCssNameMapping
  1096. */
  1097. goog.cssNameMappingStyle_;
  1098. /**
  1099. * Handles strings that are intended to be used as CSS class names.
  1100. *
  1101. * This function works in tandem with @see goog.setCssNameMapping.
  1102. *
  1103. * Without any mapping set, the arguments are simple joined with a hyphen and
  1104. * passed through unaltered.
  1105. *
  1106. * When there is a mapping, there are two possible styles in which these
  1107. * mappings are used. In the BY_PART style, each part (i.e. in between hyphens)
  1108. * of the passed in css name is rewritten according to the map. In the BY_WHOLE
  1109. * style, the full css name is looked up in the map directly. If a rewrite is
  1110. * not specified by the map, the compiler will output a warning.
  1111. *
  1112. * When the mapping is passed to the compiler, it will replace calls to
  1113. * goog.getCssName with the strings from the mapping, e.g.
  1114. * var x = goog.getCssName('foo');
  1115. * var y = goog.getCssName(this.baseClass, 'active');
  1116. * becomes:
  1117. * var x= 'foo';
  1118. * var y = this.baseClass + '-active';
  1119. *
  1120. * If one argument is passed it will be processed, if two are passed only the
  1121. * modifier will be processed, as it is assumed the first argument was generated
  1122. * as a result of calling goog.getCssName.
  1123. *
  1124. * @param {string} className The class name.
  1125. * @param {string=} opt_modifier A modifier to be appended to the class name.
  1126. * @return {string} The class name or the concatenation of the class name and
  1127. * the modifier.
  1128. */
  1129. goog.getCssName = function(className, opt_modifier) {
  1130. var getMapping = function(cssName) {
  1131. return goog.cssNameMapping_[cssName] || cssName;
  1132. };
  1133. var renameByParts = function(cssName) {
  1134. // Remap all the parts individually.
  1135. var parts = cssName.split('-');
  1136. var mapped = [];
  1137. for (var i = 0; i < parts.length; i++) {
  1138. mapped.push(getMapping(parts[i]));
  1139. }
  1140. return mapped.join('-');
  1141. };
  1142. var rename;
  1143. if (goog.cssNameMapping_) {
  1144. rename = goog.cssNameMappingStyle_ == 'BY_WHOLE' ?
  1145. getMapping : renameByParts;
  1146. } else {
  1147. rename = function(a) {
  1148. return a;
  1149. };
  1150. }
  1151. if (opt_modifier) {
  1152. return className + '-' + rename(opt_modifier);
  1153. } else {
  1154. return rename(className);
  1155. }
  1156. };
  1157. /**
  1158. * Sets the map to check when returning a value from goog.getCssName(). Example:
  1159. * <pre>
  1160. * goog.setCssNameMapping({
  1161. * "goog": "a",
  1162. * "disabled": "b",
  1163. * });
  1164. *
  1165. * var x = goog.getCssName('goog');
  1166. * // The following evaluates to: "a a-b".
  1167. * goog.getCssName('goog') + ' ' + goog.getCssName(x, 'disabled')
  1168. * </pre>
  1169. * When declared as a map of string literals to string literals, the JSCompiler
  1170. * will replace all calls to goog.getCssName() using the supplied map if the
  1171. * --closure_pass flag is set.
  1172. *
  1173. * @param {!Object} mapping A map of strings to strings where keys are possible
  1174. * arguments to goog.getCssName() and values are the corresponding values
  1175. * that should be returned.
  1176. * @param {string=} opt_style The style of css name mapping. There are two valid
  1177. * options: 'BY_PART', and 'BY_WHOLE'.
  1178. * @see goog.getCssName for a description.
  1179. */
  1180. goog.setCssNameMapping = function(mapping, opt_style) {
  1181. goog.cssNameMapping_ = mapping;
  1182. goog.cssNameMappingStyle_ = opt_style;
  1183. };
  1184. /**
  1185. * To use CSS renaming in compiled mode, one of the input files should have a
  1186. * call to goog.setCssNameMapping() with an object literal that the JSCompiler
  1187. * can extract and use to replace all calls to goog.getCssName(). In uncompiled
  1188. * mode, JavaScript code should be loaded before this base.js file that declares
  1189. * a global variable, CLOSURE_CSS_NAME_MAPPING, which is used below. This is
  1190. * to ensure that the mapping is loaded before any calls to goog.getCssName()
  1191. * are made in uncompiled mode.
  1192. *
  1193. * A hook for overriding the CSS name mapping.
  1194. * @type {Object|undefined}
  1195. */
  1196. goog.global.CLOSURE_CSS_NAME_MAPPING;
  1197. if (!COMPILED && goog.global.CLOSURE_CSS_NAME_MAPPING) {
  1198. // This does not call goog.setCssNameMapping() because the JSCompiler
  1199. // requires that goog.setCssNameMapping() be called with an object literal.
  1200. goog.cssNameMapping_ = goog.global.CLOSURE_CSS_NAME_MAPPING;
  1201. }
  1202. /**
  1203. * Gets a localized message.
  1204. *
  1205. * This function is a compiler primitive. If you give the compiler a localized
  1206. * message bundle, it will replace the string at compile-time with a localized
  1207. * version, and expand goog.getMsg call to a concatenated string.
  1208. *
  1209. * Messages must be initialized in the form:
  1210. * <code>
  1211. * var MSG_NAME = goog.getMsg('Hello {$placeholder}', {'placeholder': 'world'});
  1212. * </code>
  1213. *
  1214. * @param {string} str Translatable string, places holders in the form {$foo}.
  1215. * @param {Object=} opt_values Map of place holder name to value.
  1216. * @return {string} message with placeholders filled.
  1217. */
  1218. goog.getMsg = function(str, opt_values) {
  1219. var values = opt_values || {};
  1220. for (var key in values) {
  1221. var value = ('' + values[key]).replace(/\$/g, '$$$$');
  1222. str = str.replace(new RegExp('\\{\\$' + key + '\\}', 'gi'), value);
  1223. }
  1224. return str;
  1225. };
  1226. /**
  1227. * Gets a localized message. If the message does not have a translation, gives a
  1228. * fallback message.
  1229. *
  1230. * This is useful when introducing a new message that has not yet been
  1231. * translated into all languages.
  1232. *
  1233. * This function is a compiler primtive. Must be used in the form:
  1234. * <code>var x = goog.getMsgWithFallback(MSG_A, MSG_B);</code>
  1235. * where MSG_A and MSG_B were initialized with goog.getMsg.
  1236. *
  1237. * @param {string} a The preferred message.
  1238. * @param {string} b The fallback message.
  1239. * @return {string} The best translated message.
  1240. */
  1241. goog.getMsgWithFallback = function(a, b) {
  1242. return a;
  1243. };
  1244. /**
  1245. * Exposes an unobfuscated global namespace path for the given object.
  1246. * Note that fields of the exported object *will* be obfuscated, unless they are
  1247. * exported in turn via this function or goog.exportProperty.
  1248. *
  1249. * Also handy for making public items that are defined in anonymous closures.
  1250. *
  1251. * ex. goog.exportSymbol('public.path.Foo', Foo);
  1252. *
  1253. * ex. goog.exportSymbol('public.path.Foo.staticFunction', Foo.staticFunction);
  1254. * public.path.Foo.staticFunction();
  1255. *
  1256. * ex. goog.exportSymbol('public.path.Foo.prototype.myMethod',
  1257. * Foo.prototype.myMethod);
  1258. * new public.path.Foo().myMethod();
  1259. *
  1260. * @param {string} publicPath Unobfuscated name to export.
  1261. * @param {*} object Object the name should point to.
  1262. * @param {Object=} opt_objectToExportTo The object to add the path to; default
  1263. * is goog.global.
  1264. */
  1265. goog.exportSymbol = function(publicPath, object, opt_objectToExportTo) {
  1266. goog.exportPath_(publicPath, object, opt_objectToExportTo);
  1267. };
  1268. /**
  1269. * Exports a property unobfuscated into the object's namespace.
  1270. * ex. goog.exportProperty(Foo, 'staticFunction', Foo.staticFunction);
  1271. * ex. goog.exportProperty(Foo.prototype, 'myMethod', Foo.prototype.myMethod);
  1272. * @param {Object} object Object whose static property is being exported.
  1273. * @param {string} publicName Unobfuscated name to export.
  1274. * @param {*} symbol Object the name should point to.
  1275. */
  1276. goog.exportProperty = function(object, publicName, symbol) {
  1277. object[publicName] = symbol;
  1278. };
  1279. /**
  1280. * Inherit the prototype methods from one constructor into another.
  1281. *
  1282. * Usage:
  1283. * <pre>
  1284. * function ParentClass(a, b) { }
  1285. * ParentClass.prototype.foo = function(a) { }
  1286. *
  1287. * function ChildClass(a, b, c) {
  1288. * goog.base(this, a, b);
  1289. * }
  1290. * goog.inherits(ChildClass, ParentClass);
  1291. *
  1292. * var child = new ChildClass('a', 'b', 'see');
  1293. * child.foo(); // This works.
  1294. * </pre>
  1295. *
  1296. * In addition, a superclass' implementation of a method can be invoked as
  1297. * follows:
  1298. *
  1299. * <pre>
  1300. * ChildClass.prototype.foo = function(a) {
  1301. * ChildClass.superClass_.foo.call(this, a);
  1302. * // Other code here.
  1303. * };
  1304. * </pre>
  1305. *
  1306. * @param {Function} childCtor Child class.
  1307. * @param {Function} parentCtor Parent class.
  1308. */
  1309. goog.inherits = function(childCtor, parentCtor) {
  1310. /** @constructor */
  1311. function tempCtor() {};
  1312. tempCtor.prototype = parentCtor.prototype;
  1313. childCtor.superClass_ = parentCtor.prototype;
  1314. childCtor.prototype = new tempCtor();
  1315. /** @override */
  1316. childCtor.prototype.constructor = childCtor;
  1317. };
  1318. /**
  1319. * Call up to the superclass.
  1320. *
  1321. * If this is called from a constructor, then this calls the superclass
  1322. * contsructor with arguments 1-N.
  1323. *
  1324. * If this is called from a prototype method, then you must pass the name of the
  1325. * method as the second argument to this function. If you do not, you will get a
  1326. * runtime error. This calls the superclass' method with arguments 2-N.
  1327. *
  1328. * This function only works if you use goog.inherits to express inheritance
  1329. * relationships between your classes.
  1330. *
  1331. * This function is a compiler primitive. At compile-time, the compiler will do
  1332. * macro expansion to remove a lot of the extra overhead that this function
  1333. * introduces. The compiler will also enforce a lot of the assumptions that this
  1334. * function makes, and treat it as a compiler error if you break them.
  1335. *
  1336. * @param {!Object} me Should always be "this".
  1337. * @param {*=} opt_methodName The method name if calling a super method.
  1338. * @param {...*} var_args The rest of the arguments.
  1339. * @return {*} The return value of the superclass method.
  1340. */
  1341. goog.base = function(me, opt_methodName, var_args) {
  1342. var caller = arguments.callee.caller;
  1343. if (goog.DEBUG) {
  1344. if (!caller) {
  1345. throw Error('arguments.caller not defined. goog.base() expects not ' +
  1346. 'to be running in strict mode. See ' +
  1347. 'http://www.ecma-international.org/ecma-262/5.1/#sec-C');
  1348. }
  1349. }
  1350. if (caller.superClass_) {
  1351. // This is a constructor. Call the superclass constructor.
  1352. return caller.superClass_.constructor.apply(
  1353. me, Array.prototype.slice.call(arguments, 1));
  1354. }
  1355. var args = Array.prototype.slice.call(arguments, 2);
  1356. var foundCaller = false;
  1357. for (var ctor = me.constructor;
  1358. ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
  1359. if (ctor.prototype[opt_methodName] === caller) {
  1360. foundCaller = true;
  1361. } else if (foundCaller) {
  1362. return ctor.prototype[opt_methodName].apply(me, args);
  1363. }
  1364. }
  1365. // If we did not find the caller in the prototype chain, then one of two
  1366. // things happened:
  1367. // 1) The caller is an instance method.
  1368. // 2) This method was not called by the right caller.
  1369. if (me[opt_methodName] === caller) {
  1370. return me.constructor.prototype[opt_methodName].apply(me, args);
  1371. } else {
  1372. throw Error(
  1373. 'goog.base called from a method of one name ' +
  1374. 'to a method of a different name');
  1375. }
  1376. };
  1377. /**
  1378. * Allow for aliasing within scope functions. This function exists for
  1379. * uncompiled code - in compiled code the calls will be inlined and the aliases
  1380. * applied. In uncompiled code the function is simply run since the aliases as
  1381. * written are valid JavaScript.
  1382. * @param {function()} fn Function to call. This function can contain aliases
  1383. * to namespaces (e.g. "var dom = goog.dom") or classes
  1384. * (e.g. "var Timer = goog.Timer").
  1385. */
  1386. goog.scope = function(fn) {
  1387. fn.call(goog.global);
  1388. };