Skip to content

Commit

Permalink
Handle null input strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed May 21, 2018
1 parent 35dc66a commit 12ecdf8
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 28 deletions.
3 changes: 1 addition & 2 deletions js/lib/beautify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,13 @@ var lineBreak = acorn.lineBreak;
var allLineBreaks = acorn.allLineBreaks;

function Beautifier(source_text, options) {
source_text = source_text || '';
options = options || {};

// Allow the setting of language/file-type specific options
// with inheritance of overall settings
options = mergeOpts(options, 'css');

source_text = source_text || '';

var indentSize = options.indent_size ? parseInt(options.indent_size, 10) : 4;
var indentCharacter = options.indent_char || ' ';
var preserve_newlines = (options.preserve_newlines === undefined) ? false : options.preserve_newlines;
Expand Down
3 changes: 1 addition & 2 deletions js/lib/beautify-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function rtrim(s) {
function Beautifier(html_source, options, js_beautify, css_beautify) {
//Wrapper function to invoke all the necessary constructors and deal with the output.
html_source = html_source || '';
options = options || {};

var multi_parser,
indent_inner_html,
Expand All @@ -219,8 +220,6 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
extra_liners,
eol;

options = options || {};

// Allow the setting of language/file-type specific options
// with inheritance of overall settings
options = mergeOpts(options, 'html');
Expand Down
8 changes: 4 additions & 4 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ var MODE = {

function Beautifier(js_source_text, options) {
"use strict";
options = options || {};
js_source_text = js_source_text || '';

var output;
var tokens = [],
token_pos;
Expand Down Expand Up @@ -409,9 +412,6 @@ function Beautifier(js_source_text, options) {
return next_flags;
}

// Some interpreters have unexpected results with foo = baz || bar;
options = options ? options : {};

// Allow the setting of language/file-type specific options
// with inheritance of overall settings
options = mergeOpts(options, 'js');
Expand Down Expand Up @@ -1719,7 +1719,7 @@ module.exports.Beautifier = Beautifier;
*/

function InputScanner(input) {
var _input = input;
var _input = input || '';
var _input_length = _input.length;
var _position = 0;

Expand Down
2 changes: 1 addition & 1 deletion js/src/core/inputscanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/

function InputScanner(input) {
var _input = input;
var _input = input || '';
var _input_length = _input.length;
var _position = 0;

Expand Down
3 changes: 1 addition & 2 deletions js/src/css/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ var lineBreak = acorn.lineBreak;
var allLineBreaks = acorn.allLineBreaks;

function Beautifier(source_text, options) {
source_text = source_text || '';
options = options || {};

// Allow the setting of language/file-type specific options
// with inheritance of overall settings
options = mergeOpts(options, 'css');

source_text = source_text || '';

var indentSize = options.indent_size ? parseInt(options.indent_size, 10) : 4;
var indentCharacter = options.indent_char || ' ';
var preserve_newlines = (options.preserve_newlines === undefined) ? false : options.preserve_newlines;
Expand Down
3 changes: 1 addition & 2 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function rtrim(s) {
function Beautifier(html_source, options, js_beautify, css_beautify) {
//Wrapper function to invoke all the necessary constructors and deal with the output.
html_source = html_source || '';
options = options || {};

var multi_parser,
indent_inner_html,
Expand All @@ -71,8 +72,6 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
extra_liners,
eol;

options = options || {};

// Allow the setting of language/file-type specific options
// with inheritance of overall settings
options = mergeOpts(options, 'html');
Expand Down
6 changes: 3 additions & 3 deletions js/src/javascript/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ var MODE = {

function Beautifier(js_source_text, options) {
"use strict";
options = options || {};
js_source_text = js_source_text || '';

var output;
var tokens = [],
token_pos;
Expand Down Expand Up @@ -177,9 +180,6 @@ function Beautifier(js_source_text, options) {
return next_flags;
}

// Some interpreters have unexpected results with foo = baz || bar;
options = options ? options : {};

// Allow the setting of language/file-type specific options
// with inheritance of overall settings
options = mergeOpts(options, 'js');
Expand Down
6 changes: 5 additions & 1 deletion js/test/generated/beautify-css-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
opts.eol = '\r\\n';
expected = expected.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
if (input.indexOf('\n') !== -1) {
if (input && input.indexOf('\n') !== -1) {
input = input.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
// Ensure support for auto eol detection
Expand Down Expand Up @@ -9231,6 +9231,10 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
{
sanitytest = test_obj;

reset_options();
//============================================================
test_fragment(null, '');

reset_options();
//============================================================
// test basic css beautifier
Expand Down
6 changes: 5 additions & 1 deletion js/test/generated/beautify-html-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
opts.eol = '\r\n';
expected = expected.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
if (input.indexOf('\n') !== -1) {
if (input && input.indexOf('\n') !== -1) {
input = input.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
// Ensure support for auto eol detection
Expand Down Expand Up @@ -2998,6 +2998,10 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
{
sanitytest = test_obj;

reset_options();
//============================================================
test_fragment(null, '');

reset_options();
//============================================================
opts.end_with_newline = true;
Expand Down
6 changes: 5 additions & 1 deletion js/test/generated/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
opts.eol = '\r\\n';
expected = expected.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
if (input.indexOf('\n') !== -1) {
if (input && input.indexOf('\n') !== -1) {
input = input.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
// Ensure support for auto eol detection
Expand Down Expand Up @@ -5210,6 +5210,10 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
{
sanitytest = test_obj;

reset_options();
//============================================================
test_fragment(null, '');

reset_options();
//============================================================
opts.indent_size = 1;
Expand Down
6 changes: 5 additions & 1 deletion python/cssbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9198,6 +9198,10 @@ def testBasics(self):
self.reset_options()
t = self.decodesto

self.reset_options();
#============================================================
t(Null, "")

t("", "")
t("\n", "")
t(".tabs{}\n", ".tabs {}")
Expand Down Expand Up @@ -9349,7 +9353,7 @@ def decodesto(self, input, expectation=None):
expectation = expectation.replace('\n', '\r\n')
self.assertMultiLineEqual(
cssbeautifier.beautify(input, self.options), expectation)
if input.find('\n') != -1:
if input and input.find('\n') != -1:
input = input.replace('\n', '\r\n')
self.assertMultiLineEqual(
cssbeautifier.beautify(input, self.options), expectation)
Expand Down
2 changes: 2 additions & 0 deletions python/jsbeautifier/core/inputscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

class InputScanner:
def __init__(self, input):
if input is None:
input = ''
self.__input = input
self.__input_length = len(self.__input)
self.__position = 0
Expand Down
5 changes: 3 additions & 2 deletions python/jsbeautifier/javascript/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def __init__(self, opts = default_options() ):
self.blank_state()

def blank_state(self, js_source_text = None):
if js_source_text is None:
js_source_text = ''

# internal flags
self.flags = None
Expand Down Expand Up @@ -145,7 +147,7 @@ def blank_state(self, js_source_text = None):
self.last_type = 'TK_START_BLOCK' # last token type
self.last_last_text = '' # pre-last token text

preindent_index = 0;
preindent_index = 0
if not js_source_text == None and len(js_source_text) > 0:
while preindent_index < len(js_source_text) and \
js_source_text[preindent_index] in [' ', '\t'] :
Expand All @@ -163,7 +165,6 @@ def blank_state(self, js_source_text = None):


def beautify(self, s, opts = None ):

if opts != None:
opts = mergeOpts(opts, 'js')
self.opts = copy.copy(opts)
Expand Down
6 changes: 5 additions & 1 deletion python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5037,6 +5037,10 @@ def test_beautifier_unconverted(self):
test_fragment = self.decodesto
bt = self.bt

self.reset_options();
#============================================================
bt(Null, "")

self.reset_options();
#============================================================
self.options.indent_size = 1;
Expand Down Expand Up @@ -6170,7 +6174,7 @@ def decodesto(self, input, expectation=None):
expectation = expectation.replace('\n', '\r\n')
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
if input.find('\n') != -1:
if input and input.find('\n') != -1:
input = input.replace('\n', '\r\n')
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
Expand Down
6 changes: 5 additions & 1 deletion test/data/css/node.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
opts.eol = '\r\\n';
expected = expected.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
if (input.indexOf('\n') !== -1) {
if (input && input.indexOf('\n') !== -1) {
input = input.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
// Ensure support for auto eol detection
Expand Down Expand Up @@ -138,6 +138,10 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
{
sanitytest = test_obj;
reset_options();
//============================================================
test_fragment(null, '');
reset_options();
//============================================================
// test basic css beautifier
Expand Down
6 changes: 5 additions & 1 deletion test/data/css/python.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class CSSBeautifierTest(unittest.TestCase):
self.reset_options()
t = self.decodesto

self.reset_options();
#============================================================
t(Null, "")

t("", "")
t("\n", "")
t(".tabs{}\n", ".tabs {}")
Expand Down Expand Up @@ -256,7 +260,7 @@ class CSSBeautifierTest(unittest.TestCase):
expectation = expectation.replace('\n', '\r\n')
self.assertMultiLineEqual(
cssbeautifier.beautify(input, self.options), expectation)
if input.find('\n') != -1:
if input and input.find('\n') != -1:
input = input.replace('\n', '\r\n')
self.assertMultiLineEqual(
cssbeautifier.beautify(input, self.options), expectation)
Expand Down
6 changes: 5 additions & 1 deletion test/data/html/node.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
opts.eol = '\r\n';
expected = expected.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
if (input.indexOf('\n') !== -1) {
if (input && input.indexOf('\n') !== -1) {
input = input.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
// Ensure support for auto eol detection
Expand Down Expand Up @@ -144,6 +144,10 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
{
sanitytest = test_obj;
reset_options();
//============================================================
test_fragment(null, '');
reset_options();
//============================================================
opts.end_with_newline = true;
Expand Down
6 changes: 5 additions & 1 deletion test/data/javascript/node.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
opts.eol = '\r\\n';
expected = expected.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
if (input.indexOf('\n') !== -1) {
if (input && input.indexOf('\n') !== -1) {
input = input.replace(/[\n]/g, '\r\n');
sanitytest.expect(input, expected);
// Ensure support for auto eol detection
Expand Down Expand Up @@ -315,6 +315,10 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
{
sanitytest = test_obj;
reset_options();
//============================================================
test_fragment(null, '');
reset_options();
//============================================================
opts.indent_size = 1;
Expand Down
6 changes: 5 additions & 1 deletion test/data/javascript/python.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ class TestJSBeautifier(unittest.TestCase):
test_fragment = self.decodesto
bt = self.bt

self.reset_options();
#============================================================
bt(Null, "")

self.reset_options();
#============================================================
self.options.indent_size = 1;
Expand Down Expand Up @@ -1275,7 +1279,7 @@ class TestJSBeautifier(unittest.TestCase):
expectation = expectation.replace('\n', '\r\n')
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
if input.find('\n') != -1:
if input and input.find('\n') != -1:
input = input.replace('\n', '\r\n')
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
Expand Down

0 comments on commit 12ecdf8

Please sign in to comment.