Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client-side Support #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions lib/haml.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var Haml;

var matchers, self_close_tags, embedder, forceXML, escaperName, escapeHtmlByDefault;

function trim(text){
return text.replace(/^\s+|\s+$/g,"");
}

function html_escape(text) {
return (text + "").
replace(/&/g, "&").
Expand Down Expand Up @@ -71,8 +75,8 @@ var Haml;
if (typeof pair.start === 'number' &&
typeof pair.middle === 'number' &&
typeof pair.end === 'number') {
var key = line.substr(pair.start, pair.middle - pair.start).trim(),
value = line.substr(pair.middle + 1, pair.end - pair.middle - 1).trim();
var key = trim(line.substr(pair.start, pair.middle - pair.start)),
value = trim(line.substr(pair.middle + 1, pair.end - pair.middle - 1));
attributes[key] = value;
}
pair = {
Expand Down Expand Up @@ -162,7 +166,7 @@ var Haml;

pos += next;
}
return items.filter(function (part) { return part && part.length > 0}).join(" +\n");
return _.filter(items, function (part) { return part && part.length > 0}).join(" +\n");
}

// Used to find embedded code in interpolated strings.
Expand Down Expand Up @@ -213,15 +217,15 @@ var Haml;
attribs._content = attribs._content.substr(leaderLength);
//once we've identified the tag and its attributes, the rest is content.
// this is currently trimmed for neatness.
this.contents.unshift(attribs._content.trim());
this.contents.unshift(trim(attribs._content));
delete(attribs._content);
}
} else {
attribs = {};
}

if (classes) {
classes = classes.map(function (klass) {
classes = _.map(classes, function (klass) {
return klass.substr(1, klass.length);
}).join(' ');
if (attribs['class']) {
Expand All @@ -235,7 +239,7 @@ var Haml;
}
}
if (ids) {
ids = ids.map(function (id) {
ids = _.map(ids, function (id) {
return id.substr(1, id.length);
}).join(' ');
if (attribs.id) {
Expand Down Expand Up @@ -264,7 +268,7 @@ var Haml;
}
}

if (forceXML ? content.length > 0 : self_close_tags.indexOf(tag) == -1) {
if (forceXML ? content.length > 0 : _.indexOf(self_close_tags, tag) == -1) {
output = '"<' + tag + attribs + '>"' +
(content.length > 0 ? ' + \n' + content : "") +
' + \n"</' + tag + '>"';
Expand Down Expand Up @@ -437,25 +441,25 @@ var Haml;

// If lines is a string, turn it into an array
if (typeof lines === 'string') {
lines = lines.trim().replace(/\n\r|\r/g, '\n').split('\n');
lines = trim(lines).replace(/\n\r|\r/g, '\n').split('\n');
}

lines.forEach(function(line) {
var match, found = false;
_.each(lines, function(line){
var match, found = false;

// Collect all text as raw until outdent
if (block) {
match = block.check_indent.exec(line);
if (match) {
block.contents.push(match[1] || "");
return;
block.contents.push(match[1] || "");
return;
} else {
output.push(block.process());
block = false;
}
}

matchers.forEach(function (matcher) {
_.each(matchers, function(matcher){
if (!found) {
match = matcher.regexp.exec(line);
if (match) {
Expand Down Expand Up @@ -501,19 +505,19 @@ var Haml;

// always escaped
if((line.substr(0, 2) === "&=")) {
line = line.substr(2, line.length).trim();
line = trim(line.substr(2, line.length));
return escapedLine();
}

//never escaped
if((line.substr(0, 2) === "!=")) {
line = line.substr(2, line.length).trim();
line = trim(line.substr(2, line.length));
return unescapedLine();
}

// sometimes escaped
if ( (line[0] === '=')) {
line = line.substr(1, line.length).trim();
line = trim(line.substr(1, line.length));
if(escapeHtmlByDefault){
return escapedLine();
}else{
Expand All @@ -531,23 +535,23 @@ var Haml;
output.push(block.process());
}

var txt = output.filter(function (part) { return part && part.length > 0}).join(" +\n");
var txt = _.filter(output, function (part) { return part && part.length > 0}).join(" +\n");
if(txt.length == 0){
txt = '""';
}
return txt;
};

function optimize(js) {
var new_js = [], buffer = [], part, end;
var new_js = [], buffer = [], part, end;

function flush() {
if (buffer.length > 0) {
new_js.push(JSON.stringify(buffer.join("")) + end);
buffer = [];
}
}
js.replace(/\n\r|\r/g, '\n').split('\n').forEach(function (line) {
_.each(js.replace(/\n\r|\r/g, '\n').split('\n'), function (line) {
part = line.match(/^(\".*\")(\s*\+\s*)?$/);
if (!part) {
flush();
Expand Down Expand Up @@ -592,7 +596,7 @@ var Haml;
}).call(self);
};

Haml = function Haml(haml, config) {
Haml = function (haml, config) {
if(typeof(config) != "object"){
forceXML = config;
config = {};
Expand Down