base.js 95 KB

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