summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/CharsetMenu.jsm
blob: 2a3335dabd2e8f4f2cc2ab0637fb1b48f0489f8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

var EXPORTED_SYMBOLS = ["CharsetMenu"];

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyGetter(this, "gBundle", function() {
  const kUrl = "chrome://global/locale/charsetMenu.properties";
  return Services.strings.createBundle(kUrl);
});

ChromeUtils.defineModuleGetter(
  this,
  "Deprecated",
  "resource://gre/modules/Deprecated.jsm"
);

/**
 * This set contains encodings that are in the Encoding Standard, except:
 *  - Japanese encodings are represented by one autodetection item
 *  - x-user-defined, which practically never makes sense as an end-user-chosen
 *    override.
 *  - Encodings that IE11 doesn't have in its corresponding menu.
 */
const kEncodings = new Set([
  // Globally relevant
  "_autodetect_all", // (NOT AN ENCODING NAME; using IE-consistent magic name)
  "UTF-8",
  "windows-1252",
  // Arabic
  "windows-1256",
  "ISO-8859-6",
  // Baltic
  "windows-1257",
  "ISO-8859-4",
  // "ISO-8859-13", // Hidden since not in menu in IE11
  // Central European
  "windows-1250",
  "ISO-8859-2",
  // Chinese, Simplified
  "GBK",
  // Chinese, Traditional
  "Big5",
  // Cyrillic
  "windows-1251",
  "ISO-8859-5",
  "KOI8-R",
  "KOI8-U",
  "IBM866", // Not in menu in Chromium. Maybe drop this?
  // "x-mac-cyrillic", // Not in menu in IE11 or Chromium.
  // Greek
  "windows-1253",
  "ISO-8859-7",
  // Hebrew
  "windows-1255",
  "ISO-8859-8",
  // Japanese (NOT AN ENCODING NAME)
  "Japanese",
  // Korean
  "EUC-KR",
  // Thai
  "windows-874",
  // Turkish
  "windows-1254",
  // Vietnamese
  "windows-1258",
  // Hiding rare European encodings that aren't in the menu in IE11 and would
  // make the menu messy by sorting all over the place
  // "ISO-8859-3",
  // "ISO-8859-10",
  // "ISO-8859-14",
  // "ISO-8859-15",
  // "ISO-8859-16",
  // "macintosh"
]);

// Always at the start of the menu, in this order, followed by a separator.
const kPinned = ["_autodetect_all", "UTF-8", "windows-1252"];

kPinned.forEach(x => kEncodings.delete(x));

function CharsetComparator(a, b) {
  // Normal sorting sorts the part in parenthesis in an order that
  // happens to make the less frequently-used items first.
  let titleA = a.label.replace(/\(.*/, "") + b.value;
  let titleB = b.label.replace(/\(.*/, "") + a.value;
  // Secondarily reverse sort by encoding name to sort "windows"
  return titleA.localeCompare(titleB) || b.value.localeCompare(a.value);
}

var gCharsetInfoCache, gPinnedInfoCache;

var CharsetMenu = {
  build(parent, deprecatedShowAccessKeys = true) {
    if (!deprecatedShowAccessKeys) {
      Deprecated.warning(
        "CharsetMenu no longer supports building a menu with no access keys.",
        "https://bugzilla.mozilla.org/show_bug.cgi?id=1088710"
      );
    }
    function createDOMNode(doc, nodeInfo) {
      let node = doc.createXULElement("menuitem");
      node.setAttribute("type", "radio");
      node.setAttribute("name", nodeInfo.name + "Group");
      node.setAttribute(nodeInfo.name, nodeInfo.value);
      node.setAttribute("label", nodeInfo.label);
      if (nodeInfo.accesskey) {
        node.setAttribute("accesskey", nodeInfo.accesskey);
      }
      return node;
    }

    if (parent.hasChildNodes()) {
      // Charset menu already built
      return;
    }
    this._ensureDataReady();
    let doc = parent.ownerDocument;

    gPinnedInfoCache.forEach(charsetInfo =>
      parent.appendChild(createDOMNode(doc, charsetInfo))
    );
    parent.appendChild(doc.createXULElement("menuseparator"));
    gCharsetInfoCache.forEach(charsetInfo =>
      parent.appendChild(createDOMNode(doc, charsetInfo))
    );
  },

  getData() {
    this._ensureDataReady();
    return {
      pinnedCharsets: gPinnedInfoCache,
      otherCharsets: gCharsetInfoCache,
    };
  },

  _ensureDataReady() {
    if (!gCharsetInfoCache) {
      gPinnedInfoCache = this.getCharsetInfo(kPinned, false);
      gCharsetInfoCache = this.getCharsetInfo(kEncodings);
    }
  },

  getCharsetInfo(charsets, sort = true) {
    let list = Array.from(charsets, charset => ({
      label: this._getCharsetLabel(charset),
      accesskey: this._getCharsetAccessKey(charset),
      name: "charset",
      value: charset,
    }));

    if (sort) {
      list.sort(CharsetComparator);
    }
    return list;
  },

  _getCharsetLabel(charset) {
    if (charset == "GBK") {
      // Localization key has been revised
      charset = "gbk.bis";
    }
    try {
      return gBundle.GetStringFromName(charset);
    } catch (ex) {}
    return charset;
  },
  _getCharsetAccessKey(charset) {
    if (charset == "GBK") {
      // Localization key has been revised
      charset = "gbk.bis";
    }
    try {
      return gBundle.GetStringFromName(charset + ".key");
    } catch (ex) {}
    return "";
  },

  /**
   * For substantially similar encodings, treat two encodings as the same
   * for the purpose of the check mark.
   */
  foldCharset(charset, isAutodetected) {
    if (isAutodetected) {
      switch (charset) {
        case "Shift_JIS":
        case "EUC-JP":
        case "ISO-2022-JP":
          return "Japanese";
        default:
        // fall through
      }
    }
    switch (charset) {
      case "ISO-8859-8-I":
        return "windows-1255";

      case "gb18030":
        return "GBK";

      default:
        return charset;
    }
  },

  /**
   * This method is for comm-central callers only.
   */
  update(parent, charset) {
    let menuitem = parent
      .getElementsByAttribute("charset", this.foldCharset(charset, false))
      .item(0);
    if (menuitem) {
      menuitem.setAttribute("checked", "true");
    }
  },
};

Object.freeze(CharsetMenu);