Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Add a test for capturing markup as a function from inside a template
Browse files Browse the repository at this point in the history
  • Loading branch information
sstephenson committed Sep 22, 2010
1 parent a05004c commit 416f782
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 4 deletions.
13 changes: 13 additions & 0 deletions test/fixtures/capture.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
renderItem = (item) =>
capture =>
print safe '\n <div class="item">\n <span class="name">'
print item.name
print safe '</span>\n <span class="price">$'
print item.price
print safe '</span>\n </div>\n'
print safe '\n\n'
for item in @items
print safe '\n '
print renderItem item
print safe '\n'
print safe '\n'
10 changes: 10 additions & 0 deletions test/fixtures/capture.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% renderItem = (item) -> %>
<div class="item">
<span class="name"><%= item.name %></span>
<span class="price">$<%= item.price %></span>
</div>
<% end %>

<% for item in @items: %>
<%= renderItem item %>
<% end %>
71 changes: 71 additions & 0 deletions test/fixtures/capture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module.exports = function(__obj) {
return (function() {
var _i, _len, _ref, capture, item, print, renderItem, safe;
var __bind = function(func, context) {
return function(){ return func.apply(context, arguments); };
};
print = __bind(function(value) {
return this.print(value);
}, this);
capture = __bind(function(callback) {
return this.capture(callback);
}, this);
safe = __bind(function(value) {
return this.safe(value);
}, this);
renderItem = __bind(function(item) {
return capture(__bind(function() {
print(safe('\n <div class="item">\n <span class="name">'));
print(item.name);
print(safe('</span>\n <span class="price">$'));
print(item.price);
return print(safe('</span>\n </div>\n'));
}, this));
}, this);
print(safe('\n\n'));
_ref = this.items;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
print(safe('\n '));
print(renderItem(item));
print(safe('\n'));
}
print(safe('\n'));
return this.toString();
}).call((function() {
var key, out = [], obj = {
print: function(value) {
if (typeof value !== 'undefined' && value != null)
out.push(this.sanitize(value));
},
capture: function(callback) {
var oldOut = out, result;
out = [];
callback.call(this);
result = out.join("");
out = oldOut;
return this.safe(result);
},
sanitize: function(value) {
return value.ecoSafe ? value : this.escape(value);
},
escape: function(value) {
return ('' + value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
},
safe: function(value) {
var result = new String(value);
result.ecoSafe = true;
return result;
},
toString: function() {
return out.join("");
}
};
for (key in __obj) obj[key] = __obj[key];
return obj;
})());
}
17 changes: 17 additions & 0 deletions test/fixtures/capture.out.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@




<div class="item">
<span class="name">Caprese</span>
<span class="price">$5.25</span>
</div>



<div class="item">
<span class="name">Artichoke</span>
<span class="price">$6.25</span>
</div>


15 changes: 11 additions & 4 deletions test/test_render.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
eco = require "eco"
{fixture} = require "fixtures"

items = [
{ name: "Caprese", price: "5.25"},
{ name: "Artichoke", price: "6.25" }
]

module.exports =
"rendering hello.eco": (test) ->
test.expect 1
Expand Down Expand Up @@ -34,17 +39,19 @@ module.exports =

"rendering helpers.eco": (test) ->
output = eco.render fixture("helpers.eco"),
items: [
{ name: "Caprese", price: "5.25"},
{ name: "Artichoke", price: "6.25" }
]
items: items
contentTag: (tagName, attributes, callback) ->
attrs = " #{name}=\"#{value}\"" for name, value of attributes
@safe "<#{tagName}#{attrs.join("")}>#{callback()}</#{tagName}>"

test.same fixture("helpers.out.1"), output
test.done()

"rendering capture.eco": (test) ->
output = eco.render fixture("capture.eco"), items: items
test.same fixture("capture.out.1"), output
test.done()

"HTML is escaped by default": (test) ->
output = eco.render "<%= @emailAddress %>",
emailAddress: "<sstephenson@gmail.com>"
Expand Down

0 comments on commit 416f782

Please sign in to comment.