Skip to content

Commit

Permalink
beautify-html,css: stricter jshint tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
einars committed Mar 18, 2013
1 parent c383666 commit ed0fa77
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 46 deletions.
77 changes: 46 additions & 31 deletions beautify-css.js
@@ -1,3 +1,4 @@
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
/*
CSS Beautifier
Expand Down Expand Up @@ -35,8 +36,9 @@ function css_beautify(source_text, options) {
var indentCharacter = options.indent_char || ' ';

// compatibility
if (typeof indentSize == "string")
if (typeof indentSize === "string") {
indentSize = parseInt(indentSize, 10);
}


// tokenizer
Expand All @@ -54,12 +56,12 @@ function css_beautify(source_text, options) {
function eatString(comma) {
var start = pos;
while(next()){
if (ch=="\\"){
if (ch === "\\"){
next();
next();
} else if (ch == comma) {
} else if (ch === comma) {
break;
} else if (ch == "\n") {
} else if (ch === "\n") {
break;
}
}
Expand All @@ -68,23 +70,24 @@ function css_beautify(source_text, options) {

function eatWhitespace() {
var start = pos;
while (whiteRe.test(peek()))
while (whiteRe.test(peek())) {
pos++;
return pos != start;
}
return pos !== start;
}

function skipWhitespace() {
var start = pos;
do{
}while (whiteRe.test(next()));
return pos != start + 1;
return pos !== start + 1;
}

function eatComment() {
var start = pos;
next();
while (next()) {
if (ch == "*" && peek() == "/") {
if (ch === "*" && peek() === "/") {
pos ++;
break;
}
Expand All @@ -95,7 +98,7 @@ function css_beautify(source_text, options) {


function lookBack(str) {
return source_text.substring(pos-str.length, pos).toLowerCase() == str;
return source_text.substring(pos-str.length, pos).toLowerCase() === str;
}

// printer
Expand Down Expand Up @@ -124,73 +127,84 @@ function css_beautify(source_text, options) {
};

print.newLine = function(keepWhitespace) {
if (!keepWhitespace)
while (whiteRe.test(output[output.length - 1]))
if (!keepWhitespace) {
while (whiteRe.test(output[output.length - 1])) {
output.pop();
}
}

if (output.length)
if (output.length) {
output.push('\n');
if (indentString)
}
if (indentString) {
output.push(indentString);
}
};
print.singleSpace = function() {
if (output.length && !whiteRe.test(output[output.length - 1]))
if (output.length && !whiteRe.test(output[output.length - 1])) {
output.push(' ');
}
};
var output = [];
if (indentString)
if (indentString) {
output.push(indentString);
}
/*_____________________--------------------_____________________*/

while(true) {
var isAfterSpace = skipWhitespace();

if (!ch)
if (!ch) {
break;
}


if (ch == '{') {
if (ch === '{') {
indent();
print["{"](ch);
} else if (ch == '}') {
} else if (ch === '}') {
outdent();
print["}"](ch);
} else if (ch == '"' || ch == '\'') {
} else if (ch === '"' || ch === '\'') {
output.push(eatString(ch));
} else if (ch == ';') {
} else if (ch === ';') {
output.push(ch, '\n', indentString);
} else if (ch == '/' && peek() == '*') { // comment
} else if (ch === '/' && peek() === '*') { // comment
print.newLine();
output.push(eatComment(), "\n", indentString);
} else if (ch == '(') { // may be a url
} else if (ch === '(') { // may be a url
if (lookBack("url")) {
output.push(ch);
eatWhitespace();
if (next()) {
if (ch != ')' && ch != '"' && ch != '\'')
if (ch !== ')' && ch !== '"' && ch !== '\'') {
output.push(eatString(')'));
else
} else {
pos--;
}
}
} else {
if (isAfterSpace)
if (isAfterSpace) {
print.singleSpace();
}
output.push(ch);
eatWhitespace();
}
} else if (ch == ')') {
} else if (ch === ')') {
output.push(ch);
} else if (ch == ',') {
} else if (ch === ',') {
eatWhitespace();
output.push(ch);
print.singleSpace();
} else if (ch == ']') {
} else if (ch === ']') {
output.push(ch);
} else if (ch == '[' || ch == '=') { // no whitespace before or after
} else if (ch === '[' || ch === '=') { // no whitespace before or after
eatWhitespace();
output.push(ch);
} else {
if (isAfterSpace)
if (isAfterSpace) {
print.singleSpace();
}

output.push(ch);
}
Expand All @@ -202,5 +216,6 @@ function css_beautify(source_text, options) {
}


if (exports !== undefined)
if (exports !== undefined) {
exports.css_beautify = css_beautify;
}
33 changes: 18 additions & 15 deletions beautify-html.js
@@ -1,3 +1,4 @@
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
/*
Style HTML
Expand Down Expand Up @@ -122,7 +123,7 @@ function style_html(html_source, options) {
};

this.get_contents_to = function (name) { //get the full content of a script or style to pass to js_beautify
if (this.pos == this.input.length) {
if (this.pos === this.input.length) {
return ['', 'TK_EOF'];
}
var input_char = '';
Expand Down Expand Up @@ -166,7 +167,7 @@ function style_html(html_source, options) {
}
delete this.tags[tag + this.tags[tag + 'count'] + 'parent']; //delete the closed tags parent reference...
delete this.tags[tag + this.tags[tag + 'count']]; //...and the tag itself
if (this.tags[tag + 'count'] == 1) {
if (this.tags[tag + 'count'] === 1) {
delete this.tags[tag + 'count'];
}
else {
Expand Down Expand Up @@ -236,7 +237,7 @@ function style_html(html_source, options) {

var tag_complete = content.join('');
var tag_index;
if (tag_complete.indexOf(' ') != -1) { //if there's whitespace, thats where the tag name ends
if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends
tag_index = tag_complete.indexOf(' ');
}
else { //otherwise go with the tag ending
Expand Down Expand Up @@ -275,20 +276,20 @@ function style_html(html_source, options) {
this.tag_type = 'SINGLE';
}
else if (tag_check.charAt(0) === '!') { //peek for <!-- comment
if (tag_check.indexOf('[if') != -1) { //peek for <!--[if conditional comment
if (tag_complete.indexOf('!IE') != -1) { //this type needs a closing --> so...
if (tag_check.indexOf('[if') !== -1) { //peek for <!--[if conditional comment
if (tag_complete.indexOf('!IE') !== -1) { //this type needs a closing --> so...
comment = this.get_unformatted('-->', tag_complete); //...delegate to get_unformatted
content.push(comment);
}
if ( ! peek) {
this.tag_type = 'START';
}
}
else if (tag_check.indexOf('[endif') != -1) {//peek for <!--[endif end conditional comment
else if (tag_check.indexOf('[endif') !== -1) {//peek for <!--[endif end conditional comment
this.tag_type = 'END';
this.unindent();
}
else if (tag_check.indexOf('[cdata[') != -1) { //if it's a <[cdata[ comment...
else if (tag_check.indexOf('[cdata[') !== -1) { //if it's a <[cdata[ comment...
comment = this.get_unformatted(']]>', tag_complete); //...delegate to get_unformatted function
content.push(comment);
if ( ! peek) {
Expand Down Expand Up @@ -325,7 +326,7 @@ function style_html(html_source, options) {

this.get_unformatted = function (delimiter, orig_tag) { //function to return unformatted content in its entirety

if (orig_tag && orig_tag.toLowerCase().indexOf(delimiter) != -1) {
if (orig_tag && orig_tag.toLowerCase().indexOf(delimiter) !== -1) {
return '';
}
var input_char = '';
Expand Down Expand Up @@ -362,7 +363,7 @@ function style_html(html_source, options) {
space = true;


} while (content.toLowerCase().indexOf(delimiter) == -1);
} while (content.toLowerCase().indexOf(delimiter) === -1);
return content;
};

Expand Down Expand Up @@ -401,8 +402,9 @@ function style_html(html_source, options) {

this.get_full_indent = function (level) {
level = this.indent_level + level || 0;
if (level < 1)
if (level < 1) {
return '';
}

return Array(level + 1).join(this.indent_string);
};
Expand Down Expand Up @@ -508,8 +510,9 @@ function style_html(html_source, options) {
if (multi_parser.last_token === 'TK_CONTENT' && multi_parser.last_text === '') {
var tag_name = multi_parser.token_text.match(/\w+/)[0];
var tag_extracted_from_last_output = multi_parser.output[multi_parser.output.length -1].match(/<\s*(\w+)/);
if (tag_extracted_from_last_output === null || tag_extracted_from_last_output[1] !== tag_name)
if (tag_extracted_from_last_output === null || tag_extracted_from_last_output[1] !== tag_name) {
multi_parser.print_newline(true, multi_parser.output);
}
}
multi_parser.print_token(multi_parser.token_text);
multi_parser.current_mode = 'CONTENT';
Expand All @@ -536,15 +539,15 @@ function style_html(html_source, options) {
var text = multi_parser.token_text,
_beautifier,
script_indent_level = 1;
if (multi_parser.token_type == 'TK_SCRIPT') {
if (multi_parser.token_type === 'TK_SCRIPT') {
_beautifier = typeof js_beautify === 'function' && js_beautify;
} else if (multi_parser.token_type == 'TK_STYLE') {
} else if (multi_parser.token_type === 'TK_STYLE') {
_beautifier = typeof css_beautify === 'function' && css_beautify;
}

if (options.indent_scripts == "keep") {
if (options.indent_scripts === "keep") {
script_indent_level = 0;
} else if (options.indent_scripts == "separate") {
} else if (options.indent_scripts === "separate") {
script_indent_level = -multi_parser.indent_level;
}

Expand Down

0 comments on commit ed0fa77

Please sign in to comment.