Skip to content

Commit 2b3d140

Browse files
author
Mahir Shah
committed
feat(expandShorthandProperty): Add "includeInitialValues" paramater
- add additional paramter to expandShorthand property to fill in initial values that are not explicitly set in a shorthand declaration - update README and tests accordingly fixes #1
1 parent 0770594 commit 2b3d140

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- [Examples](#examples-1)
1818
- [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-recursivelyresolvefalse-object)
20+
- [expandShorthandProperty(property: string, value: string, [recursivelyResolve=false], [includeInitialValues=false]): Object](#expandshorthandpropertyproperty-string-value-string-recursivelyresolvefalse-includeinitialvaluesfalse-object)
2121
- [Examples](#examples-3)
2222
- [Developer/Contribution HOWTO](#developercontribution-howto)
2323

@@ -199,7 +199,7 @@ getShorthandComputedProperties('border', true);
199199
// -> []
200200
```
201201

202-
### [expandShorthandProperty(property: string, value: string, [recursivelyResolve=false]): Object](./src/expandShorthandProperty.js)
202+
### [expandShorthandProperty(property: string, value: string, [recursivelyResolve=false], [includeInitialValues=false]): Object](./src/expandShorthandProperty.js)
203203

204204
Given a property and value attempts to expand the value into its longhand equivalents. Returns an object
205205
mapping the property longhand names to the longhand values. If the property cannot be expanded (i.e. the property
@@ -208,6 +208,7 @@ is not a shorthand property) simply returns an object mapping the original prope
208208
* propertyName - the property name for the given value
209209
* propertyValue - the value of the property
210210
* [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.
211+
* [includeInitialValues=false] - when expanding the shorthand property, fill in any missing longhand values with their initial value. For example, the property declaration "border: 1px" only explicitly sets the "border-width" longhand property. If this param is true, the returned object will fill in the initial values for "border-style" and "border-color". By default, the returned object will only contain the "border-width".
211212
* throws {[ParseError](./src/errors/ParseError.js)} - if the propertyValue cannot be parsed.
212213
* throws {[UnknownPropertyError](./src/errors/UnknownPropertyError.js)} - if the propertyName is not defined in mdn.
213214
* throws {[UnsupportedPropertyError](./src/errors/UnsupportedPropertyError.js)} - if the propertyName is a shorthand property, but we don't support expanding it yet.
@@ -217,12 +218,8 @@ Currently supports the following properties:
217218
- animation
218219
- background
219220
- border
220-
- border-block-end
221-
- border-block-start
222221
- border-bottom
223222
- border-color
224-
- border-inline-end
225-
- border-inline-start
226223
- border-left
227224
- border-radius
228225
- border-right

src/expandShorthandProperty.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const R_BLOCK_COMMENT = /\/\*.*?\*\//g;
4242
* expand to additional shorthands. For example, the border property
4343
* expands to border-width, which expands further to border-left-width,
4444
* border-right-width, etc.
45+
* @param {boolean} [includeInitialValues=false] - when expanding the shorthand property, fill in any missing longhand
46+
* values with their initial value. For example, the property
47+
* declaration "border: 1px" only explicitly sets the "border-width"
48+
* longhand property. If this param is true, the returned object will
49+
* fill in the initial values for "border-style" and "border-color". By
50+
* default, the returned object will only contain the "border-width".
4551
* @return {Object} - object mapping longhand property names to values
4652
*
4753
* @throws {ParseError} - if the propertyValue cannot be parsed.
@@ -65,9 +71,11 @@ const R_BLOCK_COMMENT = /\/\*.*?\*\//g;
6571
* 'flex-shrink': 'initial',
6672
* 'flex-basis': 'initial',
6773
* }
68-
* TODO: add another param to include initial values for values not set
6974
*/
70-
module.exports = function expandShorthandProperty(propertyName, propertyValue, recursivelyResolve = false) {
75+
module.exports = function expandShorthandProperty(propertyName,
76+
propertyValue,
77+
recursivelyResolve = false,
78+
includeInitialValues = false) {
7179
if (!properties[propertyName]) {
7280
throw new UnknownPropertyError(propertyName);
7381
} else if (!isShorthandProperty(propertyName)) {
@@ -95,16 +103,23 @@ module.exports = function expandShorthandProperty(propertyName, propertyValue, r
95103
}
96104

97105
// get the first parsing and use the formatter for the specific shorthand type for this property
106+
const { shorthandType } = shorthandProperties[propertyName];
98107
const [rootNode] = parser.results;
99108
LocationIndexTracker.reset();
100-
const shorthandType = shorthandProperties[propertyName].shorthandType;
101109
const propertyExpansion = shorthandPropertyTypeToActionDictionaryFactoryMap[shorthandType]
102110
.format(propertyName, rootNode, formattedPropertyValue);
111+
const initialValuePropertyMap = getShorthandComputedProperties(propertyName, true)
112+
.reduce((initialValueMap, propertyName) => (Array.isArray(properties[propertyName].initial)
113+
? initialValueMap
114+
: Object.assign({ [propertyName]: properties[propertyName].initial }, initialValueMap)), {});
115+
const expandedProperties = includeInitialValues
116+
? Object.assign(initialValuePropertyMap, propertyExpansion)
117+
: propertyExpansion;
103118

104119
// if we need to recursively resolve, go through each value and expand it.
105120
return recursivelyResolve ? Object.entries(propertyExpansion)
106121
.reduce((propertyExpansion, [name, value]) => (
107-
Object.assign(expandShorthandProperty(name, value, true), propertyExpansion)
108-
), propertyExpansion)
109-
: propertyExpansion;
122+
Object.assign(propertyExpansion, expandShorthandProperty(name, value, true))
123+
), expandedProperties)
124+
: expandedProperties;
110125
};

test/ExpandShorthandPropertyTest.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@ describe('expandShorthandProperty', function () {
177177
});
178178
});
179179

180+
it('should return expanded border with style, color with recursively resolved properties', function () {
181+
const result = expandShorthandProperty('border', 'solid black', true);
182+
183+
assert.deepEqual(result, {
184+
'border-style': 'solid',
185+
'border-color': 'black',
186+
'border-bottom-style': 'solid',
187+
'border-left-style': 'solid',
188+
'border-right-style': 'solid',
189+
'border-top-style': 'solid',
190+
'border-bottom-color': 'black',
191+
'border-right-color': 'black',
192+
'border-left-color': 'black',
193+
'border-top-color': 'black',
194+
});
195+
});
196+
180197
it('should return expanded border with just color', function () {
181198
const result = expandShorthandProperty('border', 'black');
182199

@@ -862,7 +879,7 @@ describe('expandShorthandProperty', function () {
862879
assert.deepEqual(result, {
863880
'font-stretch': 'ultra-condensed',
864881
'font-size': '16px',
865-
'font-family': 'sans-serif'
882+
'font-family': 'sans-serif',
866883
});
867884
});
868885

@@ -894,4 +911,40 @@ describe('expandShorthandProperty', function () {
894911
});
895912
});
896913
});
914+
915+
describe('include initial values', function () {
916+
it('should include initial values for font', function () {
917+
const result = expandShorthandProperty('font', 'ultra-condensed 16px sans-serif', true, true);
918+
919+
assert.deepEqual(result, {
920+
'font-family': 'sans-serif',
921+
'font-size': '16px',
922+
'font-stretch': 'ultra-condensed',
923+
'font-style': 'normal',
924+
'font-variant': 'normal',
925+
'font-weight': 'normal',
926+
'line-height': 'normal',
927+
});
928+
});
929+
930+
it('should include initial values for border', function () {
931+
const result = expandShorthandProperty('border', '1px', true, true);
932+
933+
assert.deepEqual(result, {
934+
'border-bottom-color': 'currentcolor',
935+
'border-bottom-style': 'none',
936+
'border-bottom-width': '1px',
937+
'border-left-color': 'currentcolor',
938+
'border-left-style': 'none',
939+
'border-left-width': '1px',
940+
'border-right-color': 'currentcolor',
941+
'border-right-style': 'none',
942+
'border-right-width': '1px',
943+
'border-top-color': 'currentcolor',
944+
'border-top-style': 'none',
945+
'border-top-width': '1px',
946+
'border-width': '1px',
947+
});
948+
});
949+
});
897950
});

0 commit comments

Comments
 (0)