Skip to content

Commit

Permalink
Add in string interpolation all over the place.
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Apr 1, 2010
1 parent 8f60322 commit d8d7492
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
19 changes: 10 additions & 9 deletions lib/haml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ if (typeof exports !== 'undefined') {
var matchers, self_close_tags, embedder;

function html_escape(text) {
if (typeof text !== 'string') {
text = text + "";
}
return text.
return (text + "").
replace(/&/g, "&").
replace(/</g, "&lt;").
replace(/>/g, "&gt;").
Expand All @@ -35,8 +32,12 @@ if (typeof exports !== 'undefined') {
value = JSON.parse("[" + attribs[key] +"]")[0];
if (value === true) {
value = key;
} else if (typeof value === 'string' && embedder.test(value)) {
value = '" +\n' + parse_interpol(html_escape(value)) + ' +\n"';
} else {
value = html_escape(value);
}
result.push(" " + key + '=\\"' + html_escape(value) + '\\"');
result.push(" " + key + '=\\"' + value + '\\"');
} catch (e) {
result.push(" " + key + '=\\"" + html_escape(' + attribs[key] + ') + "\\"');
}
Expand Down Expand Up @@ -156,7 +157,7 @@ if (typeof exports !== 'undefined') {
items.push(match[1] || match[2]);
pos += next;
}
return items.join(" + ");
return items.join(" +\n");
}

// Used to find embedded code in interpolated strings.
Expand Down Expand Up @@ -393,14 +394,14 @@ if (typeof exports !== 'undefined') {
output.push(function () {
// Escaped plain text
if (line[0] === '\\') {
return JSON.stringify(line.substr(1, line.length));
return parse_interpol(line.substr(1, line.length));
}

// Plain variable data
if (line[0] === '=') {
line = line.substr(1, line.length).trim();
try {
return JSON.stringify(JSON.parse(line));
return parse_interpol(JSON.parse(line));
} catch (e) {
return line;
}
Expand All @@ -417,7 +418,7 @@ if (typeof exports !== 'undefined') {
}

// Plain text
return JSON.stringify(line);
return parse_interpol(line);
}());
}

Expand Down
3 changes: 3 additions & 0 deletions test/embedded_code.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
%head
:javascript
Page.chapter = #{JSON.stringify(chapter)};
%body
%h1 Welcome #{name}
%div{class: "div_#{id}"}
2 changes: 1 addition & 1 deletion test/embedded_code.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
Page.chapter = {"name":"Ninja","page":42};
//]]>
</script>
</head>
</head><body><h1>Welcome Tim</h1><div class="div_42"></div></body>
4 changes: 3 additions & 1 deletion test/embedded_code.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
locals: {
chapter: {name: "Ninja", page: 42}
chapter: {name: "Ninja", page: 42},
name: "Tim",
id: 42
}
}

0 comments on commit d8d7492

Please sign in to comment.