From 36205c6c0e9ec170cef949435dc0b750d7f674f0 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Fri, 10 Dec 2010 01:38:39 -0500 Subject: [PATCH] Make Bookmark objects live writeable. --- common/content/bookmarks.js | 18 +++++++++--------- common/content/completion.js | 5 ++--- common/modules/base.jsm | 6 +++--- common/modules/bookmarkcache.jsm | 24 +++++++++++++++++++++++- common/modules/highlight.jsm | 2 +- common/modules/styles.jsm | 2 +- common/modules/template.jsm | 26 ++++++++++++++++++-------- 7 files changed, 57 insertions(+), 26 deletions(-) diff --git a/common/content/bookmarks.js b/common/content/bookmarks.js index f7b9941a..4d93fa19 100644 --- a/common/content/bookmarks.js +++ b/common/content/bookmarks.js @@ -57,11 +57,10 @@ const Bookmarks = Module("bookmarks", { try { let uri = util.createURI(url); if (!force && this.isBookmarked(uri.spec)) - for (let bmark in bookmarkcache) + for (var bmark in bookmarkcache) if (bmark.url == uri.spec) { - var id = bmark.id; if (title) - services.bookmarks.setItemTitle(id, title); + bmark.title = title; break; } @@ -69,17 +68,18 @@ const Bookmarks = Module("bookmarks", { PlacesUtils.tagging.untagURI(uri, null); PlacesUtils.tagging.tagURI(uri, tags); } - if (id == undefined) - id = services.bookmarks.insertBookmark( + if (bmark == undefined) + bmark = bookmarkcache.bookmarks[ + services.bookmarks.insertBookmark( services.bookmarks[unfiled ? "unfiledBookmarksFolder" : "bookmarksMenuFolder"], - uri, -1, title || url); - if (!id) + uri, -1, title || url)]; + if (!bmark) return false; if (post !== undefined) - PlacesUtils.setPostDataForBookmark(id, post); + bmark.post = post; if (keyword) - services.bookmarks.setKeywordForBookmark(id, keyword); + bmark.keyword = keyword; } catch (e) { dactyl.log(e, 0); diff --git a/common/content/completion.js b/common/content/completion.js index bf125b96..bd5c536e 100644 --- a/common/content/completion.js +++ b/common/content/completion.js @@ -527,9 +527,8 @@ const CompletionContext = Class("CompletionContext", { // A simple binary search to find the longest substring // of the given string which also matches the current // item's text. - var m, len = substring.length; - var n = substring.length; - var i = 0; + let len = substring.length; + let i = 0, m, n = len; while (n) { m = Math.floor(n / 2); let keep = compare(fixCase(item.text), substring.substring(0, i + m)); diff --git a/common/modules/base.jsm b/common/modules/base.jsm index 0e33af38..2268bc8e 100644 --- a/common/modules/base.jsm +++ b/common/modules/base.jsm @@ -887,7 +887,7 @@ function Struct() { let args = Array.slice(arguments); const Struct = Class("Struct", StructBase, { length: args.length, - members: args + members: array.toObject(args.map(function (v, k) [v, k])) }); args.forEach(function (name, i) { Struct.prototype.__defineGetter__(name, function () this[i]); @@ -909,7 +909,7 @@ let StructBase = Class("StructBase", Array, { // Iterator over our named members __iterator__: function () { let self = this; - return ([k, self[k]] for (k in values(self.members))) + return ([k, self[k]] for (k in keys(self.members))) } }, { fromArray: function (ary) { @@ -928,7 +928,7 @@ let StructBase = Class("StructBase", Array, { * the default value. */ defaultValue: function (key, val) { - let i = this.prototype.members.indexOf(key); + let i = this.prototype.members[key]; this.prototype.__defineGetter__(i, function () (this[i] = val.call(this))); this.prototype.__defineSetter__(i, function (value) Class.replaceProperty(this, i, value)); diff --git a/common/modules/bookmarkcache.jsm b/common/modules/bookmarkcache.jsm index 3de7e3d0..e3d972b6 100644 --- a/common/modules/bookmarkcache.jsm +++ b/common/modules/bookmarkcache.jsm @@ -13,10 +13,25 @@ defineModule("bookmarkcache", { const Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "id"); const Keyword = Struct("keyword", "title", "icon", "url"); Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url)); +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; + services.bookmarks.changeBookmarkURI(this.id, val); + this.tags = tags; +}); +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("keyword", function (val) { services.bookmarks.setKeywordForBookmark(this.id, val); }); +Bookmark.setter("tags", function (val) { + services.tagging.untagURI(this.uri, null); + if (val) + services.tagging.tagURI(this.uri, val); +}); const name = "bookmark-cache"; @@ -52,6 +67,13 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, node.itemId); }, + annotate: function (id, key, val) { + if (val) + services.annotation.setItemAnnotation(id, key, val, 0, services.annotation.EXPIRE_NEVER); + else if (services.annotation.itemHasAnnotation(id, key)) + services.annotation.removeItemAnnotation(id, key); + }, + get: function (url) { let ids = services.bookmarks.getBookmarkIdsForURI(util.newURI(url), {}); for (let id in values(ids)) @@ -142,7 +164,7 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), if (property == "tags") value = services.tagging.getTagsForURI(util.newURI(bookmark.url), {}); if (property in bookmark) { - bookmark[property] = value; + bookmark[bookmark.members[property]] = value; storage.fireEvent(name, "change", { __proto__: bookmark, changed: property }); } } diff --git a/common/modules/highlight.jsm b/common/modules/highlight.jsm index 171f6ee4..d64e1d8a 100644 --- a/common/modules/highlight.jsm +++ b/common/modules/highlight.jsm @@ -15,7 +15,7 @@ const Highlight = Struct("class", "selector", "sites", "default", "value", "agent", "base", "baseClass", "style"); Highlight.liveProperty = function (name, prop) { - let i = this.prototype.members.indexOf(name); + let i = this.prototype.members[name]; this.prototype.__defineGetter__(name, function () this[i]); this.prototype.__defineSetter__(name, function (val) { this[i] = val; diff --git a/common/modules/styles.jsm b/common/modules/styles.jsm index 5c5fd242..fabe88f7 100644 --- a/common/modules/styles.jsm +++ b/common/modules/styles.jsm @@ -18,7 +18,7 @@ const namespace = "@namespace html " + XHTML.uri.quote() + ";\n" + const Sheet = Struct("name", "id", "sites", "css", "system", "agent"); Sheet.liveProperty = function (name) { - let i = this.prototype.members.indexOf(name); + let i = this.prototype.members[name]; this.prototype.__defineGetter__(name, function () this[i]); this.prototype.__defineSetter__(name, function (val) { this[i] = val; diff --git a/common/modules/template.jsm b/common/modules/template.jsm index 3a1790a2..581c551d 100644 --- a/common/modules/template.jsm +++ b/common/modules/template.jsm @@ -11,14 +11,17 @@ defineModule("template", { }); default xml namespace = XHTML; -XML.ignoreWhiteSpace = true; -XML.prettyPrinting = false; +function fixXML() { + XML.ignoreWhiteSpace = false; + XML.prettyPrinting = false; +} const Template = Module("Template", { add: function add(a, b) a + b, join: function join(c) function (a, b) a + c + b, map: function map(iter, func, sep, interruptable) { + XML.ignoreWhitespace = false; XML.prettyPrinting = false; if (iter.length) // FIXME: Kludge? iter = array.iterValues(iter); let ret = <>; @@ -78,6 +81,7 @@ const Template = Module("Template", { var desc = this.processor[1].call(this, item, item.description); } + XML.ignoreWhitespace = false; XML.prettyPrinting = false; // return