Adding mapbox-gl branch

This commit is contained in:
Andreas Hocevar
2015-03-16 18:50:27 +01:00
parent 7985f030fa
commit 57ee7f52fd
3109 changed files with 943365 additions and 0 deletions
@@ -0,0 +1,74 @@
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Object which fetches Unicode codepoint names that are locally
* stored in a bundled database. Currently, only invisible characters are
* covered by this database. See the goog.i18n.uChar.RemoteNameFetcher class for
* a remote database option.
*/
goog.provide('goog.i18n.uChar.LocalNameFetcher');
goog.require('goog.i18n.uChar');
goog.require('goog.i18n.uChar.NameFetcher');
goog.require('goog.log');
/**
* Builds the NameFetcherLocal object. This is a simple object which retrieves
* character names from a local bundled database. This database only covers
* invisible characters. See the goog.i18n.uChar class for more details.
*
* @constructor
* @implements {goog.i18n.uChar.NameFetcher}
* @final
*/
goog.i18n.uChar.LocalNameFetcher = function() {
};
/**
* A reference to the LocalNameFetcher logger.
*
* @type {goog.log.Logger}
* @private
*/
goog.i18n.uChar.LocalNameFetcher.logger_ =
goog.log.getLogger('goog.i18n.uChar.LocalNameFetcher');
/** @override */
goog.i18n.uChar.LocalNameFetcher.prototype.prefetch = function(character) {
};
/** @override */
goog.i18n.uChar.LocalNameFetcher.prototype.getName = function(character,
callback) {
var localName = goog.i18n.uChar.toName(character);
if (!localName) {
goog.i18n.uChar.LocalNameFetcher.logger_.
warning('No local name defined for character ' + character);
}
callback(localName);
};
/** @override */
goog.i18n.uChar.LocalNameFetcher.prototype.isNameAvailable = function(
character) {
return !!goog.i18n.uChar.toName(character);
};
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<!--
Copyright 2012 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>
Closure Unit Tests - goog.i18n.uChar.LocalNameFetcher
</title>
<meta charset="utf-8" />
<script src="../../base.js">
</script>
<script>
goog.require('goog.i18n.uChar.LocalNameFetcherTest');
</script>
</head>
<body>
</body>
</html>
@@ -0,0 +1,59 @@
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.i18n.uChar.LocalNameFetcherTest');
goog.setTestOnly('goog.i18n.uChar.LocalNameFetcherTest');
goog.require('goog.i18n.uChar.LocalNameFetcher');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');
var nameFetcher = null;
function setUp() {
nameFetcher = new goog.i18n.uChar.LocalNameFetcher();
}
function testGetName_exists() {
var callback = goog.testing.recordFunction(function(name) {
assertEquals('Space', name);
});
nameFetcher.getName(' ', callback);
assertEquals(1, callback.getCallCount());
}
function testGetName_variationSelector() {
var callback = goog.testing.recordFunction(function(name) {
assertEquals('Variation Selector - 1', name);
});
nameFetcher.getName('\ufe00', callback);
assertEquals(1, callback.getCallCount());
}
function testGetName_missing() {
var callback = goog.testing.recordFunction(function(name) {
assertNull(name);
});
nameFetcher.getName('P', callback);
assertEquals(1, callback.getCallCount());
}
function testIsNameAvailable_withAvailableName() {
assertTrue(nameFetcher.isNameAvailable(' '));
}
function testIsNameAvailable_withoutAvailableName() {
assertFalse(nameFetcher.isNameAvailable('a'));
}
@@ -0,0 +1,70 @@
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Definition of the goog.i18n.CharNameFetcher interface. This
* interface is used to retrieve individual character names.
*/
goog.provide('goog.i18n.uChar.NameFetcher');
/**
* NameFetcher interface. Implementations of this interface are used to retrieve
* Unicode character names.
*
* @interface
*/
goog.i18n.uChar.NameFetcher = function() {
};
/**
* Retrieves the names of a given set of characters and stores them in a cache
* for fast retrieval. Offline implementations can simply provide an empty
* implementation.
*
* @param {string} characters The list of characters in base 88 to fetch. These
* lists are stored by category and subcategory in the
* goog.i18n.charpickerdata class.
*/
goog.i18n.uChar.NameFetcher.prototype.prefetch = function(characters) {
};
/**
* Retrieves the name of a particular character.
*
* @param {string} character The character to retrieve.
* @param {function(?string)} callback The callback function called when the
* name retrieval is complete, contains a single string parameter with the
* codepoint name, this parameter will be null if the character name is not
* defined.
*/
goog.i18n.uChar.NameFetcher.prototype.getName = function(character, callback) {
};
/**
* Tests whether the name of a given character is available to be retrieved by
* the getName() function.
*
* @param {string} character The character to test.
* @return {boolean} True if the fetcher can retrieve or has a name available
* for the given character.
*/
goog.i18n.uChar.NameFetcher.prototype.isNameAvailable = function(character) {
};
@@ -0,0 +1,282 @@
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Object which fetches Unicode codepoint names from a remote data
* source. This data source should accept two parameters:
* <ol>
* <li>c - the list of codepoints in hexadecimal format
* <li>p - the name property
* </ol>
* and return a JSON object representation of the result.
* For example, calling this data source with the following URL:
* http://datasource?c=50,ff,102bd&p=name
* Should return a JSON object which looks like this:
* <pre>
* {"50":{"name":"LATIN CAPITAL LETTER P"},
* "ff":{"name":"LATIN SMALL LETTER Y WITH DIAERESIS"},
* "102bd":{"name":"CARIAN LETTER K2"}}
* </pre>.
*/
goog.provide('goog.i18n.uChar.RemoteNameFetcher');
goog.require('goog.Disposable');
goog.require('goog.Uri');
goog.require('goog.i18n.uChar');
goog.require('goog.i18n.uChar.NameFetcher');
goog.require('goog.log');
goog.require('goog.net.XhrIo');
goog.require('goog.structs.Map');
/**
* Builds the RemoteNameFetcher object. This object retrieves codepoint names
* from a remote data source.
*
* @param {string} dataSourceUri URI to the data source.
* @constructor
* @implements {goog.i18n.uChar.NameFetcher}
* @extends {goog.Disposable}
* @final
*/
goog.i18n.uChar.RemoteNameFetcher = function(dataSourceUri) {
goog.i18n.uChar.RemoteNameFetcher.base(this, 'constructor');
/**
* XHRIo object for prefetch() asynchronous calls.
*
* @type {!goog.net.XhrIo}
* @private
*/
this.prefetchXhrIo_ = new goog.net.XhrIo();
/**
* XHRIo object for getName() asynchronous calls.
*
* @type {!goog.net.XhrIo}
* @private
*/
this.getNameXhrIo_ = new goog.net.XhrIo();
/**
* URI to the data.
*
* @type {string}
* @private
*/
this.dataSourceUri_ = dataSourceUri;
/**
* A cache of all the collected names from the server.
*
* @type {!goog.structs.Map}
* @private
*/
this.charNames_ = new goog.structs.Map();
};
goog.inherits(goog.i18n.uChar.RemoteNameFetcher, goog.Disposable);
/**
* Key to the listener on XHR for prefetch(). Used to clear previous listeners.
*
* @type {goog.events.Key}
* @private
*/
goog.i18n.uChar.RemoteNameFetcher.prototype.prefetchLastListenerKey_;
/**
* Key to the listener on XHR for getName(). Used to clear previous listeners.
*
* @type {goog.events.Key}
* @private
*/
goog.i18n.uChar.RemoteNameFetcher.prototype.getNameLastListenerKey_;
/**
* A reference to the RemoteNameFetcher logger.
*
* @type {goog.log.Logger}
* @private
*/
goog.i18n.uChar.RemoteNameFetcher.logger_ =
goog.log.getLogger('goog.i18n.uChar.RemoteNameFetcher');
/** @override */
goog.i18n.uChar.RemoteNameFetcher.prototype.disposeInternal = function() {
goog.i18n.uChar.RemoteNameFetcher.base(this, 'disposeInternal');
this.prefetchXhrIo_.dispose();
this.getNameXhrIo_.dispose();
};
/** @override */
goog.i18n.uChar.RemoteNameFetcher.prototype.prefetch = function(characters) {
// Abort the current request if there is one
if (this.prefetchXhrIo_.isActive()) {
goog.i18n.uChar.RemoteNameFetcher.logger_.
info('Aborted previous prefetch() call for new incoming request');
this.prefetchXhrIo_.abort();
}
if (this.prefetchLastListenerKey_) {
goog.events.unlistenByKey(this.prefetchLastListenerKey_);
}
// Set up new listener
var preFetchCallback = goog.bind(this.prefetchCallback_, this);
this.prefetchLastListenerKey_ = goog.events.listenOnce(this.prefetchXhrIo_,
goog.net.EventType.COMPLETE, preFetchCallback);
this.fetch_(goog.i18n.uChar.RemoteNameFetcher.RequestType_.BASE_88,
characters, this.prefetchXhrIo_);
};
/**
* Callback on completion of the prefetch operation.
*
* @private
*/
goog.i18n.uChar.RemoteNameFetcher.prototype.prefetchCallback_ = function() {
this.processResponse_(this.prefetchXhrIo_);
};
/** @override */
goog.i18n.uChar.RemoteNameFetcher.prototype.getName = function(character,
callback) {
var codepoint = goog.i18n.uChar.toCharCode(character).toString(16);
if (this.charNames_.containsKey(codepoint)) {
var name = /** @type {string} */ (this.charNames_.get(codepoint));
callback(name);
return;
}
// Abort the current request if there is one
if (this.getNameXhrIo_.isActive()) {
goog.i18n.uChar.RemoteNameFetcher.logger_.
info('Aborted previous getName() call for new incoming request');
this.getNameXhrIo_.abort();
}
if (this.getNameLastListenerKey_) {
goog.events.unlistenByKey(this.getNameLastListenerKey_);
}
// Set up new listener
var getNameCallback = goog.bind(this.getNameCallback_, this, codepoint,
callback);
this.getNameLastListenerKey_ = goog.events.listenOnce(this.getNameXhrIo_,
goog.net.EventType.COMPLETE, getNameCallback);
this.fetch_(goog.i18n.uChar.RemoteNameFetcher.RequestType_.CODEPOINT,
codepoint, this.getNameXhrIo_);
};
/**
* Callback on completion of the getName operation.
*
* @param {string} codepoint The codepoint in hexadecimal format.
* @param {function(?string)} callback The callback function called when the
* name retrieval is complete, contains a single string parameter with the
* codepoint name, this parameter will be null if the character name is not
* defined.
* @private
*/
goog.i18n.uChar.RemoteNameFetcher.prototype.getNameCallback_ = function(
codepoint, callback) {
this.processResponse_(this.getNameXhrIo_);
var name = /** @type {?string} */ (this.charNames_.get(codepoint, null));
callback(name);
};
/**
* Process the response received from the server and store results in the cache.
*
* @param {!goog.net.XhrIo} xhrIo The XhrIo object used to make the request.
* @private
*/
goog.i18n.uChar.RemoteNameFetcher.prototype.processResponse_ = function(xhrIo) {
if (!xhrIo.isSuccess()) {
goog.log.error(goog.i18n.uChar.RemoteNameFetcher.logger_,
'Problem with data source: ' + xhrIo.getLastError());
return;
}
var result = xhrIo.getResponseJson();
for (var codepoint in result) {
if (result[codepoint].hasOwnProperty('name')) {
this.charNames_.set(codepoint, result[codepoint]['name']);
}
}
};
/**
* Enum for the different request types.
*
* @enum {string}
* @private
*/
goog.i18n.uChar.RemoteNameFetcher.RequestType_ = {
/**
* Request type that uses a base 88 string containing a set of codepoints to
* be fetched from the server (see goog.i18n.charpickerdata for more
* information on b88).
*/
BASE_88: 'b88',
/**
* Request type that uses a a string of comma separated codepoint values.
*/
CODEPOINT: 'c'
};
/**
* Fetches a set of codepoint names from the data source.
*
* @param {!goog.i18n.uChar.RemoteNameFetcher.RequestType_} requestType The
* request type of the operation. This parameter specifies how the server is
* called to fetch a particular set of codepoints.
* @param {string} requestInput The input to the request, this is the value that
* is passed onto the server to complete the request.
* @param {!goog.net.XhrIo} xhrIo The XHRIo object to execute the server call.
* @private
*/
goog.i18n.uChar.RemoteNameFetcher.prototype.fetch_ = function(requestType,
requestInput, xhrIo) {
var url = new goog.Uri(this.dataSourceUri_);
url.setParameterValue(requestType, requestInput);
url.setParameterValue('p', 'name');
goog.log.info(goog.i18n.uChar.RemoteNameFetcher.logger_, 'Request: ' +
url.toString());
xhrIo.send(url);
};
/** @override */
goog.i18n.uChar.RemoteNameFetcher.prototype.isNameAvailable = function(
character) {
return true;
};
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<!--
Copyright 2012 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>
Closure Unit Tests - goog.i18n.uChar.RemoteNameFetcher
</title>
<meta charset="utf-8" />
<script src="../../base.js">
</script>
<script>
goog.require('goog.i18n.uChar.RemoteNameFetcherTest');
</script>
</head>
<body>
</body>
</html>
@@ -0,0 +1,108 @@
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.i18n.uChar.RemoteNameFetcherTest');
goog.setTestOnly('goog.i18n.uChar.RemoteNameFetcherTest');
goog.require('goog.i18n.uChar.RemoteNameFetcher');
goog.require('goog.net.XhrIo');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.net.XhrIo');
goog.require('goog.testing.recordFunction');
var nameFetcher = null;
function setUp() {
goog.net.XhrIo = goog.testing.net.XhrIo;
nameFetcher = new goog.i18n.uChar.RemoteNameFetcher('http://www.example.com');
}
function tearDown() {
nameFetcher.dispose();
}
function testGetName_remote() {
var callback = goog.testing.recordFunction(function(name) {
assertEquals('Latin Capital Letter P', name);
assertTrue(nameFetcher.charNames_.containsKey('50'));
});
nameFetcher.getName('P', callback);
var responseJsonText = '{"50":{"name":"Latin Capital Letter P"}}';
nameFetcher.getNameXhrIo_.simulateResponse(200, responseJsonText);
assertEquals(1, callback.getCallCount());
}
function testGetName_existing() {
nameFetcher.charNames_.set('1049d', 'OSYMANYA LETTER OO');
var callback = goog.testing.recordFunction(function(name) {
assertEquals('OSYMANYA LETTER OO', name);
});
nameFetcher.getName('\uD801\uDC9D', callback);
assertEquals(1, callback.getCallCount());
}
function testGetName_fail() {
var callback = goog.testing.recordFunction(function(name) {
assertNull(name);
});
nameFetcher.getName('\uD801\uDC9D', callback);
assertEquals('http://www.example.com?c=1049d&p=name',
nameFetcher.getNameXhrIo_.getLastUri().toString());
nameFetcher.getNameXhrIo_.simulateResponse(400);
assertEquals(1, callback.getCallCount());
}
function testGetName_abort() {
var callback1 = goog.testing.recordFunction(function(name) {
assertNull(name);
});
nameFetcher.getName('I', callback1);
var callback2 = goog.testing.recordFunction(function(name) {
assertEquals(name, 'LATIN SMALL LETTER Y');
});
nameFetcher.getName('ÿ', callback2);
assertEquals('http://www.example.com?c=ff&p=name',
nameFetcher.getNameXhrIo_.getLastUri().toString());
var responseJsonText = '{"ff":{"name":"LATIN SMALL LETTER Y"}}';
nameFetcher.getNameXhrIo_.simulateResponse(200, responseJsonText);
assertEquals(1, callback1.getCallCount());
assertEquals(1, callback2.getCallCount());
}
function testPrefetch() {
nameFetcher.prefetch('ÿI\uD801\uDC9D');
assertEquals('http://www.example.com?b88=%C3%BFI%F0%90%92%9D&p=name',
nameFetcher.prefetchXhrIo_.getLastUri().toString());
var responseJsonText = '{"ff":{"name":"LATIN SMALL LETTER Y"},"49":{' +
'"name":"LATIN CAPITAL LETTER I"}, "1049d":{"name":"OSMYANA OO"}}';
nameFetcher.prefetchXhrIo_.simulateResponse(200, responseJsonText);
assertEquals(3, nameFetcher.charNames_.getCount());
assertEquals('LATIN SMALL LETTER Y', nameFetcher.charNames_.get('ff'));
assertEquals('LATIN CAPITAL LETTER I', nameFetcher.charNames_.get('49'));
assertEquals('OSMYANA OO', nameFetcher.charNames_.get('1049d'));
}
function testPrefetch_abort() {
nameFetcher.prefetch('I\uD801\uDC9D');
nameFetcher.prefetch('ÿ');
assertEquals('http://www.example.com?b88=%C3%BF&p=name',
nameFetcher.prefetchXhrIo_.getLastUri().toString());
}
function testIsNameAvailable() {
assertTrue(nameFetcher.isNameAvailable('a'));
}