style-to-object
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="0.2.2"></a> | ||
## [0.2.2](https://github.com/remarkablemark/style-to-object/compare/v0.2.1...v0.2.2) (2018-09-13) | ||
<a name="0.2.1"></a> | ||
@@ -7,0 +12,0 @@ ## [0.2.1](https://github.com/remarkablemark/style-to-object/compare/v0.2.0...v0.2.1) (2018-05-09) |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.StyleToObject = factory()); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(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 commentre = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g; | ||
// http://www.w3.org/TR/CSS21/grammar.html | ||
// https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027 | ||
var commentre = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g; | ||
var parse = function(css, options){ | ||
options = options || {}; | ||
var parse = function(css, options){ | ||
options = options || {}; | ||
/** | ||
* Positional. | ||
*/ | ||
/** | ||
* Positional. | ||
*/ | ||
var lineno = 1; | ||
var column = 1; | ||
var lineno = 1; | ||
var column = 1; | ||
/** | ||
* Update lineno and column based on `str`. | ||
*/ | ||
/** | ||
* Update lineno and column based on `str`. | ||
*/ | ||
function updatePosition(str) { | ||
var lines = str.match(/\n/g); | ||
if (lines) lineno += lines.length; | ||
var i = str.lastIndexOf('\n'); | ||
column = ~i ? str.length - i : column + str.length; | ||
} | ||
function updatePosition(str) { | ||
var lines = str.match(/\n/g); | ||
if (lines) lineno += lines.length; | ||
var i = str.lastIndexOf('\n'); | ||
column = ~i ? str.length - i : column + str.length; | ||
} | ||
/** | ||
* Mark position and patch `node.position`. | ||
*/ | ||
/** | ||
* Mark position and patch `node.position`. | ||
*/ | ||
function position() { | ||
var start = { line: lineno, column: column }; | ||
return function(node){ | ||
node.position = new Position(start); | ||
whitespace(); | ||
return node; | ||
}; | ||
} | ||
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 | ||
*/ | ||
/** | ||
* Store position information for a node | ||
*/ | ||
function Position(start) { | ||
this.start = start; | ||
this.end = { line: lineno, column: column }; | ||
this.source = options.source; | ||
} | ||
function Position(start) { | ||
this.start = start; | ||
this.end = { line: lineno, column: column }; | ||
this.source = options.source; | ||
} | ||
/** | ||
* Non-enumerable source string | ||
*/ | ||
/** | ||
* Non-enumerable source string | ||
*/ | ||
Position.prototype.content = css; | ||
Position.prototype.content = css; | ||
/** | ||
* Error `msg`. | ||
*/ | ||
/** | ||
* Error `msg`. | ||
*/ | ||
var errorsList = []; | ||
var errorsList = []; | ||
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 = css; | ||
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 = css; | ||
if (options.silent) { | ||
errorsList.push(err); | ||
} else { | ||
throw err; | ||
if (options.silent) { | ||
errorsList.push(err); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
/** | ||
* Parse stylesheet. | ||
*/ | ||
/** | ||
* Parse stylesheet. | ||
*/ | ||
function stylesheet() { | ||
var rulesList = rules(); | ||
function stylesheet() { | ||
var rulesList = rules(); | ||
return { | ||
type: 'stylesheet', | ||
stylesheet: { | ||
source: options.source, | ||
rules: rulesList, | ||
parsingErrors: errorsList | ||
} | ||
}; | ||
} | ||
return { | ||
type: 'stylesheet', | ||
stylesheet: { | ||
source: options.source, | ||
rules: rulesList, | ||
parsingErrors: errorsList | ||
} | ||
}; | ||
} | ||
/** | ||
* Opening brace. | ||
*/ | ||
/** | ||
* Opening brace. | ||
*/ | ||
function open() { | ||
return match(/^{\s*/); | ||
} | ||
function open() { | ||
return match(/^{\s*/); | ||
} | ||
/** | ||
* Closing brace. | ||
*/ | ||
/** | ||
* Closing brace. | ||
*/ | ||
function close() { | ||
return match(/^}/); | ||
} | ||
function close() { | ||
return match(/^}/); | ||
} | ||
/** | ||
* Parse ruleset. | ||
*/ | ||
/** | ||
* Parse ruleset. | ||
*/ | ||
function rules() { | ||
var node; | ||
var rules = []; | ||
whitespace(); | ||
comments(rules); | ||
while (css.length && css.charAt(0) != '}' && (node = atrule() || rule())) { | ||
if (node !== false) { | ||
rules.push(node); | ||
comments(rules); | ||
function rules() { | ||
var node; | ||
var rules = []; | ||
whitespace(); | ||
comments(rules); | ||
while (css.length && css.charAt(0) != '}' && (node = atrule() || rule())) { | ||
if (node !== false) { | ||
rules.push(node); | ||
comments(rules); | ||
} | ||
} | ||
return rules; | ||
} | ||
return rules; | ||
} | ||
/** | ||
* Match `re` and return captures. | ||
*/ | ||
/** | ||
* Match `re` and return captures. | ||
*/ | ||
function match(re) { | ||
var m = re.exec(css); | ||
if (!m) return; | ||
var str = m[0]; | ||
updatePosition(str); | ||
css = css.slice(str.length); | ||
return m; | ||
} | ||
function match(re) { | ||
var m = re.exec(css); | ||
if (!m) return; | ||
var str = m[0]; | ||
updatePosition(str); | ||
css = css.slice(str.length); | ||
return m; | ||
} | ||
/** | ||
* Parse whitespace. | ||
*/ | ||
/** | ||
* Parse whitespace. | ||
*/ | ||
function whitespace() { | ||
match(/^\s*/); | ||
} | ||
function whitespace() { | ||
match(/^\s*/); | ||
} | ||
/** | ||
* Parse comments; | ||
*/ | ||
/** | ||
* Parse comments; | ||
*/ | ||
function comments(rules) { | ||
var c; | ||
rules = rules || []; | ||
while (c = comment()) { | ||
if (c !== false) { | ||
rules.push(c); | ||
function comments(rules) { | ||
var c; | ||
rules = rules || []; | ||
while (c = comment()) { | ||
if (c !== false) { | ||
rules.push(c); | ||
} | ||
} | ||
return rules; | ||
} | ||
return rules; | ||
} | ||
/** | ||
* Parse comment. | ||
*/ | ||
/** | ||
* Parse comment. | ||
*/ | ||
function comment() { | ||
var pos = position(); | ||
if ('/' != css.charAt(0) || '*' != css.charAt(1)) return; | ||
function comment() { | ||
var pos = position(); | ||
if ('/' != css.charAt(0) || '*' != css.charAt(1)) return; | ||
var i = 2; | ||
while ("" != css.charAt(i) && ('*' != css.charAt(i) || '/' != css.charAt(i + 1))) ++i; | ||
i += 2; | ||
var i = 2; | ||
while ("" != css.charAt(i) && ('*' != css.charAt(i) || '/' != css.charAt(i + 1))) ++i; | ||
i += 2; | ||
if ("" === css.charAt(i-1)) { | ||
return error('End of comment missing'); | ||
} | ||
if ("" === css.charAt(i-1)) { | ||
return error('End of comment missing'); | ||
} | ||
var str = css.slice(2, i - 2); | ||
column += 2; | ||
updatePosition(str); | ||
css = css.slice(i); | ||
column += 2; | ||
var str = css.slice(2, i - 2); | ||
column += 2; | ||
updatePosition(str); | ||
css = css.slice(i); | ||
column += 2; | ||
return pos({ | ||
type: 'comment', | ||
comment: str | ||
}); | ||
} | ||
return pos({ | ||
type: 'comment', | ||
comment: str | ||
}); | ||
} | ||
/** | ||
* Parse selector. | ||
*/ | ||
/** | ||
* Parse selector. | ||
*/ | ||
function selector() { | ||
var m = match(/^([^{]+)/); | ||
if (!m) return; | ||
/* @fix Remove all comments from selectors | ||
* http://ostermiller.org/findcomment.html */ | ||
return trim(m[0]) | ||
.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '') | ||
.replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, function(m) { | ||
return m.replace(/,/g, '\u200C'); | ||
}) | ||
.split(/\s*(?![^(]*\)),\s*/) | ||
.map(function(s) { | ||
return s.replace(/\u200C/g, ','); | ||
}); | ||
} | ||
function selector() { | ||
var m = match(/^([^{]+)/); | ||
if (!m) return; | ||
/* @fix Remove all comments from selectors | ||
* http://ostermiller.org/findcomment.html */ | ||
return trim(m[0]) | ||
.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '') | ||
.replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, function(m) { | ||
return m.replace(/,/g, '\u200C'); | ||
}) | ||
.split(/\s*(?![^(]*\)),\s*/) | ||
.map(function(s) { | ||
return s.replace(/\u200C/g, ','); | ||
}); | ||
} | ||
/** | ||
* Parse declaration. | ||
*/ | ||
/** | ||
* Parse declaration. | ||
*/ | ||
function declaration() { | ||
var pos = position(); | ||
function declaration() { | ||
var pos = position(); | ||
// prop | ||
var prop = match(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/); | ||
if (!prop) return; | ||
prop = trim(prop[0]); | ||
// prop | ||
var prop = match(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/); | ||
if (!prop) return; | ||
prop = trim(prop[0]); | ||
// : | ||
if (!match(/^:\s*/)) return error("property missing ':'"); | ||
// : | ||
if (!match(/^:\s*/)) return error("property missing ':'"); | ||
// val | ||
var val = match(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/); | ||
// val | ||
var val = match(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/); | ||
var ret = pos({ | ||
type: 'declaration', | ||
property: prop.replace(commentre, ''), | ||
value: val ? trim(val[0]).replace(commentre, '') : '' | ||
}); | ||
var ret = pos({ | ||
type: 'declaration', | ||
property: prop.replace(commentre, ''), | ||
value: val ? trim(val[0]).replace(commentre, '') : '' | ||
}); | ||
// ; | ||
match(/^[;\s]*/); | ||
// ; | ||
match(/^[;\s]*/); | ||
return ret; | ||
} | ||
return ret; | ||
} | ||
/** | ||
* Parse declarations. | ||
*/ | ||
/** | ||
* Parse declarations. | ||
*/ | ||
function declarations() { | ||
var decls = []; | ||
function declarations() { | ||
var decls = []; | ||
if (!open()) return error("missing '{'"); | ||
comments(decls); | ||
if (!open()) return error("missing '{'"); | ||
comments(decls); | ||
// declarations | ||
var decl; | ||
while (decl = declaration()) { | ||
if (decl !== false) { | ||
decls.push(decl); | ||
comments(decls); | ||
// declarations | ||
var decl; | ||
while (decl = declaration()) { | ||
if (decl !== false) { | ||
decls.push(decl); | ||
comments(decls); | ||
} | ||
} | ||
if (!close()) return error("missing '}'"); | ||
return decls; | ||
} | ||
if (!close()) return error("missing '}'"); | ||
return decls; | ||
} | ||
/** | ||
* Parse keyframe. | ||
*/ | ||
/** | ||
* Parse keyframe. | ||
*/ | ||
function keyframe() { | ||
var m; | ||
var vals = []; | ||
var pos = position(); | ||
function keyframe() { | ||
var m; | ||
var vals = []; | ||
var pos = position(); | ||
while (m = match(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/)) { | ||
vals.push(m[1]); | ||
match(/^,\s*/); | ||
} | ||
while (m = match(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/)) { | ||
vals.push(m[1]); | ||
match(/^,\s*/); | ||
if (!vals.length) return; | ||
return pos({ | ||
type: 'keyframe', | ||
values: vals, | ||
declarations: declarations() | ||
}); | ||
} | ||
if (!vals.length) return; | ||
/** | ||
* Parse keyframes. | ||
*/ | ||
return pos({ | ||
type: 'keyframe', | ||
values: vals, | ||
declarations: declarations() | ||
}); | ||
} | ||
function atkeyframes() { | ||
var pos = position(); | ||
var m = match(/^@([-\w]+)?keyframes\s*/); | ||
/** | ||
* Parse keyframes. | ||
*/ | ||
if (!m) return; | ||
var vendor = m[1]; | ||
function atkeyframes() { | ||
var pos = position(); | ||
var m = match(/^@([-\w]+)?keyframes\s*/); | ||
// identifier | ||
var m = match(/^([-\w]+)\s*/); | ||
if (!m) return error("@keyframes missing name"); | ||
var name = m[1]; | ||
if (!m) return; | ||
var vendor = m[1]; | ||
if (!open()) return error("@keyframes missing '{'"); | ||
// identifier | ||
var m = match(/^([-\w]+)\s*/); | ||
if (!m) return error("@keyframes missing name"); | ||
var name = m[1]; | ||
var frame; | ||
var frames = comments(); | ||
while (frame = keyframe()) { | ||
frames.push(frame); | ||
frames = frames.concat(comments()); | ||
} | ||
if (!open()) return error("@keyframes missing '{'"); | ||
if (!close()) return error("@keyframes missing '}'"); | ||
var frame; | ||
var frames = comments(); | ||
while (frame = keyframe()) { | ||
frames.push(frame); | ||
frames = frames.concat(comments()); | ||
return pos({ | ||
type: 'keyframes', | ||
name: name, | ||
vendor: vendor, | ||
keyframes: frames | ||
}); | ||
} | ||
if (!close()) return error("@keyframes missing '}'"); | ||
/** | ||
* Parse supports. | ||
*/ | ||
return pos({ | ||
type: 'keyframes', | ||
name: name, | ||
vendor: vendor, | ||
keyframes: frames | ||
}); | ||
} | ||
function atsupports() { | ||
var pos = position(); | ||
var m = match(/^@supports *([^{]+)/); | ||
/** | ||
* Parse supports. | ||
*/ | ||
if (!m) return; | ||
var supports = trim(m[1]); | ||
function atsupports() { | ||
var pos = position(); | ||
var m = match(/^@supports *([^{]+)/); | ||
if (!open()) return error("@supports missing '{'"); | ||
if (!m) return; | ||
var supports = trim(m[1]); | ||
var style = comments().concat(rules()); | ||
if (!open()) return error("@supports missing '{'"); | ||
if (!close()) return error("@supports missing '}'"); | ||
var style = comments().concat(rules()); | ||
return pos({ | ||
type: 'supports', | ||
supports: supports, | ||
rules: style | ||
}); | ||
} | ||
if (!close()) return error("@supports missing '}'"); | ||
/** | ||
* Parse host. | ||
*/ | ||
return pos({ | ||
type: 'supports', | ||
supports: supports, | ||
rules: style | ||
}); | ||
} | ||
function athost() { | ||
var pos = position(); | ||
var m = match(/^@host\s*/); | ||
/** | ||
* Parse host. | ||
*/ | ||
if (!m) return; | ||
function athost() { | ||
var pos = position(); | ||
var m = match(/^@host\s*/); | ||
if (!open()) return error("@host missing '{'"); | ||
if (!m) return; | ||
var style = comments().concat(rules()); | ||
if (!open()) return error("@host missing '{'"); | ||
if (!close()) return error("@host missing '}'"); | ||
var style = comments().concat(rules()); | ||
return pos({ | ||
type: 'host', | ||
rules: style | ||
}); | ||
} | ||
if (!close()) return error("@host missing '}'"); | ||
/** | ||
* Parse media. | ||
*/ | ||
return pos({ | ||
type: 'host', | ||
rules: style | ||
}); | ||
} | ||
function atmedia() { | ||
var pos = position(); | ||
var m = match(/^@media *([^{]+)/); | ||
/** | ||
* Parse media. | ||
*/ | ||
if (!m) return; | ||
var media = trim(m[1]); | ||
function atmedia() { | ||
var pos = position(); | ||
var m = match(/^@media *([^{]+)/); | ||
if (!open()) return error("@media missing '{'"); | ||
if (!m) return; | ||
var media = trim(m[1]); | ||
var style = comments().concat(rules()); | ||
if (!open()) return error("@media missing '{'"); | ||
if (!close()) return error("@media missing '}'"); | ||
var style = comments().concat(rules()); | ||
return pos({ | ||
type: 'media', | ||
media: media, | ||
rules: style | ||
}); | ||
} | ||
if (!close()) return error("@media missing '}'"); | ||
return pos({ | ||
type: 'media', | ||
media: media, | ||
rules: style | ||
}); | ||
} | ||
/** | ||
* Parse custom-media. | ||
*/ | ||
function atcustommedia() { | ||
var pos = position(); | ||
var m = match(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/); | ||
if (!m) return; | ||
/** | ||
* Parse custom-media. | ||
*/ | ||
return pos({ | ||
type: 'custom-media', | ||
name: trim(m[1]), | ||
media: trim(m[2]) | ||
}); | ||
} | ||
function atcustommedia() { | ||
var pos = position(); | ||
var m = match(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/); | ||
if (!m) return; | ||
/** | ||
* Parse paged media. | ||
*/ | ||
return pos({ | ||
type: 'custom-media', | ||
name: trim(m[1]), | ||
media: trim(m[2]) | ||
}); | ||
} | ||
function atpage() { | ||
var pos = position(); | ||
var m = match(/^@page */); | ||
if (!m) return; | ||
/** | ||
* Parse paged media. | ||
*/ | ||
var sel = selector() || []; | ||
function atpage() { | ||
var pos = position(); | ||
var m = match(/^@page */); | ||
if (!m) return; | ||
if (!open()) return error("@page missing '{'"); | ||
var decls = comments(); | ||
var sel = selector() || []; | ||
// declarations | ||
var decl; | ||
while (decl = declaration()) { | ||
decls.push(decl); | ||
decls = decls.concat(comments()); | ||
} | ||
if (!open()) return error("@page missing '{'"); | ||
var decls = comments(); | ||
if (!close()) return error("@page missing '}'"); | ||
// declarations | ||
var decl; | ||
while (decl = declaration()) { | ||
decls.push(decl); | ||
decls = decls.concat(comments()); | ||
return pos({ | ||
type: 'page', | ||
selectors: sel, | ||
declarations: decls | ||
}); | ||
} | ||
if (!close()) return error("@page missing '}'"); | ||
/** | ||
* Parse document. | ||
*/ | ||
return pos({ | ||
type: 'page', | ||
selectors: sel, | ||
declarations: decls | ||
}); | ||
} | ||
function atdocument() { | ||
var pos = position(); | ||
var m = match(/^@([-\w]+)?document *([^{]+)/); | ||
if (!m) return; | ||
/** | ||
* Parse document. | ||
*/ | ||
var vendor = trim(m[1]); | ||
var doc = trim(m[2]); | ||
function atdocument() { | ||
var pos = position(); | ||
var m = match(/^@([-\w]+)?document *([^{]+)/); | ||
if (!m) return; | ||
if (!open()) return error("@document missing '{'"); | ||
var vendor = trim(m[1]); | ||
var doc = trim(m[2]); | ||
var style = comments().concat(rules()); | ||
if (!open()) return error("@document missing '{'"); | ||
if (!close()) return error("@document missing '}'"); | ||
var style = comments().concat(rules()); | ||
return pos({ | ||
type: 'document', | ||
document: doc, | ||
vendor: vendor, | ||
rules: style | ||
}); | ||
} | ||
if (!close()) return error("@document missing '}'"); | ||
/** | ||
* Parse font-face. | ||
*/ | ||
return pos({ | ||
type: 'document', | ||
document: doc, | ||
vendor: vendor, | ||
rules: style | ||
}); | ||
} | ||
function atfontface() { | ||
var pos = position(); | ||
var m = match(/^@font-face\s*/); | ||
if (!m) return; | ||
/** | ||
* Parse font-face. | ||
*/ | ||
if (!open()) return error("@font-face missing '{'"); | ||
var decls = comments(); | ||
function atfontface() { | ||
var pos = position(); | ||
var m = match(/^@font-face\s*/); | ||
if (!m) return; | ||
// declarations | ||
var decl; | ||
while (decl = declaration()) { | ||
decls.push(decl); | ||
decls = decls.concat(comments()); | ||
} | ||
if (!open()) return error("@font-face missing '{'"); | ||
var decls = comments(); | ||
if (!close()) return error("@font-face missing '}'"); | ||
// declarations | ||
var decl; | ||
while (decl = declaration()) { | ||
decls.push(decl); | ||
decls = decls.concat(comments()); | ||
return pos({ | ||
type: 'font-face', | ||
declarations: decls | ||
}); | ||
} | ||
if (!close()) return error("@font-face missing '}'"); | ||
/** | ||
* Parse import | ||
*/ | ||
return pos({ | ||
type: 'font-face', | ||
declarations: decls | ||
}); | ||
} | ||
var atimport = _compileAtrule('import'); | ||
/** | ||
* Parse import | ||
*/ | ||
/** | ||
* Parse charset | ||
*/ | ||
var atimport = _compileAtrule('import'); | ||
var atcharset = _compileAtrule('charset'); | ||
/** | ||
* Parse charset | ||
*/ | ||
/** | ||
* Parse namespace | ||
*/ | ||
var atcharset = _compileAtrule('charset'); | ||
var atnamespace = _compileAtrule('namespace'); | ||
/** | ||
* Parse namespace | ||
*/ | ||
/** | ||
* Parse non-block at-rules | ||
*/ | ||
var atnamespace = _compileAtrule('namespace'); | ||
/** | ||
* Parse non-block at-rules | ||
*/ | ||
function _compileAtrule(name) { | ||
var re = new RegExp('^@' + name + '\\s*([^;]+);'); | ||
return function() { | ||
var pos = position(); | ||
var m = match(re); | ||
if (!m) return; | ||
var ret = { type: name }; | ||
ret[name] = m[1].trim(); | ||
return pos(ret); | ||
} | ||
} | ||
/** | ||
* Parse at rule. | ||
*/ | ||
function _compileAtrule(name) { | ||
var re = new RegExp('^@' + name + '\\s*([^;]+);'); | ||
return function() { | ||
function atrule() { | ||
if (css[0] != '@') return; | ||
return atkeyframes() | ||
|| atmedia() | ||
|| atcustommedia() | ||
|| atsupports() | ||
|| atimport() | ||
|| atcharset() | ||
|| atnamespace() | ||
|| atdocument() | ||
|| atpage() | ||
|| athost() | ||
|| atfontface(); | ||
} | ||
/** | ||
* Parse rule. | ||
*/ | ||
function rule() { | ||
var pos = position(); | ||
var m = match(re); | ||
if (!m) return; | ||
var ret = { type: name }; | ||
ret[name] = m[1].trim(); | ||
return pos(ret); | ||
var sel = selector(); | ||
if (!sel) return error('selector missing'); | ||
comments(); | ||
return pos({ | ||
type: 'rule', | ||
selectors: sel, | ||
declarations: declarations() | ||
}); | ||
} | ||
} | ||
return addParent(stylesheet()); | ||
}; | ||
/** | ||
* Parse at rule. | ||
* Trim `str`. | ||
*/ | ||
function atrule() { | ||
if (css[0] != '@') return; | ||
return atkeyframes() | ||
|| atmedia() | ||
|| atcustommedia() | ||
|| atsupports() | ||
|| atimport() | ||
|| atcharset() | ||
|| atnamespace() | ||
|| atdocument() | ||
|| atpage() | ||
|| athost() | ||
|| atfontface(); | ||
function trim(str) { | ||
return str ? str.replace(/^\s+|\s+$/g, '') : ''; | ||
} | ||
/** | ||
* Parse rule. | ||
* Adds non-enumerable parent node reference to each node. | ||
*/ | ||
function rule() { | ||
var pos = position(); | ||
var sel = selector(); | ||
function addParent(obj, parent) { | ||
var isNode = obj && typeof obj.type === 'string'; | ||
var childParent = isNode ? obj : parent; | ||
if (!sel) return error('selector missing'); | ||
comments(); | ||
for (var k in obj) { | ||
var value = obj[k]; | ||
if (Array.isArray(value)) { | ||
value.forEach(function(v) { addParent(v, childParent); }); | ||
} else if (value && typeof value === 'object') { | ||
addParent(value, childParent); | ||
} | ||
} | ||
return pos({ | ||
type: 'rule', | ||
selectors: sel, | ||
declarations: declarations() | ||
}); | ||
} | ||
return addParent(stylesheet()); | ||
}; | ||
/** | ||
* Trim `str`. | ||
*/ | ||
function trim(str) { | ||
return str ? str.replace(/^\s+|\s+$/g, '') : ''; | ||
} | ||
/** | ||
* Adds non-enumerable parent node reference to each node. | ||
*/ | ||
function addParent(obj, parent) { | ||
var isNode = obj && typeof obj.type === 'string'; | ||
var childParent = isNode ? obj : parent; | ||
for (var k in obj) { | ||
var value = obj[k]; | ||
if (Array.isArray(value)) { | ||
value.forEach(function(v) { addParent(v, childParent); }); | ||
} else if (value && typeof value === 'object') { | ||
addParent(value, childParent); | ||
if (isNode) { | ||
Object.defineProperty(obj, 'parent', { | ||
configurable: true, | ||
writable: true, | ||
enumerable: false, | ||
value: parent || null | ||
}); | ||
} | ||
} | ||
if (isNode) { | ||
Object.defineProperty(obj, 'parent', { | ||
configurable: true, | ||
writable: true, | ||
enumerable: false, | ||
value: parent || null | ||
}); | ||
return obj; | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Parses inline style. | ||
* | ||
* Example: 'color:red' => { color: 'red' } | ||
* | ||
* @param {String} style - The inline style. | ||
* @param {Function} [iterator] - The iterator function. | ||
* @return {null|Object} | ||
*/ | ||
var styleToObject = function parseInlineStyle(style, iterator) { | ||
if (!style || typeof style !== 'string') return null; | ||
/** | ||
* Parses inline style. | ||
* | ||
* Example: 'color:red' => { color: 'red' } | ||
* | ||
* @param {String} style - The inline style. | ||
* @param {Function} [iterator] - The iterator function. | ||
* @return {null|Object} | ||
*/ | ||
var styleToObject = function parseInlineStyle(style, iterator) { | ||
if (!style || typeof style !== 'string') return null; | ||
// make sure to wrap declarations in placeholder | ||
var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations; | ||
var declaration, property, value; | ||
// make sure to wrap declarations in placeholder | ||
var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations; | ||
var declaration, property, value; | ||
var output = null; | ||
var hasIterator = typeof iterator === 'function'; | ||
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; | ||
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; | ||
if (hasIterator) { | ||
iterator(property, value, declaration); | ||
} else if (value) { | ||
output || (output = {}); | ||
output[property] = value; | ||
} | ||
} | ||
} | ||
return output; | ||
}; | ||
return output; | ||
}; | ||
return styleToObject; | ||
return styleToObject; | ||
}))); |
@@ -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 u=r[s];Array.isArray(u)?u.forEach(function(r){e(r,i)}):u&&"object"==typeof u&&e(u,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,u,a,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 u(){var r={line:A,column:b};return function(e){return e.position=new a(r),l(),e}}function a(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=u(),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=u(),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=u(),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=u(),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=u(),t=m(/^@([-\w]+)?document *([^{]+)/);if(t){var i=r(t[1]),s=r(t[2]);if(!c())return o("@document missing '{'");var a=v().concat(n());return f()?e({type:"document",document:s,vendor:i,rules:a}):o("@document missing '}'")}}()||function(){var r=u();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=u();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=u();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=u(),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=u();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=u(),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=u();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=u(),t=m(e);if(t){var i={type:r};return i[r]=t[1].trim(),n(i)}}}i=i||{};var A=1,b=1;a.prototype.content=t;var k=[],x=w("import"),E=w("charset"),j=w("namespace");return e(function(){var r=p();return{type:"stylesheet",stylesheet:{source:i.source,rules:r,parsingErrors:k}}}())}("p{"+t+"}").stylesheet.rules[0].declarations,c=null,f="function"==typeof i,p=0,m=o.length;p<m;p++)u=(s=o[p]).property,a=s.value,f?i(u,a,s):a&&(c||(c={}),c[u]=a);return c}}); | ||
!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";var z=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,f=function(t,n){n=n||{};var i=1,s=1;function a(r){var e=r.match(/\n/g);e&&(i+=e.length);var n=r.lastIndexOf("\n");s=~n?r.length-n:s+r.length}function u(){var e={line:i,column:s};return function(r){return r.position=new o(e),g(),r}}function o(r){this.start=r,this.end={line:i,column:s},this.source=n.source}o.prototype.content=t;var c=[];function f(r){var e=new Error(n.source+":"+i+":"+s+": "+r);if(e.reason=r,e.filename=n.source,e.line=i,e.column=s,e.source=t,!n.silent)throw e;c.push(e)}function p(){return v(/^{\s*/)}function m(){return v(/^}/)}function l(){var r,e=[];for(g(),y(e);t.length&&"}"!=t.charAt(0)&&(r=j()||O());)!1!==r&&(e.push(r),y(e));return e}function v(r){var e=r.exec(t);if(e){var n=e[0];return a(n),t=t.slice(n.length),e}}function g(){v(/^\s*/)}function y(r){var e;for(r=r||[];e=d();)!1!==e&&r.push(e);return r}function d(){var r=u();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 f("End of comment missing");var n=t.slice(2,e-2);return s+=2,a(n),t=t.slice(e),s+=2,r({type:"comment",comment:n})}}function h(){var r=v(/^([^{]+)/);if(r)return C(r[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 w(){var r=u(),e=v(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/);if(e){if(e=C(e[0]),!v(/^:\s*/))return f("property missing ':'");var n=v(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/),t=r({type:"declaration",property:e.replace(z,""),value:n?C(n[0]).replace(z,""):""});return v(/^[;\s]*/),t}}function A(){var r,e=[];if(!p())return f("missing '{'");for(y(e);r=w();)!1!==r&&(e.push(r),y(e));return m()?e:f("missing '}'")}function b(){for(var r,e=[],n=u();r=v(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/);)e.push(r[1]),v(/^,\s*/);if(e.length)return n({type:"keyframe",values:e,declarations:A()})}var r,e=E("import"),k=E("charset"),x=E("namespace");function E(t){var i=new RegExp("^@"+t+"\\s*([^;]+);");return function(){var r=u(),e=v(i);if(e){var n={type:t};return n[t]=e[1].trim(),r(n)}}}function j(){if("@"==t[0])return function(){var r=u();if(e=v(/^@([-\w]+)?keyframes\s*/)){var e,n=e[1];if(!(e=v(/^([-\w]+)\s*/)))return f("@keyframes missing name");var t,i=e[1];if(!p())return f("@keyframes missing '{'");for(var s=y();t=b();)s.push(t),s=s.concat(y());return m()?r({type:"keyframes",name:i,vendor:n,keyframes:s}):f("@keyframes missing '}'")}}()||function(){var r=u(),e=v(/^@media *([^{]+)/);if(e){var n=C(e[1]);if(!p())return f("@media missing '{'");var t=y().concat(l());return m()?r({type:"media",media:n,rules:t}):f("@media missing '}'")}}()||function(){var r=u(),e=v(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);if(e)return r({type:"custom-media",name:C(e[1]),media:C(e[2])})}()||function(){var r=u(),e=v(/^@supports *([^{]+)/);if(e){var n=C(e[1]);if(!p())return f("@supports missing '{'");var t=y().concat(l());return m()?r({type:"supports",supports:n,rules:t}):f("@supports missing '}'")}}()||e()||k()||x()||function(){var r=u(),e=v(/^@([-\w]+)?document *([^{]+)/);if(e){var n=C(e[1]),t=C(e[2]);if(!p())return f("@document missing '{'");var i=y().concat(l());return m()?r({type:"document",document:t,vendor:n,rules:i}):f("@document missing '}'")}}()||function(){var r=u();if(v(/^@page */)){var e=h()||[];if(!p())return f("@page missing '{'");for(var n,t=y();n=w();)t.push(n),t=t.concat(y());return m()?r({type:"page",selectors:e,declarations:t}):f("@page missing '}'")}}()||function(){var r=u();if(v(/^@host\s*/)){if(!p())return f("@host missing '{'");var e=y().concat(l());return m()?r({type:"host",rules:e}):f("@host missing '}'")}}()||function(){var r=u();if(v(/^@font-face\s*/)){if(!p())return f("@font-face missing '{'");for(var e,n=y();e=w();)n.push(e),n=n.concat(y());return m()?r({type:"font-face",declarations:n}):f("@font-face missing '}'")}}()}function O(){var r=u(),e=h();return e?(y(),r({type:"rule",selectors:e,declarations:A()})):f("selector missing")}return function e(r,n){var t=r&&"string"==typeof r.type;var 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)}t&&Object.defineProperty(r,"parent",{configurable:!0,writable:!0,enumerable:!1,value:n||null});return r}((r=l(),{type:"stylesheet",stylesheet:{source:n.source,rules:r,parsingErrors:c}}))};function C(r){return r?r.replace(/^\s+|\s+$/g,""):""}return function(r,e){if(!r||"string"!=typeof r)return null;for(var n,t,i,s=f("p{"+r+"}").stylesheet.rules[0].declarations,a=null,u="function"==typeof e,o=0,c=s.length;o<c;o++)t=(n=s[o]).property,i=n.value,u?e(t,i,n):i&&(a||(a={}),a[t]=i);return a}}); |
{ | ||
"name": "style-to-object", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Converts inline style to object.", | ||
@@ -34,17 +34,17 @@ "author": "Mark <mark@remarkablemark.org>", | ||
"dependencies": { | ||
"css": "2.2.3" | ||
"css": "2.2.4" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "3.0.0", | ||
"eslint": "4.11.0", | ||
"coveralls": "3.0.2", | ||
"eslint": "5.5.0", | ||
"istanbul": "0.4.5", | ||
"mocha": "4.0.1", | ||
"rollup": "0.51.5", | ||
"rollup-plugin-commonjs": "8.2.6", | ||
"rollup-plugin-node-resolve": "3.0.0", | ||
"rollup-plugin-uglify": "2.0.1", | ||
"standard-version": "4.2.0", | ||
"uglify-es": "3.1.9" | ||
"mocha": "5.2.0", | ||
"rollup": "0.65.2", | ||
"rollup-plugin-commonjs": "9.1.6", | ||
"rollup-plugin-node-resolve": "3.4.0", | ||
"rollup-plugin-uglify": "5.0.2", | ||
"standard-version": "4.4.0", | ||
"uglify-es": "3.3.9" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -18,3 +18,3 @@ # style-to-object | ||
[JSFiddle](https://jsfiddle.net/remarkablemark/ykz2meot/) | ||
[JSFiddle](https://jsfiddle.net/remarkablemark/ykz2meot/) | [repl.it](https://repl.it/@remarkablemark/style-to-object) | ||
@@ -147,2 +147,3 @@ ## Installation | ||
- [css](https://github.com/reworkcss/css) | ||
- [Contributors](https://github.com/remarkablemark/style-to-object/graphs/contributors) | ||
@@ -149,0 +150,0 @@ ## License |
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
26077
151
+ Addedcss@2.2.4(transitive)
+ Addedsource-map@0.6.1(transitive)
- Removedamdefine@1.0.1(transitive)
- Removedcss@2.2.3(transitive)
- Removedsource-map@0.1.43(transitive)
Updatedcss@2.2.4