style-to-object 0.2.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -20
- package/dist/style-to-object.js +10 -5
- package/dist/style-to-object.min.js +1 -1
- package/dist/style-to-object.min.js.map +1 -1
- package/index.d.ts +38 -0
- package/index.js +1 -0
- package/index.mjs +3 -0
- package/package.json +36 -24
- package/CHANGELOG.md +0 -64
package/README.md
CHANGED
|
@@ -3,16 +3,15 @@
|
|
|
3
3
|
[](https://nodei.co/npm/style-to-object/)
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/style-to-object)
|
|
6
|
-
[](https://david-dm.org/remarkablemark/style-to-object)
|
|
6
|
+
[](https://github.com/remarkablemark/style-to-object/actions/workflows/build.yml)
|
|
7
|
+
[](https://codecov.io/gh/remarkablemark/style-to-object)
|
|
9
8
|
[](https://www.npmjs.com/package/style-to-object)
|
|
10
9
|
|
|
11
10
|
Parses inline style to object:
|
|
12
11
|
|
|
13
12
|
```js
|
|
14
|
-
var
|
|
15
|
-
|
|
13
|
+
var parse = require('style-to-object');
|
|
14
|
+
parse('color: #C0FFEE; background: #BADA55;');
|
|
16
15
|
```
|
|
17
16
|
|
|
18
17
|
Output:
|
|
@@ -21,20 +20,20 @@ Output:
|
|
|
21
20
|
{ color: '#C0FFEE', background: '#BADA55' }
|
|
22
21
|
```
|
|
23
22
|
|
|
24
|
-
[JSFiddle](https://jsfiddle.net/remarkablemark/ykz2meot/) | [
|
|
23
|
+
[JSFiddle](https://jsfiddle.net/remarkablemark/ykz2meot/) | [Replit](https://replit.com/@remarkablemark/style-to-object) | [Examples](https://github.com/remarkablemark/style-to-object/tree/master/examples)
|
|
25
24
|
|
|
26
25
|
## Installation
|
|
27
26
|
|
|
28
27
|
[NPM](https://www.npmjs.com/package/style-to-object):
|
|
29
28
|
|
|
30
29
|
```sh
|
|
31
|
-
|
|
30
|
+
npm install style-to-object --save
|
|
32
31
|
```
|
|
33
32
|
|
|
34
33
|
[Yarn](https://yarn.fyi/style-to-object):
|
|
35
34
|
|
|
36
35
|
```sh
|
|
37
|
-
|
|
36
|
+
yarn add style-to-object
|
|
38
37
|
```
|
|
39
38
|
|
|
40
39
|
[CDN](https://unpkg.com/style-to-object/):
|
|
@@ -99,6 +98,8 @@ Output:
|
|
|
99
98
|
|
|
100
99
|
Invalid declarations/arguments:
|
|
101
100
|
|
|
101
|
+
<!-- prettier-ignore-start -->
|
|
102
|
+
|
|
102
103
|
```js
|
|
103
104
|
parse(`
|
|
104
105
|
top: ;
|
|
@@ -118,6 +119,8 @@ parse('top'); // throws Error
|
|
|
118
119
|
parse('/*'); // throws Error
|
|
119
120
|
```
|
|
120
121
|
|
|
122
|
+
<!-- prettier-ignore-end -->
|
|
123
|
+
|
|
121
124
|
### Iterator
|
|
122
125
|
|
|
123
126
|
If the 2nd argument is a function, then the parser will return `null`:
|
|
@@ -128,14 +131,18 @@ parse('color: #f00', function() {}); // null
|
|
|
128
131
|
|
|
129
132
|
But the function will iterate through each declaration:
|
|
130
133
|
|
|
134
|
+
<!-- prettier-ignore-start -->
|
|
135
|
+
|
|
131
136
|
```js
|
|
132
|
-
|
|
137
|
+
parse('color: #f00', function(name, value, declaration) {
|
|
133
138
|
console.log(name); // 'color'
|
|
134
139
|
console.log(value); // '#f00'
|
|
135
140
|
console.log(declaration); // { type: 'declaration', property: 'color', value: '#f00' }
|
|
136
141
|
});
|
|
137
142
|
```
|
|
138
143
|
|
|
144
|
+
<!-- prettier-ignore-end -->
|
|
145
|
+
|
|
139
146
|
This makes it easy to customize the output:
|
|
140
147
|
|
|
141
148
|
```js
|
|
@@ -144,9 +151,11 @@ const style = `
|
|
|
144
151
|
background: blue;
|
|
145
152
|
`;
|
|
146
153
|
const output = [];
|
|
154
|
+
|
|
147
155
|
function iterator(name, value) {
|
|
148
156
|
output.push([name, value]);
|
|
149
157
|
}
|
|
158
|
+
|
|
150
159
|
parse(style, iterator);
|
|
151
160
|
console.log(output); // [['color', 'red'], ['background', 'blue']]
|
|
152
161
|
```
|
|
@@ -156,43 +165,43 @@ console.log(output); // [['color', 'red'], ['background', 'blue']]
|
|
|
156
165
|
Run tests:
|
|
157
166
|
|
|
158
167
|
```sh
|
|
159
|
-
|
|
168
|
+
npm test
|
|
160
169
|
```
|
|
161
170
|
|
|
162
171
|
Run tests in watch mode:
|
|
163
172
|
|
|
164
173
|
```sh
|
|
165
|
-
|
|
174
|
+
npm run test:watch
|
|
166
175
|
```
|
|
167
176
|
|
|
168
177
|
Run tests with coverage:
|
|
169
178
|
|
|
170
179
|
```sh
|
|
171
|
-
|
|
172
|
-
# npm run test:coverage:report
|
|
180
|
+
npm run test:coverage
|
|
173
181
|
```
|
|
174
182
|
|
|
175
183
|
Lint files:
|
|
176
184
|
|
|
177
185
|
```sh
|
|
178
|
-
|
|
186
|
+
npm run lint
|
|
179
187
|
```
|
|
180
188
|
|
|
181
189
|
Fix lint errors:
|
|
182
190
|
|
|
183
191
|
```sh
|
|
184
|
-
|
|
192
|
+
npm run lint:fix
|
|
185
193
|
```
|
|
186
194
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
Only collaborators with credentials can release and publish:
|
|
195
|
+
Test TypeScript declaration file for style and correctness:
|
|
190
196
|
|
|
191
197
|
```sh
|
|
192
|
-
|
|
193
|
-
$ git push --follow-tags && npm publish
|
|
198
|
+
npm run lint:dts
|
|
194
199
|
```
|
|
195
200
|
|
|
201
|
+
## Release
|
|
202
|
+
|
|
203
|
+
Release and publish are automated by [Release Please](https://github.com/googleapis/release-please).
|
|
204
|
+
|
|
196
205
|
## Special Thanks
|
|
197
206
|
|
|
198
207
|
- [inline-style-parser](https://github.com/remarkablemark/inline-style-parser)
|
package/dist/style-to-object.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
2
|
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
3
|
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
-
(global = global || self, global.StyleToObject = factory());
|
|
5
|
-
}(this, function () { 'use strict';
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.StyleToObject = factory());
|
|
5
|
+
})(this, (function () { 'use strict';
|
|
6
|
+
|
|
7
|
+
var styleToObject = {exports: {}};
|
|
6
8
|
|
|
7
9
|
// http://www.w3.org/TR/CSS21/grammar.html
|
|
8
10
|
// https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027
|
|
@@ -262,6 +264,8 @@
|
|
|
262
264
|
return str ? str.replace(TRIM_REGEX, EMPTY_STRING) : EMPTY_STRING;
|
|
263
265
|
}
|
|
264
266
|
|
|
267
|
+
var parse = inlineStyleParser;
|
|
268
|
+
|
|
265
269
|
/**
|
|
266
270
|
* Parses inline style to object.
|
|
267
271
|
*
|
|
@@ -280,7 +284,7 @@
|
|
|
280
284
|
}
|
|
281
285
|
|
|
282
286
|
var declaration;
|
|
283
|
-
var declarations =
|
|
287
|
+
var declarations = parse(style);
|
|
284
288
|
var hasIterator = typeof iterator === 'function';
|
|
285
289
|
var property;
|
|
286
290
|
var value;
|
|
@@ -301,8 +305,9 @@
|
|
|
301
305
|
return output;
|
|
302
306
|
}
|
|
303
307
|
|
|
304
|
-
|
|
308
|
+
styleToObject.exports = StyleToObject;
|
|
309
|
+
styleToObject.exports.default = StyleToObject; // ESM support
|
|
305
310
|
|
|
306
|
-
return styleToObject;
|
|
311
|
+
return styleToObject.exports;
|
|
307
312
|
|
|
308
313
|
}));
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(
|
|
1
|
+
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(r="undefined"!=typeof globalThis?globalThis:r||self).StyleToObject=e()}(this,(function(){"use strict";var r={exports:{}},e=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,n=/\n/g,t=/^\s*/,o=/^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/,i=/^:\s*/,u=/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/,c=/^[;\s]*/,f=/^\s+|\s+$/g,s="";function a(r){return r?r.replace(f,s):s}var l=function(r,f){if("string"!=typeof r)throw new TypeError("First argument must be a string");if(!r)return[];f=f||{};var l=1,p=1;function h(r){var e=r.match(n);e&&(l+=e.length);var t=r.lastIndexOf("\n");p=~t?r.length-t:p+r.length}function v(){var r={line:l,column:p};return function(e){return e.position=new m(r),y(),e}}function m(r){this.start=r,this.end={line:l,column:p},this.source=f.source}function g(e){var n=new Error(f.source+":"+l+":"+p+": "+e);if(n.reason=e,n.filename=f.source,n.line=l,n.column=p,n.source=r,!f.silent)throw n}function d(e){var n=e.exec(r);if(n){var t=n[0];return h(t),r=r.slice(t.length),n}}function y(){d(t)}function x(r){var e;for(r=r||[];e=w();)!1!==e&&r.push(e);return r}function w(){var e=v();if("/"==r.charAt(0)&&"*"==r.charAt(1)){for(var n=2;s!=r.charAt(n)&&("*"!=r.charAt(n)||"/"!=r.charAt(n+1));)++n;if(n+=2,s===r.charAt(n-1))return g("End of comment missing");var t=r.slice(2,n-2);return p+=2,h(t),r=r.slice(n),p+=2,e({type:"comment",comment:t})}}function A(){var r=v(),n=d(o);if(n){if(w(),!d(i))return g("property missing ':'");var t=d(u),f=r({type:"declaration",property:a(n[0].replace(e,s)),value:t?a(t[0].replace(e,s)):s});return d(c),f}}return m.prototype.content=r,y(),function(){var r,e=[];for(x(e);r=A();)!1!==r&&(e.push(r),x(e));return e}()};function p(r,e){var n,t=null;if(!r||"string"!=typeof r)return t;for(var o,i,u=l(r),c="function"==typeof e,f=0,s=u.length;f<s;f++)o=(n=u[f]).property,i=n.value,c?e(o,i,n):i&&(t||(t={}),t[o]=i);return t}return r.exports=p,r.exports.default=p,r.exports}));
|
|
2
2
|
//# sourceMappingURL=style-to-object.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"style-to-object.min.js","sources":["../node_modules/inline-style-parser/index.js","../index.js"],"sourcesContent":["// http://www.w3.org/TR/CSS21/grammar.html\n// https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027\nvar COMMENT_REGEX = /\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\//g;\n\nvar NEWLINE_REGEX = /\\n/g;\nvar WHITESPACE_REGEX = /^\\s*/;\n\n// declaration\nvar PROPERTY_REGEX = /^(\\*?[-#/*\\\\\\w]+(\\[[0-9a-z_-]+\\])?)\\s*/;\nvar COLON_REGEX = /^:\\s*/;\nvar VALUE_REGEX = /^((?:'(?:\\\\'|.)*?'|\"(?:\\\\\"|.)*?\"|\\([^)]*?\\)|[^};])+)/;\nvar SEMICOLON_REGEX = /^[;\\s]*/;\n\n// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill\nvar TRIM_REGEX = /^\\s+|\\s+$/g;\n\n// strings\nvar NEWLINE = '\\n';\nvar FORWARD_SLASH = '/';\nvar ASTERISK = '*';\nvar EMPTY_STRING = '';\n\n// types\nvar TYPE_COMMENT = 'comment';\nvar TYPE_DECLARATION = 'declaration';\n\n/**\n * @param {String} style\n * @param {Object} [options]\n * @return {Object[]}\n * @throws {TypeError}\n * @throws {Error}\n */\nmodule.exports = function(style, options) {\n if (typeof style !== 'string') {\n throw new TypeError('First argument must be a string');\n }\n\n if (!style) return [];\n\n options = options || {};\n\n /**\n * Positional.\n */\n var lineno = 1;\n var column = 1;\n\n /**\n * Update lineno and column based on `str`.\n *\n * @param {String} str\n */\n function updatePosition(str) {\n var lines = str.match(NEWLINE_REGEX);\n if (lines) lineno += lines.length;\n var i = str.lastIndexOf(NEWLINE);\n column = ~i ? str.length - i : column + str.length;\n }\n\n /**\n * Mark position and patch `node.position`.\n *\n * @return {Function}\n */\n function position() {\n var start = { line: lineno, column: column };\n return function(node) {\n node.position = new Position(start);\n whitespace();\n return node;\n };\n }\n\n /**\n * Store position information for a node.\n *\n * @constructor\n * @property {Object} start\n * @property {Object} end\n * @property {undefined|String} source\n */\n function Position(start) {\n this.start = start;\n this.end = { line: lineno, column: column };\n this.source = options.source;\n }\n\n /**\n * Non-enumerable source string.\n */\n Position.prototype.content = style;\n\n var errorsList = [];\n\n /**\n * Error `msg`.\n *\n * @param {String} msg\n * @throws {Error}\n */\n function error(msg) {\n var err = new Error(\n options.source + ':' + lineno + ':' + column + ': ' + msg\n );\n err.reason = msg;\n err.filename = options.source;\n err.line = lineno;\n err.column = column;\n err.source = style;\n\n if (options.silent) {\n errorsList.push(err);\n } else {\n throw err;\n }\n }\n\n /**\n * Match `re` and return captures.\n *\n * @param {RegExp} re\n * @return {undefined|Array}\n */\n function match(re) {\n var m = re.exec(style);\n if (!m) return;\n var str = m[0];\n updatePosition(str);\n style = style.slice(str.length);\n return m;\n }\n\n /**\n * Parse whitespace.\n */\n function whitespace() {\n match(WHITESPACE_REGEX);\n }\n\n /**\n * Parse comments.\n *\n * @param {Object[]} [rules]\n * @return {Object[]}\n */\n function comments(rules) {\n var c;\n rules = rules || [];\n while ((c = comment())) {\n if (c !== false) {\n rules.push(c);\n }\n }\n return rules;\n }\n\n /**\n * Parse comment.\n *\n * @return {Object}\n * @throws {Error}\n */\n function comment() {\n var pos = position();\n if (FORWARD_SLASH != style.charAt(0) || ASTERISK != style.charAt(1)) return;\n\n var i = 2;\n while (\n EMPTY_STRING != style.charAt(i) &&\n (ASTERISK != style.charAt(i) || FORWARD_SLASH != style.charAt(i + 1))\n ) {\n ++i;\n }\n i += 2;\n\n if (EMPTY_STRING === style.charAt(i - 1)) {\n return error('End of comment missing');\n }\n\n var str = style.slice(2, i - 2);\n column += 2;\n updatePosition(str);\n style = style.slice(i);\n column += 2;\n\n return pos({\n type: TYPE_COMMENT,\n comment: str\n });\n }\n\n /**\n * Parse declaration.\n *\n * @return {Object}\n * @throws {Error}\n */\n function declaration() {\n var pos = position();\n\n // prop\n var prop = match(PROPERTY_REGEX);\n if (!prop) return;\n comment();\n\n // :\n if (!match(COLON_REGEX)) return error(\"property missing ':'\");\n\n // val\n var val = match(VALUE_REGEX);\n\n var ret = pos({\n type: TYPE_DECLARATION,\n property: trim(prop[0].replace(COMMENT_REGEX, EMPTY_STRING)),\n value: val\n ? trim(val[0].replace(COMMENT_REGEX, EMPTY_STRING))\n : EMPTY_STRING\n });\n\n // ;\n match(SEMICOLON_REGEX);\n\n return ret;\n }\n\n /**\n * Parse declarations.\n *\n * @return {Object[]}\n */\n function declarations() {\n var decls = [];\n\n comments(decls);\n\n // declarations\n var decl;\n while ((decl = declaration())) {\n if (decl !== false) {\n decls.push(decl);\n comments(decls);\n }\n }\n\n return decls;\n }\n\n whitespace();\n return declarations();\n};\n\n/**\n * Trim `str`.\n *\n * @param {String} str\n * @return {String}\n */\nfunction trim(str) {\n return str ? str.replace(TRIM_REGEX, EMPTY_STRING) : EMPTY_STRING;\n}\n","var parse = require('inline-style-parser');\n\n/**\n * Parses inline style to object.\n *\n * @example\n * // returns { 'line-height': '42' }\n * StyleToObject('line-height: 42;');\n *\n * @param {String} style - The inline style.\n * @param {Function} [iterator] - The iterator function.\n * @return {null|Object}\n */\nfunction StyleToObject(style, iterator) {\n var output = null;\n if (!style || typeof style !== 'string') {\n return output;\n }\n\n var declaration;\n var declarations = parse(style);\n var hasIterator = typeof iterator === 'function';\n var property;\n var value;\n\n for (var i = 0, len = declarations.length; i < len; i++) {\n declaration = declarations[i];\n property = declaration.property;\n value = declaration.value;\n\n if (hasIterator) {\n iterator(property, value, declaration);\n } else if (value) {\n output || (output = {});\n output[property] = value;\n }\n }\n\n return output;\n}\n\nmodule.exports = StyleToObject;\n"],"names":["COMMENT_REGEX","NEWLINE_REGEX","WHITESPACE_REGEX","PROPERTY_REGEX","COLON_REGEX","VALUE_REGEX","SEMICOLON_REGEX","TRIM_REGEX","EMPTY_STRING","trim","str","replace","
|
|
1
|
+
{"version":3,"file":"style-to-object.min.js","sources":["../node_modules/inline-style-parser/index.js","../index.js"],"sourcesContent":["// http://www.w3.org/TR/CSS21/grammar.html\n// https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027\nvar COMMENT_REGEX = /\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\//g;\n\nvar NEWLINE_REGEX = /\\n/g;\nvar WHITESPACE_REGEX = /^\\s*/;\n\n// declaration\nvar PROPERTY_REGEX = /^(\\*?[-#/*\\\\\\w]+(\\[[0-9a-z_-]+\\])?)\\s*/;\nvar COLON_REGEX = /^:\\s*/;\nvar VALUE_REGEX = /^((?:'(?:\\\\'|.)*?'|\"(?:\\\\\"|.)*?\"|\\([^)]*?\\)|[^};])+)/;\nvar SEMICOLON_REGEX = /^[;\\s]*/;\n\n// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill\nvar TRIM_REGEX = /^\\s+|\\s+$/g;\n\n// strings\nvar NEWLINE = '\\n';\nvar FORWARD_SLASH = '/';\nvar ASTERISK = '*';\nvar EMPTY_STRING = '';\n\n// types\nvar TYPE_COMMENT = 'comment';\nvar TYPE_DECLARATION = 'declaration';\n\n/**\n * @param {String} style\n * @param {Object} [options]\n * @return {Object[]}\n * @throws {TypeError}\n * @throws {Error}\n */\nmodule.exports = function(style, options) {\n if (typeof style !== 'string') {\n throw new TypeError('First argument must be a string');\n }\n\n if (!style) return [];\n\n options = options || {};\n\n /**\n * Positional.\n */\n var lineno = 1;\n var column = 1;\n\n /**\n * Update lineno and column based on `str`.\n *\n * @param {String} str\n */\n function updatePosition(str) {\n var lines = str.match(NEWLINE_REGEX);\n if (lines) lineno += lines.length;\n var i = str.lastIndexOf(NEWLINE);\n column = ~i ? str.length - i : column + str.length;\n }\n\n /**\n * Mark position and patch `node.position`.\n *\n * @return {Function}\n */\n function position() {\n var start = { line: lineno, column: column };\n return function(node) {\n node.position = new Position(start);\n whitespace();\n return node;\n };\n }\n\n /**\n * Store position information for a node.\n *\n * @constructor\n * @property {Object} start\n * @property {Object} end\n * @property {undefined|String} source\n */\n function Position(start) {\n this.start = start;\n this.end = { line: lineno, column: column };\n this.source = options.source;\n }\n\n /**\n * Non-enumerable source string.\n */\n Position.prototype.content = style;\n\n var errorsList = [];\n\n /**\n * Error `msg`.\n *\n * @param {String} msg\n * @throws {Error}\n */\n function error(msg) {\n var err = new Error(\n options.source + ':' + lineno + ':' + column + ': ' + msg\n );\n err.reason = msg;\n err.filename = options.source;\n err.line = lineno;\n err.column = column;\n err.source = style;\n\n if (options.silent) {\n errorsList.push(err);\n } else {\n throw err;\n }\n }\n\n /**\n * Match `re` and return captures.\n *\n * @param {RegExp} re\n * @return {undefined|Array}\n */\n function match(re) {\n var m = re.exec(style);\n if (!m) return;\n var str = m[0];\n updatePosition(str);\n style = style.slice(str.length);\n return m;\n }\n\n /**\n * Parse whitespace.\n */\n function whitespace() {\n match(WHITESPACE_REGEX);\n }\n\n /**\n * Parse comments.\n *\n * @param {Object[]} [rules]\n * @return {Object[]}\n */\n function comments(rules) {\n var c;\n rules = rules || [];\n while ((c = comment())) {\n if (c !== false) {\n rules.push(c);\n }\n }\n return rules;\n }\n\n /**\n * Parse comment.\n *\n * @return {Object}\n * @throws {Error}\n */\n function comment() {\n var pos = position();\n if (FORWARD_SLASH != style.charAt(0) || ASTERISK != style.charAt(1)) return;\n\n var i = 2;\n while (\n EMPTY_STRING != style.charAt(i) &&\n (ASTERISK != style.charAt(i) || FORWARD_SLASH != style.charAt(i + 1))\n ) {\n ++i;\n }\n i += 2;\n\n if (EMPTY_STRING === style.charAt(i - 1)) {\n return error('End of comment missing');\n }\n\n var str = style.slice(2, i - 2);\n column += 2;\n updatePosition(str);\n style = style.slice(i);\n column += 2;\n\n return pos({\n type: TYPE_COMMENT,\n comment: str\n });\n }\n\n /**\n * Parse declaration.\n *\n * @return {Object}\n * @throws {Error}\n */\n function declaration() {\n var pos = position();\n\n // prop\n var prop = match(PROPERTY_REGEX);\n if (!prop) return;\n comment();\n\n // :\n if (!match(COLON_REGEX)) return error(\"property missing ':'\");\n\n // val\n var val = match(VALUE_REGEX);\n\n var ret = pos({\n type: TYPE_DECLARATION,\n property: trim(prop[0].replace(COMMENT_REGEX, EMPTY_STRING)),\n value: val\n ? trim(val[0].replace(COMMENT_REGEX, EMPTY_STRING))\n : EMPTY_STRING\n });\n\n // ;\n match(SEMICOLON_REGEX);\n\n return ret;\n }\n\n /**\n * Parse declarations.\n *\n * @return {Object[]}\n */\n function declarations() {\n var decls = [];\n\n comments(decls);\n\n // declarations\n var decl;\n while ((decl = declaration())) {\n if (decl !== false) {\n decls.push(decl);\n comments(decls);\n }\n }\n\n return decls;\n }\n\n whitespace();\n return declarations();\n};\n\n/**\n * Trim `str`.\n *\n * @param {String} str\n * @return {String}\n */\nfunction trim(str) {\n return str ? str.replace(TRIM_REGEX, EMPTY_STRING) : EMPTY_STRING;\n}\n","var parse = require('inline-style-parser');\n\n/**\n * Parses inline style to object.\n *\n * @example\n * // returns { 'line-height': '42' }\n * StyleToObject('line-height: 42;');\n *\n * @param {String} style - The inline style.\n * @param {Function} [iterator] - The iterator function.\n * @return {null|Object}\n */\nfunction StyleToObject(style, iterator) {\n var output = null;\n if (!style || typeof style !== 'string') {\n return output;\n }\n\n var declaration;\n var declarations = parse(style);\n var hasIterator = typeof iterator === 'function';\n var property;\n var value;\n\n for (var i = 0, len = declarations.length; i < len; i++) {\n declaration = declarations[i];\n property = declaration.property;\n value = declaration.value;\n\n if (hasIterator) {\n iterator(property, value, declaration);\n } else if (value) {\n output || (output = {});\n output[property] = value;\n }\n }\n\n return output;\n}\n\nmodule.exports = StyleToObject;\nmodule.exports.default = StyleToObject; // ESM support\n"],"names":["COMMENT_REGEX","NEWLINE_REGEX","WHITESPACE_REGEX","PROPERTY_REGEX","COLON_REGEX","VALUE_REGEX","SEMICOLON_REGEX","TRIM_REGEX","EMPTY_STRING","trim","str","replace","parse","style","options","TypeError","lineno","column","updatePosition","lines","match","length","i","lastIndexOf","position","start","line","node","Position","whitespace","this","end","source","error","msg","err","Error","reason","filename","silent","re","m","exec","slice","comments","rules","c","comment","push","pos","charAt","type","declaration","prop","val","ret","property","value","prototype","content","decl","decls","declarations","StyleToObject","iterator","output","hasIterator","len","styleToObjectModule","exports","styleToObject","default"],"mappings":"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"}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses inline style to object.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* import StyleToObject from 'style-to-object';
|
|
6
|
+
* StyleToObject('line-height: 42;');
|
|
7
|
+
*/
|
|
8
|
+
declare function StyleToObject(
|
|
9
|
+
style: string,
|
|
10
|
+
iterator?: StyleToObject.Iterator
|
|
11
|
+
): { [name: string]: string } | null;
|
|
12
|
+
|
|
13
|
+
export = StyleToObject;
|
|
14
|
+
|
|
15
|
+
declare namespace StyleToObject {
|
|
16
|
+
interface DeclarationPos {
|
|
17
|
+
line: number;
|
|
18
|
+
column: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// declaration is an object from module `inline-style-parser`
|
|
22
|
+
interface Declaration {
|
|
23
|
+
type: string;
|
|
24
|
+
property: string;
|
|
25
|
+
value: string;
|
|
26
|
+
position: {
|
|
27
|
+
start: DeclarationPos;
|
|
28
|
+
end: DeclarationPos;
|
|
29
|
+
source: any;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type Iterator = (
|
|
34
|
+
property: string,
|
|
35
|
+
value: string,
|
|
36
|
+
declaration: Declaration
|
|
37
|
+
) => void;
|
|
38
|
+
}
|
package/index.js
CHANGED
package/index.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "style-to-object",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Converts inline style to object.",
|
|
5
5
|
"author": "Mark <mark@remarkablemark.org>",
|
|
6
6
|
"main": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"module": "index.mjs",
|
|
9
|
+
"exports": {
|
|
10
|
+
"import": "./index.mjs",
|
|
11
|
+
"require": "./index.js"
|
|
12
|
+
},
|
|
7
13
|
"scripts": {
|
|
8
|
-
"build": "
|
|
14
|
+
"build": "run-s build:*",
|
|
9
15
|
"build:min": "NODE_ENV=production rollup --config --file dist/style-to-object.min.js --sourcemap",
|
|
10
16
|
"build:unmin": "NODE_ENV=development rollup --config --file dist/style-to-object.js",
|
|
11
17
|
"clean": "rm -rf dist",
|
|
12
|
-
"coveralls": "nyc report --reporter=text-lcov | coveralls",
|
|
13
18
|
"lint": "eslint --ignore-path .gitignore .",
|
|
14
19
|
"lint:fix": "npm run lint -- --fix",
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
20
|
+
"lint:dts": "dtslint .",
|
|
21
|
+
"_postinstall": "husky install",
|
|
22
|
+
"postpublish": "pinst --enable",
|
|
23
|
+
"prepublishOnly": "pinst --disable && run-s lint lint:dts test clean build",
|
|
24
|
+
"test": "mocha --exclude **/*.mjs",
|
|
18
25
|
"test:coverage": "nyc npm test",
|
|
19
|
-
"test:
|
|
20
|
-
"test:watch": "
|
|
26
|
+
"test:module": "node --experimental-modules test/index.mjs",
|
|
27
|
+
"test:watch": "npm run test -- --watch"
|
|
21
28
|
},
|
|
22
29
|
"repository": {
|
|
23
30
|
"type": "git",
|
|
@@ -39,24 +46,29 @@
|
|
|
39
46
|
"inline-style-parser": "0.1.1"
|
|
40
47
|
},
|
|
41
48
|
"devDependencies": {
|
|
42
|
-
"@commitlint/cli": "^
|
|
43
|
-
"@commitlint/config-conventional": "^
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
49
|
+
"@commitlint/cli": "^17.0.1",
|
|
50
|
+
"@commitlint/config-conventional": "^17.0.0",
|
|
51
|
+
"@rollup/plugin-commonjs": "^23.0.0",
|
|
52
|
+
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
53
|
+
"dtslint": "^4.2.1",
|
|
54
|
+
"eslint": "^8.16.0",
|
|
55
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
56
|
+
"husky": "^8.0.1",
|
|
57
|
+
"lint-staged": "^13.0.0",
|
|
58
|
+
"mocha": "^10.0.0",
|
|
59
|
+
"npm-run-all": "^4.1.5",
|
|
60
|
+
"nyc": "^15.1.0",
|
|
61
|
+
"pinst": "^3.0.0",
|
|
62
|
+
"prettier": "^2.6.2",
|
|
63
|
+
"rollup": "^2.75.3",
|
|
64
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
65
|
+
"typescript": "^4.7.2"
|
|
57
66
|
},
|
|
58
67
|
"files": [
|
|
59
|
-
"/dist"
|
|
68
|
+
"/dist",
|
|
69
|
+
"index.d.ts",
|
|
70
|
+
"index.js",
|
|
71
|
+
"index.mjs"
|
|
60
72
|
],
|
|
61
73
|
"license": "MIT"
|
|
62
74
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
-
|
|
5
|
-
### [0.2.3](https://github.com/remarkablemark/style-to-object/compare/v0.2.2...v0.2.3) (2019-06-22)
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
### Build System
|
|
9
|
-
|
|
10
|
-
* **package:** add field "files" and remove `.npmignore` ([fdf3966](https://github.com/remarkablemark/style-to-object/commit/fdf3966))
|
|
11
|
-
* **package:** update script `build:min` to generate sourcemap ([a13be58](https://github.com/remarkablemark/style-to-object/commit/a13be58))
|
|
12
|
-
* **package:** upgrade devDependencies ([377bb40](https://github.com/remarkablemark/style-to-object/commit/377bb40))
|
|
13
|
-
* **rollup:** remove `uglify-es` from config as it's unneeded ([b0951e0](https://github.com/remarkablemark/style-to-object/commit/b0951e0))
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
### Tests
|
|
17
|
-
|
|
18
|
-
* organize and rename describe blocks ([8d4c004](https://github.com/remarkablemark/style-to-object/commit/8d4c004))
|
|
19
|
-
* organize data (test suites) into cases, errors, and invalids ([513732b](https://github.com/remarkablemark/style-to-object/commit/513732b))
|
|
20
|
-
* rename `test/cases.js` to `test/data.js` ([75d084d](https://github.com/remarkablemark/style-to-object/commit/75d084d))
|
|
21
|
-
* **data:** add more test cases and errors ([c9242c7](https://github.com/remarkablemark/style-to-object/commit/c9242c7))
|
|
22
|
-
* **data:** refactor test data from object to array format ([1a07a38](https://github.com/remarkablemark/style-to-object/commit/1a07a38))
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
<a name="0.2.2"></a>
|
|
27
|
-
## [0.2.2](https://github.com/remarkablemark/style-to-object/compare/v0.2.1...v0.2.2) (2018-09-13)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
<a name="0.2.1"></a>
|
|
32
|
-
## [0.2.1](https://github.com/remarkablemark/style-to-object/compare/v0.2.0...v0.2.1) (2018-05-09)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
### Bug Fixes
|
|
36
|
-
|
|
37
|
-
* **package:** upgrade css@2.2.3 which resolves security vulnerability ([d8b94c0](https://github.com/remarkablemark/style-to-object/commit/d8b94c0))
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
<a name="0.2.0"></a>
|
|
42
|
-
# [0.2.0](https://github.com/remarkablemark/style-to-object/compare/v0.1.0...v0.2.0) (2017-11-26)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
### Features
|
|
46
|
-
|
|
47
|
-
* **parser:** add optional argument iterator ([a3deea8](https://github.com/remarkablemark/style-to-object/commit/a3deea8))
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
<a name="0.1.0"></a>
|
|
52
|
-
# 0.1.0 (2017-11-23)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
### Bug Fixes
|
|
56
|
-
|
|
57
|
-
* **parser:** do not add to output if css value is empty ([0759da7](https://github.com/remarkablemark/style-to-object/commit/0759da7))
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
### Features
|
|
61
|
-
|
|
62
|
-
* **parser:** create client parser ([cd85a31](https://github.com/remarkablemark/style-to-object/commit/cd85a31))
|
|
63
|
-
* **parser:** create parser that returns null for invalid values ([24f4f02](https://github.com/remarkablemark/style-to-object/commit/24f4f02))
|
|
64
|
-
* **parser:** parse inline style to object with css.parse ([04793b0](https://github.com/remarkablemark/style-to-object/commit/04793b0))
|