style-to-object 0.2.2 → 0.2.3
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/CHANGELOG.md +22 -1
- package/README.md +84 -31
- package/dist/style-to-object.js +126 -466
- package/dist/style-to-object.min.js +2 -1
- package/dist/style-to-object.min.js.map +1 -0
- package/index.js +17 -11
- package/package.json +34 -21
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,28 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
2
2
|
|
|
3
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
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
|
+
|
|
5
26
|
<a name="0.2.2"></a>
|
|
6
27
|
## [0.2.2](https://github.com/remarkablemark/style-to-object/compare/v0.2.1...v0.2.2) (2018-09-13)
|
|
7
28
|
|
package/README.md
CHANGED
|
@@ -6,29 +6,35 @@
|
|
|
6
6
|
[](https://travis-ci.org/remarkablemark/style-to-object)
|
|
7
7
|
[](https://coveralls.io/github/remarkablemark/style-to-object?branch=master)
|
|
8
8
|
[](https://david-dm.org/remarkablemark/style-to-object)
|
|
9
|
+
[](https://www.npmjs.com/package/style-to-object)
|
|
9
10
|
|
|
10
11
|
Parses inline style to object:
|
|
11
12
|
|
|
12
13
|
```js
|
|
13
14
|
var parser = require('style-to-object');
|
|
14
15
|
parser('color: #C0FFEE; background: #BADA55;');
|
|
15
|
-
// { color: "#C0FFEE", background: "#BADA55" }
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
Output:
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
{ color: '#C0FFEE', background: '#BADA55' }
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
[JSFiddle](https://jsfiddle.net/remarkablemark/ykz2meot/) | [Repl.it](https://repl.it/@remarkablemark/style-to-object) | [Examples](https://github.com/remarkablemark/style-to-object/tree/master/examples)
|
|
19
25
|
|
|
20
26
|
## Installation
|
|
21
27
|
|
|
22
28
|
[NPM](https://www.npmjs.com/package/style-to-object):
|
|
23
29
|
|
|
24
30
|
```sh
|
|
25
|
-
npm install style-to-object --save
|
|
31
|
+
$ npm install style-to-object --save
|
|
26
32
|
```
|
|
27
33
|
|
|
28
34
|
[Yarn](https://yarn.fyi/style-to-object):
|
|
29
35
|
|
|
30
36
|
```sh
|
|
31
|
-
yarn add style-to-object
|
|
37
|
+
$ yarn add style-to-object
|
|
32
38
|
```
|
|
33
39
|
|
|
34
40
|
[CDN](https://unpkg.com/style-to-object/):
|
|
@@ -36,7 +42,7 @@ yarn add style-to-object
|
|
|
36
42
|
```html
|
|
37
43
|
<script src="https://unpkg.com/style-to-object@latest/dist/style-to-object.min.js"></script>
|
|
38
44
|
<script>
|
|
39
|
-
|
|
45
|
+
window.StyleToObject(/* string */);
|
|
40
46
|
</script>
|
|
41
47
|
```
|
|
42
48
|
|
|
@@ -46,50 +52,70 @@ Import the module:
|
|
|
46
52
|
|
|
47
53
|
```js
|
|
48
54
|
// CommonJS
|
|
49
|
-
const
|
|
55
|
+
const parse = require('style-to-object');
|
|
50
56
|
|
|
51
57
|
// ES Modules
|
|
52
|
-
import
|
|
58
|
+
import parse from 'style-to-object';
|
|
53
59
|
```
|
|
54
60
|
|
|
55
61
|
Parse single declaration:
|
|
56
62
|
|
|
57
63
|
```js
|
|
58
|
-
parse(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
parse('line-height: 42');
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Output:
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
{ 'line-height': '42' }
|
|
62
71
|
```
|
|
63
72
|
|
|
64
73
|
Parse multiple declarations:
|
|
65
74
|
|
|
66
75
|
```js
|
|
67
76
|
parse(`
|
|
68
|
-
color: #
|
|
69
|
-
z-index:
|
|
77
|
+
border-color: #ACE;
|
|
78
|
+
z-index: 1337;
|
|
70
79
|
`);
|
|
71
|
-
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Output:
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
{ 'border-color': '#ACE', 'z-index': '1337' }
|
|
72
86
|
```
|
|
73
87
|
|
|
74
88
|
Parse unknown declarations:
|
|
75
89
|
|
|
76
90
|
```js
|
|
77
|
-
parse(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
91
|
+
parse('answer: 42;');
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Output:
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
{ 'answer': '42' }
|
|
81
98
|
```
|
|
82
99
|
|
|
83
|
-
Invalid declarations:
|
|
100
|
+
Invalid declarations/arguments:
|
|
84
101
|
|
|
85
102
|
```js
|
|
86
|
-
parse(1); // null
|
|
87
|
-
parse('top:'); // null
|
|
88
103
|
parse(`
|
|
89
104
|
top: ;
|
|
90
105
|
right: 1em;
|
|
91
106
|
`); // { right: '1em' }
|
|
107
|
+
|
|
108
|
+
parse(); // null
|
|
109
|
+
parse(null); // null
|
|
110
|
+
parse(1); // null
|
|
111
|
+
parse(true); // null
|
|
112
|
+
parse('top:'); // null
|
|
113
|
+
parse(':12px'); // null
|
|
114
|
+
parse(':'); // null
|
|
115
|
+
parse(';'); // null
|
|
116
|
+
|
|
92
117
|
parse('top'); // throws Error
|
|
118
|
+
parse('/*'); // throws Error
|
|
93
119
|
```
|
|
94
120
|
|
|
95
121
|
### Iterator
|
|
@@ -97,7 +123,7 @@ parse('top'); // throws Error
|
|
|
97
123
|
If the 2nd argument is a function, then the parser will return `null`:
|
|
98
124
|
|
|
99
125
|
```js
|
|
100
|
-
|
|
126
|
+
parse('color: #f00', function() {}); // null
|
|
101
127
|
```
|
|
102
128
|
|
|
103
129
|
But the function will iterate through each declaration:
|
|
@@ -114,35 +140,62 @@ This makes it easy to customize the output:
|
|
|
114
140
|
|
|
115
141
|
```js
|
|
116
142
|
const style = `
|
|
117
|
-
color:
|
|
118
|
-
background:
|
|
143
|
+
color: red;
|
|
144
|
+
background: blue;
|
|
119
145
|
`;
|
|
120
146
|
const output = [];
|
|
121
|
-
|
|
147
|
+
function iterator(name, value) {
|
|
122
148
|
output.push([name, value]);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
console.log(output); // [['color', '
|
|
149
|
+
}
|
|
150
|
+
parse(style, iterator);
|
|
151
|
+
console.log(output); // [['color', 'red'], ['background', 'blue']]
|
|
126
152
|
```
|
|
127
153
|
|
|
128
154
|
## Testing
|
|
129
155
|
|
|
156
|
+
Run tests:
|
|
157
|
+
|
|
130
158
|
```sh
|
|
131
159
|
$ npm test
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Run tests in watch mode:
|
|
163
|
+
|
|
164
|
+
```sh
|
|
165
|
+
$ npm run test:watch
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Run tests with coverage:
|
|
169
|
+
|
|
170
|
+
```sh
|
|
171
|
+
$ npm run test:coverage
|
|
172
|
+
# npm run test:coverage:report
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Lint files:
|
|
176
|
+
|
|
177
|
+
```sh
|
|
132
178
|
$ npm run lint
|
|
133
179
|
```
|
|
134
180
|
|
|
181
|
+
Fix lint errors:
|
|
182
|
+
|
|
183
|
+
```sh
|
|
184
|
+
$ npm run lint:fix
|
|
185
|
+
```
|
|
186
|
+
|
|
135
187
|
## Release
|
|
136
188
|
|
|
189
|
+
Only collaborators with credentials can release and publish:
|
|
190
|
+
|
|
137
191
|
```sh
|
|
138
192
|
$ npm run release
|
|
139
|
-
$ npm publish
|
|
140
|
-
$ git push --follow-tags
|
|
193
|
+
$ git push --follow-tags && npm publish
|
|
141
194
|
```
|
|
142
195
|
|
|
143
196
|
## Special Thanks
|
|
144
197
|
|
|
145
|
-
- [
|
|
198
|
+
- [inline-style-parser](https://github.com/remarkablemark/inline-style-parser)
|
|
146
199
|
- [Contributors](https://github.com/remarkablemark/style-to-object/graphs/contributors)
|
|
147
200
|
|
|
148
201
|
## License
|
package/dist/style-to-object.js
CHANGED
|
@@ -1,41 +1,77 @@
|
|
|
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.StyleToObject = factory());
|
|
5
|
-
}(this,
|
|
4
|
+
(global = global || self, global.StyleToObject = factory());
|
|
5
|
+
}(this, function () { 'use strict';
|
|
6
6
|
|
|
7
7
|
// http://www.w3.org/TR/CSS21/grammar.html
|
|
8
8
|
// https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027
|
|
9
|
-
var
|
|
9
|
+
var COMMENT_REGEX = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;
|
|
10
|
+
|
|
11
|
+
var NEWLINE_REGEX = /\n/g;
|
|
12
|
+
var WHITESPACE_REGEX = /^\s*/;
|
|
13
|
+
|
|
14
|
+
// declaration
|
|
15
|
+
var PROPERTY_REGEX = /^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/;
|
|
16
|
+
var COLON_REGEX = /^:\s*/;
|
|
17
|
+
var VALUE_REGEX = /^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/;
|
|
18
|
+
var SEMICOLON_REGEX = /^[;\s]*/;
|
|
19
|
+
|
|
20
|
+
// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill
|
|
21
|
+
var TRIM_REGEX = /^\s+|\s+$/g;
|
|
22
|
+
|
|
23
|
+
// strings
|
|
24
|
+
var NEWLINE = '\n';
|
|
25
|
+
var FORWARD_SLASH = '/';
|
|
26
|
+
var ASTERISK = '*';
|
|
27
|
+
var EMPTY_STRING = '';
|
|
28
|
+
|
|
29
|
+
// types
|
|
30
|
+
var TYPE_COMMENT = 'comment';
|
|
31
|
+
var TYPE_DECLARATION = 'declaration';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {String} style
|
|
35
|
+
* @param {Object} [options]
|
|
36
|
+
* @return {Object[]}
|
|
37
|
+
* @throws {TypeError}
|
|
38
|
+
* @throws {Error}
|
|
39
|
+
*/
|
|
40
|
+
var inlineStyleParser = function(style, options) {
|
|
41
|
+
if (typeof style !== 'string') {
|
|
42
|
+
throw new TypeError('First argument must be a string');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!style) return [];
|
|
10
46
|
|
|
11
|
-
var parse = function(css, options){
|
|
12
47
|
options = options || {};
|
|
13
48
|
|
|
14
49
|
/**
|
|
15
50
|
* Positional.
|
|
16
51
|
*/
|
|
17
|
-
|
|
18
52
|
var lineno = 1;
|
|
19
53
|
var column = 1;
|
|
20
54
|
|
|
21
55
|
/**
|
|
22
56
|
* Update lineno and column based on `str`.
|
|
57
|
+
*
|
|
58
|
+
* @param {String} str
|
|
23
59
|
*/
|
|
24
|
-
|
|
25
60
|
function updatePosition(str) {
|
|
26
|
-
var lines = str.match(
|
|
61
|
+
var lines = str.match(NEWLINE_REGEX);
|
|
27
62
|
if (lines) lineno += lines.length;
|
|
28
|
-
var i = str.lastIndexOf(
|
|
63
|
+
var i = str.lastIndexOf(NEWLINE);
|
|
29
64
|
column = ~i ? str.length - i : column + str.length;
|
|
30
65
|
}
|
|
31
66
|
|
|
32
67
|
/**
|
|
33
68
|
* Mark position and patch `node.position`.
|
|
69
|
+
*
|
|
70
|
+
* @return {Function}
|
|
34
71
|
*/
|
|
35
|
-
|
|
36
72
|
function position() {
|
|
37
73
|
var start = { line: lineno, column: column };
|
|
38
|
-
return function(node){
|
|
74
|
+
return function(node) {
|
|
39
75
|
node.position = new Position(start);
|
|
40
76
|
whitespace();
|
|
41
77
|
return node;
|
|
@@ -43,9 +79,13 @@
|
|
|
43
79
|
}
|
|
44
80
|
|
|
45
81
|
/**
|
|
46
|
-
* Store position information for a node
|
|
82
|
+
* Store position information for a node.
|
|
83
|
+
*
|
|
84
|
+
* @constructor
|
|
85
|
+
* @property {Object} start
|
|
86
|
+
* @property {Object} end
|
|
87
|
+
* @property {undefined|String} source
|
|
47
88
|
*/
|
|
48
|
-
|
|
49
89
|
function Position(start) {
|
|
50
90
|
this.start = start;
|
|
51
91
|
this.end = { line: lineno, column: column };
|
|
@@ -53,112 +93,63 @@
|
|
|
53
93
|
}
|
|
54
94
|
|
|
55
95
|
/**
|
|
56
|
-
* Non-enumerable source string
|
|
96
|
+
* Non-enumerable source string.
|
|
57
97
|
*/
|
|
58
|
-
|
|
59
|
-
Position.prototype.content = css;
|
|
98
|
+
Position.prototype.content = style;
|
|
60
99
|
|
|
61
100
|
/**
|
|
62
101
|
* Error `msg`.
|
|
102
|
+
*
|
|
103
|
+
* @param {String} msg
|
|
104
|
+
* @throws {Error}
|
|
63
105
|
*/
|
|
64
|
-
|
|
65
|
-
var errorsList = [];
|
|
66
|
-
|
|
67
106
|
function error(msg) {
|
|
68
|
-
var err = new Error(
|
|
107
|
+
var err = new Error(
|
|
108
|
+
options.source + ':' + lineno + ':' + column + ': ' + msg
|
|
109
|
+
);
|
|
69
110
|
err.reason = msg;
|
|
70
111
|
err.filename = options.source;
|
|
71
112
|
err.line = lineno;
|
|
72
113
|
err.column = column;
|
|
73
|
-
err.source =
|
|
114
|
+
err.source = style;
|
|
74
115
|
|
|
75
|
-
if (options.silent) {
|
|
76
|
-
errorsList.push(err);
|
|
77
|
-
} else {
|
|
116
|
+
if (options.silent) ; else {
|
|
78
117
|
throw err;
|
|
79
118
|
}
|
|
80
119
|
}
|
|
81
120
|
|
|
82
|
-
/**
|
|
83
|
-
* Parse stylesheet.
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
function stylesheet() {
|
|
87
|
-
var rulesList = rules();
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
type: 'stylesheet',
|
|
91
|
-
stylesheet: {
|
|
92
|
-
source: options.source,
|
|
93
|
-
rules: rulesList,
|
|
94
|
-
parsingErrors: errorsList
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Opening brace.
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
function open() {
|
|
104
|
-
return match(/^{\s*/);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Closing brace.
|
|
109
|
-
*/
|
|
110
|
-
|
|
111
|
-
function close() {
|
|
112
|
-
return match(/^}/);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Parse ruleset.
|
|
117
|
-
*/
|
|
118
|
-
|
|
119
|
-
function rules() {
|
|
120
|
-
var node;
|
|
121
|
-
var rules = [];
|
|
122
|
-
whitespace();
|
|
123
|
-
comments(rules);
|
|
124
|
-
while (css.length && css.charAt(0) != '}' && (node = atrule() || rule())) {
|
|
125
|
-
if (node !== false) {
|
|
126
|
-
rules.push(node);
|
|
127
|
-
comments(rules);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
return rules;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
121
|
/**
|
|
134
122
|
* Match `re` and return captures.
|
|
123
|
+
*
|
|
124
|
+
* @param {RegExp} re
|
|
125
|
+
* @return {undefined|Array}
|
|
135
126
|
*/
|
|
136
|
-
|
|
137
127
|
function match(re) {
|
|
138
|
-
var m = re.exec(
|
|
128
|
+
var m = re.exec(style);
|
|
139
129
|
if (!m) return;
|
|
140
130
|
var str = m[0];
|
|
141
131
|
updatePosition(str);
|
|
142
|
-
|
|
132
|
+
style = style.slice(str.length);
|
|
143
133
|
return m;
|
|
144
134
|
}
|
|
145
135
|
|
|
146
136
|
/**
|
|
147
137
|
* Parse whitespace.
|
|
148
138
|
*/
|
|
149
|
-
|
|
150
139
|
function whitespace() {
|
|
151
|
-
match(
|
|
140
|
+
match(WHITESPACE_REGEX);
|
|
152
141
|
}
|
|
153
142
|
|
|
154
143
|
/**
|
|
155
|
-
* Parse comments
|
|
144
|
+
* Parse comments.
|
|
145
|
+
*
|
|
146
|
+
* @param {Object[]} [rules]
|
|
147
|
+
* @return {Object[]}
|
|
156
148
|
*/
|
|
157
|
-
|
|
158
149
|
function comments(rules) {
|
|
159
150
|
var c;
|
|
160
151
|
rules = rules || [];
|
|
161
|
-
while (c = comment()) {
|
|
152
|
+
while ((c = comment())) {
|
|
162
153
|
if (c !== false) {
|
|
163
154
|
rules.push(c);
|
|
164
155
|
}
|
|
@@ -168,464 +159,131 @@
|
|
|
168
159
|
|
|
169
160
|
/**
|
|
170
161
|
* Parse comment.
|
|
162
|
+
*
|
|
163
|
+
* @return {Object}
|
|
164
|
+
* @throws {Error}
|
|
171
165
|
*/
|
|
172
|
-
|
|
173
166
|
function comment() {
|
|
174
167
|
var pos = position();
|
|
175
|
-
if (
|
|
168
|
+
if (FORWARD_SLASH != style.charAt(0) || ASTERISK != style.charAt(1)) return;
|
|
176
169
|
|
|
177
170
|
var i = 2;
|
|
178
|
-
while (
|
|
171
|
+
while (
|
|
172
|
+
EMPTY_STRING != style.charAt(i) &&
|
|
173
|
+
(ASTERISK != style.charAt(i) || FORWARD_SLASH != style.charAt(i + 1))
|
|
174
|
+
) {
|
|
175
|
+
++i;
|
|
176
|
+
}
|
|
179
177
|
i += 2;
|
|
180
178
|
|
|
181
|
-
if (
|
|
179
|
+
if (EMPTY_STRING === style.charAt(i - 1)) {
|
|
182
180
|
return error('End of comment missing');
|
|
183
181
|
}
|
|
184
182
|
|
|
185
|
-
var str =
|
|
183
|
+
var str = style.slice(2, i - 2);
|
|
186
184
|
column += 2;
|
|
187
185
|
updatePosition(str);
|
|
188
|
-
|
|
186
|
+
style = style.slice(i);
|
|
189
187
|
column += 2;
|
|
190
188
|
|
|
191
189
|
return pos({
|
|
192
|
-
type:
|
|
190
|
+
type: TYPE_COMMENT,
|
|
193
191
|
comment: str
|
|
194
192
|
});
|
|
195
193
|
}
|
|
196
194
|
|
|
197
|
-
/**
|
|
198
|
-
* Parse selector.
|
|
199
|
-
*/
|
|
200
|
-
|
|
201
|
-
function selector() {
|
|
202
|
-
var m = match(/^([^{]+)/);
|
|
203
|
-
if (!m) return;
|
|
204
|
-
/* @fix Remove all comments from selectors
|
|
205
|
-
* http://ostermiller.org/findcomment.html */
|
|
206
|
-
return trim(m[0])
|
|
207
|
-
.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '')
|
|
208
|
-
.replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, function(m) {
|
|
209
|
-
return m.replace(/,/g, '\u200C');
|
|
210
|
-
})
|
|
211
|
-
.split(/\s*(?![^(]*\)),\s*/)
|
|
212
|
-
.map(function(s) {
|
|
213
|
-
return s.replace(/\u200C/g, ',');
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
195
|
/**
|
|
218
196
|
* Parse declaration.
|
|
197
|
+
*
|
|
198
|
+
* @return {Object}
|
|
199
|
+
* @throws {Error}
|
|
219
200
|
*/
|
|
220
|
-
|
|
221
201
|
function declaration() {
|
|
222
202
|
var pos = position();
|
|
223
203
|
|
|
224
204
|
// prop
|
|
225
|
-
var prop = match(
|
|
205
|
+
var prop = match(PROPERTY_REGEX);
|
|
226
206
|
if (!prop) return;
|
|
227
|
-
|
|
207
|
+
comment();
|
|
228
208
|
|
|
229
209
|
// :
|
|
230
|
-
if (!match(
|
|
210
|
+
if (!match(COLON_REGEX)) return error("property missing ':'");
|
|
231
211
|
|
|
232
212
|
// val
|
|
233
|
-
var val = match(
|
|
213
|
+
var val = match(VALUE_REGEX);
|
|
234
214
|
|
|
235
215
|
var ret = pos({
|
|
236
|
-
type:
|
|
237
|
-
property: prop.replace(
|
|
238
|
-
value: val
|
|
216
|
+
type: TYPE_DECLARATION,
|
|
217
|
+
property: trim(prop[0].replace(COMMENT_REGEX, EMPTY_STRING)),
|
|
218
|
+
value: val
|
|
219
|
+
? trim(val[0].replace(COMMENT_REGEX, EMPTY_STRING))
|
|
220
|
+
: EMPTY_STRING
|
|
239
221
|
});
|
|
240
222
|
|
|
241
223
|
// ;
|
|
242
|
-
match(
|
|
224
|
+
match(SEMICOLON_REGEX);
|
|
243
225
|
|
|
244
226
|
return ret;
|
|
245
227
|
}
|
|
246
228
|
|
|
247
229
|
/**
|
|
248
230
|
* Parse declarations.
|
|
231
|
+
*
|
|
232
|
+
* @return {Object[]}
|
|
249
233
|
*/
|
|
250
|
-
|
|
251
234
|
function declarations() {
|
|
252
235
|
var decls = [];
|
|
253
236
|
|
|
254
|
-
if (!open()) return error("missing '{'");
|
|
255
237
|
comments(decls);
|
|
256
238
|
|
|
257
239
|
// declarations
|
|
258
240
|
var decl;
|
|
259
|
-
while (decl = declaration()) {
|
|
241
|
+
while ((decl = declaration())) {
|
|
260
242
|
if (decl !== false) {
|
|
261
243
|
decls.push(decl);
|
|
262
244
|
comments(decls);
|
|
263
245
|
}
|
|
264
246
|
}
|
|
265
247
|
|
|
266
|
-
if (!close()) return error("missing '}'");
|
|
267
248
|
return decls;
|
|
268
249
|
}
|
|
269
250
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
*/
|
|
273
|
-
|
|
274
|
-
function keyframe() {
|
|
275
|
-
var m;
|
|
276
|
-
var vals = [];
|
|
277
|
-
var pos = position();
|
|
278
|
-
|
|
279
|
-
while (m = match(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/)) {
|
|
280
|
-
vals.push(m[1]);
|
|
281
|
-
match(/^,\s*/);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
if (!vals.length) return;
|
|
285
|
-
|
|
286
|
-
return pos({
|
|
287
|
-
type: 'keyframe',
|
|
288
|
-
values: vals,
|
|
289
|
-
declarations: declarations()
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Parse keyframes.
|
|
295
|
-
*/
|
|
296
|
-
|
|
297
|
-
function atkeyframes() {
|
|
298
|
-
var pos = position();
|
|
299
|
-
var m = match(/^@([-\w]+)?keyframes\s*/);
|
|
300
|
-
|
|
301
|
-
if (!m) return;
|
|
302
|
-
var vendor = m[1];
|
|
303
|
-
|
|
304
|
-
// identifier
|
|
305
|
-
var m = match(/^([-\w]+)\s*/);
|
|
306
|
-
if (!m) return error("@keyframes missing name");
|
|
307
|
-
var name = m[1];
|
|
308
|
-
|
|
309
|
-
if (!open()) return error("@keyframes missing '{'");
|
|
310
|
-
|
|
311
|
-
var frame;
|
|
312
|
-
var frames = comments();
|
|
313
|
-
while (frame = keyframe()) {
|
|
314
|
-
frames.push(frame);
|
|
315
|
-
frames = frames.concat(comments());
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
if (!close()) return error("@keyframes missing '}'");
|
|
319
|
-
|
|
320
|
-
return pos({
|
|
321
|
-
type: 'keyframes',
|
|
322
|
-
name: name,
|
|
323
|
-
vendor: vendor,
|
|
324
|
-
keyframes: frames
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Parse supports.
|
|
330
|
-
*/
|
|
331
|
-
|
|
332
|
-
function atsupports() {
|
|
333
|
-
var pos = position();
|
|
334
|
-
var m = match(/^@supports *([^{]+)/);
|
|
335
|
-
|
|
336
|
-
if (!m) return;
|
|
337
|
-
var supports = trim(m[1]);
|
|
338
|
-
|
|
339
|
-
if (!open()) return error("@supports missing '{'");
|
|
340
|
-
|
|
341
|
-
var style = comments().concat(rules());
|
|
342
|
-
|
|
343
|
-
if (!close()) return error("@supports missing '}'");
|
|
344
|
-
|
|
345
|
-
return pos({
|
|
346
|
-
type: 'supports',
|
|
347
|
-
supports: supports,
|
|
348
|
-
rules: style
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Parse host.
|
|
354
|
-
*/
|
|
355
|
-
|
|
356
|
-
function athost() {
|
|
357
|
-
var pos = position();
|
|
358
|
-
var m = match(/^@host\s*/);
|
|
359
|
-
|
|
360
|
-
if (!m) return;
|
|
361
|
-
|
|
362
|
-
if (!open()) return error("@host missing '{'");
|
|
363
|
-
|
|
364
|
-
var style = comments().concat(rules());
|
|
365
|
-
|
|
366
|
-
if (!close()) return error("@host missing '}'");
|
|
367
|
-
|
|
368
|
-
return pos({
|
|
369
|
-
type: 'host',
|
|
370
|
-
rules: style
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Parse media.
|
|
376
|
-
*/
|
|
377
|
-
|
|
378
|
-
function atmedia() {
|
|
379
|
-
var pos = position();
|
|
380
|
-
var m = match(/^@media *([^{]+)/);
|
|
381
|
-
|
|
382
|
-
if (!m) return;
|
|
383
|
-
var media = trim(m[1]);
|
|
384
|
-
|
|
385
|
-
if (!open()) return error("@media missing '{'");
|
|
386
|
-
|
|
387
|
-
var style = comments().concat(rules());
|
|
388
|
-
|
|
389
|
-
if (!close()) return error("@media missing '}'");
|
|
390
|
-
|
|
391
|
-
return pos({
|
|
392
|
-
type: 'media',
|
|
393
|
-
media: media,
|
|
394
|
-
rules: style
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Parse custom-media.
|
|
401
|
-
*/
|
|
402
|
-
|
|
403
|
-
function atcustommedia() {
|
|
404
|
-
var pos = position();
|
|
405
|
-
var m = match(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);
|
|
406
|
-
if (!m) return;
|
|
407
|
-
|
|
408
|
-
return pos({
|
|
409
|
-
type: 'custom-media',
|
|
410
|
-
name: trim(m[1]),
|
|
411
|
-
media: trim(m[2])
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
* Parse paged media.
|
|
417
|
-
*/
|
|
418
|
-
|
|
419
|
-
function atpage() {
|
|
420
|
-
var pos = position();
|
|
421
|
-
var m = match(/^@page */);
|
|
422
|
-
if (!m) return;
|
|
423
|
-
|
|
424
|
-
var sel = selector() || [];
|
|
425
|
-
|
|
426
|
-
if (!open()) return error("@page missing '{'");
|
|
427
|
-
var decls = comments();
|
|
428
|
-
|
|
429
|
-
// declarations
|
|
430
|
-
var decl;
|
|
431
|
-
while (decl = declaration()) {
|
|
432
|
-
decls.push(decl);
|
|
433
|
-
decls = decls.concat(comments());
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
if (!close()) return error("@page missing '}'");
|
|
437
|
-
|
|
438
|
-
return pos({
|
|
439
|
-
type: 'page',
|
|
440
|
-
selectors: sel,
|
|
441
|
-
declarations: decls
|
|
442
|
-
});
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
/**
|
|
446
|
-
* Parse document.
|
|
447
|
-
*/
|
|
448
|
-
|
|
449
|
-
function atdocument() {
|
|
450
|
-
var pos = position();
|
|
451
|
-
var m = match(/^@([-\w]+)?document *([^{]+)/);
|
|
452
|
-
if (!m) return;
|
|
453
|
-
|
|
454
|
-
var vendor = trim(m[1]);
|
|
455
|
-
var doc = trim(m[2]);
|
|
456
|
-
|
|
457
|
-
if (!open()) return error("@document missing '{'");
|
|
458
|
-
|
|
459
|
-
var style = comments().concat(rules());
|
|
460
|
-
|
|
461
|
-
if (!close()) return error("@document missing '}'");
|
|
462
|
-
|
|
463
|
-
return pos({
|
|
464
|
-
type: 'document',
|
|
465
|
-
document: doc,
|
|
466
|
-
vendor: vendor,
|
|
467
|
-
rules: style
|
|
468
|
-
});
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* Parse font-face.
|
|
473
|
-
*/
|
|
474
|
-
|
|
475
|
-
function atfontface() {
|
|
476
|
-
var pos = position();
|
|
477
|
-
var m = match(/^@font-face\s*/);
|
|
478
|
-
if (!m) return;
|
|
479
|
-
|
|
480
|
-
if (!open()) return error("@font-face missing '{'");
|
|
481
|
-
var decls = comments();
|
|
482
|
-
|
|
483
|
-
// declarations
|
|
484
|
-
var decl;
|
|
485
|
-
while (decl = declaration()) {
|
|
486
|
-
decls.push(decl);
|
|
487
|
-
decls = decls.concat(comments());
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
if (!close()) return error("@font-face missing '}'");
|
|
491
|
-
|
|
492
|
-
return pos({
|
|
493
|
-
type: 'font-face',
|
|
494
|
-
declarations: decls
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/**
|
|
499
|
-
* Parse import
|
|
500
|
-
*/
|
|
501
|
-
|
|
502
|
-
var atimport = _compileAtrule('import');
|
|
503
|
-
|
|
504
|
-
/**
|
|
505
|
-
* Parse charset
|
|
506
|
-
*/
|
|
507
|
-
|
|
508
|
-
var atcharset = _compileAtrule('charset');
|
|
509
|
-
|
|
510
|
-
/**
|
|
511
|
-
* Parse namespace
|
|
512
|
-
*/
|
|
513
|
-
|
|
514
|
-
var atnamespace = _compileAtrule('namespace');
|
|
515
|
-
|
|
516
|
-
/**
|
|
517
|
-
* Parse non-block at-rules
|
|
518
|
-
*/
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
function _compileAtrule(name) {
|
|
522
|
-
var re = new RegExp('^@' + name + '\\s*([^;]+);');
|
|
523
|
-
return function() {
|
|
524
|
-
var pos = position();
|
|
525
|
-
var m = match(re);
|
|
526
|
-
if (!m) return;
|
|
527
|
-
var ret = { type: name };
|
|
528
|
-
ret[name] = m[1].trim();
|
|
529
|
-
return pos(ret);
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
/**
|
|
534
|
-
* Parse at rule.
|
|
535
|
-
*/
|
|
536
|
-
|
|
537
|
-
function atrule() {
|
|
538
|
-
if (css[0] != '@') return;
|
|
539
|
-
|
|
540
|
-
return atkeyframes()
|
|
541
|
-
|| atmedia()
|
|
542
|
-
|| atcustommedia()
|
|
543
|
-
|| atsupports()
|
|
544
|
-
|| atimport()
|
|
545
|
-
|| atcharset()
|
|
546
|
-
|| atnamespace()
|
|
547
|
-
|| atdocument()
|
|
548
|
-
|| atpage()
|
|
549
|
-
|| athost()
|
|
550
|
-
|| atfontface();
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
/**
|
|
554
|
-
* Parse rule.
|
|
555
|
-
*/
|
|
556
|
-
|
|
557
|
-
function rule() {
|
|
558
|
-
var pos = position();
|
|
559
|
-
var sel = selector();
|
|
560
|
-
|
|
561
|
-
if (!sel) return error('selector missing');
|
|
562
|
-
comments();
|
|
563
|
-
|
|
564
|
-
return pos({
|
|
565
|
-
type: 'rule',
|
|
566
|
-
selectors: sel,
|
|
567
|
-
declarations: declarations()
|
|
568
|
-
});
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
return addParent(stylesheet());
|
|
251
|
+
whitespace();
|
|
252
|
+
return declarations();
|
|
572
253
|
};
|
|
573
254
|
|
|
574
255
|
/**
|
|
575
256
|
* Trim `str`.
|
|
257
|
+
*
|
|
258
|
+
* @param {String} str
|
|
259
|
+
* @return {String}
|
|
576
260
|
*/
|
|
577
|
-
|
|
578
261
|
function trim(str) {
|
|
579
|
-
return str ? str.replace(
|
|
262
|
+
return str ? str.replace(TRIM_REGEX, EMPTY_STRING) : EMPTY_STRING;
|
|
580
263
|
}
|
|
581
264
|
|
|
582
265
|
/**
|
|
583
|
-
*
|
|
584
|
-
*/
|
|
585
|
-
|
|
586
|
-
function addParent(obj, parent) {
|
|
587
|
-
var isNode = obj && typeof obj.type === 'string';
|
|
588
|
-
var childParent = isNode ? obj : parent;
|
|
589
|
-
|
|
590
|
-
for (var k in obj) {
|
|
591
|
-
var value = obj[k];
|
|
592
|
-
if (Array.isArray(value)) {
|
|
593
|
-
value.forEach(function(v) { addParent(v, childParent); });
|
|
594
|
-
} else if (value && typeof value === 'object') {
|
|
595
|
-
addParent(value, childParent);
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
if (isNode) {
|
|
600
|
-
Object.defineProperty(obj, 'parent', {
|
|
601
|
-
configurable: true,
|
|
602
|
-
writable: true,
|
|
603
|
-
enumerable: false,
|
|
604
|
-
value: parent || null
|
|
605
|
-
});
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
return obj;
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
/**
|
|
612
|
-
* Parses inline style.
|
|
266
|
+
* Parses inline style to object.
|
|
613
267
|
*
|
|
614
|
-
*
|
|
268
|
+
* @example
|
|
269
|
+
* // returns { 'line-height': '42' }
|
|
270
|
+
* StyleToObject('line-height: 42;');
|
|
615
271
|
*
|
|
616
272
|
* @param {String} style - The inline style.
|
|
617
273
|
* @param {Function} [iterator] - The iterator function.
|
|
618
274
|
* @return {null|Object}
|
|
619
275
|
*/
|
|
620
|
-
|
|
621
|
-
if (!style || typeof style !== 'string') return null;
|
|
622
|
-
|
|
623
|
-
// make sure to wrap declarations in placeholder
|
|
624
|
-
var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations;
|
|
625
|
-
var declaration, property, value;
|
|
626
|
-
|
|
276
|
+
function StyleToObject(style, iterator) {
|
|
627
277
|
var output = null;
|
|
278
|
+
if (!style || typeof style !== 'string') {
|
|
279
|
+
return output;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
var declaration;
|
|
283
|
+
var declarations = inlineStyleParser(style);
|
|
628
284
|
var hasIterator = typeof iterator === 'function';
|
|
285
|
+
var property;
|
|
286
|
+
var value;
|
|
629
287
|
|
|
630
288
|
for (var i = 0, len = declarations.length; i < len; i++) {
|
|
631
289
|
declaration = declarations[i];
|
|
@@ -641,8 +299,10 @@
|
|
|
641
299
|
}
|
|
642
300
|
|
|
643
301
|
return output;
|
|
644
|
-
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
var styleToObject = StyleToObject;
|
|
645
305
|
|
|
646
306
|
return styleToObject;
|
|
647
307
|
|
|
648
|
-
}))
|
|
308
|
+
}));
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
!function(r
|
|
1
|
+
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(n=n||self).StyleToObject=r()}(this,function(){"use strict";var m=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,g=/\n/g,y=/^\s*/,d=/^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/,w=/^:\s*/,A=/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/,x=/^[;\s]*/,r=/^\s+|\s+$/g,b="";function E(n){return n?n.replace(r,b):b}return function(n,r){var t,e=null;if(!n||"string"!=typeof n)return e;for(var o,i,u=function(e,t){if("string"!=typeof e)throw new TypeError("First argument must be a string");if(!e)return[];t=t||{};var o=1,i=1;function u(n){var r=n.match(g);r&&(o+=r.length);var t=n.lastIndexOf("\n");i=~t?n.length-t:i+n.length}function c(){var r={line:o,column:i};return function(n){return n.position=new f(r),l(),n}}function f(n){this.start=n,this.end={line:o,column:i},this.source=t.source}function a(n){var r=new Error(t.source+":"+o+":"+i+": "+n);if(r.reason=n,r.filename=t.source,r.line=o,r.column=i,r.source=e,!t.silent)throw r}function s(n){var r=n.exec(e);if(r){var t=r[0];return u(t),e=e.slice(t.length),r}}function l(){s(y)}function p(n){var r;for(n=n||[];r=h();)!1!==r&&n.push(r);return n}function h(){var n=c();if("/"==e.charAt(0)&&"*"==e.charAt(1)){for(var r=2;b!=e.charAt(r)&&("*"!=e.charAt(r)||"/"!=e.charAt(r+1));)++r;if(r+=2,b===e.charAt(r-1))return a("End of comment missing");var t=e.slice(2,r-2);return i+=2,u(t),e=e.slice(r),i+=2,n({type:"comment",comment:t})}}function v(){var n=c(),r=s(d);if(r){if(h(),!s(w))return a("property missing ':'");var t=s(A),e=n({type:"declaration",property:E(r[0].replace(m,b)),value:t?E(t[0].replace(m,b)):b});return s(x),e}}return f.prototype.content=e,l(),function(){var n,r=[];for(p(r);n=v();)!1!==n&&(r.push(n),p(r));return r}()}(n),c="function"==typeof r,f=0,a=u.length;f<a;f++)o=(t=u[f]).property,i=t.value,c?r(o,i,t):i&&((e=e||{})[o]=i);return e}});
|
|
2
|
+
//# sourceMappingURL=style-to-object.min.js.map
|
|
@@ -0,0 +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","style","iterator","declaration","output","property","value","declarations","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","prop","val","ret","prototype","content","decl","decls","parse","hasIterator","len"],"mappings":"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"}
|
package/index.js
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
|
-
var parse = require('
|
|
1
|
+
var parse = require('inline-style-parser');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Parses inline style.
|
|
4
|
+
* Parses inline style to object.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* @example
|
|
7
|
+
* // returns { 'line-height': '42' }
|
|
8
|
+
* StyleToObject('line-height: 42;');
|
|
7
9
|
*
|
|
8
10
|
* @param {String} style - The inline style.
|
|
9
11
|
* @param {Function} [iterator] - The iterator function.
|
|
10
12
|
* @return {null|Object}
|
|
11
13
|
*/
|
|
12
|
-
|
|
13
|
-
if (!style || typeof style !== 'string') return null;
|
|
14
|
-
|
|
15
|
-
// make sure to wrap declarations in placeholder
|
|
16
|
-
var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations;
|
|
17
|
-
var declaration, property, value;
|
|
18
|
-
|
|
14
|
+
function StyleToObject(style, iterator) {
|
|
19
15
|
var output = null;
|
|
16
|
+
if (!style || typeof style !== 'string') {
|
|
17
|
+
return output;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
var declaration;
|
|
21
|
+
var declarations = parse(style);
|
|
20
22
|
var hasIterator = typeof iterator === 'function';
|
|
23
|
+
var property;
|
|
24
|
+
var value;
|
|
21
25
|
|
|
22
26
|
for (var i = 0, len = declarations.length; i < len; i++) {
|
|
23
27
|
declaration = declarations[i];
|
|
@@ -33,4 +37,6 @@ module.exports = function parseInlineStyle(style, iterator) {
|
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
return output;
|
|
36
|
-
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = StyleToObject;
|
package/package.json
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "style-to-object",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Converts inline style to object.",
|
|
5
5
|
"author": "Mark <mark@remarkablemark.org>",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
+
"build": "npm run clean && npm run build:min && npm run build:unmin",
|
|
9
|
+
"build:min": "NODE_ENV=production rollup --config --file dist/style-to-object.min.js --sourcemap",
|
|
10
|
+
"build:unmin": "NODE_ENV=development rollup --config --file dist/style-to-object.js",
|
|
11
|
+
"clean": "rm -rf dist",
|
|
12
|
+
"coveralls": "nyc report --reporter=text-lcov | coveralls",
|
|
13
|
+
"lint": "eslint --ignore-path .gitignore .",
|
|
14
|
+
"lint:fix": "npm run lint -- --fix",
|
|
8
15
|
"prepublishOnly": "npm run build",
|
|
9
|
-
"release": "standard-version",
|
|
16
|
+
"release": "standard-version --no-verify",
|
|
10
17
|
"test": "mocha",
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"clean": "rm -rf dist",
|
|
15
|
-
"build": "npm run clean && npm run build:min && npm run build:unmin",
|
|
16
|
-
"build:min": "NODE_ENV=production rollup --config --output.file dist/style-to-object.min.js",
|
|
17
|
-
"build:unmin": "rollup --config --output.file dist/style-to-object.js"
|
|
18
|
+
"test:coverage": "nyc npm test",
|
|
19
|
+
"test:coverage:report": "nyc report --reporter=html",
|
|
20
|
+
"test:watch": "mocha --watch"
|
|
18
21
|
},
|
|
19
22
|
"repository": {
|
|
20
23
|
"type": "git",
|
|
@@ -24,26 +27,36 @@
|
|
|
24
27
|
"url": "https://github.com/remarkablemark/style-to-object/issues"
|
|
25
28
|
},
|
|
26
29
|
"keywords": [
|
|
30
|
+
"style-to-object",
|
|
27
31
|
"inline",
|
|
28
|
-
"css",
|
|
29
32
|
"style",
|
|
33
|
+
"parser",
|
|
34
|
+
"css",
|
|
30
35
|
"object",
|
|
31
36
|
"pojo"
|
|
32
37
|
],
|
|
33
38
|
"dependencies": {
|
|
34
|
-
"
|
|
39
|
+
"inline-style-parser": "0.1.1"
|
|
35
40
|
},
|
|
36
41
|
"devDependencies": {
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
42
|
+
"@commitlint/cli": "^8.0.0",
|
|
43
|
+
"@commitlint/config-conventional": "^8.0.0",
|
|
44
|
+
"coveralls": "^3.0.4",
|
|
45
|
+
"eslint": "^6.0.0",
|
|
46
|
+
"eslint-plugin-prettier": "^3.1.0",
|
|
47
|
+
"husky": "^2.4.1",
|
|
48
|
+
"lint-staged": "^8.2.1",
|
|
49
|
+
"mocha": "^6.1.4",
|
|
50
|
+
"nyc": "^14.1.1",
|
|
51
|
+
"prettier": "^1.18.2",
|
|
52
|
+
"rollup": "^1.16.2",
|
|
53
|
+
"rollup-plugin-commonjs": "^10.0.0",
|
|
54
|
+
"rollup-plugin-node-resolve": "^5.0.4",
|
|
55
|
+
"rollup-plugin-uglify": "^6.0.2",
|
|
56
|
+
"standard-version": "^6.0.1"
|
|
47
57
|
},
|
|
58
|
+
"files": [
|
|
59
|
+
"/dist"
|
|
60
|
+
],
|
|
48
61
|
"license": "MIT"
|
|
49
62
|
}
|