INIT
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Menglin "Mark" Xu <mark@remarkablemark.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
# style-to-object
|
||||
|
||||
[](https://nodei.co/npm/style-to-object/)
|
||||
|
||||
[](https://www.npmjs.com/package/style-to-object)
|
||||
[](https://bundlephobia.com/package/style-to-object)
|
||||
[](https://github.com/remarkablemark/style-to-object/actions/workflows/build.yml)
|
||||
[](https://codecov.io/gh/remarkablemark/style-to-object)
|
||||
[](https://www.npmjs.com/package/style-to-object)
|
||||
|
||||
Parse CSS inline style to JavaScript object:
|
||||
|
||||
```js
|
||||
import parse from 'style-to-object';
|
||||
|
||||
parse('color: #C0FFEE; background: #BADA55;');
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```js
|
||||
{ color: '#C0FFEE', background: '#BADA55' }
|
||||
```
|
||||
|
||||
[JSFiddle](https://jsfiddle.net/remarkablemark/ykz2meot/) | [Examples](https://github.com/remarkablemark/style-to-object/tree/master/examples)
|
||||
|
||||
## Installation
|
||||
|
||||
[NPM](https://www.npmjs.com/package/style-to-object):
|
||||
|
||||
```sh
|
||||
npm install style-to-object --save
|
||||
```
|
||||
|
||||
[Yarn](https://yarn.fyi/style-to-object):
|
||||
|
||||
```sh
|
||||
yarn add style-to-object
|
||||
```
|
||||
|
||||
[CDN](https://unpkg.com/style-to-object/):
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/style-to-object@latest/dist/style-to-object.min.js"></script>
|
||||
<script>
|
||||
window.StyleToObject(/* string */);
|
||||
</script>
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Import with ES Modules:
|
||||
|
||||
```js
|
||||
import parse from 'style-to-object';
|
||||
```
|
||||
|
||||
Require with CommonJS:
|
||||
|
||||
```js
|
||||
const parse = require('style-to-object').default;
|
||||
```
|
||||
|
||||
Parse single declaration:
|
||||
|
||||
```js
|
||||
parse('line-height: 42');
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```js
|
||||
{ 'line-height': '42' }
|
||||
```
|
||||
|
||||
Parse multiple declarations:
|
||||
|
||||
```js
|
||||
parse(`
|
||||
border-color: #ACE;
|
||||
z-index: 1337;
|
||||
`);
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```js
|
||||
{ 'border-color': '#ACE', 'z-index': '1337' }
|
||||
```
|
||||
|
||||
Parse unknown declarations:
|
||||
|
||||
```js
|
||||
parse('answer: 42;');
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```js
|
||||
{ 'answer': '42' }
|
||||
```
|
||||
|
||||
Invalid declarations/arguments:
|
||||
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
```js
|
||||
parse(`
|
||||
top: ;
|
||||
right: 1em;
|
||||
`); // { right: '1em' }
|
||||
|
||||
parse(); // null
|
||||
parse(null); // null
|
||||
parse(1); // null
|
||||
parse(true); // null
|
||||
parse('top:'); // null
|
||||
parse(':12px'); // null
|
||||
parse(':'); // null
|
||||
parse(';'); // null
|
||||
|
||||
parse('top'); // throws Error
|
||||
parse('/*'); // throws Error
|
||||
```
|
||||
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
### Iterator
|
||||
|
||||
If the 2nd argument is a function, then the parser will return `null`:
|
||||
|
||||
```js
|
||||
parse('color: #f00', () => {}); // null
|
||||
```
|
||||
|
||||
But the function will iterate through each declaration:
|
||||
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
```js
|
||||
parse('color: #f00', (name, value, declaration) => {
|
||||
console.log(name); // 'color'
|
||||
console.log(value); // '#f00'
|
||||
console.log(declaration); // { type: 'declaration', property: 'color', value: '#f00' }
|
||||
});
|
||||
```
|
||||
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
This makes it easy to customize the output:
|
||||
|
||||
```js
|
||||
const style = `
|
||||
color: red;
|
||||
background: blue;
|
||||
`;
|
||||
const output = [];
|
||||
|
||||
function iterator(name, value) {
|
||||
output.push([name, value]);
|
||||
}
|
||||
|
||||
parse(style, iterator);
|
||||
console.log(output); // [['color', 'red'], ['background', 'blue']]
|
||||
```
|
||||
|
||||
## Migration
|
||||
|
||||
### v1
|
||||
|
||||
Migrated to TypeScript. Iterator excludes `Comment`. CommonJS requires the `.default` key:
|
||||
|
||||
```js
|
||||
const parse = require('style-to-object').default;
|
||||
```
|
||||
|
||||
## Release
|
||||
|
||||
Release and publish are automated by [Release Please](https://github.com/googleapis/release-please).
|
||||
|
||||
## Special Thanks
|
||||
|
||||
- [inline-style-parser](https://github.com/remarkablemark/inline-style-parser)
|
||||
- [Contributors](https://github.com/remarkablemark/style-to-object/graphs/contributors)
|
||||
|
||||
## License
|
||||
|
||||
[MIT](https://github.com/remarkablemark/style-to-object/blob/master/LICENSE)
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
import type { Declaration } from 'inline-style-parser';
|
||||
export { Declaration };
|
||||
export interface StyleObject {
|
||||
[name: string]: string;
|
||||
}
|
||||
type Iterator = (property: string, value: string, declaration: Declaration) => void;
|
||||
/**
|
||||
* Parses inline style to object.
|
||||
*
|
||||
* @param style - Inline style.
|
||||
* @param iterator - Iterator.
|
||||
* @returns - Style object or null.
|
||||
*
|
||||
* @example Parsing inline style to object:
|
||||
*
|
||||
* ```js
|
||||
* import parse from 'style-to-object';
|
||||
* parse('line-height: 42;'); // { 'line-height': '42' }
|
||||
* ```
|
||||
*/
|
||||
export default function StyleToObject(style: string, iterator?: Iterator): StyleObject | null;
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGvD,OAAO,EAAE,WAAW,EAAE,CAAC;AAEvB,MAAM,WAAW,WAAW;IAC1B,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;CACxB;AAED,KAAK,QAAQ,GAAG,CACd,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,WAAW,KACrB,IAAI,CAAC;AAEV;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACnC,KAAK,EAAE,MAAM,EACb,QAAQ,CAAC,EAAE,QAAQ,GAClB,WAAW,GAAG,IAAI,CA0BpB"}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = StyleToObject;
|
||||
const inline_style_parser_1 = __importDefault(require("inline-style-parser"));
|
||||
/**
|
||||
* Parses inline style to object.
|
||||
*
|
||||
* @param style - Inline style.
|
||||
* @param iterator - Iterator.
|
||||
* @returns - Style object or null.
|
||||
*
|
||||
* @example Parsing inline style to object:
|
||||
*
|
||||
* ```js
|
||||
* import parse from 'style-to-object';
|
||||
* parse('line-height: 42;'); // { 'line-height': '42' }
|
||||
* ```
|
||||
*/
|
||||
function StyleToObject(style, iterator) {
|
||||
let styleObject = null;
|
||||
if (!style || typeof style !== 'string') {
|
||||
return styleObject;
|
||||
}
|
||||
const declarations = (0, inline_style_parser_1.default)(style);
|
||||
const hasIterator = typeof iterator === 'function';
|
||||
declarations.forEach((declaration) => {
|
||||
if (declaration.type !== 'declaration') {
|
||||
return;
|
||||
}
|
||||
const { property, value } = declaration;
|
||||
if (hasIterator) {
|
||||
iterator(property, value, declaration);
|
||||
}
|
||||
else if (value) {
|
||||
styleObject = styleObject || {};
|
||||
styleObject[property] = value;
|
||||
}
|
||||
});
|
||||
return styleObject;
|
||||
}
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AA6BA,gCA6BC;AAzDD,8EAAwC;AAcxC;;;;;;;;;;;;;GAaG;AACH,SAAwB,aAAa,CACnC,KAAa,EACb,QAAmB;IAEnB,IAAI,WAAW,GAAuB,IAAI,CAAC;IAE3C,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxC,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,MAAM,YAAY,GAAG,IAAA,6BAAK,EAAC,KAAK,CAAC,CAAC;IAClC,MAAM,WAAW,GAAG,OAAO,QAAQ,KAAK,UAAU,CAAC;IAEnD,YAAY,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QACnC,IAAI,WAAW,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YACvC,OAAO;QACT,CAAC;QAED,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,WAAW,CAAC;QAExC,IAAI,WAAW,EAAE,CAAC;YAChB,QAAQ,CAAC,QAAQ,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC;QACzC,CAAC;aAAM,IAAI,KAAK,EAAE,CAAC;YACjB,WAAW,GAAG,WAAW,IAAI,EAAE,CAAC;YAChC,WAAW,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC;QAChC,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
||||
+305
@@ -0,0 +1,305 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
||||
typeof define === 'function' && define.amd ? define(factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.StyleToObject = factory());
|
||||
})(this, (function () { 'use strict';
|
||||
|
||||
// http://www.w3.org/TR/CSS21/grammar.html
|
||||
// https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027
|
||||
var COMMENT_REGEX = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;
|
||||
|
||||
var NEWLINE_REGEX = /\n/g;
|
||||
var WHITESPACE_REGEX = /^\s*/;
|
||||
|
||||
// declaration
|
||||
var PROPERTY_REGEX = /^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/;
|
||||
var COLON_REGEX = /^:\s*/;
|
||||
var VALUE_REGEX = /^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/;
|
||||
var SEMICOLON_REGEX = /^[;\s]*/;
|
||||
|
||||
// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill
|
||||
var TRIM_REGEX = /^\s+|\s+$/g;
|
||||
|
||||
// strings
|
||||
var NEWLINE = '\n';
|
||||
var FORWARD_SLASH = '/';
|
||||
var ASTERISK = '*';
|
||||
var EMPTY_STRING = '';
|
||||
|
||||
// types
|
||||
var TYPE_COMMENT = 'comment';
|
||||
var TYPE_DECLARATION = 'declaration';
|
||||
|
||||
/**
|
||||
* @param {String} style
|
||||
* @param {Object} [options]
|
||||
* @return {Object[]}
|
||||
* @throws {TypeError}
|
||||
* @throws {Error}
|
||||
*/
|
||||
function index (style, options) {
|
||||
if (typeof style !== 'string') {
|
||||
throw new TypeError('First argument must be a string');
|
||||
}
|
||||
|
||||
if (!style) return [];
|
||||
|
||||
options = options || {};
|
||||
|
||||
/**
|
||||
* Positional.
|
||||
*/
|
||||
var lineno = 1;
|
||||
var column = 1;
|
||||
|
||||
/**
|
||||
* Update lineno and column based on `str`.
|
||||
*
|
||||
* @param {String} str
|
||||
*/
|
||||
function updatePosition(str) {
|
||||
var lines = str.match(NEWLINE_REGEX);
|
||||
if (lines) lineno += lines.length;
|
||||
var i = str.lastIndexOf(NEWLINE);
|
||||
column = ~i ? str.length - i : column + str.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark position and patch `node.position`.
|
||||
*
|
||||
* @return {Function}
|
||||
*/
|
||||
function position() {
|
||||
var start = { line: lineno, column: column };
|
||||
return function (node) {
|
||||
node.position = new Position(start);
|
||||
whitespace();
|
||||
return node;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Store position information for a node.
|
||||
*
|
||||
* @constructor
|
||||
* @property {Object} start
|
||||
* @property {Object} end
|
||||
* @property {undefined|String} source
|
||||
*/
|
||||
function Position(start) {
|
||||
this.start = start;
|
||||
this.end = { line: lineno, column: column };
|
||||
this.source = options.source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Non-enumerable source string.
|
||||
*/
|
||||
Position.prototype.content = style;
|
||||
|
||||
/**
|
||||
* Error `msg`.
|
||||
*
|
||||
* @param {String} msg
|
||||
* @throws {Error}
|
||||
*/
|
||||
function error(msg) {
|
||||
var err = new Error(
|
||||
options.source + ':' + lineno + ':' + column + ': ' + msg
|
||||
);
|
||||
err.reason = msg;
|
||||
err.filename = options.source;
|
||||
err.line = lineno;
|
||||
err.column = column;
|
||||
err.source = style;
|
||||
|
||||
if (options.silent) ; else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Match `re` and return captures.
|
||||
*
|
||||
* @param {RegExp} re
|
||||
* @return {undefined|Array}
|
||||
*/
|
||||
function match(re) {
|
||||
var m = re.exec(style);
|
||||
if (!m) return;
|
||||
var str = m[0];
|
||||
updatePosition(str);
|
||||
style = style.slice(str.length);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse whitespace.
|
||||
*/
|
||||
function whitespace() {
|
||||
match(WHITESPACE_REGEX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse comments.
|
||||
*
|
||||
* @param {Object[]} [rules]
|
||||
* @return {Object[]}
|
||||
*/
|
||||
function comments(rules) {
|
||||
var c;
|
||||
rules = rules || [];
|
||||
while ((c = comment())) {
|
||||
if (c !== false) {
|
||||
rules.push(c);
|
||||
}
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse comment.
|
||||
*
|
||||
* @return {Object}
|
||||
* @throws {Error}
|
||||
*/
|
||||
function comment() {
|
||||
var pos = position();
|
||||
if (FORWARD_SLASH != style.charAt(0) || ASTERISK != style.charAt(1)) return;
|
||||
|
||||
var i = 2;
|
||||
while (
|
||||
EMPTY_STRING != style.charAt(i) &&
|
||||
(ASTERISK != style.charAt(i) || FORWARD_SLASH != style.charAt(i + 1))
|
||||
) {
|
||||
++i;
|
||||
}
|
||||
i += 2;
|
||||
|
||||
if (EMPTY_STRING === style.charAt(i - 1)) {
|
||||
return error('End of comment missing');
|
||||
}
|
||||
|
||||
var str = style.slice(2, i - 2);
|
||||
column += 2;
|
||||
updatePosition(str);
|
||||
style = style.slice(i);
|
||||
column += 2;
|
||||
|
||||
return pos({
|
||||
type: TYPE_COMMENT,
|
||||
comment: str
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse declaration.
|
||||
*
|
||||
* @return {Object}
|
||||
* @throws {Error}
|
||||
*/
|
||||
function declaration() {
|
||||
var pos = position();
|
||||
|
||||
// prop
|
||||
var prop = match(PROPERTY_REGEX);
|
||||
if (!prop) return;
|
||||
comment();
|
||||
|
||||
// :
|
||||
if (!match(COLON_REGEX)) return error("property missing ':'");
|
||||
|
||||
// val
|
||||
var val = match(VALUE_REGEX);
|
||||
|
||||
var ret = pos({
|
||||
type: TYPE_DECLARATION,
|
||||
property: trim(prop[0].replace(COMMENT_REGEX, EMPTY_STRING)),
|
||||
value: val
|
||||
? trim(val[0].replace(COMMENT_REGEX, EMPTY_STRING))
|
||||
: EMPTY_STRING
|
||||
});
|
||||
|
||||
// ;
|
||||
match(SEMICOLON_REGEX);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse declarations.
|
||||
*
|
||||
* @return {Object[]}
|
||||
*/
|
||||
function declarations() {
|
||||
var decls = [];
|
||||
|
||||
comments(decls);
|
||||
|
||||
// declarations
|
||||
var decl;
|
||||
while ((decl = declaration())) {
|
||||
if (decl !== false) {
|
||||
decls.push(decl);
|
||||
comments(decls);
|
||||
}
|
||||
}
|
||||
|
||||
return decls;
|
||||
}
|
||||
|
||||
whitespace();
|
||||
return declarations();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim `str`.
|
||||
*
|
||||
* @param {String} str
|
||||
* @return {String}
|
||||
*/
|
||||
function trim(str) {
|
||||
return str ? str.replace(TRIM_REGEX, EMPTY_STRING) : EMPTY_STRING;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses inline style to object.
|
||||
*
|
||||
* @param style - Inline style.
|
||||
* @param iterator - Iterator.
|
||||
* @returns - Style object or null.
|
||||
*
|
||||
* @example Parsing inline style to object:
|
||||
*
|
||||
* ```js
|
||||
* import parse from 'style-to-object';
|
||||
* parse('line-height: 42;'); // { 'line-height': '42' }
|
||||
* ```
|
||||
*/
|
||||
function StyleToObject(style, iterator) {
|
||||
let styleObject = null;
|
||||
if (!style || typeof style !== 'string') {
|
||||
return styleObject;
|
||||
}
|
||||
const declarations = index(style);
|
||||
const hasIterator = typeof iterator === 'function';
|
||||
declarations.forEach((declaration) => {
|
||||
if (declaration.type !== 'declaration') {
|
||||
return;
|
||||
}
|
||||
const { property, value } = declaration;
|
||||
if (hasIterator) {
|
||||
iterator(property, value, declaration);
|
||||
}
|
||||
else if (value) {
|
||||
styleObject = styleObject || {};
|
||||
styleObject[property] = value;
|
||||
}
|
||||
});
|
||||
return styleObject;
|
||||
}
|
||||
|
||||
return StyleToObject;
|
||||
|
||||
}));
|
||||
//# sourceMappingURL=style-to-object.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+2
@@ -0,0 +1,2 @@
|
||||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).StyleToObject=t()}(this,(function(){"use strict";var n=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,t=/\n/g,e=/^\s*/,r=/^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/,o=/^:\s*/,i=/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/,u=/^[;\s]*/,c=/^\s+|\s+$/g,f="";function s(n){return n?n.replace(c,f):f}return function(c,a){let l=null;if(!c||"string"!=typeof c)return l;const p=function(c,a){if("string"!=typeof c)throw new TypeError("First argument must be a string");if(!c)return[];a=a||{};var l=1,p=1;function h(n){var e=n.match(t);e&&(l+=e.length);var r=n.lastIndexOf("\n");p=~r?n.length-r:p+n.length}function m(){var n={line:l,column:p};return function(t){return t.position=new v(n),g(),t}}function v(n){this.start=n,this.end={line:l,column:p},this.source=a.source}function y(n){var t=new Error(a.source+":"+l+":"+p+": "+n);if(t.reason=n,t.filename=a.source,t.line=l,t.column=p,t.source=c,!a.silent)throw t}function d(n){var t=n.exec(c);if(t){var e=t[0];return h(e),c=c.slice(e.length),t}}function g(){d(e)}function w(n){var t;for(n=n||[];t=A();)!1!==t&&n.push(t);return n}function A(){var n=m();if("/"==c.charAt(0)&&"*"==c.charAt(1)){for(var t=2;f!=c.charAt(t)&&("*"!=c.charAt(t)||"/"!=c.charAt(t+1));)++t;if(t+=2,f===c.charAt(t-1))return y("End of comment missing");var e=c.slice(2,t-2);return p+=2,h(e),c=c.slice(t),p+=2,n({type:"comment",comment:e})}}function b(){var t=m(),e=d(r);if(e){if(A(),!d(o))return y("property missing ':'");var c=d(i),a=t({type:"declaration",property:s(e[0].replace(n,f)),value:c?s(c[0].replace(n,f)):f});return d(u),a}}return v.prototype.content=c,g(),function(){var n,t=[];for(w(t);n=b();)!1!==n&&(t.push(n),w(t));return t}()}(c),h="function"==typeof a;return p.forEach((n=>{if("declaration"!==n.type)return;const{property:t,value:e}=n;h?a(t,e,n):e&&(l=l||{},l[t]=e)})),l}}));
|
||||
//# sourceMappingURL=style-to-object.min.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+22
@@ -0,0 +1,22 @@
|
||||
import type { Declaration } from 'inline-style-parser';
|
||||
export { Declaration };
|
||||
export interface StyleObject {
|
||||
[name: string]: string;
|
||||
}
|
||||
type Iterator = (property: string, value: string, declaration: Declaration) => void;
|
||||
/**
|
||||
* Parses inline style to object.
|
||||
*
|
||||
* @param style - Inline style.
|
||||
* @param iterator - Iterator.
|
||||
* @returns - Style object or null.
|
||||
*
|
||||
* @example Parsing inline style to object:
|
||||
*
|
||||
* ```js
|
||||
* import parse from 'style-to-object';
|
||||
* parse('line-height: 42;'); // { 'line-height': '42' }
|
||||
* ```
|
||||
*/
|
||||
export default function StyleToObject(style: string, iterator?: Iterator): StyleObject | null;
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGvD,OAAO,EAAE,WAAW,EAAE,CAAC;AAEvB,MAAM,WAAW,WAAW;IAC1B,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;CACxB;AAED,KAAK,QAAQ,GAAG,CACd,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,WAAW,KACrB,IAAI,CAAC;AAEV;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACnC,KAAK,EAAE,MAAM,EACb,QAAQ,CAAC,EAAE,QAAQ,GAClB,WAAW,GAAG,IAAI,CA0BpB"}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
import parse from 'inline-style-parser';
|
||||
/**
|
||||
* Parses inline style to object.
|
||||
*
|
||||
* @param style - Inline style.
|
||||
* @param iterator - Iterator.
|
||||
* @returns - Style object or null.
|
||||
*
|
||||
* @example Parsing inline style to object:
|
||||
*
|
||||
* ```js
|
||||
* import parse from 'style-to-object';
|
||||
* parse('line-height: 42;'); // { 'line-height': '42' }
|
||||
* ```
|
||||
*/
|
||||
export default function StyleToObject(style, iterator) {
|
||||
let styleObject = null;
|
||||
if (!style || typeof style !== 'string') {
|
||||
return styleObject;
|
||||
}
|
||||
const declarations = parse(style);
|
||||
const hasIterator = typeof iterator === 'function';
|
||||
declarations.forEach((declaration) => {
|
||||
if (declaration.type !== 'declaration') {
|
||||
return;
|
||||
}
|
||||
const { property, value } = declaration;
|
||||
if (hasIterator) {
|
||||
iterator(property, value, declaration);
|
||||
}
|
||||
else if (value) {
|
||||
styleObject = styleObject || {};
|
||||
styleObject[property] = value;
|
||||
}
|
||||
});
|
||||
return styleObject;
|
||||
}
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,qBAAqB,CAAC;AAcxC;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACnC,KAAa,EACb,QAAmB;IAEnB,IAAI,WAAW,GAAuB,IAAI,CAAC;IAE3C,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxC,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,MAAM,YAAY,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,WAAW,GAAG,OAAO,QAAQ,KAAK,UAAU,CAAC;IAEnD,YAAY,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QACnC,IAAI,WAAW,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YACvC,OAAO;QACT,CAAC;QAED,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,WAAW,CAAC;QAExC,IAAI,WAAW,EAAE,CAAC;YAChB,QAAQ,CAAC,QAAQ,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC;QACzC,CAAC;aAAM,IAAI,KAAK,EAAE,CAAC;YACjB,WAAW,GAAG,WAAW,IAAI,EAAE,CAAC;YAChC,WAAW,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC;QAChC,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
import parse from 'inline-style-parser';
|
||||
|
||||
/**
|
||||
* Parses inline style to object.
|
||||
*
|
||||
* @param style - Inline style.
|
||||
* @param iterator - Iterator.
|
||||
* @returns - Style object or null.
|
||||
*
|
||||
* @example Parsing inline style to object:
|
||||
*
|
||||
* ```js
|
||||
* import parse from 'style-to-object';
|
||||
* parse('line-height: 42;'); // { 'line-height': '42' }
|
||||
* ```
|
||||
*/
|
||||
function StyleToObject(style, iterator) {
|
||||
let styleObject = null;
|
||||
if (!style || typeof style !== 'string') {
|
||||
return styleObject;
|
||||
}
|
||||
const declarations = parse(style);
|
||||
const hasIterator = typeof iterator === 'function';
|
||||
declarations.forEach((declaration) => {
|
||||
if (declaration.type !== 'declaration') {
|
||||
return;
|
||||
}
|
||||
const { property, value } = declaration;
|
||||
if (hasIterator) {
|
||||
iterator(property, value, declaration);
|
||||
}
|
||||
else if (value) {
|
||||
styleObject = styleObject || {};
|
||||
styleObject[property] = value;
|
||||
}
|
||||
});
|
||||
return styleObject;
|
||||
}
|
||||
|
||||
export { StyleToObject as default };
|
||||
//# sourceMappingURL=index.mjs.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAeA;;;;;;;;;;;;;AAaG;AACW,SAAU,aAAa,CACnC,KAAa,EACb,QAAmB,EAAA;IAEnB,IAAI,WAAW,GAAuB,IAAI;IAE1C,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AACvC,QAAA,OAAO,WAAW;IACpB;AAEA,IAAA,MAAM,YAAY,GAAG,KAAK,CAAC,KAAK,CAAC;AACjC,IAAA,MAAM,WAAW,GAAG,OAAO,QAAQ,KAAK,UAAU;AAElD,IAAA,YAAY,CAAC,OAAO,CAAC,CAAC,WAAW,KAAI;AACnC,QAAA,IAAI,WAAW,CAAC,IAAI,KAAK,aAAa,EAAE;YACtC;QACF;AAEA,QAAA,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,WAAW;QAEvC,IAAI,WAAW,EAAE;AACf,YAAA,QAAQ,CAAC,QAAQ,EAAE,KAAK,EAAE,WAAW,CAAC;QACxC;aAAO,IAAI,KAAK,EAAE;AAChB,YAAA,WAAW,GAAG,WAAW,IAAI,EAAE;AAC/B,YAAA,WAAW,CAAC,QAAQ,CAAC,GAAG,KAAK;QAC/B;AACF,IAAA,CAAC,CAAC;AAEF,IAAA,OAAO,WAAW;AACpB;;;;"}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
{
|
||||
"name": "style-to-object",
|
||||
"version": "1.0.14",
|
||||
"description": "Parse CSS inline style to JavaScript object.",
|
||||
"author": "Mark <mark@remarkablemark.org>",
|
||||
"main": "./cjs/index.js",
|
||||
"module": "./esm/index.mjs",
|
||||
"types": "./esm/index.d.ts",
|
||||
"exports": {
|
||||
"import": {
|
||||
"types": "./esm/index.d.ts",
|
||||
"default": "./esm/index.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./cjs/index.d.ts",
|
||||
"default": "./cjs/index.js"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"build": "run-s build:*",
|
||||
"build:cjs": "tsc --project tsconfig.cjs.json",
|
||||
"build:esm": "tsc --project tsconfig.json",
|
||||
"build:umd": "rollup --config --failAfterWarnings",
|
||||
"clean": "rm -rf cjs coverage dist esm",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "npm run lint -- --fix",
|
||||
"lint:package": "publint",
|
||||
"lint:tsc": "tsc --noEmit",
|
||||
"prepare": "husky",
|
||||
"prepublishOnly": "run-s lint lint:tsc test clean build",
|
||||
"test": "jest",
|
||||
"test:ci": "CI=true jest --ci --colors --coverage",
|
||||
"test:esm": "npm run build && node --test **/*.test.mjs",
|
||||
"test:watch": "npm run test -- --watch"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/remarkablemark/style-to-object.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/remarkablemark/style-to-object/issues"
|
||||
},
|
||||
"keywords": [
|
||||
"style-to-object",
|
||||
"inline",
|
||||
"style",
|
||||
"parser",
|
||||
"css",
|
||||
"object",
|
||||
"pojo"
|
||||
],
|
||||
"dependencies": {
|
||||
"inline-style-parser": "0.2.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "20.1.0",
|
||||
"@commitlint/config-conventional": "20.0.0",
|
||||
"@eslint/compat": "2.0.0",
|
||||
"@eslint/eslintrc": "3.3.1",
|
||||
"@eslint/js": "9.39.1",
|
||||
"@rollup/plugin-commonjs": "29.0.0",
|
||||
"@rollup/plugin-node-resolve": "16.0.3",
|
||||
"@rollup/plugin-terser": "0.4.4",
|
||||
"@rollup/plugin-typescript": "12.3.0",
|
||||
"@types/jest": "30.0.0",
|
||||
"@types/node": "24.10.1",
|
||||
"@typescript-eslint/eslint-plugin": "8.46.4",
|
||||
"@typescript-eslint/parser": "8.46.4",
|
||||
"eslint": "9.39.1",
|
||||
"eslint-plugin-prettier": "5.5.4",
|
||||
"eslint-plugin-simple-import-sort": "12.1.1",
|
||||
"globals": "16.5.0",
|
||||
"husky": "9.1.7",
|
||||
"jest": "30.2.0",
|
||||
"lint-staged": "16.2.6",
|
||||
"npm-run-all": "4.1.5",
|
||||
"prettier": "3.6.2",
|
||||
"publint": "0.3.15",
|
||||
"rollup": "4.53.2",
|
||||
"ts-jest": "29.4.5",
|
||||
"ts-node": "10.9.2",
|
||||
"tslib": "2.8.1",
|
||||
"typescript": "5.9.3"
|
||||
},
|
||||
"files": [
|
||||
"/cjs",
|
||||
"/dist",
|
||||
"/esm",
|
||||
"/src"
|
||||
],
|
||||
"license": "MIT"
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
import type { Declaration } from 'inline-style-parser';
|
||||
import parse from 'inline-style-parser';
|
||||
|
||||
export { Declaration };
|
||||
|
||||
export interface StyleObject {
|
||||
[name: string]: string;
|
||||
}
|
||||
|
||||
type Iterator = (
|
||||
property: string,
|
||||
value: string,
|
||||
declaration: Declaration,
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* Parses inline style to object.
|
||||
*
|
||||
* @param style - Inline style.
|
||||
* @param iterator - Iterator.
|
||||
* @returns - Style object or null.
|
||||
*
|
||||
* @example Parsing inline style to object:
|
||||
*
|
||||
* ```js
|
||||
* import parse from 'style-to-object';
|
||||
* parse('line-height: 42;'); // { 'line-height': '42' }
|
||||
* ```
|
||||
*/
|
||||
export default function StyleToObject(
|
||||
style: string,
|
||||
iterator?: Iterator,
|
||||
): StyleObject | null {
|
||||
let styleObject: StyleObject | null = null;
|
||||
|
||||
if (!style || typeof style !== 'string') {
|
||||
return styleObject;
|
||||
}
|
||||
|
||||
const declarations = parse(style);
|
||||
const hasIterator = typeof iterator === 'function';
|
||||
|
||||
declarations.forEach((declaration) => {
|
||||
if (declaration.type !== 'declaration') {
|
||||
return;
|
||||
}
|
||||
|
||||
const { property, value } = declaration;
|
||||
|
||||
if (hasIterator) {
|
||||
iterator(property, value, declaration);
|
||||
} else if (value) {
|
||||
styleObject = styleObject || {};
|
||||
styleObject[property] = value;
|
||||
}
|
||||
});
|
||||
|
||||
return styleObject;
|
||||
}
|
||||
Reference in New Issue
Block a user