Comparing version 1.0.0 to 1.0.2
114
lib/hogan.js
@@ -20,3 +20,3 @@ /* | ||
this.text = text; | ||
}; | ||
} | ||
@@ -47,4 +47,5 @@ constructor.prototype = { | ||
rs: function(context, partials, section) { | ||
var buf = ''; | ||
var tail = context[context.length - 1]; | ||
var buf = '', | ||
tail = context[context.length - 1]; | ||
if (!isArray(tail)) { | ||
@@ -65,2 +66,4 @@ buf = section(context, partials); | ||
s: function(val, ctx, partials, inverted, start, end) { | ||
var pass; | ||
if (isArray(val) && val.length === 0) { | ||
@@ -74,3 +77,3 @@ return false; | ||
var pass = (val === '') || !!val; | ||
pass = (val === '') || !!val; | ||
@@ -86,2 +89,7 @@ if (!inverted && pass && ctx) { | ||
d: function(key, ctx, partials, returnFound) { | ||
var names = key.split('.'), | ||
val = this.f(names[0], ctx, partials, returnFound), | ||
cx = null; | ||
if (key === '.' && isArray(ctx[ctx.length - 2])) { | ||
@@ -91,5 +99,2 @@ return ctx[ctx.length - 1]; | ||
var names = key.split('.'); | ||
var val = this.f(names[0], ctx, partials, returnFound); | ||
var cx = null; | ||
for (var i = 1; i < names.length; i++) { | ||
@@ -119,5 +124,5 @@ if (val && typeof val == 'object' && names[i] in val) { | ||
f: function(key, ctx, partials, returnFound) { | ||
var val = false; | ||
var v = null; | ||
var found = false; | ||
var val = false, | ||
v = null, | ||
found = false; | ||
@@ -159,7 +164,9 @@ for (var i = ctx.length - 1; i >= 0; i--) { | ||
ls: function(val, ctx, partials, start, end) { | ||
var cx = ctx[ctx.length - 1]; | ||
var cx = ctx[ctx.length - 1], | ||
t = val.call(cx); | ||
if (val.length > 0) { | ||
return this.ho(val, cx, partials, this.text.substring(start, end)); | ||
} | ||
var t = val.call(cx); | ||
if (typeof t == 'function') { | ||
@@ -189,3 +196,3 @@ return this.ho(t, cx, partials, this.text.substring(start, end)); | ||
return Object.prototype.toString.call(a) === '[object Array]'; | ||
} | ||
}; | ||
@@ -197,2 +204,14 @@ return constructor; | ||
// Setup regex assignments | ||
// remove whitespace according to Mustache spec | ||
var rIsWhitespace = /\S/, | ||
rQuot = /\"/g, | ||
rNewline = /\n/g, | ||
rCr = /\r/g, | ||
rSlash = /\\/g, | ||
tagTypes = { | ||
'#': 1, '^': 2, '/': 3, '!': 4, '>': 5, | ||
'<': 6, '=': 7, '_v': 8, '{': 9, '&': 10 | ||
}; | ||
function scan(text) { | ||
@@ -205,2 +224,3 @@ var len = text.length, | ||
tagType = null, | ||
tag = null, | ||
buf = '', | ||
@@ -226,3 +246,3 @@ tokens = [], | ||
(tokens[j].tag && tagTypes[tokens[j].tag] < tagTypes['_v']) || | ||
(!tokens[j].tag && tokens[j].match(rIsWhitespace) == null); | ||
(!tokens[j].tag && tokens[j].match(rIsWhitespace) === null); | ||
if (!isAllWhitespace) { | ||
@@ -245,3 +265,3 @@ return false; | ||
} else if (!noNewLine) { | ||
tokens.push({tag:'\n'}) | ||
tokens.push({tag:'\n'}); | ||
} | ||
@@ -254,5 +274,5 @@ | ||
function changeDelimiters(text, index) { | ||
var close = '=' + ctag; | ||
var closeIndex = text.indexOf(close, index); | ||
var delimiters = trim(text.substring(text.indexOf('=', index) + 1, | ||
var close = '=' + ctag, | ||
closeIndex = text.indexOf(close, index), | ||
delimiters = trim(text.substring(text.indexOf('=', index) + 1, | ||
closeIndex)).split(' '); | ||
@@ -271,6 +291,6 @@ otag = delimiters[0]; | ||
} else { | ||
if (text[i] == '\n') { | ||
if (text.charAt(i) == '\n') { | ||
filterLine(seenTag); | ||
} else { | ||
buf += text[i]; | ||
buf += text.charAt(i); | ||
} | ||
@@ -280,4 +300,4 @@ } | ||
i += otag.length - 1; | ||
var tag = tagTypes[text[i + 1]]; | ||
tagType = tag ? text[i + 1] : '_v'; | ||
tag = tagTypes[text.charAt(i + 1)]; | ||
tagType = tag ? text.charAt(i + 1) : '_v'; | ||
seenTag = i; | ||
@@ -304,3 +324,3 @@ if (tagType == '=') { | ||
} else { | ||
buf += text[i]; | ||
buf += text.charAt(i); | ||
} | ||
@@ -323,12 +343,4 @@ } | ||
// remove whitespace according to Mustache spec | ||
var rIsWhitespace = /\S/; | ||
var tagTypes = { | ||
'#': 1, '^': 2, '/': 3, '!': 4, '>': 5, | ||
'<': 6, '=': 7, '_v': 8, '{': 9, '&': 10 | ||
}; | ||
function tagChange(tag, text, index) { | ||
if (text[index] != tag[0]) { | ||
if (text.charAt(index) != tag.charAt(0)) { | ||
return false; | ||
@@ -338,3 +350,3 @@ } | ||
for (var i = 1, l = tag.length; i < l; i++) { | ||
if (text[index + i] != tag[i]) { | ||
if (text.charAt(index + i) != tag.charAt(i)) { | ||
return false; | ||
@@ -360,3 +372,3 @@ } | ||
} else if (token.tag == '/') { | ||
if (stack.length == 0) { | ||
if (stack.length === 0) { | ||
throw new Error('Closing tag without opener: /' + token.n); | ||
@@ -403,3 +415,3 @@ } | ||
if (options.asString) { | ||
return 'function(cx,p){' + code + ';};'; | ||
return 'function(cx,p){' + code + ';}'; | ||
} | ||
@@ -412,3 +424,2 @@ | ||
var rQuot = /\"/g, rNewline = /\n/g, rCr = /\r/g, rSlash = /\\/g; | ||
function esc(s) { | ||
@@ -418,4 +429,4 @@ return s.replace(rSlash, '\\\\') | ||
.replace(rNewline, '\\n') | ||
.replace(rCr, '\\r') | ||
}; | ||
.replace(rCr, '\\r'); | ||
} | ||
@@ -452,17 +463,15 @@ function chooseMethod(s) { | ||
function section(nodes, id, method, start, end) { | ||
var code = 'if(_.s(_.' + method + '("' + esc(id) + '",c,p,1),'; | ||
code += 'c,p,0,' + start + ',' + end + ')){'; | ||
code += 'b += _.rs(c,p,'; | ||
code += 'function(c,p){ var b = "";'; | ||
code += walk(nodes); | ||
code += 'return b;});c.pop();}'; | ||
code += 'else{b += _.b; _.b = ""};'; | ||
return code; | ||
return 'if(_.s(_.' + method + '("' + esc(id) + '",c,p,1),' + | ||
'c,p,0,' + start + ',' + end + ')){' + | ||
'b += _.rs(c,p,' + | ||
'function(c,p){ var b = "";' + | ||
walk(nodes) + | ||
'return b;});c.pop();}' + | ||
'else{b += _.b; _.b = ""};'; | ||
} | ||
function invertedSection(nodes, id, method) { | ||
var code = 'if (!_.s(_.' + method + '("' + esc(id) + '",c,p,1),c,p,1,0,0)){'; | ||
code += walk(nodes); | ||
code += '};'; | ||
return code; | ||
return 'if (!_.s(_.' + method + '("' + esc(id) + '",c,p,1),c,p,1,0,0)){' + | ||
walk(nodes) + | ||
'};'; | ||
} | ||
@@ -525,1 +534,6 @@ | ||
} | ||
// Expose Hogan as an AMD module. | ||
if (typeof define === 'function' && define.amd) { | ||
define(function () { return Hogan; }); | ||
} |
{ | ||
"name": "hogan.js" | ||
, "description": "A mustache compiler." | ||
, "version": "1.0.0" | ||
, "version": "1.0.2" | ||
, "keywords": ["mustache", "template"] | ||
@@ -6,0 +6,0 @@ , "main": "./lib/hogan.js" |
## Hogan.js - A mustache compiler. | ||
[hogan.js](http://twitter.github.com/hogan.js/) is a compiler for the | ||
[Hogan.js](http://twitter.github.com/hogan.js/) is a compiler for the | ||
[Mustache](http://mustache.github.com/) templating language. For information | ||
@@ -12,11 +12,13 @@ on Mustache, see the [manpage](http://mustache.github.com/mustache.5.html) and | ||
var data = { | ||
screenName: "dhg", | ||
}; | ||
```js | ||
var data = { | ||
screenName: "dhg", | ||
}; | ||
var template = Hogan.compile("Follow @{{screenName}}."); | ||
var output = template.render(data); | ||
var template = Hogan.compile("Follow @{{screenName}}."); | ||
var output = template.render(data); | ||
// prints "Follow @dhg." | ||
console.log(output); | ||
// prints "Follow @dhg." | ||
console.log(output); | ||
``` | ||
@@ -34,11 +36,13 @@ ## Features | ||
var text = "{{^check}}{{i18n}}No{{/i18n}}{{/check}}"; | ||
text += "{{#check}}{{i18n}}Yes{{/i18n}}{{/check}}"; | ||
var tree = Hogan.parse(Hogan.scan(text)); | ||
```js | ||
var text = "{{^check}}{{i18n}}No{{/i18n}}{{/check}}"; | ||
text += "{{#check}}{{i18n}}Yes{{/i18n}}{{/check}}"; | ||
var tree = Hogan.parse(Hogan.scan(text)); | ||
// outputs "# check" | ||
console.log(tree[0].tag + " " + tree[0].name); | ||
// outputs "# check" | ||
console.log(tree[0].tag + " " + tree[0].name); | ||
// outputs "Yes" | ||
console.log(tree[1].nodes[0].nodes[0]); | ||
// outputs "Yes" | ||
console.log(tree[1].nodes[0].nodes[0]); | ||
``` | ||
@@ -45,0 +49,0 @@ It's also possible to use HoganTemplate objects without the Hogan compiler |
@@ -592,3 +592,3 @@ /* | ||
var text = "{{#filter}}foo {{bar}}{{/filter}}"; | ||
var t = Hogan.compile(text) | ||
var t = Hogan.compile(text); | ||
var s = t.render({ | ||
@@ -663,9 +663,16 @@ filter: function() { | ||
function testDefaultRenderImpl() { | ||
var ht = new Hogan.Template(); | ||
var ht = new (Hogan.Template || HoganTemplate)(); | ||
is(ht.render() === '', true, 'default renderImpl returns an array.'); | ||
} | ||
function appendText(el, text) { | ||
var textNode = document.createTextNode(text); | ||
el.appendChild(textNode); | ||
el.appendChild(document.createElement('br')); | ||
} | ||
if (!this["output"]) { | ||
var output = function (s) { | ||
return doc ? doc.getElementById('console').innerHTML += s + '\n' : console.log(s); | ||
return doc ? appendText(doc.getElementById('console'), s) : console.log(s); | ||
}; | ||
@@ -764,3 +771,3 @@ } | ||
testIndentedStandaloneComment(); | ||
testNewLineBetweenDelimiterChanges(); | ||
testNewLineBetweenDelimiterChanges(); | ||
complete(); | ||
@@ -767,0 +774,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55169
1181
93