Skip to content

Commit 68dc827

Browse files
authored
Merge pull request #2 from css-blocks/cleanup
Cleanup
2 parents 1521e22 + d658bce commit 68dc827

File tree

7 files changed

+187
-113
lines changed

7 files changed

+187
-113
lines changed

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- [Examples](#examples-1)
1818
- [getShorthandComputedProperties(property: string): Array](#getshorthandcomputedpropertiesproperty-string-array)
1919
- [Examples](#examples-2)
20-
- [expandPropertyShorthand(property: string, value: string, [recursivelyResolve=false]): Object](#expandpropertyshorthandproperty-string-value-string-recursivelyresolvefalse-object)
20+
- [expandShorthandProperty(property: string, value: string, [recursivelyResolve=false]): Object](#expandshorthandpropertyproperty-string-value-string-recursivelyresolvetrue-object)
2121
- [Examples](#examples-3)
2222

2323
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
@@ -74,15 +74,15 @@ console.log(getShorthandComputedProperties('unknown'))
7474

7575
// expandShorthandProperty
7676
// returns an obejct mapping longhand property names to their values
77-
console.log(expandPropertyShorthand('margin', '0 3px 10rem'))
77+
console.log(expandShorthandProperty('margin', '0 3px 10rem'))
7878
// => {
7979
// 'margin-top': '0',
8080
// 'margin-right': '3px',
8181
// 'margin-bottom': '10rem',
8282
// 'margin-left': '3px',
8383
// }
8484

85-
console.log(expandPropertyShorthand('background', 'fixed padding-box url(image.png) rgb(255, 255, 0) 10px top / cover repeat-x'))
85+
console.log(expandShorthandProperty('background', 'fixed padding-box url(image.png) rgb(255, 255, 0) 10px top / cover repeat-x'))
8686
// => {
8787
// 'background-attachment': 'fixed',
8888
// 'background-clip': 'padding-box',
@@ -222,7 +222,7 @@ Currently supports the following properties:
222222
##### Examples
223223

224224
```js
225-
expandPropertyShorthand('margin', '0 3px 10rem')
225+
expandShorthandProperty('margin', '0 3px 10rem')
226226
// => {
227227
// 'margin-top': '0',
228228
// 'margin-right': '3px',
@@ -232,7 +232,7 @@ expandPropertyShorthand('margin', '0 3px 10rem')
232232
```
233233

234234
```js
235-
expandPropertyShorthand('flex', 'initial')
235+
expandShorthandProperty('flex', 'initial')
236236
// => {
237237
// 'flex-grow': 'initial',
238238
// 'flex-shrink': 'initial',
@@ -241,11 +241,24 @@ expandPropertyShorthand('flex', 'initial')
241241
```
242242

243243
```js
244-
expandPropertyShorthand('border-radius', '10px 5px 2em / 20px 25px 30%')
244+
expandShorthandProperty('border-radius', '10px 5px 2em / 20px 25px 30%')
245245
// => {
246246
// 'border-top-left-radius': '10px / 20px',
247247
// 'border-top-right-radius': '5px / 25px',
248248
// 'border-bottom-left-radius': '5px / 25px',
249249
// 'border-bottom-right-radius': '2em / 30%',
250250
// }
251251
```
252+
253+
### Developer/Contribution HOWTO
254+
255+
To use a locally-built version of `css-values-parser`:
256+
257+
```
258+
$ npm install
259+
$ npm run start
260+
$ npm test
261+
```
262+
263+
This will generate grammars and javascript code required to parse the
264+
css properties.

css-property-parser.d.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export = CssPropertyParser;
2+
3+
declare namespace CssPropertyParser {
4+
interface Declarations {
5+
[property: string]: string;
6+
}
7+
8+
/**
9+
* Given a property and value attempts to expand the value into its longhand equivalents. Returns an object
10+
* mapping the property longhand names to the longhand values. If the property cannot be expanded (i.e. the property
11+
* is not a shorthand property) simply returns an object mapping the original property to the original value.
12+
*
13+
* @param {string} propertyName - the property name for the given value
14+
* @param {string} propertyValue - the value of the property
15+
* @param {boolean} [recursivelyResolve=true] - recursively resolve additional longhand properties if the shorthands
16+
* expand to additional shorthands. For example, the border property
17+
* expands to border-width, which expands further to border-left-width,
18+
* border-right-width, etc.
19+
*/
20+
function expandShorthandProperty(
21+
propertyName: string,
22+
propertyValue: string,
23+
recursivelyResolve?: boolean
24+
): Declarations;
25+
26+
/**
27+
* Given a shorthand property, returns an array of the computed properties for that shorthand property. If given
28+
* a known property that is not a shorthand, simply returns the given property. If given an unknown property,
29+
* returns an empty array.
30+
*
31+
* @param {string} shorthandProperty - the shorthand property name. For example, "background" or "border".
32+
* @returns {Array} - an array containing the computed properties for the given shorthand property. Returns an
33+
* empty array if the given property is not a valid property.
34+
*/
35+
function getShorthandComputedProperties(
36+
shorthandProperty: string
37+
): Array<string>;
38+
39+
/**
40+
* Checks if a given property is a shorthand property
41+
* @param {String} property - the property name
42+
* @returns {boolean} - true if property is a shorthand, false otherwise
43+
*/
44+
function isShorthandProperty(
45+
shorthandProperty: string
46+
): boolean;
47+
48+
/**
49+
* Checks if the given property, value pair is valid.
50+
*
51+
* @param {String} property - the property name. For example, 'border' or 'color'.
52+
* @param {String} value - the property value. For example, '1px solid black'.
53+
* @return {boolean} - true if the given value is valid for the property. Else, false.
54+
*/
55+
function isValidDeclaration(
56+
property: string,
57+
value: string
58+
): boolean;
59+
60+
}

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"name": "css-property-parser",
33
"version": "1.0.0",
44
"description": "Validate css properties and expand shorthand css properties",
5-
"main": "index.js",
5+
"main": "src/index.js",
66
"scripts": {
77
"test": "./node_modules/.bin/mocha --reporter spec --recursive",
8-
"start": "mkdir -p src/grammars/generated/json && node ./node_modules/nearley/bin/nearleyc.js ./src/grammars/nearley/formalSyntax.ne > ./src/grammars/js/formalSyntax.js && node ./src/scripts/updateBasicDataUnits.js && node ./src/scripts/formatData.js && node ./src/scripts/formatFormalSyntaxes.js && node ./src/scripts/formatGrammars.js",
8+
"start": "mkdir -p formatted-data src/grammars/generated/json && node ./node_modules/nearley/bin/nearleyc.js ./src/grammars/nearley/formalSyntax.ne > ./src/grammars/js/formalSyntax.js && node ./src/scripts/updateBasicDataUnits.js && node ./src/scripts/formatData.js && node ./src/scripts/formatFormalSyntaxes.js && node ./src/scripts/formatGrammars.js",
99
"clean": "rm -rf src/grammars/generated",
1010
"benchmark": "node test/benchmark.js",
1111
"doctoc": "node ./node_modules/doctoc/doctoc.js README.md",
@@ -15,6 +15,7 @@
1515
"pre-commit": [ "precommit-msg", "lint", "test"],
1616
"author": "mahirshah",
1717
"license": "ISC",
18+
"types": "css-property-parser.d.ts",
1819
"devDependencies": {
1920
"async": "^2.5.0",
2021
"benchmark": "^2.1.4",
@@ -29,13 +30,13 @@
2930
"microtime": "^2.1.6",
3031
"mocha": "^3.5.0",
3132
"pre-commit": "^1.2.2",
33+
"postcss-value-parser": "^3.3.0",
3234
"sinon": "^2.4.1"
3335
},
3436
"dependencies": {
3537
"fs-extra": "^3.0.1",
3638
"mdn-data": "1.0.0",
3739
"moo": "^0.4.1",
38-
"nearley": "^2.10.3",
39-
"postcss-value-parser": "^3.3.0"
40+
"nearley": "^2.10.3"
4041
}
4142
}

src/expandShorthandProperty.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const R_BLOCK_COMMENT = /\/\*.*?\*\//g;
5050
* yet.
5151
*
5252
* @example
53-
* expandPropertyShorthand('margin', '0 3px 10rem')
53+
* expandShorthandProperty('margin', '0 3px 10rem')
5454
* {
5555
* 'margin-top': '0',
5656
* 'margin-right': '3px',
@@ -59,7 +59,7 @@ const R_BLOCK_COMMENT = /\/\*.*?\*\//g;
5959
* }
6060
*
6161
* @example
62-
* expandPropertyShorthand('flex', 'initial')
62+
* expandShorthandProperty('flex', 'initial')
6363
* {
6464
* 'flex-grow': 'initial',
6565
* 'flex-shrink': 'initial',

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const expandPropertyShorthand = require('./expandShorthandProperty');
1+
const expandShorthandProperty = require('./expandShorthandProperty');
22
const getShorthandComputedProperties = require('./getShorthandComputedProperties');
33
const isShorthandProperty = require('./isShorthandProperty');
44
const isValidDeclaration = require('./isValidDeclaration');
55

66
module.exports = {
7-
expandPropertyShorthand,
7+
expandShorthandProperty,
88
getShorthandComputedProperties,
99
isShorthandProperty,
1010
isValidDeclaration,

0 commit comments

Comments
 (0)