Skip to content

Commit d51e8f0

Browse files
committed
fix: use parseKeyword in property setters
1 parent e1e4ed3 commit d51e8f0

File tree

8 files changed

+54
-105
lines changed

8 files changed

+54
-105
lines changed

lib/properties/borderStyle.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

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

55
// the valid border-styles:
66
var styles = [
@@ -16,20 +16,17 @@ var styles = [
1616
'outset',
1717
];
1818

19-
module.exports.isValid = function parse(v) {
20-
return v === '' || styles.indexOf(v) !== -1;
21-
};
22-
var isValid = module.exports.isValid;
19+
function parse(v) {
20+
return parseKeyword(v, styles);
21+
}
2322

24-
var parser = function(v) {
25-
if (isValid(v)) {
26-
return v.toLowerCase();
27-
}
28-
return undefined;
29-
};
23+
function isValid(v) {
24+
return parse(v) !== undefined;
25+
}
26+
module.exports.isValid = isValid;
3027

3128
module.exports.definition = {
32-
set: implicitSetter('border', 'style', isValid, parser),
29+
set: implicitSetter('border', 'style', isValid, parse),
3330
get: function() {
3431
return this.getPropertyValue('border-style');
3532
},

lib/properties/borderWidth.js

+9-31
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
11
'use strict';
22

3-
var parsers = require('../parsers');
4-
var implicitSetter = require('../parsers').implicitSetter;
3+
const { implicitSetter, parseKeyword, parseLength } = require('../parsers');
54

6-
// the valid border-widths:
7-
var widths = ['thin', 'medium', 'thick'];
5+
function parse(v) {
6+
return parseLength(v) || parseKeyword(v, ['medium', 'thick', 'thin']);
7+
}
88

9-
module.exports.isValid = function parse(v) {
10-
var length = parsers.parseLength(v);
11-
if (length !== undefined) {
12-
return true;
13-
}
14-
if (v === '') {
15-
return true;
16-
}
17-
v = v.toLowerCase();
18-
if (widths.indexOf(v) === -1) {
19-
return false;
20-
}
21-
return true;
22-
};
23-
var isValid = module.exports.isValid;
24-
25-
var parser = function(v) {
26-
var length = parsers.parseLength(v);
27-
if (length !== undefined) {
28-
return length;
29-
}
30-
if (isValid(v)) {
31-
return v.toLowerCase();
32-
}
33-
return undefined;
34-
};
9+
function isValid(v) {
10+
return parse(v) !== undefined;
11+
}
12+
module.exports.isValid = isValid;
3513

3614
module.exports.definition = {
37-
set: implicitSetter('border', 'width', isValid, parser),
15+
set: implicitSetter('border', 'width', isValid, parse),
3816
get: function() {
3917
return this.getPropertyValue('border-width');
4018
},

lib/properties/clip.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
'use strict';
22

3-
const { parseLengthOrPercentage } = require('../parsers');
3+
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');
44

55
var shape_regex = /^rect\((.*)\)$/i;
66

7-
var parse = function(val) {
8-
if (val === '') {
9-
return val;
10-
}
11-
val = val.toLowerCase();
12-
if (val === 'auto' || val === 'inherit') {
7+
function parse(val) {
8+
const keyword = parseKeyword(val, ['auto']);
9+
if (keyword !== undefined) {
1310
return val;
1411
}
1512
var matches = val.match(shape_regex);
@@ -30,7 +27,7 @@ var parse = function(val) {
3027
}
3128
parts = parts.join(', ');
3229
return val.replace(matches[1], parts);
33-
};
30+
}
3431

3532
module.exports.definition = {
3633
set: function(v) {

lib/properties/fontWeight.js

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
'use strict';
22

3-
var valid_weights = [
4-
'normal',
5-
'bold',
6-
'bolder',
7-
'lighter',
8-
'100',
9-
'200',
10-
'300',
11-
'400',
12-
'500',
13-
'600',
14-
'700',
15-
'800',
16-
'900',
17-
'inherit',
18-
];
3+
const { parseInteger, parseKeyword } = require('../parsers');
4+
5+
function parse(v) {
6+
const integer = parseInteger(v);
7+
if (integer) {
8+
if (integer < 1 || 1000 < integer) {
9+
return undefined;
10+
}
11+
return integer;
12+
}
13+
return parseKeyword(v, ['bold', 'bolder', 'lighter', 'normal']);
14+
}
1915

2016
module.exports.isValid = function isValid(v) {
21-
return valid_weights.indexOf(v.toLowerCase()) !== -1;
17+
return parse(v) !== undefined;
2218
};
2319

2420
module.exports.definition = {

lib/properties/height.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
'use strict';
22

3-
const { parseLengthOrPercentage } = require('../parsers');
3+
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');
44

55
function parse(v) {
6-
if (v.toLowerCase() === 'auto') {
7-
return 'auto';
8-
}
9-
if (v.toLowerCase() === 'inherit') {
10-
return 'inherit';
11-
}
12-
return parseLengthOrPercentage(v);
6+
return parseKeyword(v, ['auto']) || parseLengthOrPercentage(v);
137
}
148

159
module.exports.definition = {

lib/properties/lineHeight.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
const { parseKeyword, parseNumber, parseLengthOrPercentage } = require('../parsers');
44

5+
function parse(v) {
6+
return parseKeyword(v, ['normal']) || parseNumber(v) || parseLengthOrPercentage(v);
7+
}
8+
59
module.exports.isValid = function isValid(v) {
6-
return (
7-
parseKeyword(v, ['normal']) !== undefined ||
8-
parseNumber(v) !== undefined ||
9-
parseLengthOrPercentage(v) !== undefined
10-
);
10+
return parse(v) !== undefined;
1111
};
1212

1313
module.exports.definition = {

lib/properties/margin.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
'use strict';
22

3-
const { implicitSetter, parseLengthOrPercentage, parseInteger } = require('../parsers');
3+
const { implicitSetter, parseLengthOrPercentage, parseKeyword } = require('../parsers');
44

5-
var isValid = function(v) {
6-
if (v.toLowerCase() === 'auto') {
7-
return true;
8-
}
9-
return parseLengthOrPercentage(v) !== undefined || parseInteger(v) !== undefined;
10-
};
5+
function parse(v) {
6+
return parseKeyword(v, ['auto']) || parseLengthOrPercentage(v);
7+
}
118

12-
var parser = function(v) {
13-
var V = v.toLowerCase();
14-
if (V === 'auto') {
15-
return V;
16-
}
17-
return parseLengthOrPercentage(v);
18-
};
9+
function isValid(v) {
10+
return parse(v) !== undefined;
11+
}
1912

20-
var mySetter = implicitSetter('margin', '', isValid, parser);
13+
var mySetter = implicitSetter('margin', '', isValid, parse);
2114
var myGlobal = implicitSetter(
2215
'margin',
2316
'',
@@ -53,4 +46,4 @@ module.exports.definition = {
5346
};
5447

5548
module.exports.isValid = isValid;
56-
module.exports.parser = parser;
49+
module.exports.parser = parse;

lib/properties/width.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
'use strict';
22

3-
const { parseLengthOrPercentage } = require('../parsers');
3+
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');
44

55
function parse(v) {
6-
if (v.toLowerCase() === 'auto') {
7-
return 'auto';
8-
}
9-
if (v.toLowerCase() === 'inherit') {
10-
return 'inherit';
11-
}
12-
return parseLengthOrPercentage(v);
6+
return parseKeyword(v, ['auto']) || parseLengthOrPercentage(v);
137
}
148

159
module.exports.definition = {

0 commit comments

Comments
 (0)