gecko-dev/toolkit/modules/CharsetMenu.jsm

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

224 строки
5.8 KiB
JavaScript
Исходник Обычный вид История

/* 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"];
Bug 1514594: Part 3 - Change ChromeUtils.import API. *** Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8 This changes the behavior of ChromeUtils.import() to return an exports object, rather than a module global, in all cases except when `null` is passed as a second argument, and changes the default behavior not to pollute the global scope with the module's exports. Thus, the following code written for the old model: ChromeUtils.import("resource://gre/modules/Services.jsm"); is approximately the same as the following, in the new model: var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); Since the two behaviors are mutually incompatible, this patch will land with a scripted rewrite to update all existing callers to use the new model rather than the old. *** Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs This was done using the followng script: https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm *** Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D16747 *** Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16748 *** Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16749 *** Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs *** Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16750 --HG-- extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895 extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 21:18:31 +03:00
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);