Converts name dashes to underscore in option inputs

This commit is contained in:
L. D. MacKrell 2018-08-22 11:52:44 -07:00
parent 569afd2ac8
commit 85a8fe3e73
17 changed files with 99 additions and 1 deletions

View File

@ -50,4 +50,16 @@ function mergeOpts(allOptions, childFieldName) {
return finalOpts;
}
module.exports.mergeOpts = mergeOpts;
function normalizeOpts(options) {
var convertedOpts = {};
var key;
for (key in options) {
var newKey = key.replace(/-/g, "_");
convertedOpts[newKey] = options[key];
}
return convertedOpts;
}
module.exports.mergeOpts = mergeOpts;
module.exports.normalizeOpts = normalizeOpts;

View File

@ -27,6 +27,7 @@
*/
var mergeOpts = require('../core/options').mergeOpts;
var normalizeOpts = require('../core/options').normalizeOpts;
var acorn = require('../core/acorn');
var Output = require('../core/output').Output;
var InputScanner = require('../core/inputscanner').InputScanner;
@ -41,6 +42,7 @@ function Beautifier(source_text, options) {
// Allow the setting of language/file-type specific options
// with inheritance of overall settings
options = mergeOpts(options, 'css');
options = normalizeOpts(options);
this._options = {};
var indentSize = options.indent_size ? parseInt(options.indent_size, 10) : 4;

View File

@ -27,6 +27,7 @@
*/
var mergeOpts = require('../core/options').mergeOpts;
var normalizeOpts = require('../core/options').normalizeOpts;
var acorn = require('../core/acorn');
var Output = require('../core/output').Output;
var Tokenizer = require('../html/tokenizer').Tokenizer;
@ -247,6 +248,7 @@ function Beautifier(source_text, options, js_beautify, css_beautify) {
// Allow the setting of language/file-type specific options
// with inheritance of overall settings
options = mergeOpts(options, 'html');
options = normalizeOpts(options);
// backwards compatibility to 1.3.4
if ((options.wrap_line_length === undefined || parseInt(options.wrap_line_length, 10) === 0) &&

View File

@ -27,6 +27,7 @@
*/
var mergeOpts = require('../core/options').mergeOpts;
var normalizeOpts = require('../core/options').normalizeOpts;
var acorn = require('../core/acorn');
var Output = require('../core/output').Output;
var Tokenizer = require('./tokenizer').Tokenizer;
@ -164,6 +165,7 @@ function Beautifier(source_text, options) {
// Allow the setting of language/file-type specific options
// with inheritance of overall settings
options = mergeOpts(options, 'js');
options = normalizeOpts(options);
opt = {};

View File

@ -10326,6 +10326,12 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
//============================================================
test_fragment(null, '');
reset_options();
//============================================================
// Test user pebkac protection, converts dash names to underscored names
opts["end-with-newline"] = true;
test_fragment(null, '\n');
reset_options();
//============================================================
// test basic css beautifier

View File

@ -5591,6 +5591,12 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
//============================================================
test_fragment(null, '');
reset_options();
//============================================================
// Test user pebkac protection, converts dash names to underscored names
opts["end-with-newline"] = true;
test_fragment(null, '\n');
reset_options();
//============================================================
set_name('end_with_newline = true');

View File

@ -5396,6 +5396,12 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
//============================================================
test_fragment(null, '');
reset_options();
//============================================================
// Test user pebkac protection, converts dash names to underscored names
opts["end-with-newline"] = true;
test_fragment(null, '\n');
reset_options();
//============================================================
opts.indent_size = 1;

View File

@ -4,6 +4,7 @@ import re
import copy
from .options import BeautifierOptions
from jsbeautifier.core.options import mergeOpts
from jsbeautifier.core.options import normalizeOpts
from jsbeautifier.core.output import Output
from jsbeautifier.core.inputscanner import InputScanner
from jsbeautifier.__version__ import __version__
@ -117,6 +118,7 @@ class Beautifier:
self.__source_text = source_text
opts = mergeOpts(opts, 'css')
opts = normalizeOpts(opts)
# Continue to accept deprecated option
opts.space_around_combinator = opts.space_around_combinator or \

View File

@ -10221,6 +10221,15 @@ class CSSBeautifierTest(unittest.TestCase):
#============================================================
t(None, "")
self.reset_options()
#============================================================
# Test user pebkac protection, converts dash names to underscored names
setattr(self.options, 'end-with-newline', True)
t(None, '\n')
self.reset_options()
#============================================================
t("", "")
t("\n", "")
t(".tabs{}\n", ".tabs {}")

View File

@ -41,3 +41,13 @@ def mergeOpts(options, childFieldName):
setattr(finalOpts, key, local[key])
return finalOpts
def normalizeOpts(options):
convertedOpts = copy.copy(options)
for key in options.__dict__:
if '-' in key:
delattr(convertedOpts, key)
setattr(convertedOpts, key.replace('-', '_'), getattr(options, key, None))
return convertedOpts

View File

@ -29,6 +29,7 @@ from .tokenizer import Tokenizer
from .tokenizer import TOKEN
from .options import BeautifierOptions
from ..core.options import mergeOpts
from ..core.options import normalizeOpts
from ..core.output import Output
@ -195,6 +196,7 @@ class Beautifier:
def beautify(self, source_text='', opts=None):
if opts is not None:
opts = mergeOpts(opts, 'js')
opts = normalizeOpts(opts)
self.opts = copy.copy(opts)
# Compat with old form

View File

@ -5152,6 +5152,12 @@ class TestJSBeautifier(unittest.TestCase):
#============================================================
bt(None, "")
self.reset_options()
#============================================================
# Test user pebkac protection, converts dash names to underscored names
setattr(self.options, 'end-with-newline', True)
test_fragment(None, '\n')
self.reset_options()
#============================================================
self.options.indent_size = 1

View File

@ -165,6 +165,12 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
//============================================================
test_fragment(null, '');
reset_options();
//============================================================
// Test user pebkac protection, converts dash names to underscored names
opts["end-with-newline"] = true;
test_fragment(null, '\n');
reset_options();
//============================================================
// test basic css beautifier

View File

@ -109,6 +109,15 @@ class CSSBeautifierTest(unittest.TestCase):
#============================================================
t(None, "")
self.reset_options()
#============================================================
# Test user pebkac protection, converts dash names to underscored names
setattr(self.options, 'end-with-newline', True)
t(None, '\n')
self.reset_options()
#============================================================
t("", "")
t("\n", "")
t(".tabs{}\n", ".tabs {}")

View File

@ -170,6 +170,12 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
//============================================================
test_fragment(null, '');
reset_options();
//============================================================
// Test user pebkac protection, converts dash names to underscored names
opts["end-with-newline"] = true;
test_fragment(null, '\n');
reset_options();
//============================================================
set_name('end_with_newline = true');

View File

@ -339,6 +339,12 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
//============================================================
test_fragment(null, '');
reset_options();
//============================================================
// Test user pebkac protection, converts dash names to underscored names
opts["end-with-newline"] = true;
test_fragment(null, '\n');
reset_options();
//============================================================
opts.indent_size = 1;

View File

@ -146,6 +146,12 @@ class TestJSBeautifier(unittest.TestCase):
#============================================================
bt(None, "")
self.reset_options()
#============================================================
# Test user pebkac protection, converts dash names to underscored names
setattr(self.options, 'end-with-newline', True)
test_fragment(None, '\n')
self.reset_options()
#============================================================
self.options.indent_size = 1