Skip to content

Commit

Permalink
HtmlBeautify() adding extra newline (relate #36)
Browse files Browse the repository at this point in the history
Fix print function in plugin/beautify.js
  • Loading branch information
maksimr committed Dec 30, 2013
1 parent f436633 commit f3484e8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ module.exports = function(grunt) {
// Default task.
grunt.registerTask('test', ['nodeunit:all', 'urchin']);
grunt.registerTask('default', ['nodeunit:all', 'urchin']);
grunt.registerTask('build', ['jshint', 'urchin', 'nodeunit', 'uglify']);
grunt.registerTask('build', ['jshint', 'nodeunit:all', 'urchin', 'uglify']);
};
13 changes: 10 additions & 3 deletions plugin/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
load = global.load,
read = global.read,
hop = Object.prototype.hasOwnProperty,
print = global.print,
print = null,
hasCache = null,

objectPrototypeToString = Object.prototype.toString,
Expand Down Expand Up @@ -41,6 +41,7 @@
has.add('host-v8', isFunction(global.load) && isFunction(global.read));

if (has('host-v8')) {
print = global.write;
global.window = global;
// get rootPtah
// Need for html-beautify
Expand Down Expand Up @@ -73,7 +74,7 @@
}
};

print = global.console.log;
print = process.stdout.write.bind(process.stdout);
read = function(path) {
return fs.readFileSync(path, 'utf-8');
};
Expand All @@ -99,7 +100,13 @@
load(global.rootPtah+'beautify-css.js');
}

print(global.beautify(content, options).replace(/\n+$/g, ''));
//FIXME(maksimrv): Hm.... Why?! only for html :|
if (global.html_beautify) {
print(global.beautify(content, options).replace(/\n$/g, ''));
return;
}

print(global.beautify(content, options));

}(contentPath, options, path));
}).apply(this, (typeof process === 'object' && process.argv.splice(3)) || arguments);
2 changes: 1 addition & 1 deletion plugin/beautify.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions test/javascript/beautify_all_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
system(command, function(err, stdout, stderr) {
stdout = err || stderr || stdout;
test.equal(stdout, '(["foo", "bar"]).each(function(i) {\n return i;\n});\n', 'should be formatted string.');
test.equal(stdout, '(["foo", "bar"]).each(function(i) {\n return i;\n});', 'should be formatted string.');
test.done();
});
},
Expand All @@ -48,7 +48,7 @@
*/
system(command, function(err, stdout, stderr) {
stdout = err || stderr || stdout;
test.equal(stdout, '(["foo", "bar"]).each(function(i) {\n\t\treturn i;\n});\n', 'should be formatted string with tab.');
test.equal(stdout, '(["foo", "bar"]).each(function(i) {\n\t\treturn i;\n});', 'should be formatted string with tab.');
test.done();
});
},
Expand All @@ -64,7 +64,7 @@
*/
system(command, function(err, stdout, stderr) {
stdout = err || stderr || stdout;
test.equal(stdout, '<div>foo\n <div></div>\n</div>\n', 'should be formatted string.');
test.equal(stdout, '<div>foo\n <div></div>\n</div>', 'should be formatted string.');
test.done();
});
},
Expand All @@ -82,7 +82,23 @@
*/
system(command, function(err, stdout, stderr) {
stdout = err || stderr || stdout;
test.equal(stdout, '<div>foo\n\t\t<div></div>\n</div>\n', 'should be formatted string with tab.');
test.equal(stdout, '<div>foo\n\t\t<div></div>\n</div>', 'should be formatted string with tab.');
test.done();
});
},
'HtmlBeautify() adding extra newline (issue 36)': function(test) {
var contentPath = 'test/javascript/templates/issue_36.html',
command = this.command;

test.expect(1);
command = print(command, conf.plugin, contentPath, {}, conf.beautify.html_path);

/**
* Should simple format file.
*/
system(command, function(err, stdout, stderr) {
stdout = err || stderr || stdout;
test.equal(stdout, '<div>foo\n <div></div>\n</div>\n\n', 'should save additional newline.');
test.done();
});
},
Expand All @@ -98,7 +114,7 @@
*/
system(command, function(err, stdout, stderr) {
stdout = err || stderr || stdout;
test.equal(stdout, '.foo {\n padding: 0;\n}\n', 'should be formatted string.');
test.equal(stdout, '.foo {\n padding: 0;\n}', 'should be formatted string.');
test.done();
});
},
Expand All @@ -116,7 +132,7 @@
*/
system(command, function(err, stdout, stderr) {
stdout = err || stderr || stdout;
test.equal(stdout, '.foo {\n\t\tpadding: 0;\n}\n', 'should be formatted string with tab.');
test.equal(stdout, '.foo {\n\t\tpadding: 0;\n}', 'should be formatted string with tab.');
test.done();
});
}
Expand Down
5 changes: 5 additions & 0 deletions test/javascript/templates/issue_36.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>foo
<div></div>
</div>


0 comments on commit f3484e8

Please sign in to comment.