Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Writer.breakLine infinite loop. Closes #27.

  • Loading branch information...
commit 15b2372f789b4008337b3ec8abe40c443b8802b1 1 parent d20bacc
@donpark authored
View
33 lib/html2jade.coffee
@@ -97,24 +97,23 @@ class Writer
@writeTextLine line, output, pipe, trim, wrap
breakLine: (line) ->
- if line and line.length > 0
- lines = []
- words = line.split(/\s+/)
- line = ''
- while words.length
- word = words.shift()
- if line.length + word.length > @wrapLength
- lines.push line
- line = word
- else if line.length
- line += ' ' + word
- else
- line = word
- if line.length
+ return [] if not line or line.length is 0
+ return [ line ] if line.search /\s+/ is -1
+ lines = []
+ words = line.split(/\s+/)
+ line = ''
+ while words.length
+ word = words.shift()
+ if line.length + word.length > @wrapLength
lines.push line
- lines
- else
- []
+ line = word
+ else if line.length
+ line += ' ' + word
+ else
+ line = word
+ if line.length
+ lines.push line
+ lines
publicIdDocTypeNames =
View
319 lib/html2jade.js
@@ -1,21 +1,16 @@
(function() {
var Converter, Output, Parser, StreamOutput, StringOutput, Writer, publicIdDocTypeNames, scope, systemIdDocTypeNames, _ref;
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
- function ctor() { this.constructor = child; }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
- };
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
+
scope = typeof exports !== "undefined" && exports !== null ? exports : (_ref = this.Html2Jade) != null ? _ref : this.Html2Jade = {};
+
Parser = (function() {
+
function Parser(options) {
- if (options == null) {
- options = {};
- }
+ if (options == null) options = {};
this.jsdom = require('jsdom');
}
+
Parser.prototype.parse = function(arg, cb) {
if (arg != null) {
return this.jsdom.env(arg, function(errors, window) {
@@ -29,35 +24,35 @@
return cb('null file');
}
};
+
return Parser;
+
})();
+
Writer = (function() {
+
function Writer(options) {
var _ref2, _ref3;
- if (options == null) {
- options = {};
- }
+ if (options == null) options = {};
this.wrapLength = (_ref2 = options.wrapLength) != null ? _ref2 : 80;
this.scalate = (_ref3 = options.scalate) != null ? _ref3 : false;
this.attrSep = this.scalate ? ' ' : ', ';
}
+
Writer.prototype.tagHead = function(node) {
var classes, result;
result = node.tagName !== 'DIV' ? node.tagName.toLowerCase() : '';
- if (node.id) {
- result += '#' + node.id;
- }
+ if (node.id) result += '#' + node.id;
if (node.hasAttribute('class')) {
classes = node.getAttribute('class').split(/\s+/).filter(function(item) {
return (item != null) && item.trim().length > 0;
});
result += '.' + classes.join('.');
}
- if (result.length === 0) {
- result = 'div';
- }
+ if (result.length === 0) result = 'div';
return result;
};
+
Writer.prototype.tagAttr = function(node) {
var attr, attrs, nodeName, result, _i, _len;
attrs = node.attributes;
@@ -79,6 +74,7 @@
}
}
};
+
Writer.prototype.tagText = function(node) {
var data, _ref2;
if (((_ref2 = node.firstChild) != null ? _ref2.nodeType : void 0) !== 3) {
@@ -94,6 +90,7 @@
}
}
};
+
Writer.prototype.forEachChild = function(parent, cb) {
var child, _results;
if (parent) {
@@ -106,71 +103,49 @@
return _results;
}
};
+
Writer.prototype.writeTextContent = function(node, output, pipe, trim, wrap, escapeBackslash) {
- if (pipe == null) {
- pipe = true;
- }
- if (trim == null) {
- trim = true;
- }
- if (wrap == null) {
- wrap = true;
- }
- if (escapeBackslash == null) {
- escapeBackslash = false;
- }
+ var _this = this;
+ if (pipe == null) pipe = true;
+ if (trim == null) trim = true;
+ if (wrap == null) wrap = true;
+ if (escapeBackslash == null) escapeBackslash = false;
output.enter();
- this.forEachChild(node, __bind(function(child) {
- return this.writeText(child, output, pipe, trim, wrap, escapeBackslash);
- }, this));
+ this.forEachChild(node, function(child) {
+ return _this.writeText(child, output, pipe, trim, wrap, escapeBackslash);
+ });
return output.leave();
};
+
Writer.prototype.writeText = function(node, output, pipe, trim, wrap, escapeBackslash) {
var data, lines;
- if (pipe == null) {
- pipe = true;
- }
- if (trim == null) {
- trim = true;
- }
- if (wrap == null) {
- wrap = true;
- }
- if (escapeBackslash == null) {
- escapeBackslash = false;
- }
+ var _this = this;
+ if (pipe == null) pipe = true;
+ if (trim == null) trim = true;
+ if (wrap == null) wrap = true;
+ if (escapeBackslash == null) escapeBackslash = false;
if (node.nodeType === 3) {
data = node.data || '';
if (data.length > 0) {
lines = data.split(/\r|\n/);
- return lines.forEach(__bind(function(line) {
- return this.writeTextLine(line, output, pipe, trim, wrap, escapeBackslash);
- }, this));
+ return lines.forEach(function(line) {
+ return _this.writeTextLine(line, output, pipe, trim, wrap, escapeBackslash);
+ });
}
}
};
+
Writer.prototype.writeTextLine = function(line, output, pipe, trim, wrap, escapeBackslash) {
var lines, prefix;
- if (pipe == null) {
- pipe = true;
- }
- if (trim == null) {
- trim = true;
- }
- if (wrap == null) {
- wrap = true;
- }
- if (escapeBackslash == null) {
- escapeBackslash = false;
- }
+ var _this = this;
+ if (pipe == null) pipe = true;
+ if (trim == null) trim = true;
+ if (wrap == null) wrap = true;
+ if (escapeBackslash == null) escapeBackslash = false;
prefix = pipe ? '| ' : '';
- if (trim) {
- line = line ? line.trim() : '';
- }
+ if (trim) line = line ? line.trim() : '';
if (line && line.length > 0) {
- if (escapeBackslash) {
- line = line.replace("\\", "\\\\");
- }
+ if (escapeBackslash) line = line.replace("\\", "\\\\");
if (!wrap || line.length <= this.wrapLength) {
return output.writeln(prefix + line);
} else {
@@ -178,40 +153,40 @@
if (lines.length === 1) {
return output.writeln(prefix + line);
} else {
- return lines.forEach(__bind(function(line) {
- return this.writeTextLine(line, output, pipe, trim, wrap);
- }, this));
+ return lines.forEach(function(line) {
+ return _this.writeTextLine(line, output, pipe, trim, wrap);
+ });
}
}
}
};
+
Writer.prototype.breakLine = function(line) {
var lines, word, words;
- if (line && line.length > 0) {
- lines = [];
- words = line.split(/\s+/);
- line = '';
- while (words.length) {
- word = words.shift();
- if (line.length + word.length > this.wrapLength) {
- lines.push(line);
- line = word;
- } else if (line.length) {
- line += ' ' + word;
- } else {
- line = word;
- }
- }
- if (line.length) {
+ if (!line || line.length === 0) return [];
+ if (line.search(/\s+/ === -1)) return [line];
+ lines = [];
+ words = line.split(/\s+/);
+ line = '';
+ while (words.length) {
+ word = words.shift();
+ if (line.length + word.length > this.wrapLength) {
lines.push(line);
+ line = word;
+ } else if (line.length) {
+ line += ' ' + word;
+ } else {
+ line = word;
}
- return lines;
- } else {
- return [];
}
+ if (line.length) lines.push(line);
+ return lines;
};
+
return Writer;
+
})();
+
publicIdDocTypeNames = {
"-//W3C//DTD XHTML 1.0 Transitional//EN": "transitional",
"-//W3C//DTD XHTML 1.0 Strict//EN": "strict",
@@ -220,6 +195,7 @@
"-//W3C//DTD XHTML Basic 1.1//EN": "basic",
"-//WAPFORUM//DTD XHTML Mobile 1.2//EN": "mobile"
};
+
systemIdDocTypeNames = {
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd": "transitional",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd": "strict",
@@ -228,15 +204,16 @@
"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd": "basic",
"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd": "mobile"
};
+
Converter = (function() {
+
function Converter(options) {
var _ref2, _ref3;
- if (options == null) {
- options = {};
- }
+ if (options == null) options = {};
this.scalate = (_ref2 = options.scalate) != null ? _ref2 : false;
this.writer = (_ref3 = options.writer) != null ? _ref3 : new Writer(options);
}
+
Converter.prototype.document = function(document, output) {
var docTypeName, doctype, htmlEls, publicId, systemId;
if (document.doctype != null) {
@@ -251,24 +228,20 @@
} else if ((doctype.name != null) && doctype.name.toLowerCase() === 'html') {
docTypeName = '5';
}
- if (docTypeName != null) {
- output.writeln('!!! ' + docTypeName);
- }
+ if (docTypeName != null) output.writeln('!!! ' + docTypeName);
}
if (document.documentElement) {
return this.element(document.documentElement, output);
} else {
htmlEls = document.getElementsByTagName('html');
- if (htmlEls.length > 0) {
- return this.element(htmlEls[0], output);
- }
+ if (htmlEls.length > 0) return this.element(htmlEls[0], output);
}
};
+
Converter.prototype.element = function(node, output) {
var firstline, tagAttr, tagHead, tagName, tagText;
- if (!(node != null ? node.tagName : void 0)) {
- return;
- }
+ var _this = this;
+ if (!(node != null ? node.tagName : void 0)) return;
tagName = node.tagName.toLowerCase();
tagHead = this.writer.tagHead(node);
tagAttr = this.writer.tagAttr(node);
@@ -286,7 +259,7 @@
output.writeln(tagHead + tagAttr + '.');
output.enter();
firstline = true;
- this.writer.forEachChild(node, __bind(function(child) {
+ this.writer.forEachChild(node, function(child) {
var data;
if (child.nodeType === 3) {
data = child.data;
@@ -303,7 +276,7 @@
return output.write(data);
}
}
- }, this));
+ });
output.writeln();
return output.leave();
} else if (tagText) {
@@ -316,31 +289,36 @@
return this.children(node, output);
}
};
+
Converter.prototype.children = function(parent, output) {
+ var _this = this;
output.enter();
- this.writer.forEachChild(parent, __bind(function(child) {
+ this.writer.forEachChild(parent, function(child) {
var nodeType;
nodeType = child.nodeType;
if (nodeType === 1) {
- return this.element(child, output);
+ return _this.element(child, output);
} else if (nodeType === 3) {
if (parent._nodeName === 'code') {
- return this.text(child, output, false, true, true);
+ return _this.text(child, output, false, true, true);
} else {
- return this.text(child, output);
+ return _this.text(child, output);
}
} else if (nodeType === 8) {
- return this.comment(child, output);
+ return _this.comment(child, output);
}
- }, this));
+ });
return output.leave();
};
+
Converter.prototype.text = function(node, output, pipe, trim, wrap) {
node.normalize();
return this.writer.writeText(node, output, pipe, trim, wrap);
};
+
Converter.prototype.comment = function(node, output) {
var condition, data, lines;
+ var _this = this;
condition = node.data.match(/\s*\[(if\s+[^\]]+)\]/);
if (condition) {
output.writeln('//' + condition[1]);
@@ -351,23 +329,26 @@
data = node.data || '';
if (data.length > 0) {
lines = data.split(/\r|\n/);
- lines.forEach(__bind(function(line) {
- return this.writer.writeTextLine(line, output, false, false, false);
- }, this));
+ lines.forEach(function(line) {
+ return _this.writer.writeTextLine(line, output, false, false, false);
+ });
}
return output.leave();
}
};
+
Converter.prototype.conditional = function(node, output) {
var data, parser;
+ var _this = this;
data = node.textContent;
data = data.replace(/\s*\[if\s+[^\]]+\]>\s*/, '');
data = data.replace('<![endif]', '');
parser = new Parser();
- return parser.parse(data, __bind(function(errors, window) {
- return this.children(window.document.body, output);
- }, this));
+ return parser.parse(data, function(errors, window) {
+ return _this.children(window.document.body, output);
+ });
};
+
Converter.prototype.script = function(node, output, tagHead, tagAttr) {
if (this.scalate) {
output.writeln(':javascript');
@@ -377,6 +358,7 @@
return this.writer.writeTextContent(node, output, false, true, false, true);
}
};
+
Converter.prototype.style = function(node, output, tagHead, tagAttr) {
if (this.scalate) {
output.writeln(':css');
@@ -386,127 +368,131 @@
return this.writer.writeTextContent(node, output, false, true, false);
}
};
+
return Converter;
+
})();
+
Output = (function() {
+
function Output() {
this.indents = '';
}
+
Output.prototype.enter = function() {
return this.indents += ' ';
};
+
Output.prototype.leave = function() {
return this.indents = this.indents.substring(2);
};
+
Output.prototype.write = function(data, indent) {
- if (indent == null) {
- indent = true;
- }
+ if (indent == null) indent = true;
};
+
Output.prototype.writeln = function(data, indent) {
- if (indent == null) {
- indent = true;
- }
+ if (indent == null) indent = true;
};
+
return Output;
+
})();
+
StringOutput = (function() {
+
__extends(StringOutput, Output);
+
function StringOutput() {
StringOutput.__super__.constructor.apply(this, arguments);
this.fragments = [];
}
+
StringOutput.prototype.write = function(data, indent) {
- if (indent == null) {
- indent = true;
- }
- if (data == null) {
- data = '';
- }
+ if (indent == null) indent = true;
+ if (data == null) data = '';
if (indent) {
return this.fragments.push(this.indents + data);
} else {
return this.fragments.push(data);
}
};
+
StringOutput.prototype.writeln = function(data, indent) {
- if (indent == null) {
- indent = true;
- }
- if (data == null) {
- data = '';
- }
+ if (indent == null) indent = true;
+ if (data == null) data = '';
if (indent) {
return this.fragments.push(this.indents + data + '\n');
} else {
return this.fragments.push(data + '\n');
}
};
+
StringOutput.prototype.final = function() {
var result;
result = this.fragments.join('');
this.fragments = [];
return result;
};
+
return StringOutput;
+
})();
+
StreamOutput = (function() {
+
__extends(StreamOutput, Output);
+
function StreamOutput(stream) {
this.stream = stream;
StreamOutput.__super__.constructor.apply(this, arguments);
}
+
StreamOutput.prototype.write = function(data, indent) {
- if (indent == null) {
- indent = true;
- }
- if (data == null) {
- data = '';
- }
+ if (indent == null) indent = true;
+ if (data == null) data = '';
if (indent) {
return this.stream.write(this.indents + data);
} else {
return this.stream.write(data);
}
};
+
StreamOutput.prototype.writeln = function(data, indent) {
- if (indent == null) {
- indent = true;
- }
- if (data == null) {
- data = '';
- }
+ if (indent == null) indent = true;
+ if (data == null) data = '';
if (indent) {
return this.stream.write(this.indents + data + '\n');
} else {
return this.stream.write(data + '\n');
}
};
+
return StreamOutput;
+
})();
+
scope.Output = Output;
+
scope.StringOutput = StringOutput;
+
scope.Converter = Converter;
+
scope.Writer = Writer;
+
if (typeof exports !== "undefined" && exports !== null) {
scope.Parser = Parser;
scope.StreamOutput = StreamOutput;
scope.convert = function(input, output, options) {
var _ref2;
- if (options == null) {
- options = {};
- }
- if ((_ref2 = options.parser) == null) {
- options.parser = new Parser(options);
- }
+ if (options == null) options = {};
+ if ((_ref2 = options.parser) == null) options.parser = new Parser(options);
return options.parser.parse(input, function(errors, window) {
var _ref3;
if (errors != null ? errors.length : void 0) {
return errors;
} else {
- if (output == null) {
- output = new StreamOutput(process.stdout);
- }
+ if (output == null) output = new StreamOutput(process.stdout);
if ((_ref3 = options.converter) == null) {
options.converter = new Converter(options);
}
@@ -515,14 +501,11 @@
});
};
}
+
scope.convertHtml = function(html, options, cb) {
var _ref2;
- if (options == null) {
- options = {};
- }
- if ((_ref2 = options.parser) == null) {
- options.parser = new Parser(options);
- }
+ if (options == null) options = {};
+ if ((_ref2 = options.parser) == null) options.parser = new Parser(options);
return options.parser.parse(html, function(errors, window) {
var output, _ref3, _ref4;
if (errors != null ? errors.length : void 0) {
@@ -533,24 +516,20 @@
options.converter = new Converter(options);
}
options.converter.document(window.document, output);
- if (cb != null) {
- return cb(null, output.final());
- }
+ if (cb != null) return cb(null, output.final());
}
});
};
+
scope.convertDocument = function(document, options, cb) {
var output, _ref2, _ref3;
- if (options == null) {
- options = {};
- }
+ if (options == null) options = {};
output = (_ref2 = options.output) != null ? _ref2 : new StringOutput();
if ((_ref3 = options.converter) == null) {
options.converter = new Converter(options);
}
options.converter.document(document, output);
- if (cb != null) {
- return cb(null, output.final());
- }
+ if (cb != null) return cb(null, output.final());
};
+
}).call(this);
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "html2jade",
"description": "HTML to Jade conversion tool",
- "version": "0.1.1",
+ "version": "0.1.2",
"author": "Don Park <donpark@docuverse.com>",
"repository": "git://github.com/donpark/html2jade.git",
"main": "index",
View
2  test/data/textarea-javascript.html
@@ -0,0 +1,2 @@
+<textarea id="text-area">javascript:window.s=document.createElement('script');window.sc=document.getElementsByTagName("body")[0]||document.getElementsByTagName("head")[0];s.src="http://xyz.com/path/app.js";sc.appendChild(s)
+</textarea>

0 comments on commit 15b2372

Please sign in to comment.
Something went wrong with that request. Please try again.