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
|
/* 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/. */
this.EXPORTED_SYMBOLS = [ "CharsetMenu" ];
const { classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyGetter(this, "gBundle", function() {
const kUrl = "chrome://browser/locale/charsetMenu.properties";
return Services.strings.createBundle(kUrl);
});
/**
* This set contains encodings that are in the Encoding Standard, except:
* - XSS-dangerous encodings (except ISO-2022-JP which is assumed to be
* too common not to be included).
* - x-user-defined, which practically never makes sense as an end-user-chosen
* override.
* - Encodings that IE11 doesn't have in its correspoding menu.
*/
const kEncodings = new Set([
// Globally relevant
"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",
"gb18030",
// 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-I",
"ISO-8859-8",
// Japanese
"Shift_JIS",
"EUC-JP",
"ISO-2022-JP",
// 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 = [
"UTF-8",
"windows-1252"
];
this.CharsetMenu = Object.freeze({
build: function BuildCharsetMenu(event) {
let parent = event.target;
if (parent.lastChild.localName != "menuseparator") {
// Detector menu or charset menu already built
return;
}
let doc = parent.ownerDocument;
function createItem(encoding) {
let menuItem = doc.createElement("menuitem");
menuItem.setAttribute("type", "radio");
menuItem.setAttribute("name", "charsetGroup");
try {
menuItem.setAttribute("label", gBundle.GetStringFromName(encoding));
} catch (e) {
// Localization error but put *something* in the menu to recover.
menuItem.setAttribute("label", encoding);
}
try {
menuItem.setAttribute("accesskey",
gBundle.GetStringFromName(encoding + ".key"));
} catch (e) {
// Some items intentionally don't have an accesskey
}
menuItem.setAttribute("id", "charset." + encoding);
return menuItem;
}
// Clone the set in order to be able to remove the pinned encodings from
// the cloned set.
let encodings = new Set(kEncodings);
for (let encoding of kPinned) {
encodings.delete(encoding);
parent.appendChild(createItem(encoding));
}
parent.appendChild(doc.createElement("menuseparator"));
let list = [];
for (let encoding of encodings) {
list.push(createItem(encoding));
}
list.sort(function (a, b) {
let titleA = a.getAttribute("label");
let titleB = b.getAttribute("label");
// Normal sorting sorts the part in parenthesis in an order that
// happens to make the less frequently-used items first.
let index;
if ((index = titleA.indexOf("(")) > -1) {
titleA = titleA.substring(0, index);
}
if ((index = titleB.indexOf("(")) > -1) {
titleA = titleB.substring(0, index);
}
let comp = titleA.localeCompare(titleB);
if (comp) {
return comp;
}
// secondarily reverse sort by encoding name to sort "windows" or
// "shift_jis" first. This works regardless of localization, because
// the ids aren't localized.
let idA = a.getAttribute("id");
let idB = b.getAttribute("id");
if (idA < idB) {
return 1;
}
if (idB < idA) {
return -1;
}
return 0;
});
for (let item of list) {
parent.appendChild(item);
}
},
});
|