diff --git a/lib/properties/borderStyle.js b/lib/properties/borderStyle.js index 135e46bf..f0f93c7c 100644 --- a/lib/properties/borderStyle.js +++ b/lib/properties/borderStyle.js @@ -1,6 +1,6 @@ 'use strict'; -var implicitSetter = require('../parsers').implicitSetter; +const { implicitSetter, parseKeyword } = require('../parsers'); // the valid border-styles: var styles = [ @@ -16,20 +16,17 @@ var styles = [ 'outset', ]; -module.exports.isValid = function parse(v) { - return v === '' || styles.indexOf(v) !== -1; -}; -var isValid = module.exports.isValid; +function parse(v) { + return parseKeyword(v, styles); +} -var parser = function(v) { - if (isValid(v)) { - return v.toLowerCase(); - } - return undefined; -}; +function isValid(v) { + return parse(v) !== undefined; +} +module.exports.isValid = isValid; module.exports.definition = { - set: implicitSetter('border', 'style', isValid, parser), + set: implicitSetter('border', 'style', isValid, parse), get: function() { return this.getPropertyValue('border-style'); }, diff --git a/lib/properties/borderWidth.js b/lib/properties/borderWidth.js index 88b2aa82..acccbbb0 100644 --- a/lib/properties/borderWidth.js +++ b/lib/properties/borderWidth.js @@ -1,40 +1,18 @@ 'use strict'; -var parsers = require('../parsers'); -var implicitSetter = require('../parsers').implicitSetter; +const { implicitSetter, parseKeyword, parseLength } = require('../parsers'); -// the valid border-widths: -var widths = ['thin', 'medium', 'thick']; +function parse(v) { + return parseLength(v) || parseKeyword(v, ['medium', 'thick', 'thin']); +} -module.exports.isValid = function parse(v) { - var length = parsers.parseLength(v); - if (length !== undefined) { - return true; - } - if (v === '') { - return true; - } - v = v.toLowerCase(); - if (widths.indexOf(v) === -1) { - return false; - } - return true; -}; -var isValid = module.exports.isValid; - -var parser = function(v) { - var length = parsers.parseLength(v); - if (length !== undefined) { - return length; - } - if (isValid(v)) { - return v.toLowerCase(); - } - return undefined; -}; +function isValid(v) { + return parse(v) !== undefined; +} +module.exports.isValid = isValid; module.exports.definition = { - set: implicitSetter('border', 'width', isValid, parser), + set: implicitSetter('border', 'width', isValid, parse), get: function() { return this.getPropertyValue('border-width'); }, diff --git a/lib/properties/clip.js b/lib/properties/clip.js index e25481f8..87dfffd9 100644 --- a/lib/properties/clip.js +++ b/lib/properties/clip.js @@ -1,15 +1,12 @@ 'use strict'; -const { parseLengthOrPercentage } = require('../parsers'); +const { parseKeyword, parseLengthOrPercentage } = require('../parsers'); var shape_regex = /^rect\((.*)\)$/i; -var parse = function(val) { - if (val === '') { - return val; - } - val = val.toLowerCase(); - if (val === 'auto' || val === 'inherit') { +function parse(val) { + const keyword = parseKeyword(val, ['auto']); + if (keyword !== undefined) { return val; } var matches = val.match(shape_regex); @@ -30,7 +27,7 @@ var parse = function(val) { } parts = parts.join(', '); return val.replace(matches[1], parts); -}; +} module.exports.definition = { set: function(v) { diff --git a/lib/properties/fontWeight.js b/lib/properties/fontWeight.js index b854f6ab..9a40c192 100644 --- a/lib/properties/fontWeight.js +++ b/lib/properties/fontWeight.js @@ -1,24 +1,20 @@ 'use strict'; -var valid_weights = [ - 'normal', - 'bold', - 'bolder', - 'lighter', - '100', - '200', - '300', - '400', - '500', - '600', - '700', - '800', - '900', - 'inherit', -]; +const { parseInteger, parseKeyword } = require('../parsers'); + +function parse(v) { + const integer = parseInteger(v); + if (integer) { + if (integer < 1 || 1000 < integer) { + return undefined; + } + return integer; + } + return parseKeyword(v, ['bold', 'bolder', 'lighter', 'normal']); +} module.exports.isValid = function isValid(v) { - return valid_weights.indexOf(v.toLowerCase()) !== -1; + return parse(v) !== undefined; }; module.exports.definition = { diff --git a/lib/properties/height.js b/lib/properties/height.js index 48816196..cec9faaf 100644 --- a/lib/properties/height.js +++ b/lib/properties/height.js @@ -1,15 +1,9 @@ 'use strict'; -const { parseLengthOrPercentage } = require('../parsers'); +const { parseKeyword, parseLengthOrPercentage } = require('../parsers'); function parse(v) { - if (v.toLowerCase() === 'auto') { - return 'auto'; - } - if (v.toLowerCase() === 'inherit') { - return 'inherit'; - } - return parseLengthOrPercentage(v); + return parseKeyword(v, ['auto']) || parseLengthOrPercentage(v); } module.exports.definition = { diff --git a/lib/properties/lineHeight.js b/lib/properties/lineHeight.js index bc2d47d1..bb701366 100644 --- a/lib/properties/lineHeight.js +++ b/lib/properties/lineHeight.js @@ -2,12 +2,12 @@ const { parseKeyword, parseNumber, parseLengthOrPercentage } = require('../parsers'); +function parse(v) { + return parseKeyword(v, ['normal']) || parseNumber(v) || parseLengthOrPercentage(v); +} + module.exports.isValid = function isValid(v) { - return ( - parseKeyword(v, ['normal']) !== undefined || - parseNumber(v) !== undefined || - parseLengthOrPercentage(v) !== undefined - ); + return parse(v) !== undefined; }; module.exports.definition = { diff --git a/lib/properties/margin.js b/lib/properties/margin.js index b62e1788..53d04ad1 100644 --- a/lib/properties/margin.js +++ b/lib/properties/margin.js @@ -1,23 +1,16 @@ 'use strict'; -const { implicitSetter, parseLengthOrPercentage, parseInteger } = require('../parsers'); +const { implicitSetter, parseLengthOrPercentage, parseKeyword } = require('../parsers'); -var isValid = function(v) { - if (v.toLowerCase() === 'auto') { - return true; - } - return parseLengthOrPercentage(v) !== undefined || parseInteger(v) !== undefined; -}; +function parse(v) { + return parseKeyword(v, ['auto']) || parseLengthOrPercentage(v); +} -var parser = function(v) { - var V = v.toLowerCase(); - if (V === 'auto') { - return V; - } - return parseLengthOrPercentage(v); -}; +function isValid(v) { + return parse(v) !== undefined; +} -var mySetter = implicitSetter('margin', '', isValid, parser); +var mySetter = implicitSetter('margin', '', isValid, parse); var myGlobal = implicitSetter( 'margin', '', @@ -53,4 +46,4 @@ module.exports.definition = { }; module.exports.isValid = isValid; -module.exports.parser = parser; +module.exports.parser = parse; diff --git a/lib/properties/width.js b/lib/properties/width.js index 3c27ae8b..5c185a9f 100644 --- a/lib/properties/width.js +++ b/lib/properties/width.js @@ -1,15 +1,9 @@ 'use strict'; -const { parseLengthOrPercentage } = require('../parsers'); +const { parseKeyword, parseLengthOrPercentage } = require('../parsers'); function parse(v) { - if (v.toLowerCase() === 'auto') { - return 'auto'; - } - if (v.toLowerCase() === 'inherit') { - return 'inherit'; - } - return parseLengthOrPercentage(v); + return parseKeyword(v, ['auto']) || parseLengthOrPercentage(v); } module.exports.definition = {