Skip to content

Commit

Permalink
fix: use parseKeyword in property setters
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoublev committed May 15, 2021
1 parent e1e4ed3 commit d51e8f0
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 105 deletions.
21 changes: 9 additions & 12 deletions lib/properties/borderStyle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var implicitSetter = require('../parsers').implicitSetter;
const { implicitSetter, parseKeyword } = require('../parsers');

// the valid border-styles:
var styles = [
Expand All @@ -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');
},
Expand Down
40 changes: 9 additions & 31 deletions lib/properties/borderWidth.js
Original file line number Diff line number Diff line change
@@ -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');
},
Expand Down
13 changes: 5 additions & 8 deletions lib/properties/clip.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -30,7 +27,7 @@ var parse = function(val) {
}
parts = parts.join(', ');
return val.replace(matches[1], parts);
};
}

module.exports.definition = {
set: function(v) {
Expand Down
30 changes: 13 additions & 17 deletions lib/properties/fontWeight.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
10 changes: 2 additions & 8 deletions lib/properties/height.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
10 changes: 5 additions & 5 deletions lib/properties/lineHeight.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
25 changes: 9 additions & 16 deletions lib/properties/margin.js
Original file line number Diff line number Diff line change
@@ -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',
'',
Expand Down Expand Up @@ -53,4 +46,4 @@ module.exports.definition = {
};

module.exports.isValid = isValid;
module.exports.parser = parser;
module.exports.parser = parse;
10 changes: 2 additions & 8 deletions lib/properties/width.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down

0 comments on commit d51e8f0

Please sign in to comment.