forked from mahirshah/css-property-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetShorthandComputedProperties.js
74 lines (70 loc) · 2.53 KB
/
getShorthandComputedProperties.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const properties = require('./formatted-data/properties.json');
/**
* Given a shorthand property, returns an array of the computed properties for that shorthand property. If given
* a known property that is not a shorthand, simply returns the given property. If given an unknown property,
* returns an empty array.
*
* @param {string} shorthandProperty - the shorthand property name. For example, "background" or "border".
* @param {boolean} [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.
* @returns {Array} - an array containing the computed properties for the given shorthand property. Returns an
* empty array if the given property is not a valid property.
*
* @example
* getShorthandComputedProperties('background') ->
* [
* "background-image",
* "background-position",
* "background-size",
* "background-repeat",
* "background-origin",
* "background-clip",
* "background-attachment",
* "background-color"
* ]
*
* @example
* getShorthandComputedProperties('border', true) ->
* [
* "border-width",
* "border-style",
* "border-color",
* "border-bottom-width",
* "border-left-width",
* "border-right-width",
* "border-top-width",
* "border-bottom-style",
* "border-left-style",
* "border-right-style",
* "border-top-style",
* "border-bottom-color",
* "border-left-color",
* "border-right-color",
* "border-top-color",
* ]
*
* @example
* getShorthandComputedProperties('color') ->
* ["color"]
*
* @example
* getShorthandComputedProperties('unknownProperty') ->
* []
*/
module.exports = function getShorthandComputedProperties(shorthandProperty, recursivelyResolve = false) {
if (properties[shorthandProperty]) {
if (Array.isArray(properties[shorthandProperty].computed)) {
const computedProperties = properties[shorthandProperty].computed;
return recursivelyResolve
? computedProperties.concat(...computedProperties
.filter(property => Array.isArray(properties[property].computed))
.map(property => getShorthandComputedProperties(property, true))
)
: computedProperties;
}
return [shorthandProperty];
}
return [];
};