inline-style-parser 0.1.0 → 0.1.1

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 CHANGED
@@ -2,6 +2,15 @@
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.1.1](https://github.com/remarkablemark/inline-style-parser/compare/v0.1.0...v0.1.1) (2019-06-20)
6
+
7
+
8
+ ### Build System
9
+
10
+ * **package:** fix whitelisting of `/dist` in "files" field ([2c13b2f](https://github.com/remarkablemark/inline-style-parser/commit/2c13b2f))
11
+
12
+
13
+
5
14
  ## 0.1.0 (2019-06-19)
6
15
 
7
16
 
package/README.md CHANGED
@@ -28,6 +28,10 @@ Output:
28
28
  position: Position { start: [Object], end: [Object], source: undefined } } ]
29
29
  ```
30
30
 
31
+ [JSFiddle](https://jsfiddle.net/remarkablemark/hcxbpwq8/) | [Repl.it](https://repl.it/@remarkablemark/inline-style-parser)
32
+
33
+ See [usage](#usage) and [examples](https://github.com/remarkablemark/inline-style-parser/tree/master/examples).
34
+
31
35
  ## Installation
32
36
 
33
37
  [NPM](https://www.npmjs.com/package/inline-style-parser):
@@ -0,0 +1,267 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global = global || self, global.InlineStyleParser = factory());
5
+ }(this, function () { 'use strict';
6
+
7
+ // http://www.w3.org/TR/CSS21/grammar.html
8
+ // https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027
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 [];
46
+
47
+ options = options || {};
48
+
49
+ /**
50
+ * Positional.
51
+ */
52
+ var lineno = 1;
53
+ var column = 1;
54
+
55
+ /**
56
+ * Update lineno and column based on `str`.
57
+ *
58
+ * @param {String} str
59
+ */
60
+ function updatePosition(str) {
61
+ var lines = str.match(NEWLINE_REGEX);
62
+ if (lines) lineno += lines.length;
63
+ var i = str.lastIndexOf(NEWLINE);
64
+ column = ~i ? str.length - i : column + str.length;
65
+ }
66
+
67
+ /**
68
+ * Mark position and patch `node.position`.
69
+ *
70
+ * @return {Function}
71
+ */
72
+ function position() {
73
+ var start = { line: lineno, column: column };
74
+ return function(node) {
75
+ node.position = new Position(start);
76
+ whitespace();
77
+ return node;
78
+ };
79
+ }
80
+
81
+ /**
82
+ * Store position information for a node.
83
+ *
84
+ * @constructor
85
+ * @property {Object} start
86
+ * @property {Object} end
87
+ * @property {undefined|String} source
88
+ */
89
+ function Position(start) {
90
+ this.start = start;
91
+ this.end = { line: lineno, column: column };
92
+ this.source = options.source;
93
+ }
94
+
95
+ /**
96
+ * Non-enumerable source string.
97
+ */
98
+ Position.prototype.content = style;
99
+
100
+ /**
101
+ * Error `msg`.
102
+ *
103
+ * @param {String} msg
104
+ * @throws {Error}
105
+ */
106
+ function error(msg) {
107
+ var err = new Error(
108
+ options.source + ':' + lineno + ':' + column + ': ' + msg
109
+ );
110
+ err.reason = msg;
111
+ err.filename = options.source;
112
+ err.line = lineno;
113
+ err.column = column;
114
+ err.source = style;
115
+
116
+ if (options.silent) ; else {
117
+ throw err;
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Match `re` and return captures.
123
+ *
124
+ * @param {RegExp} re
125
+ * @return {undefined|Array}
126
+ */
127
+ function match(re) {
128
+ var m = re.exec(style);
129
+ if (!m) return;
130
+ var str = m[0];
131
+ updatePosition(str);
132
+ style = style.slice(str.length);
133
+ return m;
134
+ }
135
+
136
+ /**
137
+ * Parse whitespace.
138
+ */
139
+ function whitespace() {
140
+ match(WHITESPACE_REGEX);
141
+ }
142
+
143
+ /**
144
+ * Parse comments.
145
+ *
146
+ * @param {Object[]} [rules]
147
+ * @return {Object[]}
148
+ */
149
+ function comments(rules) {
150
+ var c;
151
+ rules = rules || [];
152
+ while ((c = comment())) {
153
+ if (c !== false) {
154
+ rules.push(c);
155
+ }
156
+ }
157
+ return rules;
158
+ }
159
+
160
+ /**
161
+ * Parse comment.
162
+ *
163
+ * @return {Object}
164
+ * @throws {Error}
165
+ */
166
+ function comment() {
167
+ var pos = position();
168
+ if (FORWARD_SLASH != style.charAt(0) || ASTERISK != style.charAt(1)) return;
169
+
170
+ var i = 2;
171
+ while (
172
+ EMPTY_STRING != style.charAt(i) &&
173
+ (ASTERISK != style.charAt(i) || FORWARD_SLASH != style.charAt(i + 1))
174
+ ) {
175
+ ++i;
176
+ }
177
+ i += 2;
178
+
179
+ if (EMPTY_STRING === style.charAt(i - 1)) {
180
+ return error('End of comment missing');
181
+ }
182
+
183
+ var str = style.slice(2, i - 2);
184
+ column += 2;
185
+ updatePosition(str);
186
+ style = style.slice(i);
187
+ column += 2;
188
+
189
+ return pos({
190
+ type: TYPE_COMMENT,
191
+ comment: str
192
+ });
193
+ }
194
+
195
+ /**
196
+ * Parse declaration.
197
+ *
198
+ * @return {Object}
199
+ * @throws {Error}
200
+ */
201
+ function declaration() {
202
+ var pos = position();
203
+
204
+ // prop
205
+ var prop = match(PROPERTY_REGEX);
206
+ if (!prop) return;
207
+ comment();
208
+
209
+ // :
210
+ if (!match(COLON_REGEX)) return error("property missing ':'");
211
+
212
+ // val
213
+ var val = match(VALUE_REGEX);
214
+
215
+ var ret = pos({
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
221
+ });
222
+
223
+ // ;
224
+ match(SEMICOLON_REGEX);
225
+
226
+ return ret;
227
+ }
228
+
229
+ /**
230
+ * Parse declarations.
231
+ *
232
+ * @return {Object[]}
233
+ */
234
+ function declarations() {
235
+ var decls = [];
236
+
237
+ comments(decls);
238
+
239
+ // declarations
240
+ var decl;
241
+ while ((decl = declaration())) {
242
+ if (decl !== false) {
243
+ decls.push(decl);
244
+ comments(decls);
245
+ }
246
+ }
247
+
248
+ return decls;
249
+ }
250
+
251
+ whitespace();
252
+ return declarations();
253
+ };
254
+
255
+ /**
256
+ * Trim `str`.
257
+ *
258
+ * @param {String} str
259
+ * @return {String}
260
+ */
261
+ function trim(str) {
262
+ return str ? str.replace(TRIM_REGEX, EMPTY_STRING) : EMPTY_STRING;
263
+ }
264
+
265
+ return inlineStyleParser;
266
+
267
+ }));
@@ -0,0 +1,2 @@
1
+ !function(n,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(n=n||self).InlineStyleParser=r()}(this,function(){"use strict";var v=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,d=/\n/g,n=/^\s*/,g=/^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/,y=/^:\s*/,w=/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/,A=/^[;\s]*/,r=/^\s+|\s+$/g,x="";function E(n){return n?n.replace(r,x):x}return function(t,e){if("string"!=typeof t)throw new TypeError("First argument must be a string");if(!t)return[];e=e||{};var o=1,i=1;function u(n){var r=n.match(d);r&&(o+=r.length);var e=n.lastIndexOf("\n");i=~e?n.length-e: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=e.source}function s(n){var r=new Error(e.source+":"+o+":"+i+": "+n);if(r.reason=n,r.filename=e.source,r.line=o,r.column=i,r.source=t,!e.silent)throw r}function a(n){var r=n.exec(t);if(r){var e=r[0];return u(e),t=t.slice(e.length),r}}function l(){a(n)}function p(n){var r;for(n=n||[];r=h();)!1!==r&&n.push(r);return n}function h(){var n=c();if("/"==t.charAt(0)&&"*"==t.charAt(1)){for(var r=2;x!=t.charAt(r)&&("*"!=t.charAt(r)||"/"!=t.charAt(r+1));)++r;if(r+=2,x===t.charAt(r-1))return s("End of comment missing");var e=t.slice(2,r-2);return i+=2,u(e),t=t.slice(r),i+=2,n({type:"comment",comment:e})}}function m(){var n=c(),r=a(g);if(r){if(h(),!a(y))return s("property missing ':'");var e=a(w),t=n({type:"declaration",property:E(r[0].replace(v,x)),value:e?E(e[0].replace(v,x)):x});return a(A),t}}return f.prototype.content=t,l(),function(){var n,r=[];for(p(r);n=m();)!1!==n&&(r.push(n),p(r));return r}()}});
2
+ //# sourceMappingURL=inline-style-parser.min.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inline-style-parser.min.js","sources":["../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"],"names":["COMMENT_REGEX","NEWLINE_REGEX","WHITESPACE_REGEX","PROPERTY_REGEX","COLON_REGEX","VALUE_REGEX","SEMICOLON_REGEX","TRIM_REGEX","EMPTY_STRING","trim","str","replace","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"],"mappings":"uMAEA,IAAIA,EAAgB,kCAEhBC,EAAgB,MAChBC,EAAmB,OAGnBC,EAAiB,yCACjBC,EAAc,QACdC,EAAc,uDACdC,EAAkB,UAGlBC,EAAa,aAMbC,EAAe,GA8OnB,SAASC,EAAKC,GACZ,OAAOA,EAAMA,EAAIC,QAAQJ,EAAYC,GAAgBA,SAlOtC,SAASI,EAAOC,GAC/B,GAAqB,iBAAVD,EACT,MAAM,IAAIE,UAAU,mCAGtB,IAAKF,EAAO,MAAO,GAEnBC,EAAUA,GAAW,GAKrB,IAAIE,EAAS,EACTC,EAAS,EAOb,SAASC,EAAeP,GACtB,IAAIQ,EAAQR,EAAIS,MAAMlB,GAClBiB,IAAOH,GAAUG,EAAME,QAC3B,IAAIC,EAAIX,EAAIY,YAvCF,MAwCVN,GAAUK,EAAIX,EAAIU,OAASC,EAAIL,E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,EAAeP,GACfE,EAAQA,EAAM8B,MAAMrB,GACpBL,GAAU,EAEHgC,EAAI,CACTE,KApKa,UAqKbJ,QAASpC,KAUb,SAASyC,IACP,IAAIH,EAAMzB,IAGN6B,EAAOjC,EAAMhB,GACjB,GAAKiD,EAAL,CAIA,GAHAN,KAGK3B,EAAMf,GAAc,OAAO4B,EAAM,wBAGtC,IAAIqB,EAAMlC,EAAMd,GAEZiD,EAAMN,EAAI,CACZE,KA7LiB,cA8LjBK,SAAU9C,EAAK2C,EAAK,GAAGzC,QAAQX,EAAeQ,IAC9CgD,MAAOH,EACH5C,EAAK4C,EAAI,GAAG1C,QAAQX,EAAeQ,IACnCA,IAMN,OAFAW,EAAMb,GAECgD,GA0BT,OA9JA3B,EAAS8B,UAAUC,QAAU9C,EA6J7BgB,IAjBA,WACE,IAKI+B,EALAC,EAAQ,GAMZ,IAJAjB,EAASiB,GAIDD,EAAOR,MACA,IAATQ,IACFC,EAAMb,KAAKY,GACXhB,EAASiB,IAIb,OAAOA,EAIFC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inline-style-parser",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "An inline style parser.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -49,7 +49,7 @@
49
49
  "standard-version": "^6.0.1"
50
50
  },
51
51
  "files": [
52
- "./dist"
52
+ "/dist"
53
53
  ],
54
54
  "license": "MIT"
55
55
  }