Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-beautify

Package Overview
Dependencies
Maintainers
2
Versions
128
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-beautify - npm Package Compare versions

Comparing version 1.5.4 to 1.5.5

js/test/beautify-css-tests.js

29

CHANGELOG.md
# Changelog
## v1.5.5
### Description
* Initial implementation of comma-first formatting - Diff-friendly literals!
* CSS: Add newline between rules
* LESS: improved function parameter formatting
* HTML: options for wrapping attributes
* General bug fixing
### Closed Issues
* Add GUI support for `--indent-inner-html`. ([#633](https://github.com/beautify-web/js-beautify/pull/633))
* Publish v1.5.5 ([#629](https://github.com/beautify-web/js-beautify/issues/629))
* CSS: Updating the documentation for the 'newline_between_rules' ([#615](https://github.com/beautify-web/js-beautify/pull/615))
* Equal Sign Removed from Filter Properties Alpha Opacity Assignment ([#599](https://github.com/beautify-web/js-beautify/issues/599))
* Keep trailing spaces on comments ([#598](https://github.com/beautify-web/js-beautify/issues/598))
* only print the file names of changed files ([#597](https://github.com/beautify-web/js-beautify/issues/597))
* CSS: support add newline between rules ([#574](https://github.com/beautify-web/js-beautify/pull/574))
* elem[array]++ changes to elem[array] ++ inserting unnecessary gap ([#570](https://github.com/beautify-web/js-beautify/issues/570))
* add support to less functions paramters braces ([#568](https://github.com/beautify-web/js-beautify/pull/568))
* yield statements are being beautified to their own newlines since 1.5.2 ([#560](https://github.com/beautify-web/js-beautify/issues/560))
* HTML beautifier inserts extra newline into <li>s ending with <code> ([#524](https://github.com/beautify-web/js-beautify/issues/524))
* Add wrap_attributes option ([#476](https://github.com/beautify-web/js-beautify/issues/476))
* Add or preserve empty line between CSS rules ([#467](https://github.com/beautify-web/js-beautify/issues/467))
* Support comma first style of variable declaration ([#245](https://github.com/beautify-web/js-beautify/issues/245))
## v1.5.4

@@ -12,2 +38,3 @@

* TypeScript oddly formatted with 1.5.3 ([#552](https://github.com/beautify-web/js-beautify/issues/552))
* HTML beautifier inserts double spaces between adjacent tags ([#525](https://github.com/beautify-web/js-beautify/issues/525))
* Keep space in font rule ([#491](https://github.com/beautify-web/js-beautify/issues/491))

@@ -77,3 +104,3 @@ * [Brackets plug in] Space after </a> disappears ([#454](https://github.com/beautify-web/js-beautify/issues/454))

* "--n" and "++n" are not indented like "n--" and "n++" are... ([#495](https://github.com/beautify-web/js-beautify/issues/495))
* Allow <style> and <script> tags to be unformatted ([#494](https://github.com/beautify-web/js-beautify/pull/494))
* Allow `<style>` and `<script>` tags to be unformatted ([#494](https://github.com/beautify-web/js-beautify/pull/494))
* Preserve new line at end of file ([#492](https://github.com/beautify-web/js-beautify/issues/492))

@@ -80,0 +107,0 @@ * Line wraps breaking numbers (causes syntax error) ([#488](https://github.com/beautify-web/js-beautify/issues/488))

40

js/lib/beautify-css.js

@@ -47,2 +47,3 @@ /*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */

end_with_newline (false) - end with a newline
newline_between_rules (true) - add a new line after every css rule

@@ -56,2 +57,3 @@ e.g

'end_with_newline': false,
'newline_between_rules': true
});

@@ -70,2 +72,3 @@ */

var end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline;
var newline_between_rules = (options.newline_between_rules === undefined) ? true : options.newline_between_rules;

@@ -84,2 +87,3 @@ // compatibility

ch;
var parenLevel = 0;

@@ -127,3 +131,3 @@ function next() {

while (whiteRe.test(peek())) {
next()
next();
result += ch;

@@ -140,3 +144,3 @@ }

while (whiteRe.test(next())) {
result += ch
result += ch;
}

@@ -148,3 +152,3 @@ return result;

var start = pos;
var singleLine = peek() === "/";
singleLine = peek() === "/";
next();

@@ -173,5 +177,5 @@ while (next()) {

function foundNestedPseudoClass() {
for (var i = pos + 1; i < source_text.length; i++){
for (var i = pos + 1; i < source_text.length; i++) {
var ch = source_text.charAt(i);
if (ch === "{"){
if (ch === "{") {
return true;

@@ -241,3 +245,3 @@ } else if (ch === ";" || ch === "}" || ch === ")") {

var output = [];

@@ -258,4 +262,4 @@ if (basebaseIndentString) {

var isAfterNewline = whitespace.indexOf('\n') !== -1;
var last_top_ch = top_ch;
var top_ch = ch;
last_top_ch = top_ch;
top_ch = ch;

@@ -295,3 +299,3 @@ if (!ch) {

}
} else if (': '.indexOf(variableOrRule[variableOrRule.length -1]) >= 0) {
} else if (': '.indexOf(variableOrRule[variableOrRule.length - 1]) >= 0) {
//we have a variable, add it and insert one space before continuing

@@ -309,2 +313,6 @@ next();

output.push("{}");
print.newLine();
if (newline_between_rules && indentLevel === 0) {
print.newLine(true);
}
} else {

@@ -329,6 +337,9 @@ indent();

}
if (newline_between_rules && indentLevel === 0) {
print.newLine(true);
}
} else if (ch === ":") {
eatWhitespace();
if ((insideRule || enteringConditionalGroup) &&
!(lookBack("&") || foundNestedPseudoClass())) {
if ((insideRule || enteringConditionalGroup) &&
!(lookBack("&") || foundNestedPseudoClass())) {
// 'property: value' delimiter

@@ -370,2 +381,3 @@ // which could be in a conditional group query

} else {
parenLevel++;
if (isAfterSpace) {

@@ -379,6 +391,7 @@ print.singleSpace();

output.push(ch);
parenLevel--;
} else if (ch === ',') {
output.push(ch);
eatWhitespace();
if (!insideRule && selectorSeparatorNewline) {
if (!insideRule && selectorSeparatorNewline && parenLevel < 1) {
print.newLine();

@@ -396,3 +409,4 @@ } else {

} else if (ch === '=') { // no whitespace before or after
eatWhitespace();
eatWhitespace()
ch = '=';
output.push(ch);

@@ -399,0 +413,0 @@ } else {

@@ -47,4 +47,4 @@ /*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */

wrap_line_length (default 250) - maximum amount of characters per line (0 = disable)
brace_style (default "collapse") - "collapse" | "expand" | "end-expand"
put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line.
brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none"
put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line, or attempt to keep them where they are.
unformatted (defaults to inline tags) - list of tags, that shouldn't be reformatted

@@ -101,2 +101,4 @@ indent_scripts (default normal) - "keep"|"separate"|"normal"

indent_handlebars,
wrap_attributes,
wrap_attributes_indent_size,
end_with_newline;

@@ -123,2 +125,4 @@

indent_handlebars = (options.indent_handlebars === undefined) ? false : options.indent_handlebars;
wrap_attributes = (options.wrap_attributes === undefined) ? 'auto' : options.wrap_attributes;
wrap_attributes_indent_size = (options.wrap_attributes_indent_size === undefined) ? indent_size : parseInt(options.wrap_attributes_indent_size, 10) || indent_size;
end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline;

@@ -164,3 +168,3 @@

return true;
}
};

@@ -311,2 +315,3 @@ this.traverse_whitespace = function() {

space = false,
first_attr = true,
tag_start, tag_end,

@@ -350,2 +355,15 @@ tag_start_char,

space = false;
if (!first_attr && wrap_attributes === 'force' && input_char !== '/') {
this.print_newline(true, content);
this.print_indentation(content);
for (var count = 0; count < wrap_attributes_indent_size; count++) {
content.push(indent_character);
}
}
for (var i = 0; i < content.length; i++) {
if (content[i] === ' ') {
first_attr = false;
break;
}
}
}

@@ -371,3 +389,3 @@

if (indent_handlebars && !tag_start_char) {
if (content.length >= 2 && content[content.length - 1] === '{' && content[content.length - 2] == '{') {
if (content.length >= 2 && content[content.length - 1] === '{' && content[content.length - 2] === '{') {
if (input_char === '#' || input_char === '/') {

@@ -769,3 +787,3 @@ tag_start = this.pos - 3;

if (tag_extracted_from_last_output === null ||
tag_extracted_from_last_output[1] !== tag_name) {
(tag_extracted_from_last_output[1] !== tag_name && !multi_parser.Utils.in_array(tag_extracted_from_last_output[1], unformatted))) {
multi_parser.print_newline(false, multi_parser.output);

@@ -772,0 +790,0 @@ }

@@ -58,3 +58,3 @@ #!/usr/bin/env node

// TODO: expand-strict is obsolete, now identical to expand. Remove in future version
"brace_style": ["collapse", "expand", "end-expand", "expand-strict"],
"brace_style": ["collapse", "expand", "end-expand", "expand-strict", "none"],
"break_chained_methods": Boolean,

@@ -64,4 +64,10 @@ "keep_array_indentation": Boolean,

"wrap_line_length": Number,
"wrap_attributes": ["auto", "force"],
"wrap_attributes_indent_size": Number,
"e4x": Boolean,
"end_with_newline": Boolean,
"comma_first": Boolean,
// CSS-only
"selector_separator_newline": Boolean,
"newline_between_rules": Boolean,
// HTML-only

@@ -103,3 +109,9 @@ "max_char": Number, // obsolete since 1.3.5

"n": ["--end_with_newline"],
"C": ["--comma_first"],
// CSS-only
"L": ["--selector_separator_newline"],
"N": ["--newline_between_rules"],
// HTML-only
"A": ["--wrap_attributes"],
"i": ["--wrap_attributes_indent_size"],
"W": ["--max_char"], // obsolete since 1.3.5

@@ -222,4 +234,4 @@ "U": ["--unformatted"],

msg.push(' -j, --jslint-happy Enable jslint-stricter mode');
msg.push(' -a, --space_after_anon_function Add a space before an anonymous function\'s parens, ie. function ()');
msg.push(' -b, --brace-style [collapse|expand|end-expand] ["collapse"]');
msg.push(' -a, --space-after-anon-function Add a space before an anonymous function\'s parens, ie. function ()');
msg.push(' -b, --brace-style [collapse|expand|end-expand|none] ["collapse"]');
msg.push(' -B, --break-chained-methods Break chained method calls across subsequent lines');

@@ -231,13 +243,19 @@ msg.push(' -k, --keep-array-indentation Preserve array indentation');

msg.push(' --good-stuff Warm the cockles of Crockford\'s heart');
msg.push(' -n, --end_with_newline End output with newline');
msg.push(' -n, --end-with-newline End output with newline');
msg.push(' -C, --comma-first Put commas at the beginning of new line instead of end');
break;
case "html":
msg.push(' -b, --brace-style [collapse|expand|end-expand] ["collapse"]');
msg.push(' -I, --indent-inner-html Indent body and head sections. Default is false.');
msg.push(' -S, --indent-scripts [keep|separate|normal] ["normal"]');
msg.push(' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]');
msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)');
msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]');
msg.push(' -U, --unformatted List of tags (defaults to inline) that should not be reformatted');
msg.push(' -b, --brace-style [collapse|expand|end-expand] ["collapse"]');
msg.push(' -I, --indent-inner-html Indent body and head sections. Default is false.');
msg.push(' -S, --indent-scripts [keep|separate|normal] ["normal"]');
msg.push(' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]');
msg.push(' -A, --wrap-attributes Wrap html tag attributes to new lines [auto|force] ["auto"]');
msg.push(' -i, --wrap-attributes-indent-size Indent wrapped tags to after N characters [indent-level]');
msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)');
msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]');
msg.push(' -U, --unformatted List of tags (defaults to inline) that should not be reformatted');
break;
case "css":
msg.push(' -L, --selector-separator-newline Add a newline between multiple selectors.')
msg.push(' -N, --newline-between-rules Add a newline between CSS rules.')
}

@@ -305,7 +323,11 @@

if (outfile) {
try {
fs.writeFileSync(outfile, pretty, 'utf8');
logToStdout('beautified ' + path.relative(process.cwd(), outfile), config);
} catch (ex) {
onOutputError(ex);
if (isFileDifferent(outfile, pretty)) {
try {
fs.writeFileSync(outfile, pretty, 'utf8');
logToStdout('beautified ' + path.relative(process.cwd(), outfile), config);
} catch (ex) {
onOutputError(ex);
}
} else {
logToStdout('beautified ' + path.relative(process.cwd(), outfile) + ' - unchanged', config);
}

@@ -317,2 +339,11 @@ } else {

function isFileDifferent(filePath, expected) {
try {
return fs.readFileSync(filePath, 'utf8') !== expected;
} catch (ex) {
// failing to read is the same as different
return true;
}
}
// workaround the fact that nopt.clean doesn't return the object passed in :P

@@ -319,0 +350,0 @@

@@ -7,3 +7,5 @@ /*global js_beautify: true */

Urlencoded = require('../lib/unpackers/urlencode_unpacker'),
run_beautifier_tests = require('./beautify-tests').run_beautifier_tests;
run_javascript_tests = require('./beautify-javascript-tests').run_javascript_tests,
run_css_tests = require('./beautify-css-tests').run_css_tests,
run_html_tests = require('./beautify-html-tests').run_html_tests;

@@ -17,7 +19,9 @@ requirejs.config({

function amd_beautifier_tests() {
console.log('Testing with node.js Require.js...');
function amd_beautifier_index_tests(name, test_runner) {
console.log('Testing ' + name + ' with node.js Require.js (index file)...');
var results = new SanityTest();
var beautify = requirejs('beautify/index');
var results = run_beautifier_tests(
new SanityTest(),
test_runner(
results,
Urlencoded,

@@ -27,7 +31,10 @@ beautify.js,

beautify.css);
console.log(results.results_raw());
if (results.get_exitcode() !== 0) {
return results;
}
return results;
}
function amd_beautifier_tests(name, test_runner) {
console.log('Testing ' + name + ' with node.js Require.js (separate file)...');
var results = new SanityTest();
var js_beautify = requirejs('beautify-lib/beautify'),

@@ -37,4 +44,4 @@ css_beautify = requirejs('beautify-lib/beautify-css'),

results = run_beautifier_tests(
new SanityTest(),
test_runner(
results,
Urlencoded,

@@ -44,2 +51,3 @@ js_beautify.js_beautify,

css_beautify.css_beautify);
console.log(results.results_raw());

@@ -49,6 +57,13 @@ return results;

if (require.main === module) {
process.exit(amd_beautifier_tests().results_raw());
process.exit(
amd_beautifier_tests('js-beautifier', run_javascript_tests).get_exitcode() +
amd_beautifier_index_tests('js-beautifier', run_javascript_tests).get_exitcode() +
amd_beautifier_tests('cs-beautifier', run_css_tests).get_exitcode() +
amd_beautifier_index_tests('css-beautifier', run_css_tests).get_exitcode() +
amd_beautifier_tests('html-beautifier', run_html_tests).get_exitcode() +
amd_beautifier_index_tests('html-beautifier', run_html_tests).get_exitcode()
);
}
exports.amd_beautifier_tests = amd_beautifier_tests;

@@ -6,8 +6,7 @@ /*global js_beautify: true */

SanityTest = require('./sanitytest'),
Benchmark = require('benchmark'),
Benchmark = require('benchmark'),
Urlencoded = require('../lib/unpackers/urlencode_unpacker'),
js_beautify = require('../index').js_beautify,
css_beautify = require('../index').css_beautify,
html_beautify = require('../index').html_beautify,
run_beautifier_tests = require('./beautify-tests').run_beautifier_tests;
html_beautify = require('../index').html_beautify;

@@ -21,3 +20,3 @@ function node_beautifier_tests() {

};
//warm-up

@@ -28,3 +27,3 @@ js_beautify(data, options);

var suite = new Benchmark.Suite;
suite.add("js-beautify (underscore)", function() {

@@ -31,0 +30,0 @@ js_beautify(data, options);

@@ -6,10 +6,18 @@ /*global js_beautify: true */

Urlencoded = require('../lib/unpackers/urlencode_unpacker'),
js_beautify = require('../index').js_beautify,
css_beautify = require('../index').css_beautify,
html_beautify = require('../index').html_beautify,
run_beautifier_tests = require('./beautify-tests').run_beautifier_tests;
run_javascript_tests = require('./beautify-javascript-tests').run_javascript_tests,
run_css_tests = require('./beautify-css-tests').run_css_tests,
run_html_tests = require('./beautify-html-tests').run_html_tests;
function node_beautifier_tests() {
console.log('Testing with node.js CommonJS...');
var results = run_beautifier_tests(new SanityTest(), Urlencoded, js_beautify, html_beautify, css_beautify);
function node_beautifier_legacy_tests(name, test_runner) {
console.log('Testing ' + name + ' with node.js CommonJS (legacy names)...');
var results = new SanityTest();
var beautify = require('../index');
test_runner(
results,
Urlencoded,
beautify.js_beautify,
beautify.html_beautify,
beautify.css_beautify);
console.log(results.results_raw());

@@ -19,6 +27,29 @@ return results;

function node_beautifier_tests(name, test_runner) {
console.log('Testing ' + name + ' with node.js CommonJS (new names)...');
var results = new SanityTest();
var beautify = require('../index');
test_runner(
results,
Urlencoded,
beautify.js,
beautify.html,
beautify.css);
console.log(results.results_raw());
return results;
}
if (require.main === module) {
process.exit(node_beautifier_tests().get_exitcode());
process.exit(
node_beautifier_tests('js-beautifier', run_javascript_tests).get_exitcode() +
node_beautifier_legacy_tests('js-beautifier', run_javascript_tests).get_exitcode() +
node_beautifier_tests('cs-beautifier', run_css_tests).get_exitcode() +
node_beautifier_legacy_tests('css-beautifier', run_css_tests).get_exitcode() +
node_beautifier_tests('html-beautifier', run_html_tests).get_exitcode() +
node_beautifier_legacy_tests('html-beautifier', run_html_tests).get_exitcode()
);
}
exports.node_beautifier_tests = node_beautifier_tests;

@@ -40,3 +40,3 @@ //

// proper array checking is a pain. i'll maybe do it later, compare strings representations instead
if ((result === expected_value) || (expected_value instanceof Array && result.join(', ') == expected_value.join(', '))) {
if ((result === expected_value) || (expected_value instanceof Array && result.join(', ') === expected_value.join(', '))) {
n_succeeded += 1;

@@ -100,3 +100,3 @@ } else {

for (var k in something) {
if (k == expected_index) {
if (k === expected_index) {
x.push(this.prettyprint(something[k], true));

@@ -103,0 +103,0 @@ expected_index += 1;

{
"name": "js-beautify",
"version": "1.5.4",
"version": "1.5.5",
"description": "jsbeautifier.org for node",

@@ -52,2 +52,3 @@ "main": "js/index.js",

"node-static": "~0.7.1",
"mustache": "~0.8.2",
"requirejs": "2.1.x",

@@ -54,0 +55,0 @@ "benchmark": "1.0.0"

# JS Beautifier
[![Build Status](https://secure.travis-ci.org/beautify-web/js-beautify.png?branch=master)](http://travis-ci.org/beautify-web/js-beautify)
[![NPM version](https://badge.fury.io/js/js-beautify.png)](http://badge.fury.io/js/js-beautify)
[![Build Status](https://img.shields.io/travis/beautify-web/js-beautify/master.svg)](http://travis-ci.org/beautify-web/js-beautify)
[![NPM version](https://img.shields.io/npm/v/js-beautify.svg)](https://www.npmjs.com/package/js-beautify)
[![Download stats](https://img.shields.io/npm/dm/js-beautify.svg)](https://www.npmjs.com/package/js-beautify)
This little beautifier will reformat and reindent bookmarklets, ugly

@@ -93,4 +95,4 @@ JavaScript, unpack scripts packed by Dean Edward’s popular packer,

-j, --jslint-happy Enable jslint-stricter mode
-a, --space_after_anon_function Add a space before an anonymous function's parens, ie. function ()
-b, --brace-style [collapse|expand|end-expand] ["collapse"]
-a, --space-after-anon-function Add a space before an anonymous function's parens, ie. function ()
-b, --brace-style [collapse|expand|end-expand|none] ["collapse"]
-B, --break-chained-methods Break chained method calls across subsequent lines

@@ -101,3 +103,4 @@ -k, --keep-array-indentation Preserve array indentation

-X, --e4x Pass E4X xml literals through untouched
-n, --end_with_newline End output with newline
-n, --end-with-newline End output with newline
-C, --comma-first Put commas at the beginning of new line instead of end
--good-stuff Warm the cockles of Crockford's heart

@@ -125,3 +128,5 @@ ```

"unescape_strings": false,
"wrap_line_length": 0
"wrap_line_length": 0,
"wrap_attributes": "auto",
"wrap_attributes_indent_size": 4
}

@@ -157,16 +162,20 @@ ```

CSS Beautifier Options:
-s, --indent-size Indentation size [4]
-c, --indent-char Indentation character [" "]
-s, --indent-size Indentation size [4]
-c, --indent-char Indentation character [" "]
-L, --selector-separator-newline Add a newline between multiple selectors
-N, --newline-between-rules Add a newline between CSS rules
HTML Beautifier Options:
-I, --indent-inner-html Indent <head> and <body> sections. Default is false.
-s, --indent-size Indentation size [4]
-c, --indent-char Indentation character [" "]
-b, --brace-style [collapse|expand|end-expand] ["collapse"]
-S, --indent-scripts [keep|separate|normal] ["normal"]
-w, --wrap-line-length Maximum characters per line (0 disables) [250]
-p, --preserve-newlines Preserve existing line-breaks (--no-preserve-newlines disables)
-m, --max-preserve-newlines Maximum number of line-breaks to be preserved in one chunk [10]
-U, --unformatted List of tags (defaults to inline) that should not be reformatted
-n, --end_with_newline End output with newline
-I, --indent-inner-html Indent <head> and <body> sections. Default is false.
-s, --indent-size Indentation size [4]
-c, --indent-char Indentation character [" "]
-b, --brace-style [collapse|expand|end-expand|none] ["collapse"]
-S, --indent-scripts [keep|separate|normal] ["normal"]
-w, --wrap-line-length Maximum characters per line (0 disables) [250]
-A, --wrap-attributes Wrap attributes to new lines [auto|force] ["auto"]
-i, --wrap-attributes-indent-size Indent wrapped attributes to after N characters [indent-size]
-p, --preserve-newlines Preserve existing line-breaks (--no-preserve-newlines disables)
-m, --max-preserve-newlines Maximum number of line-breaks to be preserved in one chunk [10]
-U, --unformatted List of tags (defaults to inline) that should not be reformatted
-n, --end-with-newline End output with newline
```

@@ -190,3 +199,2 @@

js-beautify@1.5.3
js-beautify@1.5.5

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc