Permalink
Browse files

trim templates to prevent conflicts with jQuery

  • Loading branch information...
1 parent 2aab4ed commit 725d3bb9dc94adfa929d283f8c65496c1d28a21e @SBoudrias SBoudrias committed Mar 20, 2013
Showing with 23 additions and 0 deletions.
  1. +1 −0 backbone.layoutmanager.js
  2. +3 −0 node/index.js
  3. +19 −0 test/views.js
@@ -422,6 +422,7 @@ var LayoutManager = Backbone.View.extend({
function applyTemplate(rendered) {
// Actually put the rendered contents into the element.
if (rendered) {
+ rendered = $.trim(rendered);
// If no container is specified, we must replace the content.
if (manager.noel) {
// Hold a reference to created element as replaceWith doesn't return new el.
View
@@ -14,6 +14,9 @@ var $ = require("cheerio");
// `Backbone.View#setElement`.
$.prototype.unbind = $.prototype.off = function() { return this; };
+// Set a basic trim function to allow trimming whitespace from a template
+$.trim = function(str) { return str.trim(); };
+
// Since jQuery is not being used and LayoutManager depends on a Promise
// implementation close to jQuery, we use `underscore.deferred` here which
// matches jQuery's Deferred API exactly.
View
@@ -1940,4 +1940,23 @@ test("trigger callback on a view with `keep: true`", 1, function() {
layout.removeView();
});
+// https://github.com/tbranyen/backbone.layoutmanager/issues/323
+test("templates should be trimmed before insertion", 1, function() {
+ var layout = new Backbone.Layout({
+ template: "tpl",
+ el: false,
+ fetch: function() {
+ return "\n <div>Hey</div>\n ";
+ },
+ render: function( tpl ) {
+ return tpl;
+ }
+ });
+
+ layout.render();
+
+ equal(layout.$el.text(), "Hey");
+
+});
+
})(typeof global !== "undefined" ? global : this);

0 comments on commit 725d3bb

Please sign in to comment.