Skip to content

Commit 0770594

Browse files
author
Mahir Shah
committed
Add Recurse Option to getShorthandComputedProperties
- fixes issue #4 - #4
1 parent 2d3d1f1 commit 0770594

File tree

3 files changed

+99
-17
lines changed

3 files changed

+99
-17
lines changed

README.md

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
- [API](#api)
1414
- [isShorthandProperty(property: string): boolean](#isshorthandpropertyproperty-string-boolean)
1515
- [Examples](#examples)
16-
- [[Experimental] isValidDeclaration(property: string, value: string): boolean](#experimental-isvaliddeclarationproperty-string-value-string-boolean)
16+
- [[Experimental] [isValidDeclaration(property: string, value: string): boolean](./src/isValidDeclaration.js)](#experimental-isvaliddeclarationproperty-string-value-string-booleansrcisvaliddeclarationjs)
1717
- [Examples](#examples-1)
18-
- [getShorthandComputedProperties(property: string): Array](#getshorthandcomputedpropertiesproperty-string-array)
18+
- [getShorthandComputedProperties(property: string, [recursivelyResolve=false]): Array](#getshorthandcomputedpropertiesproperty-string-recursivelyresolvefalse-array)
1919
- [Examples](#examples-2)
20-
- [expandShorthandProperty(property: string, value: string, [recursivelyResolve=false]): Object](#expandshorthandpropertyproperty-string-value-string-recursivelyresolvetrue-object)
20+
- [expandShorthandProperty(property: string, value: string, [recursivelyResolve=false]): Object](#expandshorthandpropertyproperty-string-value-string-recursivelyresolvefalse-object)
2121
- [Examples](#examples-3)
22+
- [Developer/Contribution HOWTO](#developercontribution-howto)
2223

2324
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2425

@@ -30,7 +31,7 @@
3031

3132
## Installation
3233
```
33-
$ npm instal css-property-parser
34+
$ npm install css-property-parser
3435
```
3536

3637
## Usage
@@ -142,35 +143,57 @@ isValidDeclaration('width', '300ms')
142143
// => false ('ms' is not a valid length unit)
143144
```
144145

145-
### [getShorthandComputedProperties(property: string): Array](./src/getShorthandComputedProperties.js)
146+
### [getShorthandComputedProperties(property: string, [recursivelyResolve=false]): Array](./src/getShorthandComputedProperties.js)
146147
Given a shorthand property, returns an array of the computed properties for that shorthand property. If given
147148
a known property that is not a shorthand, simply returns the given property. If given an unknown property,
148149
returns an empty array.
149150

150151
* shorthandProperty - the shorthand property name. For example, "background" or "border".
152+
* [recursivelyResolve=false] - recursively resolve additional longhand properties if the shorthands expand to additional shorthands. For example, the border property expands to border-width, which expands further to border-left-width, border-right-width, etc.
151153
* returns an array containing the computed properties for the given shorthand property. Returns an empty array if the given property is not a valid property.
152154

153155
##### Examples
154156

155157
```js
156158
getShorthandComputedProperties('background');
157-
// -> [
158-
// "background-image",
159-
// "background-position",
160-
// "background-size",
161-
// "background-repeat",
162-
// "background-origin",
163-
// "background-clip",
164-
// "background-attachment",
165-
// "background-color"
166-
// ]
159+
// -> [
160+
// "background-image",
161+
// "background-position",
162+
// "background-size",
163+
// "background-repeat",
164+
// "background-origin",
165+
// "background-clip",
166+
// "background-attachment",
167+
// "background-color"
168+
// ]
167169
```
168170

169171
```js
170172
getShorthandComputedProperties('color');
171173
// -> ["color"]
172174
```
173175

176+
```js
177+
getShorthandComputedProperties('border', true);
178+
// -> [
179+
// 'border-width',
180+
// 'border-style',
181+
// 'border-color',
182+
// 'border-bottom-width',
183+
// 'border-left-width',
184+
// 'border-right-width',
185+
// 'border-top-width',
186+
// 'border-bottom-style',
187+
// 'border-left-style',
188+
// 'border-right-style',
189+
// 'border-top-style',
190+
// 'border-bottom-color',
191+
// 'border-left-color',
192+
// 'border-right-color',
193+
// 'border-top-color'
194+
// ];
195+
```
196+
174197
```js
175198
getShorthandComputedProperties('unknownProperty');
176199
// -> []

src/getShorthandComputedProperties.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const { css: { properties } } = require('mdn-data');
66
* returns an empty array.
77
*
88
* @param {string} shorthandProperty - the shorthand property name. For example, "background" or "border".
9+
* @param {boolean} [recursivelyResolve=false] - recursively resolve additional longhand properties if the shorthands
10+
* expand to additional shorthands. For example, the border property
11+
* expands to border-width, which expands further to border-left-width,
12+
* border-right-width, etc.
913
* @returns {Array} - an array containing the computed properties for the given shorthand property. Returns an
1014
* empty array if the given property is not a valid property.
1115
*
@@ -23,17 +27,44 @@ const { css: { properties } } = require('mdn-data');
2327
* ]
2428
*
2529
* @example
30+
* getShorthandComputedProperties('border', true) ->
31+
* [
32+
* "border-width",
33+
* "border-style",
34+
* "border-color",
35+
* "border-bottom-width",
36+
* "border-left-width",
37+
* "border-right-width",
38+
* "border-top-width",
39+
* "border-bottom-style",
40+
* "border-left-style",
41+
* "border-right-style",
42+
* "border-top-style",
43+
* "border-bottom-color",
44+
* "border-left-color",
45+
* "border-right-color",
46+
* "border-top-color",
47+
* ]
48+
*
49+
* @example
2650
* getShorthandComputedProperties('color') ->
2751
* ["color"]
2852
*
2953
* @example
3054
* getShorthandComputedProperties('unknownProperty') ->
3155
* []
3256
*/
33-
module.exports = function getShorthandComputedProperties(shorthandProperty) {
57+
module.exports = function getShorthandComputedProperties(shorthandProperty, recursivelyResolve = false) {
3458
if (properties[shorthandProperty]) {
3559
if (Array.isArray(properties[shorthandProperty].computed)) {
36-
return properties[shorthandProperty].computed;
60+
const computedProperties = properties[shorthandProperty].computed;
61+
62+
return recursivelyResolve
63+
? computedProperties.concat(...computedProperties
64+
.filter(property => Array.isArray(properties[property].computed))
65+
.map(property => getShorthandComputedProperties(property, true))
66+
)
67+
: computedProperties;
3768
}
3869

3970
return [shorthandProperty];

test/GetShorthandComputedPropertiesTest.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,32 @@ describe('getShorthandComputedProperties', function () {
2525
'background-color',
2626
]);
2727
});
28+
29+
it('should not recursively resolve properties by default', function () {
30+
assert.deepEqual(getShorthandComputedProperties('border'), [
31+
'border-width',
32+
'border-style',
33+
'border-color',
34+
]);
35+
});
36+
37+
it('should recursively resolve properties', function () {
38+
assert.deepEqual(getShorthandComputedProperties('border', true), [
39+
'border-width',
40+
'border-style',
41+
'border-color',
42+
'border-bottom-width',
43+
'border-left-width',
44+
'border-right-width',
45+
'border-top-width',
46+
'border-bottom-style',
47+
'border-left-style',
48+
'border-right-style',
49+
'border-top-style',
50+
'border-bottom-color',
51+
'border-left-color',
52+
'border-right-color',
53+
'border-top-color',
54+
]);
55+
});
2856
});

0 commit comments

Comments
 (0)