Socket
Socket
Sign inDemoInstall

postcss-advanced-variables

Package Overview
Dependencies
9
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.2 to 2.0.0

index.bundle.js

60

CHANGELOG.md

@@ -1,40 +0,54 @@

## 1.2.2 (2015-10-21)
# Changes to PostCSS Advanced Variables
Removed: Old gulp file
### 2.0.0 (December 31, 2017)
## 1.2.1 (2015-10-21)
- Completely rewritten
- Added: `unresolved` option to throw errors or warnings on unresolved variables
- Added: Support for the `#{$var}` syntax
- Added: Support for iterators in `@each` at-rules
- Added: Support for boolean `@if` at-rules
(`@each $item $index in $array`)
- Added: Support for variable replacement in all at-rules
- Added: Support for neighboring variables `$a$b`
- Fixed: Number comparison in `@if` at-rules
Updated: PostCSS 5.0.10
Updated: Tests
## 1.2.2 (October 21, 2015)
## 1.2.0 (2015-10-21)
- Removed: Old gulp file
Added: Global variables set in options
## 1.2.1 (October 21, 2015)
## 1.1.0 (2015-09-08)
- Updated: PostCSS 5.0.10
- Updated: Tests
Added: Support for `!default`
## 1.2.0 (October 21, 2015)
## 1.0.0 (2015-09-07)
- Added: Global variables set in options
Updated: PostCSS 5.0.4
Updated: Chai 3.2.0
Updated: ESLint 1.0
Updated: Mocha 2.1.3
## 1.1.0 (September 8, 2015)
## 0.0.4 (2015-07-22)
- Added: Support for `!default`
Added: Support for vars in @media
## 1.0.0 (September 7, 2015)
## 0.0.3 (2015-07-08)
- Updated: PostCSS 5.0.4
- Updated: Chai 3.2.0
- Updated: ESLint 1.0
- Updated: Mocha 2.1.3
Added: Support for @else statements
## 0.0.4 (July 22, 2015)
## 0.0.2 (2015-07-07)
- Added: Support for vars in @media
Fixed: Some variable evaluations
Added: Support for deep arrays
## 0.0.3 (July 8, 2015)
## 0.0.1 (2015-07-07)
- Added: Support for @else statements
Pre-release
## 0.0.2 (July 7, 2015)
- Fixed: Some variable evaluations
- Added: Support for deep arrays
## 0.0.1 (July 7, 2015)
- Pre-release

@@ -1,248 +0,8 @@

var postcss = require('postcss');
// tooling
import transformNode from './lib/transform-node';
import postcss from 'postcss';
module.exports = postcss.plugin('postcss-advanced-variables', function (opts) {
opts = opts || {};
// Matchers
// --------
var isVariableDeclaration = /^\$[\w-]+$/;
var variablesInString = /(^|[^\\])\$(?:\(([A-z][\w-]*)\)|([A-z][\w-]*))/g;
var wrappingParen = /^\((.*)\)$/g;
var isDefaultValue = /\s+!default$/;
// Helpers
// -------
// '(hello), (goodbye)' => [[hello], [goodbye]]
function getArrayedString(string, first) {
var array = postcss.list.comma(String(string)).map(function (substring) {
return wrappingParen.test(substring) ? getArrayedString(substring.replace(wrappingParen, '$1')) : substring;
});
return first && array.length === 1 ? array[0] : array;
}
// $NAME => VALUE
function getVariable(node, name) {
var value = node.variables && name in node.variables ? node.variables[name] : node.parent && getVariable(node.parent, name);
return value;
}
// node.variables[NAME] => 'VALUE'
function setVariable(node, name, value) {
node.variables = node.variables || {};
if (isDefaultValue.test(value)) {
if (getVariable(node, name)) return;
else value = value.replace(isDefaultValue, '');
}
node.variables[name] = getArrayedString(value, true);
}
// 'Hello $NAME' => 'Hello VALUE'
function getVariableTransformedString(node, string) {
return string.replace(variablesInString, function (match, before, name1, name2) {
var value = getVariable(node, name1 || name2);
return value === undefined ? match : before + value;
});
}
// Loopers
// -------
// run over every node
function each(parent) {
var index = -1;
var node;
while (node = parent.nodes[++index]) {
if (node.type === 'decl') index = eachDecl(node, parent, index);
else if (node.type === 'rule') index = eachRule(node, parent, index);
else if (node.type === 'atrule') index = eachAtRule(node, parent, index);
if (node.nodes) each(node);
}
}
// PROPERTY: VALUE
function eachDecl(node, parent, index) {
// $NAME: VALUE
if (isVariableDeclaration.test(node.prop)) {
node.value = getVariableTransformedString(parent, node.value);
setVariable(parent, node.prop.slice(1), node.value);
node.remove();
--index;
} else {
node.prop = getVariableTransformedString(parent, node.prop);
node.value = getVariableTransformedString(parent, node.value);
}
// return index
return index;
}
// SELECTOR {RULE}
function eachRule(node, parent, index) {
node.selector = getVariableTransformedString(parent, node.selector);
return index;
}
// @NAME PARAMS
function eachAtRule(node, parent, index) {
if (node.name === 'for') index = eachAtForRule(node, parent, index);
else if (node.name === 'each') index = eachAtEachRule(node, parent, index);
else if (node.name === 'if') index = eachAtIfRule(node, parent, index);
else if (node.name === 'media') node.params = getVariableTransformedString(parent, node.params);
return index;
}
// @for NAME from START to END by INCREMENT
function eachAtForRule(node, parent, index) {
// set params
var params = postcss.list.space(node.params);
var name = params[0].trim().slice(1);
var start = +getVariableTransformedString(node, params[2]);
var end = +getVariableTransformedString(node, params[4]);
var increment = 6 in params && +getVariableTransformedString(node, params[6]) || 1;
var direction = start <= end ? 1 : -1;
// each iteration
while (start * direction <= end * direction) {
// set iterating variable
setVariable(node, name, start);
// clone node
var clone = node.clone({ parent: parent, variables: node.variables });
// process clone children
each(clone);
// increment index
index += clone.nodes.length;
// increment start
start += increment * direction;
// insert clone child nodes
parent.insertBefore(node, clone.nodes);
}
// remove node
node.remove();
// return index
return --index;
}
// @each NAME in ARRAY
function eachAtEachRule(node, parent, index) {
// set params
var params = node.params.split(' in ');
var name = params[0].trim().slice(1);
var array = getArrayedString(getVariableTransformedString(node, params.slice(1).join(' in ')), true);
var start = 0;
var end = array.length;
// each iteration
while (start < end) {
// set iterating variable
setVariable(node, name, array[start]);
// clone node
var clone = node.clone({ parent: parent, variables: node.variables });
// process clone children
each(clone);
// increment index
index += clone.nodes.length;
// increment start
++start;
// insert clone child nodes
parent.insertBefore(node, clone.nodes);
}
// remove node
node.remove();
// return index
return --index;
}
// @if LEFT OPERATOR RIGHT
function eachAtIfRule(node, parent, index) {
// set params
var params = postcss.list.space(node.params);
var left = getVariableTransformedString(node, params[0]);
var operator = params[1];
var right = getVariableTransformedString(node, params[2]);
// set next node
var next = node.next();
// evaluate expression
if (
operator === '==' && left === right ||
operator === '!=' && left !== right ||
operator === '<' && left < right ||
operator === '<=' && left <= right ||
operator === '>' && left > right ||
operator === '>=' && left >= right
) {
// process node children
each(node);
// increment index
index += node.nodes.length;
// insert child nodes
parent.insertBefore(node, node.nodes);
// if next node is an else statement
if (next && next.type === 'atrule' && next.name === 'else') {
next.remove();
}
} else if (next && next.type === 'atrule' && next.name === 'else') {
// process next children
each(next);
// increment index
index += next.nodes.length;
// insert child nodes
parent.insertBefore(node, next.nodes);
// remove next
next.remove();
}
// remove node
node.remove();
// return index
return --index;
}
return function (css) {
// Initialize each global variable.
for (var name in opts.variables || {}) {
setVariable(css, name, opts.variables[name]);
}
// Begin processing each css node.
each(css);
};
// plugin
export default postcss.plugin('postcss-advanced-variables', opts => (root, result) => {
transformNode(root, result, opts);
});

@@ -1,15 +0,108 @@

# CC0 1.0 Universal License
# CC0 1.0 Universal
Public Domain Dedication
## Statement of Purpose
The person(s) who associated a work with this deed has dedicated the work to the public domain by waiving all of his or her rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law.
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator and
subsequent owner(s) (each and all, an “owner”) of an original work of
authorship and/or a database (each, a “Work”).
You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.
Certain owners wish to permanently relinquish those rights to a Work for the
purpose of contributing to a commons of creative, cultural and scientific works
(“Commons”) that the public can reliably and without fear of later claims of
infringement build upon, modify, incorporate in other works, reuse and
redistribute as freely as possible in any form whatsoever and for any purposes,
including without limitation commercial purposes. These owners may contribute
to the Commons to promote the ideal of a free culture and the further
production of creative, cultural and scientific works, or to gain reputation or
greater distribution for their Work in part through the use and efforts of
others.
In no way are the patent or trademark rights of any person affected by CC0, nor are the rights that other persons may have in the work or in how the work is used, such as publicity or privacy rights.
For these and/or other purposes and motivations, and without any expectation of
additional consideration or compensation, the person associating CC0 with a
Work (the “Affirmer”), to the extent that he or she is an owner of Copyright
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and
publicly distribute the Work under its terms, with knowledge of his or her
Copyright and Related Rights in the Work and the meaning and intended legal
effect of CC0 on those rights.
Unless expressly stated otherwise, the person(s) who associated a work with this deed makes no warranties about the work, and disclaims liability for all uses of the work, to the fullest extent permitted by applicable law.
1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights (“Copyright and
Related Rights”). Copyright and Related Rights include, but are not limited
to, the following:
1. the right to reproduce, adapt, distribute, perform, display, communicate,
and translate a Work;
2. moral rights retained by the original author(s) and/or performer(s);
3. publicity and privacy rights pertaining to a person’s image or likeness
depicted in a Work;
4. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(i), below;
5. rights protecting the extraction, dissemination, use and reuse of data in
a Work;
6. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation thereof,
including any amended or successor version of such directive); and
7. other similar, equivalent or corresponding rights throughout the world
based on applicable law or treaty, and any national implementations
thereof.
When using or citing the work, you should not imply endorsement by the author or the affirmer.
2. Waiver. To the greatest extent permitted by, but not in contravention of,
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright
and Related Rights and associated claims and causes of action, whether now
known or unknown (including existing as well as future claims and causes of
action), in the Work (i) in all territories worldwide, (ii) for the maximum
duration provided by applicable law or treaty (including future time
extensions), (iii) in any current or future medium and for any number of
copies, and (iv) for any purpose whatsoever, including without limitation
commercial, advertising or promotional purposes (the “Waiver”). Affirmer
makes the Waiver for the benefit of each member of the public at large and
to the detriment of Affirmer’s heirs and successors, fully intending that
such Waiver shall not be subject to revocation, rescission, cancellation,
termination, or any other legal or equitable action to disrupt the quiet
enjoyment of the Work by the public as contemplated by Affirmer’s express
Statement of Purpose.
This is a [human-readable summary of the Legal Code](https://creativecommons.org/publicdomain/zero/1.0/) ([read the full text](https://creativecommons.org/publicdomain/zero/1.0/legalcode)).
3. Public License Fallback. Should any part of the Waiver for any reason be
judged legally invalid or ineffective under applicable law, then the Waiver
shall be preserved to the maximum extent permitted taking into account
Affirmer’s express Statement of Purpose. In addition, to the extent the
Waiver is so judged Affirmer hereby grants to each affected person a
royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer’s Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future time
extensions), (iii) in any current or future medium and for any number of
copies, and (iv) for any purpose whatsoever, including without limitation
commercial, advertising or promotional purposes (the “License”). The License
shall be deemed effective as of the date CC0 was applied by Affirmer to the
Work. Should any part of the License for any reason be judged legally
invalid or ineffective under applicable law, such partial invalidity or
ineffectiveness shall not invalidate the remainder of the License, and in
such case Affirmer hereby affirms that he or she will not (i) exercise any
of his or her remaining Copyright and Related Rights in the Work or (ii)
assert any associated claims and causes of action with respect to the Work,
in either case contrary to Affirmer’s express Statement of Purpose.
4. Limitations and Disclaimers.
1. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
2. Affirmer offers the Work as-is and makes no representations or warranties
of any kind concerning the Work, express, implied, statutory or
otherwise, including without limitation warranties of title,
merchantability, fitness for a particular purpose, non infringement, or
the absence of latent or other defects, accuracy, or the present or
absence of errors, whether or not discoverable, all to the greatest
extent permissible under applicable law.
3. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person’s Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the Work.
4. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to this
CC0 or use of the Work.
For more information, please see
http://creativecommons.org/publicdomain/zero/1.0/.
{
"name": "postcss-advanced-variables",
"version": "1.2.2",
"description": "PostCSS plugin that enables Sass-like variables, conditionals, and iterators in CSS",
"version": "2.0.0",
"description": "Use Sass-like variables, conditionals, and iterators in CSS",
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
"license": "CC0-1.0",
"repository": "jonathantneal/postcss-advanced-variables",
"homepage": "https://github.com/jonathantneal/postcss-advanced-variables#readme",
"bugs": "https://github.com/jonathantneal/postcss-advanced-variables/issues",
"main": "index.bundle.js",
"module": "index.js",
"files": [
"index.js",
"index.bundle.js",
"lib"
],
"scripts": {
"clean": "git clean -X -d -f",
"prepublishOnly": "npm test",
"pretest": "rollup -c .rollup.js",
"test": "echo 'Running tests...'; npm run test:js && npm run test:tape",
"test:js": "eslint index.js lib/*.js --cache",
"test:tape": "postcss-tape"
},
"engines": {
"node": ">=4.0.0"
},
"dependencies": {
"postcss": "^6.0"
},
"devDependencies": {
"babel-core": "^6.26",
"babel-eslint": "^8.1",
"babel-preset-env": "^1.6",
"echint": "^4.0",
"eslint": "^4.14",
"eslint-config-dev": "2.0",
"postcss-scss": "^1.0.2",
"postcss-tape": "2.2",
"pre-commit": "^1.2",
"rollup": "^0.53",
"rollup-plugin-babel": "^3.0"
},
"eslintConfig": {
"extends": "dev",
"parser": "babel-eslint",
"rules": {
"max-params": [
0
]
}
},
"keywords": [

@@ -18,30 +66,3 @@ "postcss",

"defaults"
],
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
"license": "CC0-1.0",
"repository": {
"type": "git",
"url": "https://github.com/jonathantneal/postcss-advanced-variables.git"
},
"bugs": {
"url": "https://github.com/jonathantneal/postcss-advanced-variables/issues"
},
"homepage": "https://github.com/jonathantneal/postcss-advanced-variables",
"dependencies": {
"postcss": "^5.0.10"
},
"devDependencies": {
"eslint": "^1.7.3",
"tap-spec": "^4.1.0",
"tape": "^4.2.2"
},
"scripts": {
"lint": "eslint . --ignore-path .gitignore",
"fixtures": "tape test/*.js | tap-spec",
"test": "npm run lint && npm run fixtures"
},
"engines": {
"iojs": ">=2.0.0",
"node": ">=0.12.0"
}
]
}

@@ -1,22 +0,24 @@

# Advanced Variables [![Build Status][ci-img]][ci]
# PostCSS Advanced Variables [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
<img align="right" width="135" height="95" src="http://postcss.github.io/postcss/logo-leftp.png" title="Philosopher’s stone, logo of PostCSS">
[![NPM Version][npm-img]][npm-url]
[![Linux Build Status][cli-img]][cli-url]
[![Windows Build Status][win-img]][win-url]
[![Gitter Chat][git-img]][git-url]
[Advanced Variables] converts Sass-like variables and conditionals into CSS.
[PostCSS Advanced Variables] lets you use Sass-like variables, conditionals,
and iterators in CSS.
```scss
/* before */
$dir: assets/icons;
@each $icon in (foo, bar, baz) {
.icon-$icon {
background: url('$dir/$icon.png');
}
.icon-$icon {
background: url('$dir/$icon.png');
}
}
@for $index from 1 to 5 by 2 {
.col-$index {
width: $(index)0%;
}
.col-$index {
width: $(index)0%;
}
}

@@ -27,23 +29,23 @@

.icon-foo {
background: url('assets/icons/foo.png');
background: url('assets/icons/foo.png');
}
.icon-bar {
background: url('assets/icons/bar.png');
background: url('assets/icons/bar.png');
}
.icon-baz {
background: url('assets/icons/baz.png');
background: url('assets/icons/baz.png');
}
.col-1 {
width: 10%;
width: 10%;
}
.col-3 {
width: 30%;
width: 30%;
}
.col-5 {
width: 50%;
width: 50%;
}

@@ -54,3 +56,3 @@ ```

Add [Advanced Variables] to your build tool:
Add [PostCSS Advanced Variables] to your build tool:

@@ -63,4 +65,6 @@ ```bash

Use [PostCSS Advanced Variables] to process your CSS:
```js
require('postcss-advanced-variables')({ /* options */ }).process(YOUR_CSS);
require('postcss-advanced-variables').process(YOUR_CSS);
```

@@ -76,8 +80,8 @@

Load [Advanced Variables] as a PostCSS plugin:
Use [PostCSS Advanced Variables] as a plugin:
```js
postcss([
require('postcss-advanced-variables')({ /* options */ })
]);
require('postcss-advanced-variables')(/* options */)
]).process(YOUR_CSS);
```

@@ -93,3 +97,3 @@

Enable [Advanced Variables] within your Gulpfile:
Use [PostCSS Advanced Variables] in your Gulpfile:

@@ -100,9 +104,9 @@ ```js

gulp.task('css', function () {
return gulp.src('./css/src/*.css').pipe(
postcss([
require('postcss-advanced-variables')({ /* options */ })
])
).pipe(
gulp.dest('./css')
);
return gulp.src('./src/*.css').pipe(
postcss([
require('postcss-advanced-variables')(/* options */)
])
).pipe(
gulp.dest('.')
);
});

@@ -119,3 +123,3 @@ ```

Enable [Advanced Variables] within your Gruntfile:
Use [PostCSS Advanced Variables] in your Gruntfile:

@@ -126,12 +130,12 @@ ```js

grunt.initConfig({
postcss: {
options: {
processors: [
require('postcss-advanced-variables')({ /* options */ })
]
},
dist: {
src: 'css/*.css'
}
postcss: {
options: {
use: [
require('postcss-advanced-variables')(/* options */)
]
},
dist: {
src: '*.css'
}
}
});

@@ -144,20 +148,30 @@ ```

Type: `Object`
Default: `{}`
The `variables` option lets you specify your own global variables.
Specifies your own global variables.
```js
require('postcss-advanced-variables')({
variables: {
'site-width': '960px'
}
});
```
The `variables` option also accepts a function, which will be given 2 arguments;
the name of the unresolved variable, and the PostCSS node that used it.
```js
require('postcss-advanced-variables')({
variables: {
'site-width': '960px'
}
variables(name, node) {
if (name === 'site-width') {
return '960px';
}
return undefined;
}
});
```
```css
/* before */
```scss
.hero {
max-width: $site-width;
max-width: $site-width;
}

@@ -168,11 +182,31 @@

.hero {
max-width: 960px;
max-width: 960px;
}
```
[ci]: https://travis-ci.org/jonathantneal/postcss-advanced-variables
[ci-img]: https://travis-ci.org/jonathantneal/postcss-advanced-variables.svg
### `unresolved`
The `unresolved` option lets you determine how unresolved variables should be
handled. The available options are `throw`, `warn`, and `ignore`. The default
option is to `throw`.
```js
require('postcss-advanced-variables')({
unresolved: 'ignore' // ignore unresolved variables
});
```
[npm-url]: https://www.npmjs.com/package/postcss-advanced-variables
[npm-img]: https://img.shields.io/npm/v/postcss-advanced-variables.svg
[cli-url]: https://travis-ci.org/jonathantneal/postcss-advanced-variables
[cli-img]: https://img.shields.io/travis/jonathantneal/postcss-advanced-variables.svg
[win-url]: https://ci.appveyor.com/project/jonathantneal/postcss-advanced-variables
[win-img]: https://img.shields.io/appveyor/ci/jonathantneal/postcss-advanced-variables.svg
[git-url]: https://gitter.im/postcss/postcss
[git-img]: https://img.shields.io/badge/chat-gitter-blue.svg
[PostCSS Advanced Variables]: https://github.com/jonathantneal/postcss-advanced-variables
[PostCSS]: https://github.com/postcss/postcss
[Gulp PostCSS]: https://github.com/postcss/gulp-postcss
[Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
[PostCSS]: https://github.com/postcss/postcss
[Advanced Variables]: https://github.com/jonathantneal/postcss-advanced-variables
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc