Skip to content

Commit bae4eb9

Browse files
committed
Merge pull request #102 from caisui/fix/template
Fix/template
2 parents fcf7389 + c4501b3 commit bae4eb9

File tree

13 files changed

+80
-457
lines changed

13 files changed

+80
-457
lines changed

common/content/commandline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ const CommandLine = Module("commandline", {
609609
action = this._echoMultiline;
610610
}
611611

612-
if ((flags & this.FORCE_MULTILINE) || (/\n/.test(str) || typeof str == "xml" || str instanceof TemplateSupportsXML ) && !(flags & this.FORCE_SINGLELINE))
612+
if ((flags & this.FORCE_MULTILINE) || (/\n/.test(str) || str instanceof TemplateSupportsXML ) && !(flags & this.FORCE_SINGLELINE))
613613
action = this._echoMultiline;
614614

615615
if (single)

common/content/javascript.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,12 @@ const JavaScript = Module("javascript", {
637637
options.add(["inspectcontentobjects"],
638638
"Allow completion of JavaScript objects coming from web content. POSSIBLY INSECURE!",
639639
"boolean", false);
640-
options.add(["expandtemplate"],
641-
"Expand TemplateLiteral",
642-
"boolean", !("XMLList" in window));
640+
641+
// TODO: delete me when minVersion is greater than 34
642+
if (!liberator.has("template")) {
643+
options.add(["expandtemplate"],
644+
"Expand TemplateLiteral",
645+
"boolean", !("XMLList" in window));
646+
}
643647
}
644648
})

common/content/liberator-overlay.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
(function () {
77
const modules = {};
8-
const BASE = "liberator://template/chrome://liberator/content/"
8+
// TODO: FIXME when minVersion is greater than 34
9+
var {isSupport: TemplateIsSupport} = Components.utils.import("resource://liberator/CheckTemplate.jsm", {});
10+
const BASE = TemplateIsSupport ?
11+
"chrome://liberator/content/" : "liberator://template/chrome://liberator/content/";
912

1013
modules.modules = modules;
1114

@@ -30,8 +33,8 @@
3033

3134
let prefix = [BASE];
3235

33-
//Cu.import("resource://liberator/template-tag.js", modules);
34-
loader.loadSubScript("resource://liberator/template-tag.js", modules);
36+
// TODO: FIXME when minVersion is greater than 34
37+
Components.utils.import("resource://liberator/template-tag" + (TemplateIsSupport ? ".js" : "-old.js"), modules);
3538

3639
// TODO: This list is much too long, we should try to minimize
3740
// the number of required components for easier porting to new applications

common/content/liberator.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ const Liberator = Module("liberator", {
332332
* should be loaded.
333333
*/
334334
loadScript: function (uri, context) {
335+
// TODO: delete me when minVersion is greater than 34
335336
if (options.expandtemplate) {
336337
var prefix = "liberator://template/";
337338
if (uri.lastIndexOf(prefix, 0) === -1)
@@ -345,6 +346,7 @@ const Liberator = Module("liberator", {
345346
if (!context)
346347
context = userContext;
347348

349+
// TODO: delete me when minVersion is greater than 34
348350
if (options.expandtemplate) {
349351
var obj = new Object;
350352
Cu.import("resource://liberator/template.js", obj);

common/content/statusline.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
/**
99
* @param {String} name
1010
* @param {String} description
11-
* @param {String|TemplateXML} node
11+
* @param {String|TemplateSupportsXML} node
1212
* String : the id attribute value of the existing node
13-
* TemplateXML: a TemplateXML instance. e.g) xml`<xul:label ...>`
13+
* TemplateSupportsXML: a TemplateSupportsXML instance. e.g) xml`<xul:label ...>`
1414
* @param {Function} updater
1515
* @param {Object} extraInfo
1616
*/
@@ -26,13 +26,13 @@ const StatusField = Class("StatusField", {
2626
if (!this.node)
2727
throw new Error('the element is not found: "' + node + '"');
2828
}
29-
else if (node instanceof TemplateXML) {
29+
else if (node instanceof TemplateSupportsXML) {
3030
this.node = util.xmlToDom(node, document);
3131
this.node.setAttribute("id", "liberator-status-" + name);
3232
statusline._statuslineWidget.appendChild(this.node);
3333
}
3434
else
35-
throw new TypeError("the argument node must be String or TemplateXML: " + node);
35+
throw new TypeError("the argument node must be String or TemplateSupportsXML: " + node);
3636

3737
this.node.hidden = true;
3838
if (extraInfo)

common/content/template.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const Template = Module("template", {
3333
},
3434

3535
maybeXML: function maybeXML(val) {
36-
if (typeof val == "xml" || val instanceof TemplateSupportsXML)
36+
if (val instanceof TemplateSupportsXML)
3737
return val;
3838

3939
try {
@@ -120,8 +120,6 @@ const Template = Module("template", {
120120
if (processStrings && false)
121121
str = template.highlightFilter(str, "\n", function () xml`<span highlight="NonText">^J</span>`);
122122
return xml`<span highlight="Object">${str}</span>`;
123-
case "xml":
124-
return arg;
125123
default:
126124
return `<unknown type>`;
127125
}
@@ -181,7 +179,7 @@ const Template = Module("template", {
181179
},
182180

183181
highlightSubstrings: function highlightSubstrings(str, iter, highlight) {
184-
if (typeof str == "xml" || str instanceof TemplateSupportsXML)
182+
if (str instanceof TemplateSupportsXML)
185183
return str;
186184
if (str == "")
187185
return xml`${str}`;

common/content/util.js

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@
55

66
/** @scope modules */
77

8-
//xxx:
9-
this.$$Namespace = function (prefix, uri) {
10-
return "XMLList" in window ? Namespace(prefix, uri)
11-
: {
12-
prefix: prefix,
13-
uri: uri,
14-
toString: function () {
15-
return this.uri;
16-
},
8+
const {XHTML, XUL, NS} = (function () {
9+
function Namespace(prefix, uri) {
10+
this.prefix = prefix;
11+
this.uri = uri;
12+
}
13+
Namespace.prototype.toString = function toString() { return this.uri; };
14+
return {
15+
XHTML: new Namespace("html", "http://www.w3.org/1999/xhtml"),
16+
XUL : new Namespace("xul", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"),
17+
NS : new Namespace("liberator", "http://vimperator.org/namespaces/liberator"),
1718
};
18-
}
19-
const XHTML = $$Namespace("html", "http://www.w3.org/1999/xhtml");
20-
const XUL = $$Namespace("xul", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
21-
const NS = $$Namespace("liberator", "http://vimperator.org/namespaces/liberator");
22-
if ("XMLList" in window) eval("default xml namespace = XHTML");
23-
delete this.$$Namespace;
19+
})();
2420

2521
const Util = Module("util", {
2622
init: function () {
@@ -737,9 +733,6 @@ const Util = Module("util", {
737733
* @returns {Node|DocumentFragment}
738734
*/
739735
xmlToDom: function xmlToDom(node, doc, nodes) {
740-
if (typeof node === "xml")
741-
return this.xmlToDomForE4X(node, doc, nodes);
742-
743736
var dom = this.xmlToDomForTemplate(node, doc, nodes);
744737

745738
//xxx: change highlight's namespace
@@ -753,39 +746,6 @@ const Util = Module("util", {
753746
}
754747
return dom;
755748
},
756-
/**
757-
* Converts an E4X XML literal to a DOM node.
758-
*
759-
* @param {Node} node
760-
* @param {Document} doc
761-
* @param {Object} nodes
762-
* @returns {Node}
763-
* @deprecated
764-
* @see util.xmlToDom
765-
*/
766-
xmlToDomForE4X: function xmlToDomForE4X(node, doc, nodes) {
767-
if (node.length() != 1) {
768-
let domnode = doc.createDocumentFragment();
769-
for each (let child in node)
770-
domnode.appendChild(arguments.callee(child, doc, nodes));
771-
return domnode;
772-
}
773-
switch (node.nodeKind()) {
774-
case "text":
775-
return doc.createTextNode(String(node));
776-
case "element":
777-
let domnode = doc.createElementNS(node.namespace(), node.localName());
778-
for each (let attr in node.attributes())
779-
domnode.setAttributeNS(attr.name() == "highlight" ? NS.uri : attr.namespace(), attr.name(), String(attr));
780-
for each (let child in node.children())
781-
domnode.appendChild(arguments.callee(child, doc, nodes));
782-
if (nodes && node.attribute("key"))
783-
nodes[node.attribute("key")] = domnode;
784-
return domnode;
785-
default:
786-
return null;
787-
}
788-
},
789749
/**
790750
* Converts an string of TemplateXML object to a DOM node.
791751
*

common/modules/CheckTemplate.jsm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// vim: ft=javascript
2+
// TODO: delete me when minVersion is greater than 34
3+
4+
var EXPORTED_SYMBOLS = ["isSupport"];
5+
6+
try {
7+
var isSupport = eval("((a,b)=>a.length)`${1}` === 2");
8+
} catch (ex) {
9+
Components.utils.reportError(ex);
10+
isSupport = false;
11+
}

common/modules/template-tag-old.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// TODO: delete me when minVersion is greater than 34
2+
3+
var EXPORTED_SYMBOLS = ["xml", "TemplateSupportsXML"];
4+
(function () {
5+
var {xml, TemplateSupportsXML} = Components.utils.import("resource://liberator/template-tag.js", {});
6+
this.xml = function xml_tagged_hack(portion, args) {
7+
try {
8+
return xml.apply(xml, [portion].concat(args));
9+
} catch (ex) {
10+
Components.utils.reportError(ex);
11+
Components.utils.reportError(ex.stack);
12+
throw ex;
13+
}
14+
};
15+
16+
for (var a in xml)
17+
this.xml[a] = xml[a];
18+
this.TemplateSupportsXML = TemplateSupportsXML;
19+
}).call(this);

0 commit comments

Comments
 (0)