Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

untabified source code for better reading

  • Loading branch information...
commit a6840b81f1ec9701b708b30195904b676ccc9b58 1 parent f23404a
Matthias Georgi authored

Showing 1 changed file with 76 additions and 76 deletions. Show diff stats Hide diff stats

  1. +76 76 template.js
152 template.js
@@ -31,7 +31,7 @@ function Template(id) {
31 31
32 32 if (this.element) {
33 33 this.element.parentNode.removeChild(this.element);
34   - this.compileNode(this.element);
  34 + this.compileNode(this.element);
35 35 }
36 36 else {
37 37 throw "template not found: " + id;
@@ -42,10 +42,10 @@ function Template(id) {
42 42 Template.Helper = {
43 43
44 44 linkTo: function(text, url) {
45   - if (url.indexOf('http://') == -1 && url[0] != '/' && url[0] != '#') {
46   - url = 'http://' + url;
47   - }
48   - return '<a href="' + url +'">' + text + '</a>';
  45 + if (url.indexOf('http://') == -1 && url[0] != '/' && url[0] != '#') {
  46 + url = 'http://' + url;
  47 + }
  48 + return '<a href="' + url +'">' + text + '</a>';
49 49 }
50 50
51 51 };
@@ -62,14 +62,14 @@ Template.prototype = {
62 62 if (data.constructor == Array) {
63 63 this.expandArray(data, node);
64 64 }
65   - else {
  65 + else {
66 66 this.expandObject(data, node);
67 67 }
68 68 },
69 69
70 70 expandArray: function(data, node) {
71 71 var parent = node.parentNode;
72   - var sibling = node.nextSibling;
  72 + var sibling = node.nextSibling;
73 73 parent.removeChild(node);
74 74 for (var i = 0; i < data.length; i++) {
75 75 var child = node.cloneNode(true);
@@ -79,29 +79,29 @@ Template.prototype = {
79 79 },
80 80
81 81 expandByContext: function(object, node) {
82   - var names = node.className.split(' ');
83   - var found = false;
84   -
85   - for (var i = 0; i < names.length; i++) {
86   - var name = names[i];
87   - if (object[name]) {
88   - this.expandData(object[name], node);
89   - found = true;
90   - }
91   - }
92   -
93   - if (!found) {
94   - this.expandObject(object, node);
95   - }
  82 + var names = node.className.split(' ');
  83 + var found = false;
  84 +
  85 + for (var i = 0; i < names.length; i++) {
  86 + var name = names[i];
  87 + if (object[name]) {
  88 + this.expandData(object[name], node);
  89 + found = true;
  90 + }
  91 + }
  92 +
  93 + if (!found) {
  94 + this.expandObject(object, node);
  95 + }
96 96 },
97 97
98 98 expandObject: function(object, node) {
99 99 var i;
100 100 var nodes = [];
101 101
102   - for (i = 0; i < node.childNodes.length; i++) {
103   - nodes.push(node.childNodes[i]);
104   - }
  102 + for (i = 0; i < node.childNodes.length; i++) {
  103 + nodes.push(node.childNodes[i]);
  104 + }
105 105
106 106 for (i = 0; i < node.attributes.length; i++) {
107 107 var attr = node.attributes[i];
@@ -113,12 +113,12 @@ Template.prototype = {
113 113 for (i = 0; i < nodes.length; i++) {
114 114 var child = nodes[i];
115 115 if (child.nodeType == 1) {
116   - this.expandByContext(object, child);
  116 + this.expandByContext(object, child);
117 117 }
118 118 if (child.nodeType == 3 && this.eval[child.nodeValue]) {
119   - var span = document.createElement('span');
  119 + var span = document.createElement('span');
120 120 span.innerHTML = this.eval[child.nodeValue](object);
121   - child.parentNode.replaceChild(span, child);
  121 + child.parentNode.replaceChild(span, child);
122 122 }
123 123 }
124 124 },
@@ -127,50 +127,50 @@ Template.prototype = {
127 127 compile: function(str) {
128 128 var len = str.length;
129 129 var expr = false;
130   - var cur = '';
  130 + var cur = '';
131 131 var out = [];
132   - var braces = 0;
  132 + var braces = 0;
133 133
134 134 for (var i = 0; i < len; i++) {
135 135 var c = str[i];
136 136
137   - if (expr) {
138   - if (c == '{') {
139   - braces += 1;
140   - }
141   - if (c == '}') {
142   - braces -= 1;
143   - if (braces == 0) {
144   - expr = false;
145   - if (cur.length > 0) {
146   - out.push("(" + cur + ")");
147   - }
148   - cur = "";
149   - }
150   - }
151   - else {
152   - cur += c;
153   - }
154   - }
155   - else {
156   - switch (c) {
157   - case "'":
  137 + if (expr) {
  138 + if (c == '{') {
  139 + braces += 1;
  140 + }
  141 + if (c == '}') {
  142 + braces -= 1;
  143 + if (braces == 0) {
  144 + expr = false;
  145 + if (cur.length > 0) {
  146 + out.push("(" + cur + ")");
  147 + }
  148 + cur = "";
  149 + }
  150 + }
  151 + else {
  152 + cur += c;
  153 + }
  154 + }
  155 + else {
  156 + switch (c) {
  157 + case "'":
158 158 cur += "\\'";
159   - break;
160   - case "\\":
  159 + break;
  160 + case "\\":
161 161 cur += "\\\\";
162   - break;
163   - case '{':
164   - expr = true;
165   - braces += 1;
  162 + break;
  163 + case '{':
  164 + expr = true;
  165 + braces += 1;
166 166 if (cur.length > 0) {
167   - out.push("'" + cur + "'");
  167 + out.push("'" + cur + "'");
168 168 }
169 169 cur = "";
170 170 break;
171   - case "\n":
172   - break;
173   - default:
  171 + case "\n":
  172 + break;
  173 + default:
174 174 cur += c;
175 175 }
176 176 }
@@ -180,27 +180,27 @@ Template.prototype = {
180 180 out.push("'" + cur + "'");
181 181 }
182 182
183   - this.eval[str] = new Function('data', 'with(Template.Helper) with (data) return ' + out.join('+') + ';' );
  183 + this.eval[str] = new Function('data', 'with(Template.Helper) with (data) return ' + out.join('+') + ';' );
184 184 },
185 185
186 186 compileNode: function(node) {
187   - var i;
  187 + var i;
188 188
189   - if (node.nodeType == 1) {
  189 + if (node.nodeType == 1) {
190 190 for (i = 0; i < node.attributes.length; i++) {
191   - var value = node.attributes[i].value;
192   - if (value.indexOf('{') > -1) {
193   - this.compile(value);
194   - }
195   - }
196   - for (i = 0; i < node.childNodes.length; i++) {
197   - this.compileNode(node.childNodes[i]);
198   - }
199   - }
200   -
201   - if (node.nodeType == 3 && node.nodeValue.indexOf('{') > -1) {
202   - this.compile(node.nodeValue);
203   - }
  191 + var value = node.attributes[i].value;
  192 + if (value.indexOf('{') > -1) {
  193 + this.compile(value);
  194 + }
  195 + }
  196 + for (i = 0; i < node.childNodes.length; i++) {
  197 + this.compileNode(node.childNodes[i]);
  198 + }
  199 + }
  200 +
  201 + if (node.nodeType == 3 && node.nodeValue.indexOf('{') > -1) {
  202 + this.compile(node.nodeValue);
  203 + }
204 204
205 205 }
206 206

0 comments on commit a6840b8

Please sign in to comment.
Something went wrong with that request. Please try again.