Bug 1200494 - Part 1 - Remove Intl API fallbacks from DownloadUtils.jsm now that Fennec no longer requires it. r=mak draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sat, 11 Feb 2017 17:45:29 +0100
changeset 486266 f5931ae62737ae5e1be52f45e9cb1a193991de9a
parent 486170 7020030ddeb1c79d164a0042dc6f270c30bd69e7
child 486267 c31a50e9d1b6c8f0fa15527f6028cb743fc260af
push id45938
push usermozilla@buttercookie.de
push dateFri, 17 Feb 2017 19:04:50 +0000
reviewersmak
bugs1200494
milestone54.0a1
Bug 1200494 - Part 1 - 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: 70yy4oZ1dq4
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
@@ -41,21 +41,16 @@ const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
                                   "resource://gre/modules/PluralForm.jsm");
 
-this.__defineGetter__("gDecimalSymbol", function() {
-    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,
@@ -344,45 +339,38 @@ this.DownloadUtils = {
 
     let dts = Cc["@mozilla.org/intl/scriptabledateformat;1"]
               .getService(Ci.nsIScriptableDateFormat);
 
     // Figure out when today begins
     let today = new Date(aNow.getFullYear(), aNow.getMonth(), aNow.getDate());
 
     // Get locale to use for date/time formatting
-    // TODO: Remove Intl fallback when bug 1215247 is fixed.
-    const locale = typeof Intl === "undefined"
-                   ? undefined
-                   : Cc["@mozilla.org/chrome/chrome-registry;1"]
-                       .getService(Ci.nsIXULChromeRegistry)
-                       .getSelectedLocale("global", true);
+    const locale = Cc["@mozilla.org/chrome/chrome-registry;1"]
+                     .getService(Ci.nsIXULChromeRegistry)
+                     .getSelectedLocale("global", true);
 
     // Figure out if the time is from today, yesterday, this week, etc.
     let dateTimeCompact;
     if (aDate >= today) {
       // After today started, show the time
       dateTimeCompact = dts.FormatTime("",
                                        dts.timeFormatNoSeconds,
                                        aDate.getHours(),
                                        aDate.getMinutes(),
                                        0);
     } else if (today - aDate < (24 * 60 * 60 * 1000)) {
       // After yesterday started, show yesterday
       dateTimeCompact = gBundle.GetStringFromName(gStr.yesterday);
     } else if (today - aDate < (6 * 24 * 60 * 60 * 1000)) {
       // After last week started, show day of week
-      dateTimeCompact = typeof Intl === "undefined"
-                        ? aDate.toLocaleFormat("%A")
-                        : aDate.toLocaleDateString(locale, { weekday: "long" });
+      dateTimeCompact = aDate.toLocaleDateString(locale, { weekday: "long" });
     } else {
       // Show month/day
-      let month = typeof Intl === "undefined"
-                  ? aDate.toLocaleFormat("%B")
-                  : aDate.toLocaleDateString(locale, { month: "long" });
+      let month = aDate.toLocaleDateString(locale, { month: "long" });
       let date = aDate.getDate();
       dateTimeCompact = gBundle.formatStringFromName(gStr.monthDate, [month, date], 2);
     }
 
     let dateTimeFull = dts.FormatDateTime("",
                                           dts.dateFormatLong,
                                           dts.timeFormatNoSeconds,
                                           aDate.getFullYear(),
@@ -484,25 +472,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 1200494.
-      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,44 +73,35 @@ 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);
 
-  // TODO: Remove Intl fallback when bug 1215247 is fixed.
-  const locale = typeof Intl === "undefined"
-                 ? undefined
-                 : Components.classes["@mozilla.org/chrome/chrome-registry;1"]
-                                     .getService(Components.interfaces.nsIXULChromeRegistry)
-                                     .getSelectedLocale("global", true);
+  const locale = Components.classes["@mozilla.org/chrome/chrome-registry;1"]
+                                   .getService(Components.interfaces.nsIXULChromeRegistry)
+                                   .getSelectedLocale("global", true);
 
   let dts = Components.classes["@mozilla.org/intl/scriptabledateformat;1"].
             getService(Components.interfaces.nsIScriptableDateFormat);
 
   testGetReadableDates(today_11_30, dts.FormatTime("", dts.timeFormatNoSeconds,
                                                    11, 30, 0));
   testGetReadableDates(today_12_30, dts.FormatTime("", dts.timeFormatNoSeconds,
                                                    12, 30, 0));
   testGetReadableDates(yesterday_11_30, "Yesterday");
   testGetReadableDates(yesterday_12_30, "Yesterday");
   testGetReadableDates(twodaysago,
-                       typeof Intl === "undefined"
-                       ? twodaysago.toLocaleFormat("%A")
-                       : twodaysago.toLocaleDateString(locale, { weekday: "long" }));
+                       twodaysago.toLocaleDateString(locale, { weekday: "long" }));
   testGetReadableDates(sixdaysago,
-                       typeof Intl === "undefined"
-                       ? sixdaysago.toLocaleFormat("%A")
-                       : sixdaysago.toLocaleDateString(locale, { weekday: "long" }));
+                       sixdaysago.toLocaleDateString(locale, { weekday: "long" }));
   testGetReadableDates(sevendaysago,
-                       (typeof Intl === "undefined"
-                        ? sevendaysago.toLocaleFormat("%B")
-                        : sevendaysago.toLocaleDateString(locale, { month: "long" })) + " " +
+                       sevendaysago.toLocaleDateString(locale, { month: "long" }) + " " +
                        sevendaysago.getDate().toString().padStart(2, "0"));
 
   let [, dateTimeFull] = DownloadUtils.getReadableDates(today_11_30);
   do_check_eq(dateTimeFull, dts.FormatDateTime("", dts.dateFormatLong,
                                                    dts.timeFormatNoSeconds,
                                                    2000, 12, 31, 11, 30, 0));
 }