mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-07 18:39:57 +00:00
104 lines
3.0 KiB
JavaScript
Executable File
104 lines
3.0 KiB
JavaScript
Executable File
//--
|
|
//-- Filesystem utilities
|
|
//--
|
|
|
|
function convertUTF8ToUnicode(u)
|
|
{
|
|
return config.browser.isOpera || !window.netscape ? manualConvertUTF8ToUnicode(u) : mozConvertUTF8ToUnicode(u);
|
|
}
|
|
|
|
//# UTF-8 encoding rules:
|
|
//# 0x0000 - 0x007F: 0xxxxxxx
|
|
//# 0x0080 - 0x07FF: 110xxxxx 10xxxxxx
|
|
//# 0x0800 - 0xFFFF: 1110xxxx 10xxxxxx 10xxxxxx
|
|
|
|
function manualConvertUTF8ToUnicode(utf)
|
|
{
|
|
var uni = utf;
|
|
var src = 0;
|
|
var dst = 0;
|
|
var b1, b2, b3;
|
|
var c;
|
|
while(src < utf.length) {
|
|
b1 = utf.charCodeAt(src++);
|
|
if(b1 < 0x80) {
|
|
dst++;
|
|
} else if(b1 < 0xE0) {
|
|
b2 = utf.charCodeAt(src++);
|
|
c = String.fromCharCode(((b1 & 0x1F) << 6) | (b2 & 0x3F));
|
|
uni = uni.substring(0,dst++).concat(c,utf.substr(src));
|
|
} else {
|
|
b2 = utf.charCodeAt(src++);
|
|
b3 = utf.charCodeAt(src++);
|
|
c = String.fromCharCode(((b1 & 0xF) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F));
|
|
uni = uni.substring(0,dst++).concat(c,utf.substr(src));
|
|
}
|
|
}
|
|
return uni;
|
|
}
|
|
|
|
function mozConvertUTF8ToUnicode(u)
|
|
{
|
|
try {
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
|
|
converter.charset = "UTF-8";
|
|
} catch(ex) {
|
|
return manualConvertUTF8ToUnicode(u);
|
|
} // fallback
|
|
var s = converter.ConvertToUnicode(u);
|
|
var fin = converter.Finish();
|
|
return fin.length > 0 ? s+fin : s;
|
|
}
|
|
|
|
//# convert unicode string to a format suitable for saving to file
|
|
//# this should be UTF8, unless the browser does not support saving non-ASCII characters
|
|
function convertUnicodeToFileFormat(s)
|
|
{
|
|
return config.browser.isOpera || !window.netscape ? (config.browser.isIE ? convertUnicodeToHtmlEntities(s) : s) : mozConvertUnicodeToUTF8(s);
|
|
}
|
|
|
|
function convertUnicodeToHtmlEntities(s)
|
|
{
|
|
var re = /[^\u0000-\u007F]/g;
|
|
return s.replace(re,function($0) {return "&#" + $0.charCodeAt(0).toString() + ";";});
|
|
}
|
|
|
|
function convertUnicodeToUTF8(s)
|
|
{
|
|
// return convertUnicodeToFileFormat to allow plugin migration
|
|
return convertUnicodeToFileFormat(s);
|
|
}
|
|
|
|
function manualConvertUnicodeToUTF8(s)
|
|
{
|
|
return unescape(encodeURIComponent(s));
|
|
}
|
|
|
|
function mozConvertUnicodeToUTF8(s)
|
|
{
|
|
try {
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
|
|
converter.charset = "UTF-8";
|
|
} catch(ex) {
|
|
return manualConvertUnicodeToUTF8(s);
|
|
} // fallback
|
|
var u = converter.ConvertFromUnicode(s);
|
|
var fin = converter.Finish();
|
|
return fin.length > 0 ? u + fin : u;
|
|
}
|
|
|
|
function convertUriToUTF8(uri,charSet)
|
|
{
|
|
if(window.netscape == undefined || charSet == undefined || charSet == "")
|
|
return uri;
|
|
try {
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
var converter = Components.classes["@mozilla.org/intl/utf8converterservice;1"].getService(Components.interfaces.nsIUTF8ConverterService);
|
|
} catch(ex) {
|
|
return uri;
|
|
}
|
|
return converter.convertURISpecToUTF8(uri,charSet);
|
|
}
|