Bug 1428172 - Align mozIntl with Intl when working with constructors. r=nalexander

MozReview-Commit-ID: 5jGk1jXKkay

--HG--
extra : rebase_source : fc5445104a6d14cbacd417ce9b9b4b0bd0ea8212
This commit is contained in:
Zibi Braniecki 2018-01-04 13:01:43 -08:00
parent 3016fb8d35
commit 1a111ee852
21 changed files with 69 additions and 43 deletions

View File

@ -314,7 +314,7 @@ var AboutNetAndCertErrorListener = {
// and adjusting the date per the interval would make the cert valid, warn the user:
if (Math.abs(difference) > 60 * 60 * 24 && (now - lastFetched) <= 60 * 60 * 24 * 5 &&
certRange.notBefore < approximateDate && certRange.notAfter > approximateDate) {
let formatter = Services.intl.createDateTimeFormat(undefined, {
let formatter = new Services.intl.DateTimeFormat(undefined, {
dateStyle: "short"
});
let systemDate = formatter.format(new Date());
@ -350,7 +350,7 @@ var AboutNetAndCertErrorListener = {
// so we shouldn't exclude the possibility that the cert has become valid
// since the build date.
if (buildDate > systemDate && new Date(certRange.notAfter) > buildDate) {
let formatter = Services.intl.createDateTimeFormat(undefined, {
let formatter = new Services.intl.DateTimeFormat(undefined, {
dateStyle: "short"
});

View File

@ -1016,7 +1016,7 @@ function formatDate(datestr, unknown) {
if (!date.valueOf())
return unknown;
const dateTimeFormatter = Services.intl.createDateTimeFormat(undefined, {
const dateTimeFormatter = new Services.intl.DateTimeFormat(undefined, {
dateStyle: "long", timeStyle: "long"
});
return dateTimeFormatter.format(date);

View File

@ -151,7 +151,7 @@ add_task(async function checkWrongSystemTimeWarning() {
});
}
let formatter = Services.intl.createDateTimeFormat(undefined, {
let formatter = new Services.intl.DateTimeFormat(undefined, {
dateStyle: "short"
});

View File

@ -192,7 +192,7 @@ FeedWriter.prototype = {
timeStyle: "short",
dateStyle: "long"
};
this.__dateFormatter = Services.intl.createDateTimeFormat(undefined, dtOptions);
this.__dateFormatter = new Services.intl.DateTimeFormat(undefined, dtOptions);
}
return this.__dateFormatter;
},

View File

@ -413,7 +413,7 @@ var PlacesOrganizer = {
const dtOptions = {
dateStyle: "long"
};
let dateFormatter = Services.intl.createDateTimeFormat(undefined, dtOptions);
let dateFormatter = new Services.intl.DateTimeFormat(undefined, dtOptions);
// Remove existing menu items. Last item is the restoreFromFile item.
while (restorePopup.childNodes.length > 1)

View File

@ -556,7 +556,7 @@ PlacesTreeView.prototype = {
get _todayFormatter() {
if (!this.__todayFormatter) {
const dtOptions = { timeStyle: "short" };
this.__todayFormatter = Services.intl.createDateTimeFormat(undefined, dtOptions);
this.__todayFormatter = new Services.intl.DateTimeFormat(undefined, dtOptions);
}
return this.__todayFormatter;
},
@ -568,7 +568,7 @@ PlacesTreeView.prototype = {
dateStyle: "short",
timeStyle: "short"
};
this.__dateFormatter = Services.intl.createDateTimeFormat(undefined, dtOptions);
this.__dateFormatter = new Services.intl.DateTimeFormat(undefined, dtOptions);
}
return this.__dateFormatter;
},

View File

@ -138,7 +138,7 @@
// a redirecting uri could be put in the tree while we test.
break;
}
let timeStr = Services.intl.createDateTimeFormat(undefined, dtOptions).format(timeObj);
let timeStr = new Services.intl.DateTimeFormat(undefined, dtOptions).format(timeObj);
is(text, timeStr, "Date format is correct");
break;

View File

@ -491,7 +491,7 @@ var gCookiesWindow = {
formatExpiresString(aExpires) {
if (aExpires) {
var date = new Date(1000 * aExpires);
const dateTimeFormatter = Services.intl.createDateTimeFormat(undefined, {
const dateTimeFormatter = new Services.intl.DateTimeFormat(undefined, {
dateStyle: "long", timeStyle: "long"
});
return dateTimeFormatter.format(date);

View File

@ -41,5 +41,5 @@ interface mozIMozIntl : nsISupports
jsval getDisplayNames([optional] in jsval locales, [optional] in jsval options);
jsval getLocaleInfo([optional] in jsval locales);
jsval createDateTimeFormat([optional] in jsval locales, [optional] in jsval options);
readonly attribute jsval DateTimeFormat;
};

View File

@ -69,27 +69,31 @@ class MozIntl {
return this._cache.getLocaleInfo(getLocales(locales), ...args);
}
createDateTimeFormat(locales, options, ...args) {
get DateTimeFormat() {
if (!this._cache.hasOwnProperty("DateTimeFormat")) {
mozIntlHelper.addDateTimeFormatConstructor(this._cache);
}
let resolvedLocales =
this._cache.DateTimeFormat.supportedLocalesOf(getLocales(locales));
let DateTimeFormat = this._cache.DateTimeFormat;
if (options) {
if (options.dateStyle || options.timeStyle) {
options.pattern = osPrefs.getDateTimePattern(
getDateTimePatternStyle(options.dateStyle),
getDateTimePatternStyle(options.timeStyle),
resolvedLocales[0]);
} else {
// make sure that user doesn't pass a pattern explicitly
options.pattern = undefined;
class MozDateTimeFormat extends this._cache.DateTimeFormat {
constructor(locales, options, ...args) {
let resolvedLocales = DateTimeFormat.supportedLocalesOf(getLocales(locales));
if (options) {
if (options.dateStyle || options.timeStyle) {
options.pattern = osPrefs.getDateTimePattern(
getDateTimePatternStyle(options.dateStyle),
getDateTimePatternStyle(options.timeStyle),
resolvedLocales[0]);
} else {
// make sure that user doesn't pass a pattern explicitly
options.pattern = undefined;
}
}
super(resolvedLocales, options, ...args);
}
}
return new this._cache.DateTimeFormat(resolvedLocales, options, ...args);
return MozDateTimeFormat;
}
}

View File

@ -6,6 +6,7 @@ Components.utils.import("resource://gre/modules/Services.jsm");
function run_test() {
test_methods_presence();
test_methods_calling();
test_constructors();
ok(true);
}
@ -14,13 +15,34 @@ function test_methods_presence() {
equal(Services.intl.getCalendarInfo instanceof Function, true);
equal(Services.intl.getDisplayNames instanceof Function, true);
equal(Services.intl.getLocaleInfo instanceof Function, true);
equal(Services.intl.createDateTimeFormat instanceof Function, true);
equal(Services.intl.getLocaleInfo instanceof Object, true);
}
function test_methods_calling() {
Services.intl.getCalendarInfo("pl");
Services.intl.getDisplayNames("ar");
Services.intl.getLocaleInfo("de");
Services.intl.createDateTimeFormat("fr");
new Services.intl.DateTimeFormat("fr");
ok(true);
}
function test_constructors() {
let dtf = new Intl.DateTimeFormat();
let dtf2 = new Services.intl.DateTimeFormat();
equal(typeof dtf, typeof dtf2);
Assert.throws(() => {
// This is an observable difference between Intl and mozIntl.
//
// Old ECMA402 APIs (edition 1 and 2) allowed for constructors to be called
// as functions.
// Starting from ed.3 all new constructors are throwing when called without |new|.
//
// All MozIntl APIs do not implement the legacy behavior and throw
// when called without |new|.
//
// For more information see https://github.com/tc39/ecma402/pull/84 .
Services.intl.DateTimeFormat();
}, /class constructors must be invoked with |new|/);
}

View File

@ -1448,7 +1448,7 @@ function UserAutoCompleteResult(aSearchString, matchingLogins, {isSecure, messag
this.matchCount = matchingLogins.length + this._showInsecureFieldWarning;
this._messageManager = messageManager;
this._stringBundle = Services.strings.createBundle("chrome://passwordmgr/locale/passwordmgr.properties");
this._dateAndTimeFormatter = Services.intl.createDateTimeFormat(undefined, { dateStyle: "medium" });
this._dateAndTimeFormatter = new Services.intl.DateTimeFormat(undefined, { dateStyle: "medium" });
this._isPasswordField = isPasswordField;

View File

@ -191,7 +191,7 @@ XPCOMUtils.defineLazyGetter(LoginManagerContextMenu, "_stringBundle", function()
});
XPCOMUtils.defineLazyGetter(LoginManagerContextMenu, "dateAndTimeFormatter", function() {
return Services.intl.createDateTimeFormat(undefined, {
return new Services.intl.DateTimeFormat(undefined, {
dateStyle: "medium"
});
});

View File

@ -60,9 +60,9 @@ let signonReloadDisplay = {
};
// Formatter for localization.
let dateFormatter = Services.intl.createDateTimeFormat(undefined,
let dateFormatter = new Services.intl.DateTimeFormat(undefined,
{ dateStyle: "medium" });
let dateAndTimeFormatter = Services.intl.createDateTimeFormat(undefined,
let dateAndTimeFormatter = new Services.intl.DateTimeFormat(undefined,
{ dateStyle: "medium",
timeStyle: "short" });

View File

@ -134,7 +134,7 @@ async function reinitializeForm(index) {
}
function generateDateString(date) {
let dateAndTimeFormatter = Services.intl.createDateTimeFormat(undefined,
let dateAndTimeFormatter = new Services.intl.DateTimeFormat(undefined,
{ dateStyle: "medium" });
return dateAndTimeFormatter.format(date);
}

View File

@ -102,7 +102,7 @@ function checkLoginItems(logins, items) {
}
let duplicates = findDuplicates(logins);
let dateAndTimeFormatter = Services.intl.createDateTimeFormat(undefined,
let dateAndTimeFormatter = new Services.intl.DateTimeFormat(undefined,
{ dateStyle: "medium" });
for (let login of logins) {
if (login.username && !duplicates.has(login.username)) {

View File

@ -24,7 +24,7 @@ matchingLogins.push(new nsLoginInfo("http://mochi.test:8888", "http://autocomple
"zzzuser4", "zzzpass4", "uname", "pword"));
let meta = matchingLogins[0].QueryInterface(Ci.nsILoginMetaInfo);
let dateAndTimeFormatter = Services.intl.createDateTimeFormat(undefined,
let dateAndTimeFormatter = new Services.intl.DateTimeFormat(undefined,
{ dateStyle: "medium" });
let time = dateAndTimeFormatter.format(new Date(meta.timePasswordChanged));
const LABEL_NO_USERNAME = "No username (" + time + ")";

View File

@ -424,7 +424,7 @@ var PingPicker = {
for (let p of this._archivedPings) {
pingTypes.add(p.type);
const pingDate = new Date(p.timestampCreated);
const datetimeText = Services.intl.createDateTimeFormat(undefined, {
const datetimeText = new Services.intl.DateTimeFormat(undefined, {
dateStyle: "short",
timeStyle: "medium"
}).format(pingDate);

View File

@ -73,8 +73,8 @@ function populateReportList() {
var dateFormatter;
var timeFormatter;
try {
dateFormatter = Services.intl.createDateTimeFormat(undefined, { dateStyle: "short" });
timeFormatter = Services.intl.createDateTimeFormat(undefined, { timeStyle: "short" });
dateFormatter = new Services.intl.DateTimeFormat(undefined, { dateStyle: "short" });
timeFormatter = new Services.intl.DateTimeFormat(undefined, { timeStyle: "short" });
} catch (e) {
// XXX Fallback to be removed once bug 1215247 is complete
// and the Intl API is available on all platforms.

View File

@ -349,7 +349,7 @@ this.DownloadUtils = {
// Figure out if the time is from today, yesterday, this week, etc.
if (aDate >= today) {
let dts = Services.intl.createDateTimeFormat(undefined, {
let dts = new Services.intl.DateTimeFormat(undefined, {
timeStyle: "short"
});
dateTimeCompact = dts.format(aDate);
@ -369,7 +369,7 @@ this.DownloadUtils = {
const dtOptions = { dateStyle: "long", timeStyle: "short" };
dateTimeFull =
Services.intl.createDateTimeFormat(undefined, dtOptions).format(aDate);
new Services.intl.DateTimeFormat(undefined, dtOptions).format(aDate);
return [dateTimeCompact, dateTimeFull];
},

View File

@ -78,12 +78,12 @@ function testAllGetReadableDates() {
const sixdaysago = new Date(2000, 11, 25, 11, 30, 15);
const sevendaysago = new Date(2000, 11, 24, 11, 30, 15);
let cDtf = Services.intl.createDateTimeFormat;
let cDtf = Services.intl.DateTimeFormat;
testGetReadableDates(today_11_30,
cDtf(undefined, {timeStyle: "short"}).format(today_11_30));
(new cDtf(undefined, {timeStyle: "short"})).format(today_11_30));
testGetReadableDates(today_12_30,
cDtf(undefined, {timeStyle: "short"}).format(today_12_30));
(new cDtf(undefined, {timeStyle: "short"})).format(today_12_30));
testGetReadableDates(yesterday_11_30, "Yesterday");
testGetReadableDates(yesterday_12_30, "Yesterday");
@ -98,7 +98,7 @@ function testAllGetReadableDates() {
let [, dateTimeFull] = DownloadUtils.getReadableDates(today_11_30);
const dtOptions = { dateStyle: "long", timeStyle: "short" };
Assert.equal(dateTimeFull, cDtf(undefined, dtOptions).format(today_11_30));
Assert.equal(dateTimeFull, (new cDtf(undefined, dtOptions)).format(today_11_30));
}
function run_test() {