Permalink
Browse files

HtmlBeautify() adding extra newline (relate #36)

Fix print function in plugin/beautify.js
  • Loading branch information...
1 parent f436633 commit f3484e832f2cc1cab0a0c50d37683117ef8afdee @maksimr committed Dec 30, 2013
Showing with 39 additions and 11 deletions.
  1. +1 −1 Gruntfile.js
  2. +10 −3 plugin/beautify.js
  3. +1 −1 plugin/beautify.min.js
  4. +22 −6 test/javascript/beautify_all_test.js
  5. +5 −0 test/javascript/templates/issue_36.html
View
@@ -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']);
};
View
@@ -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,
@@ -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
@@ -73,7 +74,7 @@
}
};
- print = global.console.log;
+ print = process.stdout.write.bind(process.stdout);
read = function(path) {
return fs.readFileSync(path, 'utf-8');
};
@@ -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);
@@ -1 +1 @@
-(function(contentPath,options,path){"use strict";var global=function(){return this||eval.call(null,"this")}(),load=global.load,read=global.read,hop=Object.prototype.hasOwnProperty,print=global.print,hasCache=null,objectPrototypeToString=Object.prototype.toString,isFunction=function(it){return objectPrototypeToString.call(it)==="[object Function]"},isUndefined=function(it){return typeof it==="undefined"},has=function(name){hasCache[name]=isFunction(hasCache[name])?hasCache[name](global):hasCache[name];return hasCache[name]};hasCache=has.cache={};has.add=function(name,test,now,force){if(isUndefined(hasCache[name])||force){hasCache[name]=test}return now&&has(name)};has.add("host-node",global.process&&/node(\.exe||js)?$/.test(global.process.execPath));has.add("host-v8",isFunction(global.load)&&isFunction(global.read));if(has("host-v8")){global.window=global;global.rootPtah=path.replace(/[\w-.]+.js$/,"")}if(has("host-node")){(function(){var fs=require("fs");load=function(path){var context={},property;if(path.charAt(0)!=="/"&&path.charAt(1)!==":"){path=global.process.cwd()+"/"+path}context=require(path);for(property in context){if(hop.call(context,property)){global[property]=context[property]}}};print=global.console.log;read=function(path){return fs.readFileSync(path,"utf-8")}})()}(function(contentPath,options,path){var defOps={indent_size:4,indent_char:" "},content=read(contentPath);options=options&&JSON.parse(options)||defOps;load(path);global.beautify=global.js_beautify||global.html_beautify||global.css_beautify;if(has("host-v8")&&global.html_beautify){load(global.rootPtah+"beautify.js");load(global.rootPtah+"beautify-css.js")}print(global.beautify(content,options).replace(/\n+$/g,""))})(contentPath,options,path)}).apply(this,typeof process==="object"&&process.argv.splice(3)||arguments);
+(function(contentPath,options,path){"use strict";var global=function(){return this||eval.call(null,"this")}(),load=global.load,read=global.read,hop=Object.prototype.hasOwnProperty,print=null,hasCache=null,objectPrototypeToString=Object.prototype.toString,isFunction=function(it){return objectPrototypeToString.call(it)==="[object Function]"},isUndefined=function(it){return typeof it==="undefined"},has=function(name){hasCache[name]=isFunction(hasCache[name])?hasCache[name](global):hasCache[name];return hasCache[name]};hasCache=has.cache={};has.add=function(name,test,now,force){if(isUndefined(hasCache[name])||force){hasCache[name]=test}return now&&has(name)};has.add("host-node",global.process&&/node(\.exe||js)?$/.test(global.process.execPath));has.add("host-v8",isFunction(global.load)&&isFunction(global.read));if(has("host-v8")){print=global.write;global.window=global;global.rootPtah=path.replace(/[\w-.]+.js$/,"")}if(has("host-node")){(function(){var fs=require("fs");load=function(path){var context={},property;if(path.charAt(0)!=="/"&&path.charAt(1)!==":"){path=global.process.cwd()+"/"+path}context=require(path);for(property in context){if(hop.call(context,property)){global[property]=context[property]}}};print=process.stdout.write.bind(process.stdout);read=function(path){return fs.readFileSync(path,"utf-8")}})()}(function(contentPath,options,path){var defOps={indent_size:4,indent_char:" "},content=read(contentPath);options=options&&JSON.parse(options)||defOps;load(path);global.beautify=global.js_beautify||global.html_beautify||global.css_beautify;if(has("host-v8")&&global.html_beautify){load(global.rootPtah+"beautify.js");load(global.rootPtah+"beautify-css.js")}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);
@@ -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();
});
},
@@ -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();
});
},
@@ -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();
});
},
@@ -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();
});
},
@@ -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();
});
},
@@ -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();
});
}
@@ -0,0 +1,5 @@
+<div>foo
+ <div></div>
+</div>
+
+

0 comments on commit f3484e8

Please sign in to comment.