diff --git a/js/helper-functions.js b/js/helper-functions.js index cac4032..cbbfe5d 100644 --- a/js/helper-functions.js +++ b/js/helper-functions.js @@ -108,10 +108,10 @@ String.prototype.setFormat = function(styleEnumValue) { var type = StyleEnum.toString(styleEnumValue); var style = defaultStyles[type] ? defaultStyles[type] : defaultStyles.standard; - var color = style.color ? style.color : defaultStyles.standard.color; + var color = style.color && isValidColor(style.color) ? style.color : defaultStyles.standard.color; var bold = style.bold ? style.bold : defaultStyles.standard.bold; var italic = style.italic ? style.italic : defaultStyles.standard.italic; - var backgroundColor = style.backgroundColor ? + var backgroundColor = style.backgroundColor && isValidColor(style.backgroundColor) ? style.backgroundColor : defaultStyles.standard.backgroundColor; if (bold) { @@ -122,7 +122,7 @@ String.prototype.setFormat = function(styleEnumValue) { result += "i"; } - if (color && isValidColor(color)) { + if (color) { result += CONSTANTS.SEMI_COLON; result += color; } else { @@ -130,17 +130,11 @@ String.prototype.setFormat = function(styleEnumValue) { color = null; } - if (backgroundColor && isValidColor(backgroundColor)) { + if (backgroundColor) { if (bold || italic || color) { result += CONSTANTS.SEMI_COLON; } result += backgroundColor; - } else { - result += bold ? CONSTANTS.EMPTY : CONSTANTS.SEMI_COLON; - result += italic ? CONSTANTS.EMPTY : CONSTANTS.SEMI_COLON; - result += color ? CONSTANTS.EMPTY : CONSTANTS.SEMI_COLON; - - result += defaultStyles.standard.backgroundColor; } return wrappedFormatting(result, this); diff --git a/spec/helping-functions-spec.js b/spec/helping-functions-spec.js index cd254c1..7e06a32 100644 --- a/spec/helping-functions-spec.js +++ b/spec/helping-functions-spec.js @@ -200,6 +200,10 @@ describe("Set format", function() { beforeEach(function() { this.exampleText = "Hello World"; }); + + afterEach(function() { + defaultStyles['name']['color'] = "green"; + }); it("Title format", function() { expect(this.exampleText.setFormat(StyleEnum.TITLE)).toBe("[[b;red;#000]Hello World]"); @@ -232,6 +236,18 @@ describe("Set format", function() { it("Accepts empty style and text", function() { expect("".setFormat("")).toBe(""); }); + + it('Accepts invalid styles', function() { + defaultStyles['title']['color'] = "cat"; + expect(this.exampleText.setFormat(StyleEnum.TITLE)).toBe("[[b;white;#000]Hello World]"); + expect(this.exampleText.setTitle()).toBe("[[b;white;#000]Hello World]"); + }); + + it('Accepts empty default styles', function() { + defaultStyles['name']['color'] = null; + expect(this.exampleText.setFormat(StyleEnum.NAME)).toBe("[[b;white;#000]Hello World]"); + expect(this.exampleText.setName()).toBe("[[b;white;#000]Hello World]"); + }); }); describe("Date formatting", function() {