This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for capturing markup as a function from inside a template
- Loading branch information
1 parent
a05004c
commit 416f782
Showing
5 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"'); | ||
}, | ||
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; | ||
})()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters