Bug 1265326 - Made font menu handle multiple comma-separated font options. r=aceman a=rkent
authorJorg K
Sat, 23 Apr 2016 14:49:00 +0200
changeset 31290 e12dfb7067615187dbe2e93d310a9050fa331bf0
parent 31289 7cbef619e80d9a94e86d7879d4c746145ce0b5f9
child 31291 3dec139562278bc8d36a03a1f2166aebeff282ee
push id1
push userclokep@gmail.com
push dateMon, 07 May 2018 22:45:56 +0000
treeherdercomm-esr60@57eacde5ef40 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaceman, rkent
bugs1265326
Bug 1265326 - Made font menu handle multiple comma-separated font options. r=aceman a=rkent
editor/ui/composer/content/editor.js
--- a/editor/ui/composer/content/editor.js
+++ b/editor/ui/composer/content/editor.js
@@ -722,17 +722,17 @@ function onFontFaceChange(fontFaceMenuLi
   let editorFontOptions = editorFontToLower.split(",");
   let editorOptionsCount = editorFontOptions.length;
   let matchedFontIndex = editorOptionsCount; // initialise to high invalid value
 
   // The font menu has this structure:
   // 0: Variable Width
   // 1: Fixed Width
   // 2: Separator
-  // 3: Helvetica, Arial (stored as Helvetica, Arial, sans-serif
+  // 3: Helvetica, Arial (stored as Helvetica, Arial, sans-serif)
   // 4: Times (stored as Times New Roman, Times, serif)
   // 5: Courier (stored as Courier New, Courier, monospace)
   // 6: Separator, "menuseparator.fontFaceMenuAfterDefaultFonts"
   // from 7: Used Font Section (for quick selection)
   // followed by separator, "menuseparator.fontFaceMenuAfterUsedFonts"
   // followed by all other available fonts.
   // The following variable keeps track of where we are when we loop over the menu.
   let afterUsedFontSection = false;
@@ -949,60 +949,94 @@ function initFontFaceMenu(menuPopup)
   initLocalFontFaceMenu(menuPopup);
 
   if (menuPopup)
   {
     var children = menuPopup.childNodes;
     if (!children) return;
 
     var mixed = { value: false };
-    var state = GetCurrentEditor().getFontFaceState(mixed);
+    var editorFont = GetCurrentEditor().getFontFaceState(mixed);
     if (!mixed.value)
     {
-      switch (state)
+      switch (editorFont)
       {
       case "":
       case "serif":
       case "sans-serif":
         // Generic variable width.
-        state = "";
+        editorFont = "";
         break;
       case "tt":
       case "monospace":
         // Generic fixed width.
-        state = "tt";
+        editorFont = "tt";
         break;
       default:
-        state = state.toLowerCase().replace(/, /g, ","); // bug 1139524
+        editorFont = editorFont.toLowerCase().replace(/, /g, ","); // bug 1139524
       }
     }
 
+    var editorFontOptions = editorFont.split(',');
+    var matchedOption = editorFontOptions.length;  // initialise to high invalid value
     for (var i = 0; i < children.length; i++)
     {
       var menuItem = children[i];
       if (menuItem.localName == "menuitem")
       {
+        var matchFound = false;
         if (!mixed.value)
         {
-          var faceType = menuItem.getAttribute("value").toLowerCase().replace(/, /g, ",");
-          if (faceType == state)
+          var menuFont = menuItem.getAttribute("value").toLowerCase().replace(/, /g, ",");
+
+          // First compare the entire font string to match items that contain commas.
+          if (menuFont == editorFont)
           {
             menuItem.setAttribute("checked", "true");
             break;
           }
+
+          // Next compare the individual options.
+          else if (editorFontOptions.length > 1)
+          {
+            var matchPos = editorFontOptions.indexOf(menuFont);
+            if (matchPos >= 0 && matchPos < matchedOption) {
+              // This menu font comes earlier in the list of options,
+              // so prefer it.
+              menuItem.setAttribute("checked", "true");
+
+              // If we matched the first option, we don't need to look for
+              // a better match.
+              if (matchPos == 0)
+                break;
+
+              matchedOption = matchPos;
+              matchFound = true;
+            }
+          }
         }
 
-        // in case none match, make sure we've cleared the checkmark
-        menuItem.removeAttribute("checked");
+        // In case this item doesn't match, make sure we've cleared the checkmark.
+        if (!matchFound)
+          menuItem.removeAttribute("checked");
       }
     }
   }
 }
 
-const kFixedFontFaceMenuItems = 8; // number of fixed font face menuitems
+// Number of fixed font face menuitems, these are:
+// Variable Width
+// Fixed Width
+// ==separator
+// Helvetica, Arial
+// Times
+// Courier
+// ==separator
+// ==separator
+const kFixedFontFaceMenuItems = 8;
 
 function initLocalFontFaceMenu(menuPopup)
 {
   if (!gLocalFonts)
   {
     // Build list of all local fonts once per editor
     try 
     {