Bug 1370228 - "Context menu title with placeholder always shows ellipsis" [r=mixedpuppy]
authorLukas Jung <lukas.jung>
Thu, 21 Dec 2017 08:33:00 +0200
changeset 397335 8882102c7640
parent 397334 29495c8b47f5
child 397336 5dec62f3eeb1
push id98502
push useraciure@mozilla.com
push dateFri, 22 Dec 2017 10:30:52 +0000
treeherdermozilla-inbound@8882102c7640 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1370228
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1370228 - "Context menu title with placeholder always shows ellipsis" [r=mixedpuppy]
browser/components/extensions/ext-menus.js
browser/components/extensions/test/browser/browser_ext_contextMenus.js
--- a/browser/components/extensions/ext-menus.js
+++ b/browser/components/extensions/ext-menus.js
@@ -177,20 +177,36 @@ var gMenuBuilder = {
   customizeElement(element, item, contextData) {
     let label = item.title;
     if (label) {
       if (contextData.isTextSelected && label.indexOf("%s") > -1) {
         let selection = contextData.selectionText.trim();
         // The rendering engine will truncate the title if it's longer than 64 characters.
         // But if it makes sense let's try truncate selection text only, to handle cases like
         // 'look up "%s" in MyDictionary' more elegantly.
-        let maxSelectionLength = gMaxLabelLength - label.length + 2;
-        if (maxSelectionLength > 4) {
-          selection = selection.substring(0, maxSelectionLength - 3) + "...";
+
+        let codePointsToRemove = 0;
+
+        let selectionArray = Array.from(selection);
+
+        let completeLabelLength = label.length - 2 + selectionArray.length;
+        if (completeLabelLength > gMaxLabelLength) {
+          codePointsToRemove = completeLabelLength - gMaxLabelLength;
         }
+
+        if (codePointsToRemove) {
+          let ellipsis = "\u2026";
+          try {
+            ellipsis = Services.prefs.getComplexValue("intl.ellipsis",
+                                                      Ci.nsIPrefLocalizedString).data;
+          } catch (e) { }
+          codePointsToRemove += 1;
+          selection = selectionArray.slice(0, -codePointsToRemove).join("") + ellipsis;
+        }
+
         label = label.replace(/%s/g, selection);
       }
 
       element.setAttribute("label", label);
     }
 
     if (item.id && item.extension && item.extension.id) {
       element.setAttribute("id", `${makeWidgetId(item.extension.id)}_${item.id}`);
--- a/browser/components/extensions/test/browser/browser_ext_contextMenus.js
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus.js
@@ -277,17 +277,17 @@ add_task(async function() {
 
   result = await extension.awaitMessage("browser.contextMenus.onClicked");
   checkClickInfo(result);
 
   // Bring up context menu again
   extensionMenuRoot = await openExtensionContextMenu();
 
   // Check some menu items
-  items = extensionMenuRoot.getElementsByAttribute("label", "selection is: 'just some text 123456789012345678901234567890...'");
+  items = extensionMenuRoot.getElementsByAttribute("label", "selection is: 'just some text 12345678901234567890123456789012\u2026'");
   is(items.length, 1, "contextMenu item for selection was found (context=selection)");
   let selectionItem = items[0];
 
   items = extensionMenuRoot.getElementsByAttribute("label", "selection");
   is(items.length, 0, "contextMenu item label update worked (context=selection)");
 
   await closeExtensionContextMenu(selectionItem);
 
@@ -313,17 +313,17 @@ add_task(async function() {
     range.setEnd(textNode, textNode.length);
     selection.addRange(range);
   });
 
   // Bring up context menu again
   extensionMenuRoot = await openExtensionContextMenu("#longtext");
 
   // Check some menu items
-  items = extensionMenuRoot.getElementsByAttribute("label", "selection is: 'Sed ut perspiciatis unde omnis iste natus err...'");
+  items = extensionMenuRoot.getElementsByAttribute("label", "selection is: 'Sed ut perspiciatis unde omnis iste natus error\u2026'");
   is(items.length, 1, `contextMenu item for longtext selection was found (context=selection)`);
   await closeExtensionContextMenu(items[0]);
 
   expectedClickInfo = {
     menuItemId: "ext-selection",
     pageUrl: PAGE,
   };