Bug 1344543 - Remove Intl API fallbacks from DownloadUtils.jsm now that Fennec no longer requires it. r=mak draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Wed, 19 Jul 2017 21:17:11 +0200
changeset 611491 f5519e7f2e9be7860e838e54ae3a3c80c26153f4
parent 611480 16348548c920328c277547c015ab4cf5aa1dfe86
child 638179 c04f4a8ece07a2b7a9ee47f83479b40a2e02946c
push id69240
push usermozilla@buttercookie.de
push dateWed, 19 Jul 2017 20:18:37 +0000
reviewersmak
bugs1344543
milestone56.0a1
Bug 1344543 - Remove Intl API fallbacks from DownloadUtils.jsm now that Fennec no longer requires it. r=mak Every little bit of code removal helps. MozReview-Commit-ID: LmSOE6MzFm3
toolkit/mozapps/downloads/DownloadUtils.jsm
toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
--- a/toolkit/mozapps/downloads/DownloadUtils.jsm
+++ b/toolkit/mozapps/downloads/DownloadUtils.jsm
@@ -44,25 +44,16 @@ const Cu = Components.utils;
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
                                   "resource://gre/modules/PluralForm.jsm");
 
 const MS_PER_DAY = 24 * 60 * 60 * 1000;
 
-Object.defineProperty(this, "gDecimalSymbol", {
-  configurable: true,
-  enumerable: true,
-  get() {
-    delete this.gDecimalSymbol;
-      return this.gDecimalSymbol = Number(5.4).toLocaleString().match(/\D/);
-  },
-});
-
 var localeNumberFormatCache = new Map();
 function getLocaleNumberFormat(fractionDigits) {
   // Backward compatibility: don't use localized digits
   let locale = Intl.NumberFormat().resolvedOptions().locale +
                "-u-nu-latn";
   let key = locale + "_" + fractionDigits;
   if (!localeNumberFormatCache.has(key)) {
     localeNumberFormatCache.set(key,
@@ -354,63 +345,38 @@ this.DownloadUtils = {
     }
 
     // Figure out when today begins
     let today = new Date(aNow.getFullYear(), aNow.getMonth(), aNow.getDate());
 
     let dateTimeCompact;
     let dateTimeFull;
 
-    // For Android, we have to keep the non Intl API version which uses
-    // deprecated toLocaleFormat until we get Intl API.
-    //
-    // For the rest of the platform, we'll use a combination of mozIntl,
-    // Intl API and localization.
-    if (typeof Intl === "undefined") {
-      // Figure out if the time is from today, yesterday, this week, etc.
-      if (aDate >= today) {
-        dateTimeCompact = aDate.toLocaleFormat("%X");
-      } else if (today - aDate < (MS_PER_DAY)) {
-        // After yesterday started, show yesterday
-        dateTimeCompact = gBundle.GetStringFromName(gStr.yesterday);
-      } else if (today - aDate < (6 * MS_PER_DAY)) {
-        // After last week started, show day of week
-        dateTimeCompact = aDate.toLocaleFormat("%A");
-      } else {
-        // Show month/day
-        let month = aDate.toLocaleFormat("%B");
-        let date = aDate.getDate();
-        dateTimeCompact = gBundle.formatStringFromName(gStr.monthDate, [month, date], 2);
-      }
-
-      dateTimeFull = aDate.toLocaleFormat("%x %X");
+    // Figure out if the time is from today, yesterday, this week, etc.
+    if (aDate >= today) {
+      let dts = Services.intl.createDateTimeFormat(undefined, {
+        timeStyle: "short"
+      });
+      dateTimeCompact = dts.format(aDate);
+    } else if (today - aDate < (MS_PER_DAY)) {
+      // After yesterday started, show yesterday
+      dateTimeCompact = gBundle.GetStringFromName(gStr.yesterday);
+    } else if (today - aDate < (6 * MS_PER_DAY)) {
+      // After last week started, show day of week
+      dateTimeCompact = aDate.toLocaleDateString(undefined, { weekday: "long" });
     } else {
-      // Figure out if the time is from today, yesterday, this week, etc.
-      if (aDate >= today) {
-        let dts = Services.intl.createDateTimeFormat(undefined, {
-          timeStyle: "short"
-        });
-        dateTimeCompact = dts.format(aDate);
-      } else if (today - aDate < (MS_PER_DAY)) {
-        // After yesterday started, show yesterday
-        dateTimeCompact = gBundle.GetStringFromName(gStr.yesterday);
-      } else if (today - aDate < (6 * MS_PER_DAY)) {
-        // After last week started, show day of week
-        dateTimeCompact = aDate.toLocaleDateString(undefined, { weekday: "long" });
-      } else {
-        // Show month/day
-        let month = aDate.toLocaleDateString(undefined, { month: "long" });
-        let date = aDate.getDate();
-        dateTimeCompact = gBundle.formatStringFromName(gStr.monthDate, [month, date], 2);
-      }
+      // Show month/day
+      let month = aDate.toLocaleDateString(undefined, { month: "long" });
+      let date = aDate.getDate();
+      dateTimeCompact = gBundle.formatStringFromName(gStr.monthDate, [month, date], 2);
+    }
 
-      const dtOptions = { dateStyle: "long", timeStyle: "short" };
-      dateTimeFull =
-        Services.intl.createDateTimeFormat(undefined, dtOptions).format(aDate);
-    }
+    const dtOptions = { dateStyle: "long", timeStyle: "short" };
+    dateTimeFull =
+      Services.intl.createDateTimeFormat(undefined, dtOptions).format(aDate);
 
     return [dateTimeCompact, dateTimeFull];
   },
 
   /**
    * Get the appropriate display host string for a URI string depending on if
    * the URI has an eTLD + 1, is an IP address, a local file, or other protocol
    *
@@ -499,25 +465,18 @@ this.DownloadUtils = {
     // Get rid of insignificant bits by truncating to 1 or 0 decimal points
     // 0 -> 0; 1.2 -> 1.2; 12.3 -> 12.3; 123.4 -> 123; 234.5 -> 235
     // added in bug 462064: (unitIndex != 0) makes sure that no decimal digit for bytes appears when aBytes < 100
     let fractionDigits = (aBytes > 0) && (aBytes < 100) && (unitIndex != 0) ? 1 : 0;
 
     // Don't try to format Infinity values using NumberFormat.
     if (aBytes === Infinity) {
       aBytes = "Infinity";
-    } else if (typeof Intl != "undefined") {
-      aBytes = getLocaleNumberFormat(fractionDigits)
-                 .format(aBytes);
     } else {
-      // FIXME: Fall back to the old hack, will be fixed in bug 1344543.
-      aBytes = aBytes.toFixed(fractionDigits);
-      if (gDecimalSymbol != ".") {
-        aBytes = aBytes.replace(".", gDecimalSymbol);
-      }
+      aBytes = getLocaleNumberFormat(fractionDigits).format(aBytes);
     }
 
     return [aBytes, gBundle.GetStringFromName(gStr.units[unitIndex])];
   },
 
   /**
    * Converts a number of seconds to the two largest units. Time values are
    * whole numbers, and units have the correct plural/singular form.
--- a/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
@@ -73,49 +73,37 @@ function testAllGetReadableDates() {
   const today_11_30     = new Date(2000, 11, 31, 11, 30, 15);
   const today_12_30     = new Date(2000, 11, 31, 12, 30, 15);
   const yesterday_11_30 = new Date(2000, 11, 30, 11, 30, 15);
   const yesterday_12_30 = new Date(2000, 11, 30, 12, 30, 15);
   const twodaysago      = new Date(2000, 11, 29, 11, 30, 15);
   const sixdaysago      = new Date(2000, 11, 25, 11, 30, 15);
   const sevendaysago    = new Date(2000, 11, 24, 11, 30, 15);
 
-  let cDtf = typeof Intl === "undefined" ? null : Services.intl.createDateTimeFormat;
+  let cDtf = Services.intl.createDateTimeFormat;
 
   testGetReadableDates(today_11_30,
-                       typeof Intl === "undefined"
-                       ? today_11_30.toLocaleFormat("%X")
-                       : cDtf(undefined, {timeStyle: "short"}).format(today_11_30));
+                       cDtf(undefined, {timeStyle: "short"}).format(today_11_30));
   testGetReadableDates(today_12_30,
-                       typeof Intl === "undefined"
-                       ? today_12_30.toLocaleFormat("%X")
-                       : cDtf(undefined, {timeStyle: "short"}).format(today_12_30));
+                       cDtf(undefined, {timeStyle: "short"}).format(today_12_30));
 
   testGetReadableDates(yesterday_11_30, "Yesterday");
   testGetReadableDates(yesterday_12_30, "Yesterday");
   testGetReadableDates(twodaysago,
-                       typeof Intl === "undefined"
-                       ? twodaysago.toLocaleFormat("%A")
-                       : twodaysago.toLocaleDateString(undefined, { weekday: "long" }));
+                       twodaysago.toLocaleDateString(undefined, { weekday: "long" }));
   testGetReadableDates(sixdaysago,
-                       typeof Intl === "undefined"
-                       ? sixdaysago.toLocaleFormat("%A")
-                       : sixdaysago.toLocaleDateString(undefined, { weekday: "long" }));
+                       sixdaysago.toLocaleDateString(undefined, { weekday: "long" }));
   testGetReadableDates(sevendaysago,
-                       (typeof Intl === "undefined"
-                        ? sevendaysago.toLocaleFormat("%B")
-                        : sevendaysago.toLocaleDateString(undefined, { month: "long" })) + " " +
+                       sevendaysago.toLocaleDateString(undefined, { month: "long" }) + " " +
                        sevendaysago.getDate().toString().padStart(2, "0"));
 
   let [, dateTimeFull] = DownloadUtils.getReadableDates(today_11_30);
 
   const dtOptions = { dateStyle: "long", timeStyle: "short" };
-  do_check_eq(dateTimeFull, typeof Intl === "undefined"
-                            ? today_11_30.toLocaleFormat("%x %X")
-                            : cDtf(undefined, dtOptions).format(today_11_30));
+  do_check_eq(dateTimeFull, cDtf(undefined, dtOptions).format(today_11_30));
 }
 
 function run_test() {
   testConvertByteUnits(-1, "-1", "bytes");
   testConvertByteUnits(1, _("1"), "bytes");
   testConvertByteUnits(42, _("42"), "bytes");
   testConvertByteUnits(123, _("123"), "bytes");
   testConvertByteUnits(1024, _("1.0"), "KB");