Skip to content

Commit

Permalink
allow a string of text to an argument to be a raw value (so p("foo") …
Browse files Browse the repository at this point in the history
…=> <p>foo</p>)
  • Loading branch information
smtlaissezfaire committed Apr 29, 2010
1 parent b468e26 commit 216b368
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/jm.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,38 @@ jm.Builder.prototype = (function() {
}
};

var isFunction = function(obj) {
return obj instanceof Function;
};

var isString = function(obj) {
return typeof(obj) === 'string';
};

var isFunctionOrString = function(obj) {
return isFunction(obj) || isString(obj);
};

builder.node = function(tag_name, pairs, self_closing, body) {
var string = "";
var x;
var string = "",
self = this,
x;

// allow functions to be passed alone without an options object
if (!self_closing && pairs instanceof Function && typeof(body) === 'undefined') {
if (!self_closing && isFunctionOrString(pairs) && typeof(body) === 'undefined') {
body = pairs;
pairs = {};
}

// allow function arg to be a string of raw text
if (isString(body)) {
x = body;

body = function() {
self.text(x);
};
}

string += "<" + tag_name;

if (pairs) {
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ describe("JM", function() {
result.should.equal("<p>foo</p>");
});

it("should output raw text if given a string as it's first argument", function() {
var result = builder.p("foo");
result.should.equal("<p>foo</p>");
});

describe("indentation", function() {
it("should be off by default", function() {
builder.indentation.should.be(false);
Expand Down

0 comments on commit 216b368

Please sign in to comment.