style-to-object
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="0.2.0"></a> | ||
# [0.2.0](https://github.com/remarkablemark/style-to-object/compare/v0.1.0...v0.2.0) (2017-11-26) | ||
### Features | ||
* **parser:** add optional argument iterator ([a3deea8](https://github.com/remarkablemark/style-to-object/commit/a3deea8)) | ||
<a name="0.1.0"></a> | ||
@@ -7,0 +17,0 @@ # 0.1.0 (2017-11-23) |
@@ -611,10 +611,11 @@ (function (global, factory) { | ||
/** | ||
* Parses inline style to object (server). | ||
* Parses inline style. | ||
* | ||
* Example: 'color:red' => { color: 'red' } | ||
* | ||
* @param {String} style | ||
* @return {Object|null} | ||
* @param {String} style - The inline style. | ||
* @param {Function} [iterator] - The iterator function. | ||
* @return {null|Object} | ||
*/ | ||
var styleToObject = function parseInlineStyleServer(style) { | ||
var styleToObject = function parseInlineStyle(style, iterator) { | ||
if (!style || typeof style !== 'string') return null; | ||
@@ -624,10 +625,19 @@ | ||
var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations; | ||
var output = {}; | ||
var declaration, property, value; | ||
declarations.forEach(function(declaration) { | ||
var value = declaration.value; | ||
if (value) { | ||
output[declaration.property] = value; | ||
var output = null; | ||
var hasIterator = typeof iterator === 'function'; | ||
for (var i = 0, len = declarations.length; i < len; i++) { | ||
declaration = declarations[i]; | ||
property = declaration.property; | ||
value = declaration.value; | ||
if (hasIterator) { | ||
iterator(property, value, declaration); | ||
} else if (value) { | ||
output || (output = {}); | ||
output[property] = value; | ||
} | ||
}); | ||
} | ||
@@ -634,0 +644,0 @@ return output; |
@@ -1,1 +0,1 @@ | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):r.StyleToObject=e()}(this,function(){"use strict";function r(r){return r?r.replace(/^\s+|\s+$/g,""):""}function e(r,n){var t=r&&"string"==typeof r.type,i=t?r:n;for(var s in r){var a=r[s];Array.isArray(a)?a.forEach(function(r){e(r,i)}):a&&"object"==typeof a&&e(a,i)}return t&&Object.defineProperty(r,"parent",{configurable:!0,writable:!0,enumerable:!1,value:n||null}),r}var n=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;return function(t){if(!t||"string"!=typeof t)return null;var i={};return function(t,i){function s(r){var e=r.match(/\n/g);e&&(A+=e.length);var n=r.lastIndexOf("\n");b=~n?r.length-n:b+r.length}function a(){var r={line:A,column:b};return function(e){return e.position=new u(r),l(),e}}function u(r){this.start=r,this.end={line:A,column:b},this.source=i.source}function o(r){var e=new Error(i.source+":"+A+":"+b+": "+r);if(e.reason=r,e.filename=i.source,e.line=A,e.column=b,e.source=t,!i.silent)throw e;k.push(e)}function c(){return m(/^{\s*/)}function f(){return m(/^}/)}function p(){var e,n=[];for(l(),v(n);t.length&&"}"!=t.charAt(0)&&(e=function(){if("@"==t[0])return function(){var r=a(),e=m(/^@([-\w]+)?keyframes\s*/);if(e){var n=e[1];if(!(e=m(/^([-\w]+)\s*/)))return o("@keyframes missing name");var t=e[1];if(!c())return o("@keyframes missing '{'");for(var i,s=v();i=h();)s.push(i),s=s.concat(v());return f()?r({type:"keyframes",name:t,vendor:n,keyframes:s}):o("@keyframes missing '}'")}}()||function(){var e=a(),t=m(/^@media *([^{]+)/);if(t){var i=r(t[1]);if(!c())return o("@media missing '{'");var s=v().concat(n());return f()?e({type:"media",media:i,rules:s}):o("@media missing '}'")}}()||function(){var e=a(),n=m(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);if(n)return e({type:"custom-media",name:r(n[1]),media:r(n[2])})}()||function(){var e=a(),t=m(/^@supports *([^{]+)/);if(t){var i=r(t[1]);if(!c())return o("@supports missing '{'");var s=v().concat(n());return f()?e({type:"supports",supports:i,rules:s}):o("@supports missing '}'")}}()||E()||x()||j()||function(){var e=a(),t=m(/^@([-\w]+)?document *([^{]+)/);if(t){var i=r(t[1]),s=r(t[2]);if(!c())return o("@document missing '{'");var u=v().concat(n());return f()?e({type:"document",document:s,vendor:i,rules:u}):o("@document missing '}'")}}()||function(){var r=a();if(m(/^@page */)){var e=g()||[];if(!c())return o("@page missing '{'");for(var n,t=v();n=y();)t.push(n),t=t.concat(v());return f()?r({type:"page",selectors:e,declarations:t}):o("@page missing '}'")}}()||function(){var r=a();if(m(/^@host\s*/)){if(!c())return o("@host missing '{'");var e=v().concat(n());return f()?r({type:"host",rules:e}):o("@host missing '}'")}}()||function(){var r=a();if(m(/^@font-face\s*/)){if(!c())return o("@font-face missing '{'");for(var e,n=v();e=y();)n.push(e),n=n.concat(v());return f()?r({type:"font-face",declarations:n}):o("@font-face missing '}'")}}()}()||function(){var r=a(),e=g();return e?(v(),r({type:"rule",selectors:e,declarations:d()})):o("selector missing")}());)!1!==e&&(n.push(e),v(n));return n}function m(r){var e=r.exec(t);if(e){var n=e[0];return s(n),t=t.slice(n.length),e}}function l(){m(/^\s*/)}function v(r){var e;for(r=r||[];e=function(){var r=a();if("/"==t.charAt(0)&&"*"==t.charAt(1)){for(var e=2;""!=t.charAt(e)&&("*"!=t.charAt(e)||"/"!=t.charAt(e+1));)++e;if(e+=2,""===t.charAt(e-1))return o("End of comment missing");var n=t.slice(2,e-2);return b+=2,s(n),t=t.slice(e),b+=2,r({type:"comment",comment:n})}}();)!1!==e&&r.push(e);return r}function g(){var e=m(/^([^{]+)/);if(e)return r(e[0]).replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g,"").replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g,function(r){return r.replace(/,/g,"")}).split(/\s*(?![^(]*\)),\s*/).map(function(r){return r.replace(/\u200C/g,",")})}function y(){var e=a(),t=m(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/);if(t){if(t=r(t[0]),!m(/^:\s*/))return o("property missing ':'");var i=m(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/),s=e({type:"declaration",property:t.replace(n,""),value:i?r(i[0]).replace(n,""):""});return m(/^[;\s]*/),s}}function d(){var r=[];if(!c())return o("missing '{'");v(r);for(var e;e=y();)!1!==e&&(r.push(e),v(r));return f()?r:o("missing '}'")}function h(){for(var r,e=[],n=a();r=m(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/);)e.push(r[1]),m(/^,\s*/);if(e.length)return n({type:"keyframe",values:e,declarations:d()})}function w(r){var e=new RegExp("^@"+r+"\\s*([^;]+);");return function(){var n=a(),t=m(e);if(t){var i={type:r};return i[r]=t[1].trim(),n(i)}}}i=i||{};var A=1,b=1;u.prototype.content=t;var k=[],E=w("import"),x=w("charset"),j=w("namespace");return e({type:"stylesheet",stylesheet:{rules:p(),parsingErrors:k}})}("p{"+t+"}").stylesheet.rules[0].declarations.forEach(function(r){var e=r.value;e&&(i[r.property]=e)}),i}}); | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):r.StyleToObject=e()}(this,function(){"use strict";function r(r){return r?r.replace(/^\s+|\s+$/g,""):""}function e(r,n){var t=r&&"string"==typeof r.type,i=t?r:n;for(var s in r){var a=r[s];Array.isArray(a)?a.forEach(function(r){e(r,i)}):a&&"object"==typeof a&&e(a,i)}return t&&Object.defineProperty(r,"parent",{configurable:!0,writable:!0,enumerable:!1,value:n||null}),r}var n=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;return function(t,i){if(!t||"string"!=typeof t)return null;for(var s,a,u,o=function(t,i){function s(r){var e=r.match(/\n/g);e&&(A+=e.length);var n=r.lastIndexOf("\n");b=~n?r.length-n:b+r.length}function a(){var r={line:A,column:b};return function(e){return e.position=new u(r),l(),e}}function u(r){this.start=r,this.end={line:A,column:b},this.source=i.source}function o(r){var e=new Error(i.source+":"+A+":"+b+": "+r);if(e.reason=r,e.filename=i.source,e.line=A,e.column=b,e.source=t,!i.silent)throw e;k.push(e)}function c(){return m(/^{\s*/)}function f(){return m(/^}/)}function p(){var e,n=[];for(l(),v(n);t.length&&"}"!=t.charAt(0)&&(e=function(){if("@"==t[0])return function(){var r=a(),e=m(/^@([-\w]+)?keyframes\s*/);if(e){var n=e[1];if(!(e=m(/^([-\w]+)\s*/)))return o("@keyframes missing name");var t=e[1];if(!c())return o("@keyframes missing '{'");for(var i,s=v();i=h();)s.push(i),s=s.concat(v());return f()?r({type:"keyframes",name:t,vendor:n,keyframes:s}):o("@keyframes missing '}'")}}()||function(){var e=a(),t=m(/^@media *([^{]+)/);if(t){var i=r(t[1]);if(!c())return o("@media missing '{'");var s=v().concat(n());return f()?e({type:"media",media:i,rules:s}):o("@media missing '}'")}}()||function(){var e=a(),n=m(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);if(n)return e({type:"custom-media",name:r(n[1]),media:r(n[2])})}()||function(){var e=a(),t=m(/^@supports *([^{]+)/);if(t){var i=r(t[1]);if(!c())return o("@supports missing '{'");var s=v().concat(n());return f()?e({type:"supports",supports:i,rules:s}):o("@supports missing '}'")}}()||x()||E()||j()||function(){var e=a(),t=m(/^@([-\w]+)?document *([^{]+)/);if(t){var i=r(t[1]),s=r(t[2]);if(!c())return o("@document missing '{'");var u=v().concat(n());return f()?e({type:"document",document:s,vendor:i,rules:u}):o("@document missing '}'")}}()||function(){var r=a();if(m(/^@page */)){var e=g()||[];if(!c())return o("@page missing '{'");for(var n,t=v();n=y();)t.push(n),t=t.concat(v());return f()?r({type:"page",selectors:e,declarations:t}):o("@page missing '}'")}}()||function(){var r=a();if(m(/^@host\s*/)){if(!c())return o("@host missing '{'");var e=v().concat(n());return f()?r({type:"host",rules:e}):o("@host missing '}'")}}()||function(){var r=a();if(m(/^@font-face\s*/)){if(!c())return o("@font-face missing '{'");for(var e,n=v();e=y();)n.push(e),n=n.concat(v());return f()?r({type:"font-face",declarations:n}):o("@font-face missing '}'")}}()}()||function(){var r=a(),e=g();return e?(v(),r({type:"rule",selectors:e,declarations:d()})):o("selector missing")}());)!1!==e&&(n.push(e),v(n));return n}function m(r){var e=r.exec(t);if(e){var n=e[0];return s(n),t=t.slice(n.length),e}}function l(){m(/^\s*/)}function v(r){var e;for(r=r||[];e=function(){var r=a();if("/"==t.charAt(0)&&"*"==t.charAt(1)){for(var e=2;""!=t.charAt(e)&&("*"!=t.charAt(e)||"/"!=t.charAt(e+1));)++e;if(e+=2,""===t.charAt(e-1))return o("End of comment missing");var n=t.slice(2,e-2);return b+=2,s(n),t=t.slice(e),b+=2,r({type:"comment",comment:n})}}();)!1!==e&&r.push(e);return r}function g(){var e=m(/^([^{]+)/);if(e)return r(e[0]).replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g,"").replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g,function(r){return r.replace(/,/g,"")}).split(/\s*(?![^(]*\)),\s*/).map(function(r){return r.replace(/\u200C/g,",")})}function y(){var e=a(),t=m(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/);if(t){if(t=r(t[0]),!m(/^:\s*/))return o("property missing ':'");var i=m(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/),s=e({type:"declaration",property:t.replace(n,""),value:i?r(i[0]).replace(n,""):""});return m(/^[;\s]*/),s}}function d(){var r=[];if(!c())return o("missing '{'");v(r);for(var e;e=y();)!1!==e&&(r.push(e),v(r));return f()?r:o("missing '}'")}function h(){for(var r,e=[],n=a();r=m(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/);)e.push(r[1]),m(/^,\s*/);if(e.length)return n({type:"keyframe",values:e,declarations:d()})}function w(r){var e=new RegExp("^@"+r+"\\s*([^;]+);");return function(){var n=a(),t=m(e);if(t){var i={type:r};return i[r]=t[1].trim(),n(i)}}}i=i||{};var A=1,b=1;u.prototype.content=t;var k=[],x=w("import"),E=w("charset"),j=w("namespace");return e({type:"stylesheet",stylesheet:{rules:p(),parsingErrors:k}})}("p{"+t+"}").stylesheet.rules[0].declarations,c=null,f="function"==typeof i,p=0,m=o.length;p<m;p++)a=(s=o[p]).property,u=s.value,f?i(a,u,s):u&&(c||(c={}),c[a]=u);return c}}); |
30
index.js
var parse = require('css/lib/parse'); | ||
/** | ||
* Parses inline style to object (server). | ||
* Parses inline style. | ||
* | ||
* Example: 'color:red' => { color: 'red' } | ||
* | ||
* @param {String} style | ||
* @return {Object|null} | ||
* @param {String} style - The inline style. | ||
* @param {Function} [iterator] - The iterator function. | ||
* @return {null|Object} | ||
*/ | ||
module.exports = function parseInlineStyleServer(style) { | ||
module.exports = function parseInlineStyle(style, iterator) { | ||
if (!style || typeof style !== 'string') return null; | ||
@@ -16,12 +17,21 @@ | ||
var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations; | ||
var output = {}; | ||
var declaration, property, value; | ||
declarations.forEach(function(declaration) { | ||
var value = declaration.value; | ||
if (value) { | ||
output[declaration.property] = value; | ||
var output = null; | ||
var hasIterator = typeof iterator === 'function'; | ||
for (var i = 0, len = declarations.length; i < len; i++) { | ||
declaration = declarations[i]; | ||
property = declaration.property; | ||
value = declaration.value; | ||
if (hasIterator) { | ||
iterator(property, value, declaration); | ||
} else if (value) { | ||
output || (output = {}); | ||
output[property] = value; | ||
} | ||
}); | ||
} | ||
return output; | ||
}; |
{ | ||
"name": "style-to-object", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Converts inline style to object.", | ||
@@ -5,0 +5,0 @@ "author": "Mark <mark@remarkablemark.org>", |
@@ -18,2 +18,4 @@ # style-to-object | ||
[JSFiddle](https://jsfiddle.net/remarkablemark/ykz2meot/) | ||
## Installation | ||
@@ -47,2 +49,5 @@ | ||
```js | ||
// CommonJS | ||
const parser = require('style-to-object'); | ||
// ES Modules | ||
@@ -92,2 +97,35 @@ import parser from 'style-to-object'; | ||
### Iterator | ||
If the 2nd argument is a function, then the parser will return `null`: | ||
```js | ||
parser('color: #f00', function() {}); // null | ||
``` | ||
But the function will iterate through each declaration: | ||
```js | ||
parser('color: #f00', function(name, value, declaration) { | ||
console.log(name); // 'color' | ||
console.log(value); // '#f00' | ||
console.log(declaration); // { type: 'declaration', property: 'color', value: '#f00' } | ||
}); | ||
``` | ||
This makes it easy to customize the output: | ||
```js | ||
const style = ` | ||
color: #f00; | ||
background: #ba4; | ||
`; | ||
const output = []; | ||
const iterator = (name, value) => { | ||
output.push([name, value]); | ||
}; | ||
parser(style, iterator); | ||
console.log(output); // [['color', '#f00'], ['background', '#ba4']] | ||
``` | ||
## Testing | ||
@@ -100,2 +138,10 @@ | ||
## Release | ||
```sh | ||
$ npm run release | ||
$ npm publish | ||
$ git push --follow-tags | ||
``` | ||
## Special Thanks | ||
@@ -102,0 +148,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24442
554
150