Skip to content

Commit

Permalink
Pass the non-string-attribs test.
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Mar 31, 2010
1 parent d5745fb commit d6ea9ed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
29 changes: 18 additions & 11 deletions lib/haml.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Haml = module.exports = function (haml) {
// Normalize file endings
haml = haml.trim() + "\n";

var props = {};
var props = {xml:true};

function combine(tokens) {
var code = [];
Expand All @@ -34,9 +34,7 @@ var Haml = module.exports = function (haml) {
if (token._children) {
contents = combine(token._children);
}
if (!token.open) {
token = compileTag(token, true);
}
token = compileTag(token, true);
tag = token.open + contents + token.close;
}
if (typeof token === 'string' || token.static) {
Expand Down Expand Up @@ -190,7 +188,7 @@ var Haml = module.exports = function (haml) {
case 7: // escaped Variable content
if (c === "\n") {
tokens[tokens.length] = "\"\" + htmlEscape(" + haml.substr(start, i-start).trim() + ") + \"\"";
start = i = i + 1;
start = i;
indent = 0;
state = 0;
}
Expand Down Expand Up @@ -258,6 +256,15 @@ var Haml = module.exports = function (haml) {
var value = code.substr(start, end - start).trim();
try {
value = JSON.parse(value);
sys.p(value);
if (typeof value === 'number') {
value = value + "";
} else if (typeof value !== 'string' || value === '') {
if (!value) {
continue;
}
value = key;
}
obj._statics[key] = true;
if (key in obj) {
obj[key] += " " + value;
Expand All @@ -268,6 +275,7 @@ var Haml = module.exports = function (haml) {
if (key in obj._statics) {
delete obj._statics[key];
}
if (value === 'undefined') { continue; }
if (key in obj) {
obj[key] = "htmlEscape(" + JSON.stringify(obj[key] + " ") + " + " + value + ")";
} else {
Expand Down Expand Up @@ -296,6 +304,7 @@ var Haml = module.exports = function (haml) {
attribs = attribs.length > 0 ? " " + attribs.join(" ") : "";
var result = {
static: static,
close: ""
};
if (SELF_CLOSE[tag]) {
if (props.xml) {
Expand All @@ -321,31 +330,29 @@ var Haml = module.exports = function (haml) {
switch(code) {
case '': // Default
case 'transitional':
props.xml = true;
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
case 'strict':
props.xml = true;
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
case 'frameset':
props.xml = true;
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">';
case '1.1':
props.xml = true;
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
case 'basic':
props.xml = true;
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">';
case 'mobile':
props.xml = true;
return '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">';
case '5':
props.xml = false;
return '<!DOCTYPE html>';
case '4':
case '4 transitional':
props.xml = false;
return '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
case '4 strict':
props.xml = false;
return '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
case '4 frameset':
props.xml = false;
return '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">';
}
return '!!!' + originalCode;
Expand Down
4 changes: 2 additions & 2 deletions test/non-string-attribs.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#plain= "Plain Text"
#escaped&= "<escaped>"
#plain!= "Plain Text"
#escaped= "<escaped>"
%input{checked: true}
%input{checked: false}
%input{checked: null}
Expand Down
4 changes: 1 addition & 3 deletions test/non-string-attribs.html
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
<div id="plain">Plain Text
</div><div id="escaped">&lt;escaped&gt;
</div><input checked="checked" /><input /><input /><input /><input checked="0" /><input />
<div id="plain">Plain Text</div><div id="escaped">&lt;escaped&gt;</div><input checked="checked" /><input /><input /><input /><input checked="0" /><input />

0 comments on commit d6ea9ed

Please sign in to comment.