mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-02-13 00:25:47 +01:00
Automagically grab form charset in ;S.
This commit is contained in:
@@ -10,14 +10,25 @@ defineModule("bookmarkcache", {
|
||||
require: ["services", "storage", "util"]
|
||||
});
|
||||
|
||||
var Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "id");
|
||||
var Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "charset", "id");
|
||||
var Keyword = Struct("keyword", "title", "icon", "url");
|
||||
Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
|
||||
update(Bookmark.prototype, {
|
||||
get extra() [
|
||||
["keyword", this.keyword, "Keyword"],
|
||||
["tags", this.tags.join(", "), "Tag"]
|
||||
].filter(function (item) item[1]),
|
||||
|
||||
get uri() util.newURI(this.url),
|
||||
|
||||
encodeURIComponent: function _encodeURIComponent(str) {
|
||||
if (!this.charset || this.charset === "UTF-8")
|
||||
return encodeURIComponent(str);
|
||||
let conv = services.CharsetConv(this.charset);
|
||||
return escape(conv.ConvertFromUnicode(str) + conv.Finish());
|
||||
}
|
||||
})
|
||||
Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
|
||||
Bookmark.prototype.__defineGetter__("extra", function () [
|
||||
["keyword", this.keyword, "Keyword"],
|
||||
["tags", this.tags.join(", "), "Tag"]
|
||||
].filter(function (item) item[1]));
|
||||
Bookmark.setter("url", function (val) {
|
||||
let tags = this.tags;
|
||||
this.tags = null;
|
||||
@@ -26,6 +37,7 @@ Bookmark.setter("url", function (val) {
|
||||
});
|
||||
Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); });
|
||||
Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); });
|
||||
Bookmark.setter("charset", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.CHARSET, val); });
|
||||
Bookmark.setter("keyword", function (val) { services.bookmarks.setKeywordForBookmark(this.id, val); });
|
||||
Bookmark.setter("tags", function (val) {
|
||||
services.tagging.untagURI(this.uri, null);
|
||||
@@ -37,6 +49,7 @@ var name = "bookmark-cache";
|
||||
|
||||
var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
|
||||
POST: "bookmarkProperties/POSTData",
|
||||
CHARSET: "dactyl/charset",
|
||||
|
||||
init: function init() {
|
||||
services.bookmarks.addObserver(this, false);
|
||||
@@ -68,12 +81,14 @@ var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
|
||||
let keyword = services.bookmarks.getKeywordForBookmark(node.itemId);
|
||||
let tags = services.tagging.getTagsForURI(uri, {}) || [];
|
||||
let post = BookmarkCache.getAnnotation(node.itemId, this.POST);
|
||||
return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, node.itemId);
|
||||
let charset = BookmarkCache.getAnnotation(node.itemId, this.CHARSET);
|
||||
return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, charset, node.itemId);
|
||||
},
|
||||
|
||||
annotate: function (id, key, val) {
|
||||
annotate: function (id, key, val, timespan) {
|
||||
if (val)
|
||||
services.annotation.setItemAnnotation(id, key, val, 0, services.annotation.EXPIRE_NEVER);
|
||||
services.annotation.setItemAnnotation(id, key, val, 0,
|
||||
timespan || services.annotation.EXPIRE_NEVER);
|
||||
else if (services.annotation.itemHasAnnotation(id, key))
|
||||
services.annotation.removeItemAnnotation(id, key);
|
||||
},
|
||||
@@ -159,7 +174,9 @@ var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
|
||||
onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
|
||||
if (isAnnotation)
|
||||
if (property === this.POST)
|
||||
[property, value] = ["post", BookmarkCache.getAnnotation(itemId, this.POST)];
|
||||
[property, value] = ["post", BookmarkCache.getAnnotation(itemId, property)];
|
||||
else if (property === this.CHARSET)
|
||||
[property, value] = ["charset", BookmarkCache.getAnnotation(itemId, property)];
|
||||
else
|
||||
return;
|
||||
|
||||
|
||||
@@ -624,8 +624,8 @@ var JavaScript = Module("javascript", {
|
||||
"Uint16Array", "Uint32Array", "Uint8Array", "XML",
|
||||
"XMLHttpProgressEvent", "XMLList", "XMLSerializer", "XPCNativeWrapper",
|
||||
"XPCSafeJSWrapper", "XULControllers", "decodeURI", "decodeURIComponent",
|
||||
"encodeURI", "encodeURIComponent", "eval", "isFinite", "isNaN",
|
||||
"isXMLName", "parseFloat", "parseInt", "undefined", "uneval"
|
||||
"encodeURI", "encodeURIComponent", "escape", "eval", "isFinite", "isNaN",
|
||||
"isXMLName", "parseFloat", "parseInt", "undefined", "unescape", "uneval"
|
||||
].concat([k.substr(6) for (k in keys(Ci)) if (/^nsIDOM/.test(k))])
|
||||
.concat([k.substr(3) for (k in keys(Ci)) if (/^nsI/.test(k))])
|
||||
.concat(this.magicalNames)
|
||||
|
||||
@@ -64,6 +64,7 @@ var Services = Module("Services", {
|
||||
this.add("windowWatcher", "@mozilla.org/embedcomp/window-watcher;1", Ci.nsIWindowWatcher);
|
||||
this.add("zipReader", "@mozilla.org/libjar/zip-reader-cache;1", Ci.nsIZipReaderCache);
|
||||
|
||||
this.addClass("CharsetConv", "@mozilla.org/intl/scriptableunicodeconverter", Ci.nsIScriptableUnicodeConverter, "charset");
|
||||
this.addClass("File", "@mozilla.org/file/local;1", Ci.nsILocalFile);
|
||||
this.addClass("file:", "@mozilla.org/network/protocol;1?name=file", Ci.nsIFileProtocolHandler);
|
||||
this.addClass("Find", "@mozilla.org/embedcomp/rangefind;1", Ci.nsIFind);
|
||||
|
||||
@@ -1070,21 +1070,35 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
|
||||
// Nuances gleaned from browser.jar/content/browser/browser.js
|
||||
parseForm: function parseForm(field) {
|
||||
function encode(name, value, param) {
|
||||
if (param)
|
||||
value = value + "%s";
|
||||
if (post)
|
||||
return name + "=" + value;
|
||||
return encodeURIComponent(name) + "=" + (param ? value : encodeURIComponent(value));
|
||||
param = param ? "%s" : "";
|
||||
if (post) // Seems wrong.
|
||||
return encodeComponent(name + "=" + value + param);
|
||||
return encodeComponent(name) + "=" + encodeComponent(value) + param;
|
||||
}
|
||||
|
||||
let form = field.form;
|
||||
let doc = form.ownerDocument;
|
||||
let charset = doc.charset;
|
||||
|
||||
let charset = doc.characterSet;
|
||||
let converter = services.CharsetConv(charset);
|
||||
for each (let cs in form.acceptCharset.split(/\s*,\s*|\s+/)) {
|
||||
let c = services.CharsetConv(cs);
|
||||
if (c) {
|
||||
converter = services.CharsetConv(cs);
|
||||
charset = cs;
|
||||
}
|
||||
}
|
||||
|
||||
let uri = util.newURI(doc.baseURI.replace(/\?.*/, ""), charset);
|
||||
let url = util.newURI(form.action, charset, uri).spec;
|
||||
|
||||
let post = form.method.toUpperCase() == "POST";
|
||||
|
||||
let encodeComponent = encodeURIComponent;
|
||||
if (charset !== "UTF-8")
|
||||
encodeComponent = function encodeComponent(str)
|
||||
escape(converter.ConvertFromUnicode(str) + converter.Finish());
|
||||
|
||||
let elems = [];
|
||||
if (field instanceof Ci.nsIDOMHTMLInputElement && field.type == "submit")
|
||||
elems.push(encode(field.name, field.value));
|
||||
@@ -1101,8 +1115,8 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
|
||||
}
|
||||
}
|
||||
if (post)
|
||||
return [url, elems.map(encodeURIComponent).join('&'), elems];
|
||||
return [url + "?" + elems.join('&'), null];
|
||||
return [url, elems.join('&'), elems, charset];
|
||||
return [url + "?" + elems.join('&'), null, charset];
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user