diff options
author | Kris Maglione <maglione.k@gmail.com> | 2011-01-09 15:36:10 -0500 |
---|---|---|
committer | Kris Maglione <maglione.k@gmail.com> | 2011-01-09 15:36:10 -0500 |
commit | 3485424f6e72161aef139d9f5723cbd0760b38e2 (patch) | |
tree | 1afd4bceb101b47d4ad39134203b09db384a305e /common/modules | |
parent | a2e6e655c7104e8fd6bd111a27f40543f3266e2c (diff) | |
download | pentadactyl-3485424f6e72161aef139d9f5723cbd0760b38e2.tar.gz |
Automagically grab form charset in ;S.
Diffstat (limited to 'common/modules')
-rw-r--r-- | common/modules/bookmarkcache.jsm | 35 | ||||
-rw-r--r-- | common/modules/javascript.jsm | 4 | ||||
-rw-r--r-- | common/modules/services.jsm | 1 | ||||
-rw-r--r-- | common/modules/util.jsm | 30 |
4 files changed, 51 insertions, 19 deletions
diff --git a/common/modules/bookmarkcache.jsm b/common/modules/bookmarkcache.jsm index faa37313..3e1273d6 100644 --- a/common/modules/bookmarkcache.jsm +++ b/common/modules/bookmarkcache.jsm @@ -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; diff --git a/common/modules/javascript.jsm b/common/modules/javascript.jsm index d48b4659..2dd6c8cf 100644 --- a/common/modules/javascript.jsm +++ b/common/modules/javascript.jsm @@ -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) diff --git a/common/modules/services.jsm b/common/modules/services.jsm index 7d4138f4..8cd60af9 100644 --- a/common/modules/services.jsm +++ b/common/modules/services.jsm @@ -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); diff --git a/common/modules/util.jsm b/common/modules/util.jsm index 149e6627..6fe6c639 100644 --- a/common/modules/util.jsm +++ b/common/modules/util.jsm @@ -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]; }, /** |