postcss-value-parser
Advanced tools
Comparing version 3.3.0 to 3.3.1
@@ -1,23 +0,23 @@ | ||
var parse = require('./parse'); | ||
var walk = require('./walk'); | ||
var stringify = require('./stringify'); | ||
var parse = require("./parse"); | ||
var walk = require("./walk"); | ||
var stringify = require("./stringify"); | ||
function ValueParser(value) { | ||
if (this instanceof ValueParser) { | ||
this.nodes = parse(value); | ||
return this; | ||
} | ||
return new ValueParser(value); | ||
if (this instanceof ValueParser) { | ||
this.nodes = parse(value); | ||
return this; | ||
} | ||
return new ValueParser(value); | ||
} | ||
ValueParser.prototype.toString = function () { | ||
return Array.isArray(this.nodes) ? stringify(this.nodes) : ''; | ||
ValueParser.prototype.toString = function() { | ||
return Array.isArray(this.nodes) ? stringify(this.nodes) : ""; | ||
}; | ||
ValueParser.prototype.walk = function (cb, bubble) { | ||
walk(this.nodes, cb, bubble); | ||
return this; | ||
ValueParser.prototype.walk = function(cb, bubble) { | ||
walk(this.nodes, cb, bubble); | ||
return this; | ||
}; | ||
ValueParser.unit = require('./unit'); | ||
ValueParser.unit = require("./unit"); | ||
@@ -24,0 +24,0 @@ ValueParser.walk = walk; |
437
lib/parse.js
@@ -1,242 +0,251 @@ | ||
var openParentheses = '('.charCodeAt(0); | ||
var closeParentheses = ')'.charCodeAt(0); | ||
var singleQuote = '\''.charCodeAt(0); | ||
var openParentheses = "(".charCodeAt(0); | ||
var closeParentheses = ")".charCodeAt(0); | ||
var singleQuote = "'".charCodeAt(0); | ||
var doubleQuote = '"'.charCodeAt(0); | ||
var backslash = '\\'.charCodeAt(0); | ||
var slash = '/'.charCodeAt(0); | ||
var comma = ','.charCodeAt(0); | ||
var colon = ':'.charCodeAt(0); | ||
var star = '*'.charCodeAt(0); | ||
var backslash = "\\".charCodeAt(0); | ||
var slash = "/".charCodeAt(0); | ||
var comma = ",".charCodeAt(0); | ||
var colon = ":".charCodeAt(0); | ||
var star = "*".charCodeAt(0); | ||
module.exports = function (input) { | ||
var tokens = []; | ||
var value = input; | ||
module.exports = function(input) { | ||
var tokens = []; | ||
var value = input; | ||
var next, quote, prev, token, escape, escapePos, whitespacePos; | ||
var pos = 0; | ||
var code = value.charCodeAt(pos); | ||
var max = value.length; | ||
var stack = [{ nodes: tokens }]; | ||
var balanced = 0; | ||
var parent; | ||
var next, quote, prev, token, escape, escapePos, whitespacePos; | ||
var pos = 0; | ||
var code = value.charCodeAt(pos); | ||
var max = value.length; | ||
var stack = [{ nodes: tokens }]; | ||
var balanced = 0; | ||
var parent; | ||
var name = ''; | ||
var before = ''; | ||
var after = ''; | ||
var name = ""; | ||
var before = ""; | ||
var after = ""; | ||
while (pos < max) { | ||
// Whitespaces | ||
if (code <= 32) { | ||
next = pos; | ||
do { | ||
next += 1; | ||
code = value.charCodeAt(next); | ||
} while (code <= 32); | ||
token = value.slice(pos, next); | ||
while (pos < max) { | ||
// Whitespaces | ||
if (code <= 32) { | ||
next = pos; | ||
do { | ||
next += 1; | ||
code = value.charCodeAt(next); | ||
} while (code <= 32); | ||
token = value.slice(pos, next); | ||
prev = tokens[tokens.length - 1]; | ||
if (code === closeParentheses && balanced) { | ||
after = token; | ||
} else if (prev && prev.type === 'div') { | ||
prev.after = token; | ||
} else if (code === comma || code === colon || code === slash && value.charCodeAt(next + 1) !== star) { | ||
before = token; | ||
} else { | ||
tokens.push({ | ||
type: 'space', | ||
sourceIndex: pos, | ||
value: token | ||
}); | ||
} | ||
prev = tokens[tokens.length - 1]; | ||
if (code === closeParentheses && balanced) { | ||
after = token; | ||
} else if (prev && prev.type === "div") { | ||
prev.after = token; | ||
} else if ( | ||
code === comma || | ||
code === colon || | ||
(code === slash && value.charCodeAt(next + 1) !== star) | ||
) { | ||
before = token; | ||
} else { | ||
tokens.push({ | ||
type: "space", | ||
sourceIndex: pos, | ||
value: token | ||
}); | ||
} | ||
pos = next; | ||
pos = next; | ||
// Quotes | ||
} else if (code === singleQuote || code === doubleQuote) { | ||
next = pos; | ||
quote = code === singleQuote ? '\'' : '"'; | ||
token = { | ||
type: 'string', | ||
sourceIndex: pos, | ||
quote: quote | ||
}; | ||
do { | ||
escape = false; | ||
next = value.indexOf(quote, next + 1); | ||
if (~next) { | ||
escapePos = next; | ||
while (value.charCodeAt(escapePos - 1) === backslash) { | ||
escapePos -= 1; | ||
escape = !escape; | ||
} | ||
} else { | ||
value += quote; | ||
next = value.length - 1; | ||
token.unclosed = true; | ||
} | ||
} while (escape); | ||
token.value = value.slice(pos + 1, next); | ||
// Quotes | ||
} else if (code === singleQuote || code === doubleQuote) { | ||
next = pos; | ||
quote = code === singleQuote ? "'" : '"'; | ||
token = { | ||
type: "string", | ||
sourceIndex: pos, | ||
quote: quote | ||
}; | ||
do { | ||
escape = false; | ||
next = value.indexOf(quote, next + 1); | ||
if (~next) { | ||
escapePos = next; | ||
while (value.charCodeAt(escapePos - 1) === backslash) { | ||
escapePos -= 1; | ||
escape = !escape; | ||
} | ||
} else { | ||
value += quote; | ||
next = value.length - 1; | ||
token.unclosed = true; | ||
} | ||
} while (escape); | ||
token.value = value.slice(pos + 1, next); | ||
tokens.push(token); | ||
pos = next + 1; | ||
code = value.charCodeAt(pos); | ||
tokens.push(token); | ||
pos = next + 1; | ||
code = value.charCodeAt(pos); | ||
// Comments | ||
} else if (code === slash && value.charCodeAt(pos + 1) === star) { | ||
token = { | ||
type: 'comment', | ||
sourceIndex: pos | ||
}; | ||
// Comments | ||
} else if (code === slash && value.charCodeAt(pos + 1) === star) { | ||
token = { | ||
type: "comment", | ||
sourceIndex: pos | ||
}; | ||
next = value.indexOf('*/', pos); | ||
if (next === -1) { | ||
token.unclosed = true; | ||
next = value.length; | ||
} | ||
next = value.indexOf("*/", pos); | ||
if (next === -1) { | ||
token.unclosed = true; | ||
next = value.length; | ||
} | ||
token.value = value.slice(pos + 2, next); | ||
tokens.push(token); | ||
token.value = value.slice(pos + 2, next); | ||
tokens.push(token); | ||
pos = next + 2; | ||
code = value.charCodeAt(pos); | ||
pos = next + 2; | ||
code = value.charCodeAt(pos); | ||
// Dividers | ||
} else if (code === slash || code === comma || code === colon) { | ||
token = value[pos]; | ||
// Dividers | ||
} else if (code === slash || code === comma || code === colon) { | ||
token = value[pos]; | ||
tokens.push({ | ||
type: 'div', | ||
sourceIndex: pos - before.length, | ||
value: token, | ||
before: before, | ||
after: '' | ||
}); | ||
before = ''; | ||
tokens.push({ | ||
type: "div", | ||
sourceIndex: pos - before.length, | ||
value: token, | ||
before: before, | ||
after: "" | ||
}); | ||
before = ""; | ||
pos += 1; | ||
code = value.charCodeAt(pos); | ||
pos += 1; | ||
code = value.charCodeAt(pos); | ||
// Open parentheses | ||
} else if (openParentheses === code) { | ||
// Whitespaces after open parentheses | ||
next = pos; | ||
do { | ||
next += 1; | ||
code = value.charCodeAt(next); | ||
} while (code <= 32); | ||
token = { | ||
type: 'function', | ||
sourceIndex: pos - name.length, | ||
value: name, | ||
before: value.slice(pos + 1, next) | ||
}; | ||
pos = next; | ||
// Open parentheses | ||
} else if (openParentheses === code) { | ||
// Whitespaces after open parentheses | ||
next = pos; | ||
do { | ||
next += 1; | ||
code = value.charCodeAt(next); | ||
} while (code <= 32); | ||
token = { | ||
type: "function", | ||
sourceIndex: pos - name.length, | ||
value: name, | ||
before: value.slice(pos + 1, next) | ||
}; | ||
pos = next; | ||
if (name === 'url' && code !== singleQuote && code !== doubleQuote) { | ||
next -= 1; | ||
do { | ||
escape = false; | ||
next = value.indexOf(')', next + 1); | ||
if (~next) { | ||
escapePos = next; | ||
while (value.charCodeAt(escapePos - 1) === backslash) { | ||
escapePos -= 1; | ||
escape = !escape; | ||
} | ||
} else { | ||
value += ')'; | ||
next = value.length - 1; | ||
token.unclosed = true; | ||
} | ||
} while (escape); | ||
// Whitespaces before closed | ||
whitespacePos = next; | ||
do { | ||
whitespacePos -= 1; | ||
code = value.charCodeAt(whitespacePos); | ||
} while (code <= 32); | ||
if (pos !== whitespacePos + 1) { | ||
token.nodes = [{ | ||
type: 'word', | ||
sourceIndex: pos, | ||
value: value.slice(pos, whitespacePos + 1) | ||
}]; | ||
} else { | ||
token.nodes = []; | ||
} | ||
if (token.unclosed && whitespacePos + 1 !== next) { | ||
token.after = ''; | ||
token.nodes.push({ | ||
type: 'space', | ||
sourceIndex: whitespacePos + 1, | ||
value: value.slice(whitespacePos + 1, next) | ||
}); | ||
} else { | ||
token.after = value.slice(whitespacePos + 1, next); | ||
} | ||
pos = next + 1; | ||
code = value.charCodeAt(pos); | ||
tokens.push(token); | ||
} else { | ||
balanced += 1; | ||
token.after = ''; | ||
tokens.push(token); | ||
stack.push(token); | ||
tokens = token.nodes = []; | ||
parent = token; | ||
if (name === "url" && code !== singleQuote && code !== doubleQuote) { | ||
next -= 1; | ||
do { | ||
escape = false; | ||
next = value.indexOf(")", next + 1); | ||
if (~next) { | ||
escapePos = next; | ||
while (value.charCodeAt(escapePos - 1) === backslash) { | ||
escapePos -= 1; | ||
escape = !escape; | ||
} | ||
name = ''; | ||
} else { | ||
value += ")"; | ||
next = value.length - 1; | ||
token.unclosed = true; | ||
} | ||
} while (escape); | ||
// Whitespaces before closed | ||
whitespacePos = next; | ||
do { | ||
whitespacePos -= 1; | ||
code = value.charCodeAt(whitespacePos); | ||
} while (code <= 32); | ||
if (pos !== whitespacePos + 1) { | ||
token.nodes = [ | ||
{ | ||
type: "word", | ||
sourceIndex: pos, | ||
value: value.slice(pos, whitespacePos + 1) | ||
} | ||
]; | ||
} else { | ||
token.nodes = []; | ||
} | ||
if (token.unclosed && whitespacePos + 1 !== next) { | ||
token.after = ""; | ||
token.nodes.push({ | ||
type: "space", | ||
sourceIndex: whitespacePos + 1, | ||
value: value.slice(whitespacePos + 1, next) | ||
}); | ||
} else { | ||
token.after = value.slice(whitespacePos + 1, next); | ||
} | ||
pos = next + 1; | ||
code = value.charCodeAt(pos); | ||
tokens.push(token); | ||
} else { | ||
balanced += 1; | ||
token.after = ""; | ||
tokens.push(token); | ||
stack.push(token); | ||
tokens = token.nodes = []; | ||
parent = token; | ||
} | ||
name = ""; | ||
// Close parentheses | ||
} else if (closeParentheses === code && balanced) { | ||
pos += 1; | ||
code = value.charCodeAt(pos); | ||
// Close parentheses | ||
} else if (closeParentheses === code && balanced) { | ||
pos += 1; | ||
code = value.charCodeAt(pos); | ||
parent.after = after; | ||
after = ''; | ||
balanced -= 1; | ||
stack.pop(); | ||
parent = stack[balanced]; | ||
tokens = parent.nodes; | ||
parent.after = after; | ||
after = ""; | ||
balanced -= 1; | ||
stack.pop(); | ||
parent = stack[balanced]; | ||
tokens = parent.nodes; | ||
// Words | ||
} else { | ||
next = pos; | ||
do { | ||
if (code === backslash) { | ||
next += 1; | ||
} | ||
next += 1; | ||
code = value.charCodeAt(next); | ||
} while (next < max && !( | ||
code <= 32 || | ||
code === singleQuote || | ||
code === doubleQuote || | ||
code === comma || | ||
code === colon || | ||
code === slash || | ||
code === openParentheses || | ||
code === closeParentheses && balanced | ||
)); | ||
token = value.slice(pos, next); | ||
// Words | ||
} else { | ||
next = pos; | ||
do { | ||
if (code === backslash) { | ||
next += 1; | ||
} | ||
next += 1; | ||
code = value.charCodeAt(next); | ||
} while ( | ||
next < max && | ||
!( | ||
code <= 32 || | ||
code === singleQuote || | ||
code === doubleQuote || | ||
code === comma || | ||
code === colon || | ||
code === slash || | ||
code === openParentheses || | ||
(code === closeParentheses && balanced) | ||
) | ||
); | ||
token = value.slice(pos, next); | ||
if (openParentheses === code) { | ||
name = token; | ||
} else { | ||
tokens.push({ | ||
type: 'word', | ||
sourceIndex: pos, | ||
value: token | ||
}); | ||
} | ||
if (openParentheses === code) { | ||
name = token; | ||
} else { | ||
tokens.push({ | ||
type: "word", | ||
sourceIndex: pos, | ||
value: token | ||
}); | ||
} | ||
pos = next; | ||
} | ||
pos = next; | ||
} | ||
} | ||
for (pos = stack.length - 1; pos; pos -= 1) { | ||
stack[pos].unclosed = true; | ||
} | ||
for (pos = stack.length - 1; pos; pos -= 1) { | ||
stack[pos].unclosed = true; | ||
} | ||
return stack[0].nodes; | ||
return stack[0].nodes; | ||
}; |
function stringifyNode(node, custom) { | ||
var type = node.type; | ||
var value = node.value; | ||
var buf; | ||
var customResult; | ||
var type = node.type; | ||
var value = node.value; | ||
var buf; | ||
var customResult; | ||
if (custom && (customResult = custom(node)) !== undefined) { | ||
return customResult; | ||
} else if (type === 'word' || type === 'space') { | ||
return value; | ||
} else if (type === 'string') { | ||
buf = node.quote || ''; | ||
return buf + value + (node.unclosed ? '' : buf); | ||
} else if (type === 'comment') { | ||
return '/*' + value + (node.unclosed ? '' : '*/'); | ||
} else if (type === 'div') { | ||
return (node.before || '') + value + (node.after || ''); | ||
} else if (Array.isArray(node.nodes)) { | ||
buf = stringify(node.nodes); | ||
if (type !== 'function') { | ||
return buf; | ||
} | ||
return value + '(' + (node.before || '') + buf + (node.after || '') + (node.unclosed ? '' : ')'); | ||
if (custom && (customResult = custom(node)) !== undefined) { | ||
return customResult; | ||
} else if (type === "word" || type === "space") { | ||
return value; | ||
} else if (type === "string") { | ||
buf = node.quote || ""; | ||
return buf + value + (node.unclosed ? "" : buf); | ||
} else if (type === "comment") { | ||
return "/*" + value + (node.unclosed ? "" : "*/"); | ||
} else if (type === "div") { | ||
return (node.before || "") + value + (node.after || ""); | ||
} else if (Array.isArray(node.nodes)) { | ||
buf = stringify(node.nodes); | ||
if (type !== "function") { | ||
return buf; | ||
} | ||
return value; | ||
return ( | ||
value + | ||
"(" + | ||
(node.before || "") + | ||
buf + | ||
(node.after || "") + | ||
(node.unclosed ? "" : ")") | ||
); | ||
} | ||
return value; | ||
} | ||
function stringify(nodes, custom) { | ||
var result, i; | ||
var result, i; | ||
if (Array.isArray(nodes)) { | ||
result = ''; | ||
for (i = nodes.length - 1; ~i; i -= 1) { | ||
result = stringifyNode(nodes[i], custom) + result; | ||
} | ||
return result; | ||
if (Array.isArray(nodes)) { | ||
result = ""; | ||
for (i = nodes.length - 1; ~i; i -= 1) { | ||
result = stringifyNode(nodes[i], custom) + result; | ||
} | ||
return stringifyNode(nodes, custom); | ||
return result; | ||
} | ||
return stringifyNode(nodes, custom); | ||
} | ||
module.exports = stringify; |
@@ -1,41 +0,49 @@ | ||
var minus = '-'.charCodeAt(0); | ||
var plus = '+'.charCodeAt(0); | ||
var dot = '.'.charCodeAt(0); | ||
var minus = "-".charCodeAt(0); | ||
var plus = "+".charCodeAt(0); | ||
var dot = ".".charCodeAt(0); | ||
var exp = "e".charCodeAt(0); | ||
var EXP = "E".charCodeAt(0); | ||
module.exports = function (value) { | ||
var pos = 0; | ||
var length = value.length; | ||
var dotted = false; | ||
var containsNumber = false; | ||
var code; | ||
var number = ''; | ||
module.exports = function(value) { | ||
var pos = 0; | ||
var length = value.length; | ||
var dotted = false; | ||
var sciPos = -1; | ||
var containsNumber = false; | ||
var code; | ||
while (pos < length) { | ||
code = value.charCodeAt(pos); | ||
while (pos < length) { | ||
code = value.charCodeAt(pos); | ||
if (code >= 48 && code <= 57) { | ||
number += value[pos]; | ||
containsNumber = true; | ||
} else if (code === dot) { | ||
if (dotted) { | ||
break; | ||
} | ||
dotted = true; | ||
number += value[pos]; | ||
} else if (code === plus || code === minus) { | ||
if (pos !== 0) { | ||
break; | ||
} | ||
number += value[pos]; | ||
} else { | ||
break; | ||
} | ||
pos += 1; | ||
if (code >= 48 && code <= 57) { | ||
containsNumber = true; | ||
} else if (code === exp || code === EXP) { | ||
if (sciPos > -1) { | ||
break; | ||
} | ||
sciPos = pos; | ||
} else if (code === dot) { | ||
if (dotted) { | ||
break; | ||
} | ||
dotted = true; | ||
} else if (code === plus || code === minus) { | ||
if (pos !== 0) { | ||
break; | ||
} | ||
} else { | ||
break; | ||
} | ||
return containsNumber ? { | ||
number: number, | ||
pos += 1; | ||
} | ||
if (sciPos + 1 === pos) pos--; | ||
return containsNumber | ||
? { | ||
number: value.slice(0, pos), | ||
unit: value.slice(pos) | ||
} : false; | ||
} | ||
: false; | ||
}; |
module.exports = function walk(nodes, cb, bubble) { | ||
var i, max, node, result; | ||
var i, max, node, result; | ||
for (i = 0, max = nodes.length; i < max; i += 1) { | ||
node = nodes[i]; | ||
if (!bubble) { | ||
result = cb(node, i, nodes); | ||
} | ||
for (i = 0, max = nodes.length; i < max; i += 1) { | ||
node = nodes[i]; | ||
if (!bubble) { | ||
result = cb(node, i, nodes); | ||
} | ||
if (result !== false && node.type === 'function' && Array.isArray(node.nodes)) { | ||
walk(node.nodes, cb, bubble); | ||
} | ||
if ( | ||
result !== false && | ||
node.type === "function" && | ||
Array.isArray(node.nodes) | ||
) { | ||
walk(node.nodes, cb, bubble); | ||
} | ||
if (bubble) { | ||
cb(node, i, nodes); | ||
} | ||
if (bubble) { | ||
cb(node, i, nodes); | ||
} | ||
} | ||
}; |
{ | ||
"name": "postcss-value-parser", | ||
"version": "3.3.0", | ||
"version": "3.3.1", | ||
"description": "Transforms css values and at-rule params into the tree", | ||
@@ -10,19 +10,33 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"eslint": "^2.1.0", | ||
"eslint-config-postcss": "^2.0.0", | ||
"tap-spec": "^4.1.0", | ||
"eslint": "^5.6.1", | ||
"husky": "^1.0.0", | ||
"lint-staged": "^7.3.0", | ||
"prettier": "^1.4.4", | ||
"tap-spec": "^5.0.0", | ||
"tape": "^4.2.0" | ||
}, | ||
"scripts": { | ||
"test": "tape test/*.js | tap-spec", | ||
"posttest": "eslint ." | ||
"lint:prettier": "prettier '**/*.js' --list-different", | ||
"lint:js": "eslint . --cache", | ||
"lint": "yarn lint:js && yarn lint:prettier", | ||
"pretest": "yarn lint", | ||
"test": "tape test/*.js | tap-spec" | ||
}, | ||
"eslintConfig": { | ||
"extends": "postcss/es5", | ||
"rules": { | ||
"max-len": 0, | ||
"no-bitwise": 0, | ||
"complexity": 0, | ||
"no-use-before-define": 0, | ||
"consistent-return": 0 | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended" | ||
}, | ||
"lint-staged": { | ||
"*.js": [ | ||
"eslint", | ||
"prettier --write", | ||
"git add" | ||
] | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
@@ -29,0 +43,0 @@ }, |
@@ -0,0 +0,0 @@ # postcss-value-parser |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
354
19086
6
1