зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1352324 - (Part 3) Update PhoneNumber utility to fulfill Form Autofill. r=MattN
- Fixed the path of resources. - Fixed the getter of nationalNumber with national prefix. - Updated to support parsing numbers by countryCode. - Added a getter of countryCode. - Added the licensing header. MozReview-Commit-ID: 6FEIK4KzGVL --HG-- extra : rebase_source : f0ea0c7b51d5a9c93958745af44cfcf9b90934d8
This commit is contained in:
Родитель
d3886a2572
Коммит
fa01fc963c
|
@ -5,6 +5,7 @@
|
||||||
[features/formautofill@mozilla.org] chrome.jar:
|
[features/formautofill@mozilla.org] chrome.jar:
|
||||||
% resource formautofill %res/
|
% resource formautofill %res/
|
||||||
res/ (*.jsm)
|
res/ (*.jsm)
|
||||||
|
res/phonenumberutils/ (phonenumberutils/*.jsm)
|
||||||
|
|
||||||
% content formautofill %content/
|
% content formautofill %content/
|
||||||
content/ (content/*)
|
content/ (content/*)
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
/* This Source Code Form is subject to the terms of the Apache License, Version
|
||||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
* 2.0. If a copy of the Apache License was not distributed with this file, You
|
||||||
|
* can obtain one at https://www.apache.org/licenses/LICENSE-2.0 */
|
||||||
|
|
||||||
// Don't modify this code. Please use:
|
// This library came from https://github.com/andreasgal/PhoneNumber.js but will
|
||||||
// https://github.com/andreasgal/PhoneNumber.js
|
// be further maintained by our own in Form Autofill codebase.
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
@ -12,9 +13,9 @@ const Cu = Components.utils;
|
||||||
|
|
||||||
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
||||||
XPCOMUtils.defineLazyModuleGetter(this, "PHONE_NUMBER_META_DATA",
|
XPCOMUtils.defineLazyModuleGetter(this, "PHONE_NUMBER_META_DATA",
|
||||||
"resource://gre/modules/PhoneNumberMetaData.jsm");
|
"resource://formautofill/phonenumberutils/PhoneNumberMetaData.jsm");
|
||||||
XPCOMUtils.defineLazyModuleGetter(this, "PhoneNumberNormalizer",
|
XPCOMUtils.defineLazyModuleGetter(this, "PhoneNumberNormalizer",
|
||||||
"resource://gre/modules/PhoneNumberNormalizer.jsm");
|
"resource://formautofill/phonenumberutils/PhoneNumberNormalizer.jsm");
|
||||||
this.PhoneNumber = (function (dataBase) {
|
this.PhoneNumber = (function (dataBase) {
|
||||||
// Use strict in our context only - users might not want it
|
// Use strict in our context only - users might not want it
|
||||||
'use strict';
|
'use strict';
|
||||||
|
@ -199,7 +200,7 @@ this.PhoneNumber = (function (dataBase) {
|
||||||
function NationalNumber(regionMetaData, number) {
|
function NationalNumber(regionMetaData, number) {
|
||||||
this.region = regionMetaData.region;
|
this.region = regionMetaData.region;
|
||||||
this.regionMetaData = regionMetaData;
|
this.regionMetaData = regionMetaData;
|
||||||
this.nationalNumber = number;
|
this.number = number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NationalNumber represents the result of parsing a phone number. We have
|
// NationalNumber represents the result of parsing a phone number. We have
|
||||||
|
@ -209,13 +210,13 @@ this.PhoneNumber = (function (dataBase) {
|
||||||
NationalNumber.prototype = {
|
NationalNumber.prototype = {
|
||||||
// +1 949-726-2896
|
// +1 949-726-2896
|
||||||
get internationalFormat() {
|
get internationalFormat() {
|
||||||
var value = FormatNumber(this.regionMetaData, this.nationalNumber, true);
|
var value = FormatNumber(this.regionMetaData, this.number, true);
|
||||||
Object.defineProperty(this, "internationalFormat", { value: value, enumerable: true });
|
Object.defineProperty(this, "internationalFormat", { value: value, enumerable: true });
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
// (949) 726-2896
|
// (949) 726-2896
|
||||||
get nationalFormat() {
|
get nationalFormat() {
|
||||||
var value = FormatNumber(this.regionMetaData, this.nationalNumber, false);
|
var value = FormatNumber(this.regionMetaData, this.number, false);
|
||||||
Object.defineProperty(this, "nationalFormat", { value: value, enumerable: true });
|
Object.defineProperty(this, "nationalFormat", { value: value, enumerable: true });
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
|
@ -226,11 +227,24 @@ this.PhoneNumber = (function (dataBase) {
|
||||||
Object.defineProperty(this, "internationalNumber", { value: value, enumerable: true });
|
Object.defineProperty(this, "internationalNumber", { value: value, enumerable: true });
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
|
// 9497262896
|
||||||
|
get nationalNumber() {
|
||||||
|
var value = this.nationalFormat ? this.nationalFormat.replace(NON_DIALABLE_CHARS, "")
|
||||||
|
: null;
|
||||||
|
Object.defineProperty(this, "nationalNumber", { value: value, enumerable: true });
|
||||||
|
return value;
|
||||||
|
},
|
||||||
// country name 'US'
|
// country name 'US'
|
||||||
get countryName() {
|
get countryName() {
|
||||||
var value = this.region ? this.region : null;
|
var value = this.region ? this.region : null;
|
||||||
Object.defineProperty(this, "countryName", { value: value, enumerable: true });
|
Object.defineProperty(this, "countryName", { value: value, enumerable: true });
|
||||||
return value;
|
return value;
|
||||||
|
},
|
||||||
|
// country code '+1'
|
||||||
|
get countryCode() {
|
||||||
|
var value = this.regionMetaData.countryCode ? "+" + this.regionMetaData.countryCode : null;
|
||||||
|
Object.defineProperty(this, "countryCode", { value: value, enumerable: true });
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -258,14 +272,18 @@ this.PhoneNumber = (function (dataBase) {
|
||||||
// Parse an international number that starts with the country code. Return
|
// Parse an international number that starts with the country code. Return
|
||||||
// null if the number is not a valid international number.
|
// null if the number is not a valid international number.
|
||||||
function ParseInternationalNumber(number) {
|
function ParseInternationalNumber(number) {
|
||||||
var ret;
|
|
||||||
|
|
||||||
// Parse and strip the country code.
|
// Parse and strip the country code.
|
||||||
var countryCode = ParseCountryCode(number);
|
var countryCode = ParseCountryCode(number);
|
||||||
if (!countryCode)
|
if (!countryCode)
|
||||||
return null;
|
return null;
|
||||||
number = number.substr(countryCode.length);
|
number = number.substr(countryCode.length);
|
||||||
|
|
||||||
|
return ParseNumberByCountryCode(number, countryCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ParseNumberByCountryCode(number, countryCode) {
|
||||||
|
var ret;
|
||||||
|
|
||||||
// Lookup the meta data for the region (or regions) and if the rest of
|
// Lookup the meta data for the region (or regions) and if the rest of
|
||||||
// the number parses for that region, return the parsed number.
|
// the number parses for that region, return the parsed number.
|
||||||
var entry = dataBase[countryCode];
|
var entry = dataBase[countryCode];
|
||||||
|
@ -275,7 +293,7 @@ this.PhoneNumber = (function (dataBase) {
|
||||||
entry[n] = ParseMetaData(countryCode, entry[n]);
|
entry[n] = ParseMetaData(countryCode, entry[n]);
|
||||||
if (n > 0)
|
if (n > 0)
|
||||||
entry[n].formats = entry[0].formats;
|
entry[n].formats = entry[0].formats;
|
||||||
ret = ParseNationalNumber(number, entry[n])
|
ret = ParseNationalNumberAndCheckNationalPrefix(number, entry[n]);
|
||||||
if (ret)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -283,7 +301,34 @@ this.PhoneNumber = (function (dataBase) {
|
||||||
}
|
}
|
||||||
if (typeof entry == "string")
|
if (typeof entry == "string")
|
||||||
entry = dataBase[countryCode] = ParseMetaData(countryCode, entry);
|
entry = dataBase[countryCode] = ParseMetaData(countryCode, entry);
|
||||||
return ParseNationalNumber(number, entry);
|
return ParseNationalNumberAndCheckNationalPrefix(number, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ParseNationalNumberAndCheckNationalPrefix(number, md) {
|
||||||
|
var ret;
|
||||||
|
|
||||||
|
// This is not an international number. See if its a national one for
|
||||||
|
// the current region. National numbers can start with the national
|
||||||
|
// prefix, or without.
|
||||||
|
if (md.nationalPrefixForParsing) {
|
||||||
|
// Some regions have specific national prefix parse rules. Apply those.
|
||||||
|
var withoutPrefix = number.replace(md.nationalPrefixForParsing,
|
||||||
|
md.nationalPrefixTransformRule || '');
|
||||||
|
ret = ParseNationalNumber(withoutPrefix, md)
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
} else {
|
||||||
|
// If there is no specific national prefix rule, just strip off the
|
||||||
|
// national prefix from the beginning of the number (if there is one).
|
||||||
|
var nationalPrefix = md.nationalPrefix;
|
||||||
|
if (nationalPrefix && number.indexOf(nationalPrefix) == 0 &&
|
||||||
|
(ret = ParseNationalNumber(number.substr(nationalPrefix.length), md))) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret = ParseNationalNumber(number, md)
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse a national number for a specific region. Return null if the
|
// Parse a national number for a specific region. Return null if the
|
||||||
|
@ -315,6 +360,16 @@ this.PhoneNumber = (function (dataBase) {
|
||||||
if (number[0] === '+')
|
if (number[0] === '+')
|
||||||
return ParseInternationalNumber(number.replace(LEADING_PLUS_CHARS_PATTERN, ""));
|
return ParseInternationalNumber(number.replace(LEADING_PLUS_CHARS_PATTERN, ""));
|
||||||
|
|
||||||
|
// If "defaultRegion" is a country code, use it to parse the number directly.
|
||||||
|
var matches = String(defaultRegion).match(/^\+?(\d+)/);
|
||||||
|
if (matches) {
|
||||||
|
var countryCode = ParseCountryCode(matches[1]);
|
||||||
|
if (!countryCode) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return ParseNumberByCountryCode(number, countryCode);
|
||||||
|
}
|
||||||
|
|
||||||
// Lookup the meta data for the given region.
|
// Lookup the meta data for the given region.
|
||||||
var md = FindMetaDataForRegion(defaultRegion.toUpperCase());
|
var md = FindMetaDataForRegion(defaultRegion.toUpperCase());
|
||||||
|
|
||||||
|
@ -333,26 +388,7 @@ this.PhoneNumber = (function (dataBase) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is not an international number. See if its a national one for
|
ret = ParseNationalNumberAndCheckNationalPrefix(number, md);
|
||||||
// the current region. National numbers can start with the national
|
|
||||||
// prefix, or without.
|
|
||||||
if (md.nationalPrefixForParsing) {
|
|
||||||
// Some regions have specific national prefix parse rules. Apply those.
|
|
||||||
var withoutPrefix = number.replace(md.nationalPrefixForParsing,
|
|
||||||
md.nationalPrefixTransformRule || '');
|
|
||||||
ret = ParseNationalNumber(withoutPrefix, md)
|
|
||||||
if (ret)
|
|
||||||
return ret;
|
|
||||||
} else {
|
|
||||||
// If there is no specific national prefix rule, just strip off the
|
|
||||||
// national prefix from the beginning of the number (if there is one).
|
|
||||||
var nationalPrefix = md.nationalPrefix;
|
|
||||||
if (nationalPrefix && number.indexOf(nationalPrefix) == 0 &&
|
|
||||||
(ret = ParseNationalNumber(number.substr(nationalPrefix.length), md))) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ret = ParseNationalNumber(number, md)
|
|
||||||
if (ret)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
/* This Source Code Form is subject to the terms of the Apache License, Version
|
||||||
|
* 2.0. If a copy of the Apache License was not distributed with this file, You
|
||||||
|
* can obtain one at https://www.apache.org/licenses/LICENSE-2.0 */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This data was generated base on libphonenumber v8.4.1 via the script in
|
* This data was generated base on libphonenumber v8.4.1 via the script in
|
||||||
* https://github.com/andreasgal/PhoneNumber.js
|
* https://github.com/andreasgal/PhoneNumber.js
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
/* This Source Code Form is subject to the terms of the Apache License, Version
|
||||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
* 2.0. If a copy of the Apache License was not distributed with this file, You
|
||||||
|
* can obtain one at https://www.apache.org/licenses/LICENSE-2.0 */
|
||||||
|
|
||||||
// Don't modify this code. Please use:
|
// This library came from https://github.com/andreasgal/PhoneNumber.js but will
|
||||||
// https://github.com/andreasgal/PhoneNumber.js
|
// be further maintained by our own in Form Autofill codebase.
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче