clean-html
Advanced tools
Comparing version 1.4.0 to 1.4.1
52
index.js
@@ -23,3 +23,3 @@ var htmlparser = require('htmlparser2'), | ||
//common self closing svg elements | ||
// common self closing svg elements | ||
'circle', | ||
@@ -56,4 +56,2 @@ 'ellipse', | ||
'p', | ||
'script', | ||
'style', | ||
'table', | ||
@@ -84,2 +82,6 @@ 'td', | ||
'replace-nbsp': false, | ||
'unsupported-tags': [ | ||
'script', | ||
'style' | ||
], | ||
'wrap': 120 | ||
@@ -186,6 +188,2 @@ }; | ||
if (node.parent && (node.parent.type == 'script' || node.parent.type == 'style')) { | ||
return text; | ||
} | ||
if (options['replace-nbsp']) { | ||
@@ -222,2 +220,6 @@ text = text.replace(/ /g, ' '); | ||
function renderTag(node) { | ||
if (options['unsupported-tags'].indexOf(node.name) != -1) { | ||
return ''; | ||
} | ||
if (shouldRemove(node)) { | ||
@@ -296,5 +298,3 @@ if (isEmpty(node)) { | ||
// remove extra line breaks | ||
html = html.replace(/\n+/g, '\n'); | ||
return html; | ||
return html.replace(/\n+/g, '\n'); | ||
} | ||
@@ -313,8 +313,13 @@ | ||
function wrap(line, indent) { | ||
// find the last space before the column limit | ||
var bound = line.lastIndexOf(' ', options['wrap']); | ||
if (bound == -1) { | ||
// there are no spaces before the colum limit | ||
// so find the first space after it | ||
bound = line.indexOf(' ', options['wrap']); | ||
if (bound == -1) { | ||
// there are no spaces in the line | ||
// so we can't wrap it | ||
return line; | ||
@@ -327,2 +332,8 @@ } | ||
if (line1.trim().length == 0) { | ||
// there are no spaces in the line other than the indent | ||
// so we can't wrap it | ||
return line; | ||
} | ||
if (line2.length > options['wrap']) { | ||
@@ -336,12 +347,25 @@ line2 = wrap(line2, indent); | ||
function indent(html) { | ||
var indentLevel = 0; | ||
var indentLevel = 0, | ||
inComment = false; | ||
return html.replace(/.*\n/g, function (line) { | ||
var openTags = [], | ||
result, | ||
tagRegEx = /<\/?(\w+).*?>/g, | ||
tag, | ||
tagName, | ||
result; | ||
tagName; | ||
while (result = tagRegEx.exec(line)) { | ||
if (line.lastIndexOf('<!--') > line.lastIndexOf('-->')) { | ||
inComment = true; | ||
} else if (line.indexOf('-->') != -1) { | ||
inComment = false; | ||
} | ||
while (!inComment && (result = tagRegEx.exec(line))) { | ||
// don't increase indent if tag is inside a comment | ||
if (line.lastIndexOf('<!--', result.index) < result.index | ||
&& line.indexOf('-->', result.index) > result.index) { | ||
continue; | ||
} | ||
tag = result[0]; | ||
@@ -348,0 +372,0 @@ tagName = result[1]; |
{ | ||
"name": "clean-html", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "HTML cleaner and beautifier", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -107,3 +107,3 @@ ## HTML cleaner and beautifier | ||
Type: Array | ||
Default: `['body', 'blockquote', 'br', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'hr', 'link', 'meta', 'p', 'script', 'style', 'table', 'title', 'td', 'tr']` | ||
Default: `['body', 'blockquote', 'br', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'hr', 'link', 'meta', 'p', 'table', 'title', 'td', 'tr']` | ||
@@ -110,0 +110,0 @@ ### indent |
@@ -1,4 +0,16 @@ | ||
1.3.9 | ||
1.4.1 | ||
----- | ||
Maximum call stack error when trying to wrap lines without spaces has been | ||
fixed. | ||
Support for conditional comments has been added. | ||
Trying to preserve CSS and JavaScript formatting is a pain, so style and | ||
script tags are no longer supported in this release. They will simply be | ||
removed from the output. | ||
1.4.0 | ||
----- | ||
The license has been switched from ISC to [Unlicense](http://unlicense.org). | ||
@@ -5,0 +17,0 @@ |
74
test.js
@@ -27,3 +27,3 @@ var assert = require('assert'), | ||
// test that tags are lowercased | ||
// test that tag is lowercased | ||
cleaner.clean('<A HREF="http://foo">bar</A>', function (html) { | ||
@@ -33,12 +33,2 @@ assert.equal(html, '<a href="http://foo">bar</a>'); | ||
// test that script tags are unchanged | ||
cleaner.clean('<script type="text/javascript">console.log("foo");</script>', function (html) { | ||
assert.equal(html, '<script type="text/javascript">console.log("foo");</script>') | ||
}); | ||
// test that style tags are unchanged | ||
cleaner.clean('<style>a { color: red; }</style>', function (html) { | ||
assert.equal(html, '<style>a { color: red; }</style>') | ||
}); | ||
// test that line breaks are not added around comments when break-around-comments is false | ||
@@ -62,7 +52,7 @@ cleaner.clean('foo<!-- bar -->qux', {'break-around-comments': false}, function (html) { | ||
// test that attributes are not removed when not included in remove-attributes | ||
// test that attribute is not removed when not included in remove-attributes | ||
cleaner.clean('<span color="red">foo</span>', {'remove-attributes': []}, function (html) { | ||
assert.equal(html, '<span color="red">foo</span>'); | ||
}); | ||
// test that attributes are removed when included in remove-attributes | ||
// test that attribute is removed when included in remove-attributes | ||
cleaner.clean('<span color="red">foo</span>', {'remove-attributes': ['color']}, function (html) { | ||
@@ -72,7 +62,7 @@ assert.equal(html, '<span>foo</span>'); | ||
// test that comments are not removed when remove-comments is false | ||
// test that comment is not removed when remove-comments is false | ||
cleaner.clean('<!-- foo -->', {'remove-comments': false}, function (html) { | ||
assert.equal(html, '<!-- foo -->'); | ||
}); | ||
// test that comments are removed when remove-comments is true | ||
// test that comment is removed when remove-comments is true | ||
cleaner.clean('<!-- foo -->', {'remove-comments': true}, function (html) { | ||
@@ -82,7 +72,7 @@ assert.equal(html, ''); | ||
// test that empty tags are not removed when not included in remove-empty-tags | ||
// test that empty tag is not removed when not included in remove-empty-tags | ||
cleaner.clean('<p></p>', {'remove-empty-tags': []}, function (html) { | ||
assert.equal(html, '<p></p>'); | ||
}); | ||
// test that empty tags are removed when included in remove-empty-tags | ||
// test that empty tag is removed when included in remove-empty-tags | ||
cleaner.clean('<p></p>', {'remove-empty-tags': ['p']}, function (html) { | ||
@@ -92,7 +82,7 @@ assert.equal(html, ''); | ||
// test that tags are not removed when not included in remove-tags | ||
// test that tag is not removed when not included in remove-tags | ||
cleaner.clean('<font face="arial">foo</font>', {'remove-tags': []}, function (html) { | ||
assert.equal(html, '<font face="arial">foo</font>'); | ||
}); | ||
// test that tags are removed when included in remove-tags | ||
// test that tag is removed and child is preserved when included in remove-tags | ||
cleaner.clean('<font face="arial">foo</font>', {'remove-tags': ['font']}, function (html) { | ||
@@ -102,2 +92,7 @@ assert.equal(html, 'foo'); | ||
// test that unsupported tags are removed | ||
cleaner.clean('<script>foo</script>\n<style>bar</style>', function (html) { | ||
assert.equal(html, ''); | ||
}); | ||
// test that non-breaking space is not replaced by a single space when replace-nbsp is false | ||
@@ -118,2 +113,3 @@ cleaner.clean('Foo Bar', {'replace-nbsp': false}, function (html) { | ||
}); | ||
// test that indent is not added when child is comment and break-around-comments is false | ||
@@ -123,2 +119,3 @@ cleaner.clean('foo<span><!-- bar --></span>qux', {'break-around-comments': false, 'indent': ' '}, function (html) { | ||
}); | ||
// test that indent is added when child is comment and break-around-comments is true | ||
@@ -128,6 +125,8 @@ cleaner.clean('foo<span><!-- bar --></span>qux', {'break-around-comments': true, 'indent': ' '}, function (html) { | ||
}); | ||
// test that indent is not added when child tag is not included in break-around-tags | ||
cleaner.clean('foo<span><span>bar</span></span>qux', {'indent': ' '}, function (html) { | ||
assert.equal(html, 'foo<span><span>bar</span></span>qux'); | ||
cleaner.clean('foo<span><div>bar</div></span>qux', {'break-around-tags': [], 'indent': ' '}, function (html) { | ||
assert.equal(html, 'foo<span><div>bar</div></span>qux'); | ||
}); | ||
// test that indent is added when child tag is included in break-around-tags | ||
@@ -137,2 +136,3 @@ cleaner.clean('foo<span><div>bar</div></span>qux', {'break-around-tags': ['div'], 'indent': ' '}, function (html) { | ||
}); | ||
// test that indent is added when child tag is not included in break-around-tags but descendant is | ||
@@ -143,2 +143,34 @@ cleaner.clean('foo<span><span><div>bar</div></span></span>qux', {'break-around-tags': ['div'], 'indent': ' '}, function (html) { | ||
// test that indent is not added inside comment | ||
cleaner.clean('<!-- foo<span><div>bar</div></span>qux -->', {'break-around-tags': ['div'], 'indent': ' '}, function (html) { | ||
assert.equal(html, '<!-- foo<span><div>bar</div></span>qux -->'); | ||
}); | ||
// test that indent is not added inside multiline comment | ||
cleaner.clean('<!--\nfoo<span><div>bar</div></span>qux\n-->', {'break-around-tags': ['div'], 'indent': ' '}, function (html) { | ||
assert.equal(html, '<!--\nfoo<span><div>bar</div></span>qux\n-->'); | ||
}); | ||
// test that indent is not added after comment | ||
cleaner.clean('<!--[if IE 7]><div><![endif]--><div>foo</div>', {'break-around-tags': ['div'], 'indent': ' '}, function (html) { | ||
assert.equal(html, '<!--[if IE 7]><div><![endif]-->\n<div>foo</div>'); | ||
}); | ||
// test that indent is not added after multiline comment | ||
cleaner.clean('<!--[if IE 7]>\n<div>\n<![endif]--><div>foo</div>', {'break-around-tags': ['div'], 'indent': ' '}, function (html) { | ||
assert.equal(html, '<!--[if IE 7]>\n<div>\n<![endif]-->\n<div>foo</div>'); | ||
}); | ||
// wrap tests | ||
// test that long line is wrapped and indented | ||
cleaner.clean('<div><div>I prefer the concrete, the graspable, the proveable.</div></div>', {'wrap': 40}, function (html) { | ||
assert.equal(html, '<div>\n <div>I prefer the concrete, the\n graspable, the proveable.</div>\n</div>'); | ||
}); | ||
// test that long line without whitespace is indented but not wrapped | ||
cleaner.clean('<div><div>Iprefertheconcrete,thegraspable,theproveable.</div></div>', {'wrap': 40}, function (html) { | ||
assert.equal(html, '<div>\n <div>Iprefertheconcrete,thegraspable,theproveable.</div>\n</div>'); | ||
}); | ||
// end to end test | ||
@@ -145,0 +177,0 @@ var input = `<table width="100%" border="0" cellspacing="0" cellpadding="0"> |
32298
8
584