Skip to content

Commit

Permalink
fix: remove unused insert nodes
Browse files Browse the repository at this point in the history
Fixes #34
Fixes #50
Closes #108
  • Loading branch information
Bertrand Laporte authored and PK1A committed Mar 26, 2014
1 parent 601e25f commit d71dd37
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 350 deletions.
14 changes: 0 additions & 14 deletions hsp/compiler/jsgenerator/processors.js
Expand Up @@ -205,20 +205,6 @@ exports["foreach"] = function (node, walker) {
return ['n.$foreach(', expr.code, ',"', node.key, '","', node.item, '",', forType, ',', expr.exprIdx, ',', content, ')'].join('');
};

/*
* Manages insertion expressions.
* @param {Node} node the current Node object as built by the treebuilder.
* @param {TreeWalker} walker the template walker instance.
* @return {String} a snippet of Javascript code built from the node.
*/
exports["insert"] = function (node, walker) {
node.category = "functionref";
var expr = formatExpression(node, 1, walker);
var exprcode = expr.code.length === 0 ? "0" : "{" + expr.code + "}";

return ['n.$insert(', exprcode, ',', expr.exprIdx, ')'].join('');
};

/*
* Manages element and component nodes.
* @param {Node} node the current Node object as built by the treebuilder.
Expand Down
34 changes: 2 additions & 32 deletions hsp/compiler/treebuilder/syntaxTree.js
Expand Up @@ -293,7 +293,7 @@ var SyntaxTree = klass({
* @return {Integer} the index of the block where the function stopped or -1 if all blocks have been handled.
*/
__text : function (index, blocks, out) {
var length = blocks.length, buffer = [], insertIndex = -1;
var length = blocks.length, buffer = [];

//Regroups adjacent text and expression blocks by looking at the next ones
var nextIndex = index, goAhead = (length > nextIndex), block;
Expand All @@ -318,13 +318,8 @@ var SyntaxTree = klass({

if (block.category === "invalidexpression") {
this._logError("Invalid expression", block);
} else if (block.category !== "functionref") {
buffer.push(block);
} else {
// this is an insert statement
insertIndex = nextIndex;
nextIndex++; // will be handled below
goAhead = false;
buffer.push(block);
}
} else if (block.type === "comment") {
// ignore comments
Expand Down Expand Up @@ -370,11 +365,6 @@ var SyntaxTree = klass({
out.push(node);
}

if (insertIndex > -1) {
// an insert block has to be added after the text block
this.__insert(insertIndex, blocks, out);
}

// return the last index that was handled
return nextIndex > index ? nextIndex - 1 : index;
},
Expand Down Expand Up @@ -408,26 +398,6 @@ var SyntaxTree = klass({
return index;
},

/**
* Manages an insert block.
* @param {Array} blocks the full list of blocks.
* @param {Integer} index the index of the block to manage.
* @param {Array} out the output as an array of Node.
* @return {Integer} the index of the block where the function stopped or -1 if all blocks have been handled.
*/
__insert : function (index, blocks, out) {
var node = new Node("insert"), block = blocks[index];
node.path = block.path;
node.args = block.args;

if (node.path.length > 1) {
this._logError("Long paths for insert statements are not supported yet: " + node.path.join("."), block);
} else {
out.push(node);
}
return index;
},

/**
* Manages an if block.
* @param {Array} blocks the full list of blocks.
Expand Down
6 changes: 2 additions & 4 deletions hsp/rt.js
Expand Up @@ -20,7 +20,6 @@ var klass = require("./klass"),
log = require("./rt/log"),
$root = require("./rt/$root"),
$RootNode = $root.$RootNode,
$InsertNode = $root.$InsertNode,
$CptNode = $root.$CptNode,
$CptAttElement = $root.$CptAttElement,
cptwrapper = require("./rt/cptwrapper");
Expand Down Expand Up @@ -56,8 +55,8 @@ var NodeGenerator = klass({
vs["scope"] = vs; // self reference (used for variables - cf. expression handler)

var root = null;
if (tplctxt.$constructor && (tplctxt.$constructor === $InsertNode || tplctxt.$constructor === $CptNode)) {
// we use the insert node as root node
if (tplctxt.$constructor && tplctxt.$constructor === $CptNode) {
// we use the component node as root node
root = tplctxt;
root.init(vs, this.nodedefs, argNames, ctlWrapper, ctlInitArgs);
} else {
Expand Down Expand Up @@ -225,7 +224,6 @@ var nodes = {};
var nodeList = [
"$text", require("./rt/$text"),
"$if", require("./rt/$if"),
"$insert", $InsertNode,
"$foreach", require("./rt/$foreach"),
"elt", require("./rt/eltnode"),
"cpt", $CptNode,
Expand Down
123 changes: 3 additions & 120 deletions hsp/rt/$root.js
Expand Up @@ -33,8 +33,9 @@ var CPT_TYPES={
var DOCUMENT_FRAGMENT_NODE = 11;

/**
* Root node - created at the root of each template Contains the listeners requested by the child nodes Is replaced by
* the $InsertNode (child class) when the template is inserted in another template
* Root node - created at the root of each template
* Contains the listeners requested by the child nodes
* Is replaced by the $CptNode (child class) when the template is inserted in another template
*/
var $RootNode = klass({
$extends : TNode,
Expand Down Expand Up @@ -320,123 +321,6 @@ var getObject = exports.getObject = function (path, scope) {
return o;
};

/**
* Insert node Allows to insert the content generated by another template
*/
var $InsertNode = klass({
$extends : $RootNode,

/**
* $InsertNode generator
* @param {Map<Expression>|int} exps the map of the expressions used by the node. 0 is passed if no expression is
* used
* @param {int} the index of the expression referencing the template
*/
$constructor : function (exps, expIdx) {
this.isInsertNode = true;
this.isDOMless = true;
this.exps = exps; // used by the $RootNode constructor
$RootNode.$constructor.call(this);
this.expIdx = expIdx;
},

$dispose : function () {
$RootNode.$dispose.call(this);
delete this.exps;
},

/**
* Create a node instance referencing the current node as base class As the $InsertNode is DOMless it will not
* create a DOM node for itself - but will create nodes for its children instead (through the $RootNode of the
* template process function)
* @return {TNode} the new node instance
*/
createNodeInstance : function (parent) {
var ni = TNode.createNodeInstance.call(this, parent);

// get the expression associated to the insert node
var v = ni.eh.getExpr(ni.expIdx);
if (v && v.getFuncRef) {
// get the function reference used by the insert expression
v = v.getFuncRef(ni.vscope, null);

ni.func = v.fn;
ni.tplArgs = v.args;

ni.isTemplate = (ni.func.isTemplate === true);
if (ni.isTemplate) {
// sub-template insertion
ni.func.apply(ni, ni.getArguments());
} else {
// JS function call
// so we have to manage a text node instead of a sub-template
ni.isDOMless = false;
ni.node = doc.createTextNode(ni.getContent());
ni.parent.node.appendChild(ni.node);
}

} else {
log.error("[$InsertNode] Invalid template function");
}

return ni;
},

/**
* Calculates and return the insert arguments in the current vscope
*/
getArguments : function () {
var tplArgs = this.tplArgs, args = [], eh = this.eh;
for (var i = 0, sz = tplArgs.length; sz > i; i += 2) {
if (tplArgs[i]) {
// arg is an expression
args.push(eh.getValue(tplArgs[i + 1], this.vscope, null));
} else {
// arg is a literal value
args.push(tplArgs[i + 1]);
}
}
return args;
},

/**
* Calculates the content of the insert text node when it is not a sub-template
*/
getContent : function () {
if (!this.isTemplate) {
return this.func.apply({}, this.getArguments());
}
return "";
},

/**
* Refresh the sub-template arguments and the child nodes, if needed
*/
refresh : function () {
if (this.adirty) {
// the variables of the insert statement have changed
// (i.e. the arguments of the sub-templates)
// so we have to update the vscope of the sub-template (which is different from the vscope of the parent's
// node)

if (this.isTemplate) {
var pscope = this.parent.vscope; // parent scope used to determine the new arguments values
var tplArgs = this.tplArgs, eh = this.eh;
for (var i = 0, idx = 0, sz = tplArgs.length; sz > i; i += 2, idx++) {
if (tplArgs[i]) {
// arg is an expression
this.updateArgument(idx, eh.getValue(tplArgs[i + 1], pscope, null));
}
}
} else {
// is JS function
this.node.nodeValue = this.getContent();
}
}
TNode.refresh.call(this);
}
});

/**
* Component node Allows to insert the content generated by a component template
*/
Expand Down Expand Up @@ -854,7 +738,6 @@ cptComponent.setDependency("$CptNode",$CptNode);
cptComponent.setDependency("TNode",TNode);
cptComponent.setDependency("$CptAttElement",$CptAttElement);
exports.$RootNode = $RootNode;
exports.$InsertNode = $InsertNode;
exports.$CptNode = $CptNode;
exports.$CptAttElement = $CptAttElement;

2 changes: 1 addition & 1 deletion public/playground/layout.hsp
Expand Up @@ -9,7 +9,7 @@
</div>
<div class="playground" onmouseover="{ctl.hideSampleList()}">
<div class="descriptionContainer">
{sampleList(data,ctl)}
<#sampleList data="{data}" ctl="{ctl}"/>

// TODO remove this fixed id
<div id="description">
Expand Down
Expand Up @@ -43,23 +43,43 @@
"name": "test",
"args": ["person"],
"content": [
{type: "text", "value": "Before "},
{type: "insert", path:["_content1"], args:[]},
{type: "text", "value": " "},
{type: "insert", path:["_content2"], args:[
{type:"expression", category:"string", value:"First Name"},
{type:"expression", category:"objectref", path:["person","firstName"]}
]},
{type: "text", "value": " After"}
{
"type": "textblock",
"content": [
{type: "text", "value": "Before "},
{
"type": "expression",
"category": "functionref",
"bound": true,
"path": ["_content1"],
"args": [],
"line": 3,
"column": 2
},
{type: "text", "value": " "},
{
"type": "expression",
"category": "functionref",
"bound": true,
"path": ["_content2"],
"args": [
{"type": "expression","category": "string","value": "First Name","code": "First Name"},
{"type": "expression","category": "objectref", "path": ["person","firstName"]}
]
},
{"type": "text","value": " After"}
]
}
]
}
]


##### Template Code
test=[
n.$text(0,["Before "]),
n.$insert({e1:[4,1,_content1]},1),
n.$text(0,[" "]),
n.$insert({e1:[4,1,_content2,0,"First Name",1,2],e2:[1,2,"person","firstName"]},1),
n.$text(0,[" After"])
n.$text({
e1:[4,1,_content1],
e2:[4,1,_content2,0,"First Name",1,3],
e3:[1,2,"person","firstName"]
},["Before ",1," ",2," After"])
]
Expand Up @@ -13,5 +13,5 @@

##### Template Code
test=[
n.$insert({e1:[4,1,_content1]},1)
n.$text({e1:[4,1,_content1]},["",1])
]
2 changes: 1 addition & 1 deletion public/test/compiler/tests.js
Expand Up @@ -89,7 +89,7 @@ describe('Block Parser: ', function () {
};

var samples = ut.getSampleNames(__dirname + "/samples");
//samples=["log2"];
//samples=["insert2"];

for (var i = 0, sz = samples.length; sz > i; i++) {
// create one test for each sample
Expand Down

0 comments on commit d71dd37

Please sign in to comment.