diff --git a/Rakefile b/Rakefile index 166f76c..537672b 100644 --- a/Rakefile +++ b/Rakefile @@ -55,3 +55,12 @@ var CLOSURE_NO_DEPS = true; end puts "Saved to #{outfile}" end + +require 'rake/testtask' + +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = false +end diff --git a/support/libphonenumber.js b/support/libphonenumber.js index 890925a..dc74ab2 100644 --- a/support/libphonenumber.js +++ b/support/libphonenumber.js @@ -24,6 +24,8 @@ var CLOSURE_NO_DEPS = true; * global CLOSURE_NO_DEPS is set to true. This allows projects to * include their own deps file(s) from different locations. * + * + * @provideGoog */ @@ -83,6 +85,20 @@ goog.DEBUG = true; goog.LOCALE = 'en'; // default to en +/** + * @define {boolean} Whether this code is running on trusted sites. + * + * On untrusted sites, several native functions can be defined or overridden by + * external libraries like Prototype, Datejs, and JQuery and setting this flag + * to false forces closure to use its own implementations when possible. + * + * If your javascript can be loaded by a third party site and you are wary about + * relying on non-standard implementations, specify + * "--define goog.TRUSTED_SITE=false" to the JSCompiler. + */ +goog.TRUSTED_SITE = true; + + /** * Creates object stubs for a namespace. The presence of one or more * goog.provide() calls indicate that the file defines the given @@ -271,7 +287,7 @@ goog.addDependency = function(relPath, provides, requires) { -// NOTE(user): The debug DOM loader was included in base.js as an orignal +// NOTE(nnaze): The debug DOM loader was included in base.js as an orignal // way to do "debug-mode" development. The dependency system can sometimes // be confusing, as can the debug DOM loader's asyncronous nature. // @@ -314,10 +330,10 @@ goog.ENABLE_DEBUG_LOADER = true; goog.require = function(name) { // if the object already exists we do not need do do anything - // TODO(user): If we start to support require based on file name this has + // TODO(arv): If we start to support require based on file name this has // to change - // TODO(user): If we allow goog.foo.* this has to change - // TODO(user): If we implement dynamic load after page load we should probably + // TODO(arv): If we allow goog.foo.* this has to change + // TODO(arv): If we implement dynamic load after page load we should probably // not remove this code for the compiled output if (!COMPILED) { if (goog.isProvided_(name)) { @@ -389,12 +405,14 @@ goog.nullFunction = function() {}; /** * The identity function. Returns its first argument. * - * @param {...*} var_args The arguments of the function. - * @return {*} The first argument. + * @param {*=} opt_returnValue The single value that will be returned. + * @param {...*} var_args Optional trailing arguments. These are ignored. + * @return {?} The first argument. We can't know the type -- just pass it along + * without type. * @deprecated Use goog.functions.identity instead. */ -goog.identityFunction = function(var_args) { - return arguments[0]; +goog.identityFunction = function(opt_returnValue, var_args) { + return opt_returnValue; }; @@ -427,11 +445,28 @@ goog.abstractMethod = function() { */ goog.addSingletonGetter = function(ctor) { ctor.getInstance = function() { - return ctor.instance_ || (ctor.instance_ = new ctor()); + if (ctor.instance_) { + return ctor.instance_; + } + if (goog.DEBUG) { + // NOTE: JSCompiler can't optimize away Array#push. + goog.instantiatedSingletons_[goog.instantiatedSingletons_.length] = ctor; + } + return ctor.instance_ = new ctor; }; }; +/** + * All singleton classes that have been instantiated, for testing. Don't read + * it directly, use the {@code goog.testing.singleton} module. The compiler + * removes this variable if unused. + * @type {!Array.} + * @private + */ +goog.instantiatedSingletons_ = []; + + if (!COMPILED && goog.ENABLE_DEBUG_LOADER) { /** * Object used to keep track of urls that have already been added. This @@ -524,6 +559,23 @@ if (!COMPILED && goog.ENABLE_DEBUG_LOADER) { goog.writeScriptTag_ = function(src) { if (goog.inHtmlDocument_()) { var doc = goog.global.document; + + // If the user tries to require a new symbol after document load, + // something has gone terribly wrong. Doing a document.write would + // wipe out the page. + if (doc.readyState == 'complete') { + // Certain test frameworks load base.js multiple times, which tries + // to write deps.js each time. If that happens, just fail silently. + // These frameworks wipe the page between each load of base.js, so this + // is OK. + var isDeps = /\bdeps.js$/.test(src); + if (isDeps) { + return false; + } else { + throw Error('Cannot write "' + src + '" after document load'); + } + } + doc.write( '