mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2026-06-04 03:32:17 +00:00
Major refactoring of rendering mechanism
We now use a fake DOM implementation on the server to let us share more rendering code between the text output vs. DOM output paths.
This commit is contained in:
@@ -126,53 +126,11 @@ ElementRenderer.prototype.getAttribute = function(name,defaultValue) {
|
||||
}
|
||||
};
|
||||
|
||||
ElementRenderer.prototype.render = function(type) {
|
||||
var isHtml = type === "text/html",
|
||||
output = [],attr,a,v;
|
||||
if(isHtml) {
|
||||
output.push("<",this.widget.tag);
|
||||
if(this.widget.attributes) {
|
||||
attr = [];
|
||||
for(a in this.widget.attributes) {
|
||||
attr.push(a);
|
||||
}
|
||||
attr.sort();
|
||||
for(a=0; a<attr.length; a++) {
|
||||
v = this.widget.attributes[attr[a]];
|
||||
if(v !== undefined) {
|
||||
if($tw.utils.isArray(v)) {
|
||||
v = v.join(" ");
|
||||
} else if(typeof v === "object") {
|
||||
var s = [];
|
||||
for(var p in v) {
|
||||
s.push(p + ":" + v[p] + ";");
|
||||
}
|
||||
v = s.join("");
|
||||
}
|
||||
output.push(" ",attr[a],"='",$tw.utils.htmlEncode(v),"'");
|
||||
}
|
||||
}
|
||||
}
|
||||
output.push(">\n");
|
||||
}
|
||||
if($tw.config.htmlVoidElements.indexOf(this.widget.tag) === -1) {
|
||||
$tw.utils.each(this.widget.children,function(node) {
|
||||
if(node.render) {
|
||||
output.push(node.render(type));
|
||||
}
|
||||
});
|
||||
if(isHtml) {
|
||||
output.push("</",this.widget.tag,">");
|
||||
}
|
||||
}
|
||||
return output.join("");
|
||||
};
|
||||
|
||||
ElementRenderer.prototype.renderInDom = function() {
|
||||
// Check if our widget is providing an element
|
||||
if(this.widget.tag) {
|
||||
// Create the element
|
||||
this.domNode = document.createElementNS(this.namespace,this.widget.tag);
|
||||
this.domNode = this.renderTree.document.createElementNS(this.namespace,this.widget.tag);
|
||||
// Assign any specified event handlers
|
||||
$tw.utils.addEventListeners(this.domNode,this.widget.events);
|
||||
// Assign the attributes
|
||||
@@ -184,8 +142,8 @@ ElementRenderer.prototype.renderInDom = function() {
|
||||
self.domNode.appendChild(node.renderInDom());
|
||||
}
|
||||
});
|
||||
// Call postRenderInDom if the widget provides it
|
||||
if(this.widget.postRenderInDom) {
|
||||
// Call postRenderInDom if the widget provides it and we're in the browser
|
||||
if($tw.browser && this.widget.postRenderInDom) {
|
||||
this.widget.postRenderInDom();
|
||||
}
|
||||
// Return the dom node
|
||||
|
||||
@@ -22,12 +22,8 @@ var EntityRenderer = function(renderTree,parentRenderer,parseTreeNode) {
|
||||
this.parseTreeNode = parseTreeNode;
|
||||
};
|
||||
|
||||
EntityRenderer.prototype.render = function(type) {
|
||||
return type === "text/html" ? this.parseTreeNode.entity : $tw.utils.entityDecode(this.parseTreeNode.entity);
|
||||
};
|
||||
|
||||
EntityRenderer.prototype.renderInDom = function() {
|
||||
return document.createTextNode($tw.utils.entityDecode(this.parseTreeNode.entity));
|
||||
return this.renderTree.document.createTextNode($tw.utils.entityDecode(this.parseTreeNode.entity));
|
||||
};
|
||||
|
||||
exports.entity = EntityRenderer
|
||||
|
||||
@@ -69,19 +69,9 @@ MacroCallRenderer.prototype.substituteParameters = function(text,macroCallParseT
|
||||
return text;
|
||||
};
|
||||
|
||||
MacroCallRenderer.prototype.render = function(type) {
|
||||
var output = [];
|
||||
$tw.utils.each(this.children,function(node) {
|
||||
if(node.render) {
|
||||
output.push(node.render(type));
|
||||
}
|
||||
});
|
||||
return output.join("");
|
||||
};
|
||||
|
||||
MacroCallRenderer.prototype.renderInDom = function() {
|
||||
// Create the element
|
||||
this.domNode = document.createElement(this.parseTreeNode.isBlock ? "div" : "span");
|
||||
this.domNode = this.renderTree.document.createElement(this.parseTreeNode.isBlock ? "div" : "span");
|
||||
this.domNode.setAttribute("data-macro-name",this.parseTreeNode.name);
|
||||
// Render any child nodes
|
||||
var self = this;
|
||||
|
||||
@@ -22,12 +22,8 @@ var RawRenderer = function(renderTree,parentRenderer,parseTreeNode) {
|
||||
this.parseTreeNode = parseTreeNode;
|
||||
};
|
||||
|
||||
RawRenderer.prototype.render = function(type) {
|
||||
return this.parseTreeNode.html;
|
||||
};
|
||||
|
||||
RawRenderer.prototype.renderInDom = function() {
|
||||
var domNode = document.createElement("div");
|
||||
var domNode = this.renderTree.document.createElement("div");
|
||||
domNode.innerHTML = this.parseTreeNode.html;
|
||||
return domNode;
|
||||
};
|
||||
|
||||
@@ -22,12 +22,8 @@ var TextRenderer = function(renderTree,parentRenderer,parseTreeNode) {
|
||||
this.parseTreeNode = parseTreeNode;
|
||||
};
|
||||
|
||||
TextRenderer.prototype.render = function(type) {
|
||||
return type === "text/html" ? $tw.utils.htmlEncode(this.parseTreeNode.text) : this.parseTreeNode.text;
|
||||
};
|
||||
|
||||
TextRenderer.prototype.renderInDom = function() {
|
||||
return document.createTextNode(this.parseTreeNode.text);
|
||||
return this.renderTree.document.createTextNode(this.parseTreeNode.text);
|
||||
};
|
||||
|
||||
exports.text = TextRenderer
|
||||
|
||||
@@ -20,6 +20,7 @@ Options include:
|
||||
wiki: mandatory reference to wiki associated with this render tree
|
||||
context: optional hashmap of context variables (see below)
|
||||
parentRenderer: optional reference to a parent renderer node for the context chain
|
||||
document: optional document object to use instead of global document
|
||||
Context variables include:
|
||||
tiddlerTitle: title of the tiddler providing the context
|
||||
templateTitle: title of the tiddler providing the current template
|
||||
@@ -30,6 +31,7 @@ var WikiRenderTree = function(parser,options) {
|
||||
this.wiki = options.wiki;
|
||||
this.context = options.context || {};
|
||||
this.parentRenderer = options.parentRenderer;
|
||||
this.document = options.document || (typeof(document) === "object" ? document : null);
|
||||
// Hashmap of the renderer classes
|
||||
if(!this.rendererClasses) {
|
||||
WikiRenderTree.prototype.rendererClasses = $tw.modules.applyMethods("wikirenderer");
|
||||
@@ -64,19 +66,6 @@ WikiRenderTree.prototype.createRenderer = function(parentRenderer,parseTreeNode)
|
||||
return new RenderNodeClass(this,parentRenderer,parseTreeNode);
|
||||
};
|
||||
|
||||
/*
|
||||
Render as a string
|
||||
*/
|
||||
WikiRenderTree.prototype.render = function(type) {
|
||||
var output = [];
|
||||
$tw.utils.each(this.rendererTree,function(node) {
|
||||
if(node.render) {
|
||||
output.push(node.render(type));
|
||||
}
|
||||
});
|
||||
return output.join("");
|
||||
};
|
||||
|
||||
/*
|
||||
Render to the DOM
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user