Utils: += DirectoryService, UnescapeHTML, NS_*

This commit is contained in:
satyr 2010-04-17 09:27:15 +09:00
Родитель 48bd8ae00b
Коммит e53575e8e9
6 изменённых файлов: 56 добавлений и 63 удалений

Просмотреть файл

@ -91,9 +91,7 @@ var Editor = {
executable.followLinks = true;
executable.initWithPath(isOSX ? "/usr/bin/open" : editor);
if (executable.exists()) {
var file = (Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("TmpD", Ci.nsIFile));
var file = Utils.DirectoryService.get("TmpD", Ci.nsIFile);
file.append("ubiquity.tmp.js");
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);

Просмотреть файл

@ -57,15 +57,11 @@ const DEFAULT_MAX_SUGGESTIONS = 5;
const PREF_MAX_SUGGESTIONS = "extensions.ubiquity.maxSuggestions";
const MIN_MAX_SUGGS = 1;
const MAX_MAX_SUGGS = 42;
const NS_XHTML = "http://www.w3.org/1999/xhtml";
const NS_XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
const {NS_XHTML, NS_XUL} = Utils;
const DEFAULT_HELP = (
'<div class="default" xmlns="' + NS_XHTML + '">' +
L("ubiquity.cmdmanager.defaulthelp") +
'</div>');
const NULL_QUERY = {suggestionList: [], finished: true, hasResults: false};
var gDomNodes = {};

Просмотреть файл

@ -188,9 +188,7 @@ var UbiquitySetup = {
Utils.IOService.newFileURI(this.__getExtDir()).spec,
isInstalledAsXpi: function isInstalledAsXpi() {
let profileDir = (Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("ProfD", Ci.nsIFile));
let profileDir = Utils.DirectoryService.get("ProfD", Ci.nsIFile);
let extDir = this.__getExtDir();
return profileDir.contains(extDir, false);
},

Просмотреть файл

@ -53,7 +53,13 @@ const ToString = Object.prototype.toString;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
var Utils = {
toString: function toString() "[object UbiqUtils]",
toString: function toString() "[object UbiquityUtils]",
NS_MATHML: "http://www.w3.org/1998/Math/MathML",
NS_XHTML: "http://www.w3.org/1999/xhtml",
NS_SVG: "http://www.w3.org/2000/svg",
NS_EM: "http://www.mozilla.org/2004/em-rdf#",
NS_XUL: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
// === {{{ Utils.currentChromeWindow }}} ===
// A reference to the application chrome window that currently has focus.
@ -93,6 +99,11 @@ for each (let f in [
function ConsoleService()
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService),
// === {{{ Utils.DirectoryService }}} ===
// Shortcut to {{{nsIDirectoryService}}}.
function DirectoryService()
Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties),
// === {{{ Utils.ExtensionManager }}} ===
// Shortcut to {{{nsIExtensionManager}}}.
function ExtensionManager()
@ -103,17 +114,23 @@ for each (let f in [
function IOService()
Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService),
// === {{{ Utils.UnescapeHTML }}} ===
// Shortcut to {{{nsIScriptableUnescapeHTML}}}.
function UnescapeHTML()
Cc["@mozilla.org/feed-unescapehtml;1"]
.getService(Ci.nsIScriptableUnescapeHTML),
// === {{{ Utils.UnicodeConverter }}} ===
// Shortcut to {{{nsIScriptableUnicodeConverter}}}.
function UnicodeConverter() (
Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.getService(Ci.nsIScriptableUnicodeConverter)),
function UnicodeConverter()
Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.getService(Ci.nsIScriptableUnicodeConverter),
// === {{{ Utils.WindowMediator }}} ===
// Shortcut to {{{nsIWindowMediator}}}.
function WindowMediator() (
Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator)),
function WindowMediator()
Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator),
// ** {{{ Utils.json }}} **
// **//Deprecated.//** A cached {{{nsIJSON}}} instance.
@ -121,9 +138,9 @@ for each (let f in [
// === {{{ Utils.hiddenWindow }}} ===
// Returns the application's global hidden window.
function hiddenWindow() (
Cc["@mozilla.org/appshell/appShellService;1"]
.getService(Ci.nsIAppShellService).hiddenDOMWindow),
function hiddenWindow()
Cc["@mozilla.org/appshell/appShellService;1"]
.getService(Ci.nsIAppShellService).hiddenDOMWindow,
// === {{{ Utils.appName }}} ===
// The chrome application name found in {{{nsIXULAppInfo}}}.
@ -847,16 +864,7 @@ escapeHtml.fn = function escapeHtml_sub($) {
// Returns a version of the {{{string}}} with all occurrences of HTML character
// references (e.g. &spades; &#x2665; &#9827; etc.) in it decoded.
function unescapeHtml(s) String(s).replace(unescapeHtml.re, unescapeHtml.fn);
unescapeHtml.re = /(?:&#?\w+;)+/g;
unescapeHtml.fn = function unescapeHtml_parse(ref) {
var {div} = unescapeHtml_parse;
div.innerHTML = ref;
return div.textContent;
};
defineLazyProperty(unescapeHtml.fn, function div() (
Utils.hiddenWindow.document.createElementNS(
"http://www.w3.org/1999/xhtml", "div")));
function unescapeHtml(s) Utils.UnescapeHTML.unescape(s);
// === {{{ Utils.convertFromUnicode(toCharset, text) }}} ===
// Encodes the given unicode text to a given character set.

Просмотреть файл

@ -512,9 +512,7 @@ CmdUtils.CreateCommand({
this.blur();
}, false);
}
var file = (
Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties)
.get("ProfD", Ci.nsIFile));
var file = Utils.DirectoryService.get("ProfD", Ci.nsIFile);
file.append("extensions");
file.append(data.id);
if (file.exists() && file.isDirectory()) {
@ -548,7 +546,7 @@ CmdUtils.CreateCommand({
em.gExtensionsView.selectItem(em.document.getElementById(self._urn(id)));
},
_extraData: function ve__extraData(id) {
const PREFIX_NS_EM = "http://www.mozilla.org/2004/em-rdf#";
const {NS_EM} = Utils;
var rdfs =
Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
var {datasource} = Utils.ExtensionManager;
@ -559,7 +557,7 @@ CmdUtils.CreateCommand({
};
for (let key in data) {
let target = datasource.GetTarget(
itemResource, rdfs.GetResource(PREFIX_NS_EM + key), true);
itemResource, rdfs.GetResource(NS_EM + key), true);
if (target instanceof Ci.nsIRDFLiteral) data[key] = target.Value;
}
data.disabled = data.isDisabled === "true";

Просмотреть файл

@ -1,18 +1,17 @@
Components.utils.import("resource://ubiquity/modules/annotation_memory.js");
Components.utils.import("resource://ubiquity/modules/utils.js");
Components.utils.import("resource://ubiquity/tests/framework.js");
var EXPORTED_SYMBOLS = ["TestAnnotationMemory"];
EXPORTED_SYMBOLS = ["TestAnnotationMemory"];
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://ubiquity/modules/annotation_memory.js");
Cu.import("resource://ubiquity/modules/utils.js");
Cu.import("resource://ubiquity/tests/framework.js");
function TestAnnotationMemory(test) {
return new AnnotationService(getTempConnection(test));
}
function getTempDbFile() {
var Ci = Components.interfaces;
var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties);
var file = dirSvc.get("TmpD", Ci.nsIFile);
var file = Utils.DirectoryService.get("TmpD", Ci.nsIFile);
file.append("testdb.sqlite");
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0x600);
return file;
@ -51,21 +50,21 @@ function testMemoryPersists() {
annSvc = new AnnotationService(connection);
}
var url = Utils.url("http://www.foo.com");
annSvc.setPageAnnotation(url, "perm", "foo");
var uri = Utils.uri("http://www.foo.com");
annSvc.setPageAnnotation(uri, "perm", "foo");
this.assertEquals(annSvc.getPagesWithAnnotation("perm").length, 1);
annSvc.setPageAnnotation(url, "temp", "foo", 0, annSvc.EXPIRE_SESSION);
annSvc.setPageAnnotation(uri, "temp", "foo", 0, annSvc.EXPIRE_SESSION);
this.assertEquals(annSvc.getPagesWithAnnotation("temp").length, 1);
reopenDb();
this.assertEquals(annSvc.getPagesWithAnnotation("perm").length, 1);
annSvc.removePageAnnotation(url, "perm");
annSvc.removePageAnnotation(uri, "perm");
this.assertEquals(annSvc.getPagesWithAnnotation("temp").length, 0);
annSvc.setPageAnnotation(url, "temp", "foo", 0, annSvc.EXPIRE_SESSION);
annSvc.removePageAnnotation(url, "temp");
annSvc.setPageAnnotation(uri, "temp", "foo", 0, annSvc.EXPIRE_SESSION);
annSvc.removePageAnnotation(uri, "temp");
this.assertEquals(annSvc.getPagesWithAnnotation("temp").length, 0);
reopenDb();
@ -78,10 +77,9 @@ function testMemoryPersists() {
function testGetPagesWithAnnotation() {
var annSvc = new AnnotationService(getTempConnection(this));
var url = Utils.url("http://www.foo.com");
var uri = Utils.uri("http://www.foo.com");
this.assertEquals(annSvc.getPagesWithAnnotation("blah").length, 0);
annSvc.setPageAnnotation(url, "blah", "foo");
annSvc.setPageAnnotation(uri, "blah", "foo");
var results = annSvc.getPagesWithAnnotation("blah");
this.assertEquals(results.length, 1);
this.assertEquals(results[0].spec, "http://www.foo.com/");
@ -89,25 +87,22 @@ function testGetPagesWithAnnotation() {
function testPageHasAnnotation() {
var annSvc = new AnnotationService(getTempConnection(this));
var url = Utils.url("http://www.foo.com");
annSvc.setPageAnnotation(url, "blah", "foo");
this.assertEquals(annSvc.pageHasAnnotation(url, "blah"), true);
var uri = Utils.uri("http://www.foo.com");
annSvc.setPageAnnotation(uri, "blah", "foo");
this.assertEquals(annSvc.pageHasAnnotation(uri, "blah"), true);
}
function testGetPageAnnotation() {
var annSvc = new AnnotationService(getTempConnection(this));
var url = Utils.url("http://www.foo.com");
annSvc.setPageAnnotation(url, "blah", "foo");
this.assertEquals(annSvc.getPageAnnotation(url, "blah"), "foo");
var uri = Utils.uri("http://www.foo.com");
annSvc.setPageAnnotation(uri, "blah", "foo");
this.assertEquals(annSvc.getPageAnnotation(uri, "blah"), "foo");
}
function testRemovePageAnnotation() {
var annSvc = new AnnotationService(getTempConnection(this));
var uri = Utils.uri("http://www.foo.com");
annSvc.setPageAnnotation(uri, "blah", "foo");
annSvc.removePageAnnotation(uri, "blah");
this.assertEquals(annSvc.getPagesWithAnnotation("blah").length, 0);