Fix bin/publish: copy docs.dist from project root

Fix bin/publish: use correct .env path for rspade_system
Fix bin/publish script: prevent grep exit code 1 from terminating script

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-10-21 02:08:33 +00:00
commit f6fac6c4bc
79758 changed files with 10547827 additions and 0 deletions

122
vendor/spatie/ignition/node_modules/postcss-calc/CHANGELOG.md generated vendored Executable file
View File

@@ -0,0 +1,122 @@
# 7.0.5
- Fixed: reduction
# 7.0.4
- Fixed: strips away important factors from multiplications in calc() ([#107](https://github.com/postcss/postcss-calc/issues/107))
# 7.0.3
- Fixed: substracted css-variable from zero ([#111](https://github.com/postcss/postcss-calc/issues/111))
# 7.0.2
- Fixed: incorrect reduction of subtraction from zero ([#88](https://github.com/postcss/postcss-calc/issues/88))
- Fixed: doesn't remove calc for single function
- Fixed: relax parser on unknown units ([#76](https://github.com/postcss/postcss-calc/issues/76))
- Fixed: handle numbers with exponen composed ([#83](https://github.com/postcss/postcss-calc/pull/83))
- Fixed: handle plus sign before value ([#79](https://github.com/postcss/postcss-calc/pull/79))
- Fixed: better handle precision for nested calc ([#75](https://github.com/postcss/postcss-calc/pull/75))
- Fixed: properly handle nested add and sub expression inside sub expression ([#64](https://github.com/postcss/postcss-calc/issues/64))
- Fixed: handle uppercase units and functions ([#71](https://github.com/postcss/postcss-calc/pull/71))
- Fixed: do not break `calc` with single var ([cssnano/cssnano#725](https://github.com/cssnano/cssnano/issues/725))
- Updated: `postcss` to 7.0.27 (patch)
- Updated: `postcss-selector-parser` to 6.0.2
- Updated: `postcss-value-parser` to 4.0.2
# 7.0.1
- Updated: `postcss` to 7.0.2 (patch)
- Updated: `postcss-selector-parser` to 5.0.0-rc.4 (patch)
- Updated: `postcss-value-parser` to 3.3.1 (patch)
# 7.0.0
- Changed: Updated postcss-selector-parser to version 5.0.0-rc.3
- Changed: Dropped reduce-css-calc as a dependency
- Fixed: Support constant() and env() ([#42](https://github.com/postcss/postcss-calc/issues/42), [#48](https://github.com/postcss/postcss-calc/issues/48))
- Fixed: Support custom properties with "calc" in its name ([#50](https://github.com/postcss/postcss-calc/issues/50))
- Fixed: Remove unnecessary whitespace around `*` and `/` ([cssnano#625](https://github.com/cssnano/cssnano/issues/625))
- Fixed: Arithmetic bugs around subtraction ([#49](https://github.com/postcss/postcss-calc/issues/49))
- Fixed: Handling of nested calc statements ([reduce-css-calc#49](https://github.com/MoOx/reduce-css-calc/issues/49))
- Fixed: Bugs regarding complex calculations ([reduce-cs-calc#45](https://github.com/MoOx/reduce-css-calc/issues/45))
- Fixed: `100%` incorrectly being transformed to `1` ([reduce-css-calc#44](https://github.com/MoOx/reduce-css-calc/issues/44))
- Added: support for case-insensitive calc statements
# 6.0.2 - 2018-09-25
- Fixed: use PostCSS 7 (thanks to @douglasduteil)
# 6.0.1 - 2017-10-10
- Fixed: throwing error for attribute selectors without a value
# 6.0.0 - 2017-05-08
- Breaking: Updated PostCSS from v5.x to v6.x, and reduce-css-calc from v1.x
to v2.x (thanks to @andyjansson).
# 5.3.1 - 2016-08-22
- Fixed: avoid security issue related to ``reduce-css-calc@< 1.2.4``.
# 5.3.0 - 2016-07-11
- Added: support for selector transformation via `selectors` option.
([#29](https://github.com/postcss/postcss-calc/pull/29) - @uniquegestaltung)
# 5.2.1 - 2016-04-10
- Fixed: support for multiline value
([#27](https://github.com/postcss/postcss-calc/pull/27))
# 5.2.0 - 2016-01-08
- Added: "mediaQueries" option for `@media` support
([#22](https://github.com/postcss/postcss-calc/pull/22))
# 5.1.0 - 2016-01-07
- Added: "warnWhenCannotResolve" option to warn when calc() are not reduced to a single value
([#20](https://github.com/postcss/postcss-calc/pull/20))
# 5.0.0 - 2015-08-25
- Removed: compatibility with postcss v4.x
- Added: compatibility with postcss v5.x
# 4.1.0 - 2015-04-09
- Added: compatibility with postcss v4.1.x ([#12](https://github.com/postcss/postcss-calc/pull/12))
# 4.0.1 - 2015-04-09
- Fixed: `preserve` option does not create duplicated values ([#7](https://github.com/postcss/postcss-calc/issues/7))
# 4.0.0 - 2015-01-26
- Added: compatibility with postcss v4.x
- Changed: partial compatiblity with postcss v3.x (stack traces have lost filename)
# 3.0.0 - 2014-11-24
- Added: GNU like exceptions ([#4](https://github.com/postcss/postcss-calc/issues/4))
- Added: `precision` option ([#5](https://github.com/postcss/postcss-calc/issues/5))
- Added: `preserve` option ([#6](https://github.com/postcss/postcss-calc/issues/6))
# 2.1.0 - 2014-10-15
- Added: source of the error (gnu like message) (fix [#3](https://github.com/postcss/postcss-calc/issues/3))
# 2.0.1 - 2014-08-10
- Fixed: correctly ignore unrecognized values (fix [#2](https://github.com/postcss/postcss-calc/issues/2))
# 2.0.0 - 2014-08-06
- Changed: Plugin now return a function to have a consistent api. ([ref 1](https://github.com/ianstormtaylor/rework-color-function/issues/6), [ref 2](https://twitter.com/jongleberry/status/496552790416576513))
# 1.0.0 - 2014-08-04
✨ First release based on [rework-calc](https://github.com/reworkcss/rework-calc) v1.1.0 (code mainly exported to [`reduce-css-calc`](https://github.com/MoOx/reduce-css-calc))

20
vendor/spatie/ignition/node_modules/postcss-calc/LICENSE generated vendored Executable file
View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2014 Maxime Thirouin
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.

189
vendor/spatie/ignition/node_modules/postcss-calc/README.md generated vendored Executable file
View File

@@ -0,0 +1,189 @@
# PostCSS Calc [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][PostCSS]
[![NPM Version][npm-img]][npm-url]
[![Build Status][cli-img]][cli-url]
[![Support Chat][git-img]][git-url]
[PostCSS Calc] lets you reduce `calc()` references whenever it's possible. This
can be particularly useful with the [PostCSS Custom Properties] plugin.
When multiple units are mixed together in the same expression, the `calc()`
statement is left as is, to fallback to the [W3C calc() implementation].
## Installation
```bash
npm install postcss-calc
```
## Usage
```js
// dependencies
var fs = require("fs")
var postcss = require("postcss")
var calc = require("postcss-calc")
// css to be processed
var css = fs.readFileSync("input.css", "utf8")
// process css
var output = postcss()
.use(calc())
.process(css)
.css
```
**Example** (with [PostCSS Custom Properties] enabled as well):
```js
// dependencies
var fs = require("fs")
var postcss = require("postcss")
var customProperties = require("postcss-custom-properties")
var calc = require("postcss-calc")
// css to be processed
var css = fs.readFileSync("input.css", "utf8")
// process css
var output = postcss()
.use(customProperties())
.use(calc())
.process(css)
.css
```
Using this `input.css`:
```css
:root {
--main-font-size: 16px;
}
body {
font-size: var(--main-font-size);
}
h1 {
font-size: calc(var(--main-font-size) * 2);
height: calc(100px - 2em);
margin-bottom: calc(
var(--main-font-size)
* 1.5
)
}
```
you will get:
```css
body {
font-size: 16px
}
h1 {
font-size: 32px;
height: calc(100px - 2em);
margin-bottom: 24px
}
```
Checkout [tests] for more examples.
### Options
#### `precision` (default: `5`)
Allow you to define the precision for decimal numbers.
```js
var out = postcss()
.use(calc({precision: 10}))
.process(css)
.css
```
#### `preserve` (default: `false`)
Allow you to preserve calc() usage in output so browsers will handle decimal
precision themselves.
```js
var out = postcss()
.use(calc({preserve: true}))
.process(css)
.css
```
#### `warnWhenCannotResolve` (default: `false`)
Adds warnings when calc() are not reduced to a single value.
```js
var out = postcss()
.use(calc({warnWhenCannotResolve: true}))
.process(css)
.css
```
#### `mediaQueries` (default: `false`)
Allows calc() usage as part of media query declarations.
```js
var out = postcss()
.use(calc({mediaQueries: true}))
.process(css)
.css
```
#### `selectors` (default: `false`)
Allows calc() usage as part of selectors.
```js
var out = postcss()
.use(calc({selectors: true}))
.process(css)
.css
```
Example:
```css
div[data-size="calc(3*3)"] {
width: 100px;
}
```
---
## Contributing
Work on a branch, install dev-dependencies, respect coding style & run tests
before submitting a bug fix or a feature.
```bash
git clone git@github.com:postcss/postcss-calc.git
git checkout -b patch-1
npm install
npm test
```
## [Changelog](CHANGELOG.md)
## [License](LICENSE)
[cli-img]: https://img.shields.io/travis/postcss/postcss-calc/master.svg
[cli-url]: https://travis-ci.org/postcss/postcss-calc
[git-img]: https://img.shields.io/badge/support-chat-blue.svg
[git-url]: https://gitter.im/postcss/postcss
[npm-img]: https://img.shields.io/npm/v/postcss-calc.svg
[npm-url]: https://www.npmjs.com/package/postcss-calc
[PostCSS]: https://github.com/postcss
[PostCSS Calc]: https://github.com/postcss/postcss-calc
[PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties
[tests]: src/__tests__/index.js
[W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation

View File

@@ -0,0 +1,42 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _postcss = require("postcss");
var _transform = _interopRequireDefault(require("./lib/transform"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = (0, _postcss.plugin)('postcss-calc', function (opts) {
var options = Object.assign({
precision: 5,
preserve: false,
warnWhenCannotResolve: false,
mediaQueries: false,
selectors: false
}, opts);
return function (css, result) {
css.walk(function (node) {
var type = node.type;
if (type === 'decl') {
(0, _transform.default)(node, "value", options, result);
}
if (type === 'atrule' && options.mediaQueries) {
(0, _transform.default)(node, "params", options, result);
}
if (type === 'rule' && options.selectors) {
(0, _transform.default)(node, "selector", options, result);
}
});
};
});
exports.default = _default;
module.exports = exports.default;

View File

@@ -0,0 +1,157 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var conversions = {
// Absolute length units
'px': {
'px': 1,
'cm': 96 / 2.54,
'mm': 96 / 25.4,
'q': 96 / 101.6,
'in': 96,
'pt': 96 / 72,
'pc': 16
},
'cm': {
'px': 2.54 / 96,
'cm': 1,
'mm': 0.1,
'q': 0.025,
'in': 2.54,
'pt': 2.54 / 72,
'pc': 2.54 / 6
},
'mm': {
'px': 25.4 / 96,
'cm': 10,
'mm': 1,
'q': 0.25,
'in': 25.4,
'pt': 25.4 / 72,
'pc': 25.4 / 6
},
'q': {
'px': 101.6 / 96,
'cm': 40,
'mm': 4,
'q': 1,
'in': 101.6,
'pt': 101.6 / 72,
'pc': 101.6 / 6
},
'in': {
'px': 1 / 96,
'cm': 1 / 2.54,
'mm': 1 / 25.4,
'q': 1 / 101.6,
'in': 1,
'pt': 1 / 72,
'pc': 1 / 6
},
'pt': {
'px': 0.75,
'cm': 72 / 2.54,
'mm': 72 / 25.4,
'q': 72 / 101.6,
'in': 72,
'pt': 1,
'pc': 12
},
'pc': {
'px': 0.0625,
'cm': 6 / 2.54,
'mm': 6 / 25.4,
'q': 6 / 101.6,
'in': 6,
'pt': 6 / 72,
'pc': 1
},
// Angle units
'deg': {
'deg': 1,
'grad': 0.9,
'rad': 180 / Math.PI,
'turn': 360
},
'grad': {
'deg': 400 / 360,
'grad': 1,
'rad': 200 / Math.PI,
'turn': 400
},
'rad': {
'deg': Math.PI / 180,
'grad': Math.PI / 200,
'rad': 1,
'turn': Math.PI * 2
},
'turn': {
'deg': 1 / 360,
'grad': 0.0025,
'rad': 0.5 / Math.PI,
'turn': 1
},
// Duration units
's': {
's': 1,
'ms': 0.001
},
'ms': {
's': 1000,
'ms': 1
},
// Frequency units
'hz': {
'hz': 1,
'khz': 1000
},
'khz': {
'hz': 0.001,
'khz': 1
},
// Resolution units
'dpi': {
'dpi': 1,
'dpcm': 1 / 2.54,
'dppx': 1 / 96
},
'dpcm': {
'dpi': 2.54,
'dpcm': 1,
'dppx': 2.54 / 96
},
'dppx': {
'dpi': 96,
'dpcm': 96 / 2.54,
'dppx': 1
}
};
function convertUnit(value, sourceUnit, targetUnit, precision) {
var sourceUnitNormalized = sourceUnit.toLowerCase();
var targetUnitNormalized = targetUnit.toLowerCase();
if (!conversions[targetUnitNormalized]) {
throw new Error("Cannot convert to " + targetUnit);
}
if (!conversions[targetUnitNormalized][sourceUnitNormalized]) {
throw new Error("Cannot convert from " + sourceUnit + " to " + targetUnit);
}
var converted = conversions[targetUnitNormalized][sourceUnitNormalized] * value;
if (precision !== false) {
precision = Math.pow(10, parseInt(precision) || 5);
return Math.round(converted * precision) / precision;
}
return converted;
}
var _default = convertUnit;
exports.default = _default;
module.exports = exports.default;

View File

@@ -0,0 +1,320 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _convertUnit = _interopRequireDefault(require("./convertUnit"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function isValueType(type) {
switch (type) {
case 'LengthValue':
case 'AngleValue':
case 'TimeValue':
case 'FrequencyValue':
case 'ResolutionValue':
case 'EmValue':
case 'ExValue':
case 'ChValue':
case 'RemValue':
case 'VhValue':
case 'VwValue':
case 'VminValue':
case 'VmaxValue':
case 'PercentageValue':
case 'Number':
return true;
}
return false;
}
function flip(operator) {
return operator === '+' ? '-' : '+';
}
function isAddSubOperator(operator) {
return operator === '+' || operator === '-';
}
function collectAddSubItems(preOperator, node, collected, precision) {
if (!isAddSubOperator(preOperator)) {
throw new Error(`invalid operator ${preOperator}`);
}
var type = node.type;
if (isValueType(type)) {
var itemIndex = collected.findIndex(function (x) {
return x.node.type === type;
});
if (itemIndex >= 0) {
if (node.value === 0) {
return;
}
var _covertNodesUnits = covertNodesUnits(collected[itemIndex].node, node, precision),
reducedNode = _covertNodesUnits.left,
current = _covertNodesUnits.right;
if (collected[itemIndex].preOperator === '-') {
collected[itemIndex].preOperator = '+';
reducedNode.value *= -1;
}
if (preOperator === "+") {
reducedNode.value += current.value;
} else {
reducedNode.value -= current.value;
} // make sure reducedNode.value >= 0
if (reducedNode.value >= 0) {
collected[itemIndex] = {
node: reducedNode,
preOperator: '+'
};
} else {
reducedNode.value *= -1;
collected[itemIndex] = {
node: reducedNode,
preOperator: '-'
};
}
} else {
// make sure node.value >= 0
if (node.value >= 0) {
collected.push({
node,
preOperator
});
} else {
node.value *= -1;
collected.push({
node,
preOperator: flip(preOperator)
});
}
}
} else if (type === "MathExpression") {
if (isAddSubOperator(node.operator)) {
collectAddSubItems(preOperator, node.left, collected, precision);
var collectRightOperator = preOperator === '-' ? flip(node.operator) : node.operator;
collectAddSubItems(collectRightOperator, node.right, collected, precision);
} else {
// * or /
var _reducedNode = reduce(node, precision); // prevent infinite recursive call
if (_reducedNode.type !== "MathExpression" || isAddSubOperator(_reducedNode.operator)) {
collectAddSubItems(preOperator, _reducedNode, collected, precision);
} else {
collected.push({
node: _reducedNode,
preOperator
});
}
}
} else {
collected.push({
node,
preOperator
});
}
}
function reduceAddSubExpression(node, precision) {
var collected = [];
collectAddSubItems('+', node, collected, precision);
var withoutZeroItem = collected.filter(function (item) {
return !(isValueType(item.node.type) && item.node.value === 0);
});
var firstNonZeroItem = withoutZeroItem[0]; // could be undefined
// prevent producing "calc(-var(--a))" or "calc()"
// which is invalid css
if (!firstNonZeroItem || firstNonZeroItem.preOperator === '-' && !isValueType(firstNonZeroItem.node.type)) {
var firstZeroItem = collected.find(function (item) {
return isValueType(item.node.type) && item.node.value === 0;
});
withoutZeroItem.unshift(firstZeroItem);
} // make sure the preOperator of the first item is +
if (withoutZeroItem[0].preOperator === '-' && isValueType(withoutZeroItem[0].node.type)) {
withoutZeroItem[0].node.value *= -1;
withoutZeroItem[0].preOperator = '+';
}
var root = withoutZeroItem[0].node;
for (var i = 1; i < withoutZeroItem.length; i++) {
root = {
type: 'MathExpression',
operator: withoutZeroItem[i].preOperator,
left: root,
right: withoutZeroItem[i].node
};
}
return root;
}
function reduceDivisionExpression(node) {
if (!isValueType(node.right.type)) {
return node;
}
if (node.right.type !== 'Number') {
throw new Error(`Cannot divide by "${node.right.unit}", number expected`);
}
return applyNumberDivision(node.left, node.right.value);
} // apply (expr) / number
function applyNumberDivision(node, divisor) {
if (divisor === 0) {
throw new Error('Cannot divide by zero');
}
if (isValueType(node.type)) {
node.value /= divisor;
return node;
}
if (node.type === "MathExpression" && isAddSubOperator(node.operator)) {
// turn (a + b) / num into a/num + b/num
// is good for further reduction
// checkout the test case
// "should reduce division before reducing additions"
return {
type: "MathExpression",
operator: node.operator,
left: applyNumberDivision(node.left, divisor),
right: applyNumberDivision(node.right, divisor)
};
} // it is impossible to reduce it into a single value
// .e.g the node contains css variable
// so we just preserve the division and let browser do it
return {
type: "MathExpression",
operator: '/',
left: node,
right: {
type: "Number",
value: divisor
}
};
}
function reduceMultiplicationExpression(node) {
// (expr) * number
if (node.right.type === 'Number') {
return applyNumberMultiplication(node.left, node.right.value);
} // number * (expr)
if (node.left.type === 'Number') {
return applyNumberMultiplication(node.right, node.left.value);
}
return node;
} // apply (expr) / number
function applyNumberMultiplication(node, multiplier) {
if (isValueType(node.type)) {
node.value *= multiplier;
return node;
}
if (node.type === "MathExpression" && isAddSubOperator(node.operator)) {
// turn (a + b) * num into a*num + b*num
// is good for further reduction
// checkout the test case
// "should reduce multiplication before reducing additions"
return {
type: "MathExpression",
operator: node.operator,
left: applyNumberMultiplication(node.left, multiplier),
right: applyNumberMultiplication(node.right, multiplier)
};
} // it is impossible to reduce it into a single value
// .e.g the node contains css variable
// so we just preserve the division and let browser do it
return {
type: "MathExpression",
operator: '*',
left: node,
right: {
type: "Number",
value: multiplier
}
};
}
function covertNodesUnits(left, right, precision) {
switch (left.type) {
case 'LengthValue':
case 'AngleValue':
case 'TimeValue':
case 'FrequencyValue':
case 'ResolutionValue':
if (right.type === left.type && right.unit && left.unit) {
var converted = (0, _convertUnit.default)(right.value, right.unit, left.unit, precision);
right = {
type: left.type,
value: converted,
unit: left.unit
};
}
return {
left,
right
};
default:
return {
left,
right
};
}
}
function reduce(node, precision) {
if (node.type === "MathExpression") {
if (isAddSubOperator(node.operator)) {
// reduceAddSubExpression will call reduce recursively
return reduceAddSubExpression(node, precision);
}
node.left = reduce(node.left, precision);
node.right = reduce(node.right, precision);
switch (node.operator) {
case "/":
return reduceDivisionExpression(node, precision);
case "*":
return reduceMultiplicationExpression(node, precision);
}
return node;
}
return node;
}
var _default = reduce;
exports.default = _default;
module.exports = exports.default;

View File

@@ -0,0 +1,81 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var order = {
"*": 0,
"/": 0,
"+": 1,
"-": 1
};
function round(value, prec) {
if (prec !== false) {
var precision = Math.pow(10, prec);
return Math.round(value * precision) / precision;
}
return value;
}
function stringify(node, prec) {
switch (node.type) {
case "MathExpression":
{
var left = node.left,
right = node.right,
op = node.operator;
var str = "";
if (left.type === 'MathExpression' && order[op] < order[left.operator]) {
str += `(${stringify(left, prec)})`;
} else {
str += stringify(left, prec);
}
str += order[op] ? ` ${node.operator} ` : node.operator;
if (right.type === 'MathExpression' && order[op] < order[right.operator]) {
str += `(${stringify(right, prec)})`;
} else {
str += stringify(right, prec);
}
return str;
}
case 'Number':
return round(node.value, prec);
case 'Function':
return node.value;
default:
return round(node.value, prec) + node.unit;
}
}
function _default(calc, node, originalValue, options, result, item) {
var str = stringify(node, options.precision);
var shouldPrintCalc = node.type === "MathExpression" || node.type === "Function";
if (shouldPrintCalc) {
// if calc expression couldn't be resolved to a single value, re-wrap it as
// a calc()
str = `${calc}(${str})`; // if the warnWhenCannotResolve option is on, inform the user that the calc
// expression could not be resolved to a single value
if (options.warnWhenCannotResolve) {
result.warn("Could not reduce expression: " + originalValue, {
plugin: 'postcss-calc',
node: item
});
}
}
return str;
}
module.exports = exports.default;

View File

@@ -0,0 +1,81 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
var _parser = require("../parser");
var _reducer = _interopRequireDefault(require("./reducer"));
var _stringifier = _interopRequireDefault(require("./stringifier"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// eslint-disable-next-line import/no-unresolved
var MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
function transformValue(value, options, result, item) {
return (0, _postcssValueParser.default)(value).walk(function (node) {
// skip anything which isn't a calc() function
if (node.type !== 'function' || !MATCH_CALC.test(node.value)) {
return node;
} // stringify calc expression and produce an AST
var contents = _postcssValueParser.default.stringify(node.nodes);
var ast = _parser.parser.parse(contents); // reduce AST to its simplest form, that is, either to a single value
// or a simplified calc expression
var reducedAst = (0, _reducer.default)(ast, options.precision); // stringify AST and write it back
node.type = 'word';
node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
return false;
}).toString();
}
function transformSelector(value, options, result, item) {
return (0, _postcssSelectorParser.default)(function (selectors) {
selectors.walk(function (node) {
// attribute value
// e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
if (node.type === 'attribute' && node.value) {
node.setValue(transformValue(node.value, options, result, item));
} // tag value
// e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
if (node.type === 'tag') {
node.value = transformValue(node.value, options, result, item);
}
return;
});
}).processSync(value);
}
var _default = function _default(node, property, options, result) {
var value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node); // if the preserve option is enabled and the value has changed, write the
// transformed value into a cloned node which is inserted before the current
// node, preserving the original value. Otherwise, overwrite the original
// value.
if (options.preserve && node[property] !== value) {
var clone = node.clone();
clone[property] = value;
node.parent.insertBefore(node, clone);
} else {
node[property] = value;
}
};
exports.default = _default;
module.exports = exports.default;

3804
vendor/spatie/ignition/node_modules/postcss-calc/dist/parser.js generated vendored Executable file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,873 @@
# API Documentation
*Please use only this documented API when working with the parser. Methods
not documented here are subject to change at any point.*
## `parser` function
This is the module's main entry point.
```js
const parser = require('postcss-selector-parser');
```
### `parser([transform], [options])`
Creates a new `processor` instance
```js
const processor = parser();
```
Or, with optional transform function
```js
const transform = selectors => {
selectors.walkUniversals(selector => {
selector.remove();
});
};
const processor = parser(transform)
// Example
const result = processor.processSync('*.class');
// => .class
```
[See processor documentation](#processor)
Arguments:
* `transform (function)`: Provide a function to work with the parsed AST.
* `options (object)`: Provide default options for all calls on the returned `Processor`.
### `parser.attribute([props])`
Creates a new attribute selector.
```js
parser.attribute({attribute: 'href'});
// => [href]
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.className([props])`
Creates a new class selector.
```js
parser.className({value: 'button'});
// => .button
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.combinator([props])`
Creates a new selector combinator.
```js
parser.combinator({value: '+'});
// => +
```
Arguments:
* `props (object)`: The new node's properties.
Notes:
* **Descendant Combinators** The value of descendant combinators created by the
parser always just a single space (`" "`). For descendant selectors with no
comments, additional space is now stored in `node.spaces.before`. Depending
on the location of comments, additional spaces may be stored in
`node.raws.spaces.before`, `node.raws.spaces.after`, or `node.raws.value`.
* **Named Combinators** Although, nonstandard and unlikely to ever become a standard,
named combinators like `/deep/` and `/for/` are parsed as combinators. The
`node.value` is name after being unescaped and normalized as lowercase. The
original value for the combinator name is stored in `node.raws.value`.
### `parser.comment([props])`
Creates a new comment.
```js
parser.comment({value: '/* Affirmative, Dave. I read you. */'});
// => /* Affirmative, Dave. I read you. */
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.id([props])`
Creates a new id selector.
```js
parser.id({value: 'search'});
// => #search
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.nesting([props])`
Creates a new nesting selector.
```js
parser.nesting();
// => &
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.pseudo([props])`
Creates a new pseudo selector.
```js
parser.pseudo({value: '::before'});
// => ::before
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.root([props])`
Creates a new root node.
```js
parser.root();
// => (empty)
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.selector([props])`
Creates a new selector node.
```js
parser.selector();
// => (empty)
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.string([props])`
Creates a new string node.
```js
parser.string();
// => (empty)
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.tag([props])`
Creates a new tag selector.
```js
parser.tag({value: 'button'});
// => button
```
Arguments:
* `props (object)`: The new node's properties.
### `parser.universal([props])`
Creates a new universal selector.
```js
parser.universal();
// => *
```
Arguments:
* `props (object)`: The new node's properties.
## Node types
### `node.type`
A string representation of the selector type. It can be one of the following;
`attribute`, `class`, `combinator`, `comment`, `id`, `nesting`, `pseudo`,
`root`, `selector`, `string`, `tag`, or `universal`. Note that for convenience,
these constants are exposed on the main `parser` as uppercased keys. So for
example you can get `id` by querying `parser.ID`.
```js
parser.attribute({attribute: 'href'}).type;
// => 'attribute'
```
### `node.parent`
Returns the parent node.
```js
root.nodes[0].parent === root;
```
### `node.toString()`, `String(node)`, or `'' + node`
Returns a string representation of the node.
```js
const id = parser.id({value: 'search'});
console.log(String(id));
// => #search
```
### `node.next()` & `node.prev()`
Returns the next/previous child of the parent node.
```js
const next = id.next();
if (next && next.type !== 'combinator') {
throw new Error('Qualified IDs are not allowed!');
}
```
### `node.replaceWith(node)`
Replace a node with another.
```js
const attr = selectors.first.first;
const className = parser.className({value: 'test'});
attr.replaceWith(className);
```
Arguments:
* `node`: The node to substitute the original with.
### `node.remove()`
Removes the node from its parent node.
```js
if (node.type === 'id') {
node.remove();
}
```
### `node.clone()`
Returns a copy of a node, detached from any parent containers that the
original might have had.
```js
const cloned = parser.id({value: 'search'});
String(cloned);
// => #search
```
### `node.isAtPosition(line, column)`
Return a `boolean` indicating whether this node includes the character at the
position of the given line and column. Returns `undefined` if the nodes lack
sufficient source metadata to determine the position.
Arguments:
* `line`: 1-index based line number relative to the start of the selector.
* `column`: 1-index based column number relative to the start of the selector.
### `node.spaces`
Extra whitespaces around the node will be moved into `node.spaces.before` and
`node.spaces.after`. So for example, these spaces will be moved as they have
no semantic meaning:
```css
h1 , h2 {}
```
For descendent selectors, the value is always a single space.
```css
h1 h2 {}
```
Additional whitespace is found in either the `node.spaces.before` and `node.spaces.after` depending on the presence of comments or other whitespace characters. If the actual whitespace does not start or end with a single space, the node's raw value is set to the actual space(s) found in the source.
### `node.source`
An object describing the node's start/end, line/column source position.
Within the following CSS, the `.bar` class node ...
```css
.foo,
.bar {}
```
... will contain the following `source` object.
```js
source: {
start: {
line: 2,
column: 3
},
end: {
line: 2,
column: 6
}
}
```
### `node.sourceIndex`
The zero-based index of the node within the original source string.
Within the following CSS, the `.baz` class node will have a `sourceIndex` of `12`.
```css
.foo, .bar, .baz {}
```
## Container types
The `root`, `selector`, and `pseudo` nodes have some helper methods for working
with their children.
### `container.nodes`
An array of the container's children.
```js
// Input: h1 h2
selectors.at(0).nodes.length // => 3
selectors.at(0).nodes[0].value // => 'h1'
selectors.at(0).nodes[1].value // => ' '
```
### `container.first` & `container.last`
The first/last child of the container.
```js
selector.first === selector.nodes[0];
selector.last === selector.nodes[selector.nodes.length - 1];
```
### `container.at(index)`
Returns the node at position `index`.
```js
selector.at(0) === selector.first;
selector.at(0) === selector.nodes[0];
```
Arguments:
* `index`: The index of the node to return.
### `container.atPosition(line, column)`
Returns the node at the source position `index`.
```js
selector.at(0) === selector.first;
selector.at(0) === selector.nodes[0];
```
Arguments:
* `index`: The index of the node to return.
### `container.index(node)`
Return the index of the node within its container.
```js
selector.index(selector.nodes[2]) // => 2
```
Arguments:
* `node`: A node within the current container.
### `container.length`
Proxy to the length of the container's nodes.
```js
container.length === container.nodes.length
```
### `container` Array iterators
The container class provides proxies to certain Array methods; these are:
* `container.map === container.nodes.map`
* `container.reduce === container.nodes.reduce`
* `container.every === container.nodes.every`
* `container.some === container.nodes.some`
* `container.filter === container.nodes.filter`
* `container.sort === container.nodes.sort`
Note that these methods only work on a container's immediate children; recursive
iteration is provided by `container.walk`.
### `container.each(callback)`
Iterate the container's immediate children, calling `callback` for each child.
You may return `false` within the callback to break the iteration.
```js
let className;
selectors.each((selector, index) => {
if (selector.type === 'class') {
className = selector.value;
return false;
}
});
```
Note that unlike `Array#forEach()`, this iterator is safe to use whilst adding
or removing nodes from the container.
Arguments:
* `callback (function)`: A function to call for each node, which receives `node`
and `index` arguments.
### `container.walk(callback)`
Like `container#each`, but will also iterate child nodes as long as they are
`container` types.
```js
selectors.walk((selector, index) => {
// all nodes
});
```
Arguments:
* `callback (function)`: A function to call for each node, which receives `node`
and `index` arguments.
This iterator is safe to use whilst mutating `container.nodes`,
like `container#each`.
### `container.walk` proxies
The container class provides proxy methods for iterating over types of nodes,
so that it is easier to write modules that target specific selectors. Those
methods are:
* `container.walkAttributes`
* `container.walkClasses`
* `container.walkCombinators`
* `container.walkComments`
* `container.walkIds`
* `container.walkNesting`
* `container.walkPseudos`
* `container.walkTags`
* `container.walkUniversals`
### `container.split(callback)`
This method allows you to split a group of nodes by returning `true` from
a callback. It returns an array of arrays, where each inner array corresponds
to the groups that you created via the callback.
```js
// (input) => h1 h2>>h3
const list = selectors.first.split(selector => {
return selector.type === 'combinator';
});
// (node values) => [['h1', ' '], ['h2', '>>'], ['h3']]
```
Arguments:
* `callback (function)`: A function to call for each node, which receives `node`
as an argument.
### `container.prepend(node)` & `container.append(node)`
Add a node to the start/end of the container. Note that doing so will set
the parent property of the node to this container.
```js
const id = parser.id({value: 'search'});
selector.append(id);
```
Arguments:
* `node`: The node to add.
### `container.insertBefore(old, new)` & `container.insertAfter(old, new)`
Add a node before or after an existing node in a container:
```js
selectors.walk(selector => {
if (selector.type !== 'class') {
const className = parser.className({value: 'theme-name'});
selector.parent.insertAfter(selector, className);
}
});
```
Arguments:
* `old`: The existing node in the container.
* `new`: The new node to add before/after the existing node.
### `container.removeChild(node)`
Remove the node from the container. Note that you can also use
`node.remove()` if you would like to remove just a single node.
```js
selector.length // => 2
selector.remove(id)
selector.length // => 1;
id.parent // undefined
```
Arguments:
* `node`: The node to remove.
### `container.removeAll()` or `container.empty()`
Remove all children from the container.
```js
selector.removeAll();
selector.length // => 0
```
## Root nodes
A root node represents a comma separated list of selectors. Indeed, all
a root's `toString()` method does is join its selector children with a ','.
Other than this, it has no special functionality and acts like a container.
### `root.trailingComma`
This will be set to `true` if the input has a trailing comma, in order to
support parsing of legacy CSS hacks.
## Selector nodes
A selector node represents a single complex selector. For example, this
selector string `h1 h2 h3, [href] > p`, is represented as two selector nodes.
It has no special functionality of its own.
## Pseudo nodes
A pseudo selector extends a container node; if it has any parameters of its
own (such as `h1:not(h2, h3)`), they will be its children. Note that the pseudo
`value` will always contain the colons preceding the pseudo identifier. This
is so that both `:before` and `::before` are properly represented in the AST.
## Attribute nodes
### `attribute.quoted`
Returns `true` if the attribute's value is wrapped in quotation marks, false if it is not.
Remains `undefined` if there is no attribute value.
```css
[href=foo] /* false */
[href='foo'] /* true */
[href="foo"] /* true */
[href] /* undefined */
```
### `attribute.qualifiedAttribute`
Returns the attribute name qualified with the namespace if one is given.
### `attribute.offsetOf(part)`
Returns the offset of the attribute part specified relative to the
start of the node of the output string. This is useful in raising
error messages about a specific part of the attribute, especially
in combination with `attribute.sourceIndex`.
Returns `-1` if the name is invalid or the value doesn't exist in this
attribute.
The legal values for `part` are:
* `"ns"` - alias for "namespace"
* `"namespace"` - the namespace if it exists.
* `"attribute"` - the attribute name
* `"attributeNS"` - the start of the attribute or its namespace
* `"operator"` - the match operator of the attribute
* `"value"` - The value (string or identifier)
* `"insensitive"` - the case insensitivity flag
### `attribute.raws.unquoted`
Returns the unquoted content of the attribute's value.
Remains `undefined` if there is no attribute value.
```css
[href=foo] /* foo */
[href='foo'] /* foo */
[href="foo"] /* foo */
[href] /* undefined */
```
### `attribute.spaces`
Like `node.spaces` with the `before` and `after` values containing the spaces
around the element, the parts of the attribute can also have spaces before
and after them. The for each of `attribute`, `operator`, `value` and
`insensitive` there is corresponding property of the same nam in
`node.spaces` that has an optional `before` or `after` string containing only
whitespace.
Note that corresponding values in `attributes.raws.spaces` contain values
including any comments. If set, these values will override the
`attribute.spaces` value. Take care to remove them if changing
`attribute.spaces`.
### `attribute.raws`
The raws object stores comments and other information necessary to re-render
the node exactly as it was in the source.
If a comment is embedded within the identifiers for the `namespace`, `attribute`
or `value` then a property is placed in the raws for that value containing the full source of the propery including comments.
If a comment is embedded within the space between parts of the attribute
then the raw for that space is set accordingly.
Setting an attribute's property `raws` value to be deleted.
For now, changing the spaces required also updating or removing any of the
raws values that override them.
Example: `[ /*before*/ href /* after-attr */ = /* after-operator */ te/*inside-value*/st/* wow */ /*omg*/i/*bbq*/ /*whodoesthis*/]` would parse as:
```js
{
attribute: "href",
operator: "=",
value: "test",
spaces: {
before: '',
after: '',
attribute: { before: ' ', after: ' ' },
operator: { after: ' ' },
value: { after: ' ' },
insensitive: { after: ' ' }
},
raws: {
spaces: {
attribute: { before: ' /*before*/ ', after: ' /* after-attr */ ' },
operator: { after: ' /* after-operator */ ' },
value: { after: '/* wow */ /*omg*/' },
insensitive: { after: '/*bbq*/ /*whodoesthis*/' }
},
unquoted: 'test',
value: 'te/*inside-value*/st'
}
}
```
## `Processor`
### `ProcessorOptions`
* `lossless` - When `true`, whitespace is preserved. Defaults to `true`.
* `updateSelector` - When `true`, if any processor methods are passed a postcss
`Rule` node instead of a string, then that Rule's selector is updated
with the results of the processing. Defaults to `true`.
### `process|processSync(selectors, [options])`
Processes the `selectors`, returning a string from the result of processing.
Note: when the `updateSelector` option is set, the rule's selector
will be updated with the resulting string.
**Example:**
```js
const parser = require("postcss-selector-parser");
const processor = parser();
let result = processor.processSync(' .class');
console.log(result);
// => .class
// Asynchronous operation
let promise = processor.process(' .class').then(result => {
console.log(result)
// => .class
});
// To have the parser normalize whitespace values, utilize the options
result = processor.processSync(' .class ', {lossless: false});
console.log(result);
// => .class
// For better syntax errors, pass a PostCSS Rule node.
const postcss = require('postcss');
rule = postcss.rule({selector: ' #foo > a, .class '});
processor.process(rule, {lossless: false, updateSelector: true}).then(result => {
console.log(result);
// => #foo>a,.class
console.log("rule:", rule.selector);
// => rule: #foo>a,.class
})
```
Arguments:
* `selectors (string|postcss.Rule)`: Either a selector string or a PostCSS Rule
node.
* `[options] (object)`: Process options
### `ast|astSync(selectors, [options])`
Like `process()` and `processSync()` but after
processing the `selectors` these methods return the `Root` node of the result
instead of a string.
Note: when the `updateSelector` option is set, the rule's selector
will be updated with the resulting string.
### `transform|transformSync(selectors, [options])`
Like `process()` and `processSync()` but after
processing the `selectors` these methods return the value returned by the
processor callback.
Note: when the `updateSelector` option is set, the rule's selector
will be updated with the resulting string.
### Error Handling Within Selector Processors
The root node passed to the selector processor callback
has a method `error(message, options)` that returns an
error object. This method should always be used to raise
errors relating to the syntax of selectors. The options
to this method are passed to postcss's error constructor
([documentation](http://api.postcss.org/Container.html#error)).
#### Async Error Example
```js
let processor = (root) => {
return new Promise((resolve, reject) => {
root.walkClasses((classNode) => {
if (/^(.*)[-_]/.test(classNode.value)) {
let msg = "classes may not have underscores or dashes in them";
reject(root.error(msg, {
index: classNode.sourceIndex + RegExp.$1.length + 1,
word: classNode.value
}));
}
});
resolve();
});
};
const postcss = require("postcss");
const parser = require("postcss-selector-parser");
const selectorProcessor = parser(processor);
const plugin = postcss.plugin('classValidator', (options) => {
return (root) => {
let promises = [];
root.walkRules(rule => {
promises.push(selectorProcessor.process(rule));
});
return Promise.all(promises);
};
});
postcss(plugin()).process(`
.foo-bar {
color: red;
}
`.trim(), {from: 'test.css'}).catch((e) => console.error(e.toString()));
// CssSyntaxError: classValidator: ./test.css:1:5: classes may not have underscores or dashes in them
//
// > 1 | .foo-bar {
// | ^
// 2 | color: red;
// 3 | }
```
#### Synchronous Error Example
```js
let processor = (root) => {
root.walkClasses((classNode) => {
if (/.*[-_]/.test(classNode.value)) {
let msg = "classes may not have underscores or dashes in them";
throw root.error(msg, {
index: classNode.sourceIndex,
word: classNode.value
});
}
});
};
const postcss = require("postcss");
const parser = require("postcss-selector-parser");
const selectorProcessor = parser(processor);
const plugin = postcss.plugin('classValidator', (options) => {
return (root) => {
root.walkRules(rule => {
selectorProcessor.processSync(rule);
});
};
});
postcss(plugin()).process(`
.foo-bar {
color: red;
}
`.trim(), {from: 'test.css'}).catch((e) => console.error(e.toString()));
// CssSyntaxError: classValidator: ./test.css:1:5: classes may not have underscores or dashes in them
//
// > 1 | .foo-bar {
// | ^
// 2 | color: red;
// 3 | }
```

View File

@@ -0,0 +1,497 @@
# 6.0.6
- Fixed: parse quoted attributes containing a newline correctly
# 6.0.5
- Perf: rework unesc for a 63+% performance boost
# 6.0.4
- Fixed: ts errors
# 6.0.3
- Fixed: replace node built-in "util" module with "util-deprecate"
- Fixed: handle uppercase pseudo elements
- Fixed: do not create invalid combinator before comment
# 6.0.2
- Fixed an issue with parsing and stringifying an empty attribute value
# 6.0.1
- Fixed an issue with unicode surrogate pair parsing
# 6.0.0
- Updated: `cssesc` to 3.0.0 (major)
- Fixed: Issues with escaped `id` and `class` selectors
# 5.0.0
- Allow escaped dot within class name.
- Update PostCSS to 7.0.7 (patch)
# 5.0.0-rc.4
- Fixed an issue where comments immediately after an insensitive (in attribute)
were not parsed correctly.
- Updated `cssesc` to 2.0.0 (major).
- Removed outdated integration tests.
- Added tests for custom selectors, tags with attributes, the universal
selector with pseudos, and tokens after combinators.
# 5.0.0-rc.1
To ease adoption of the v5.0 release, we have relaxed the node version
check performed by npm at installation time to allow for node 4, which
remains officially unsupported, but likely to continue working for the
time being.
# 5.0.0-rc.0
This release has **BREAKING CHANGES** that were required to fix regressions
in 4.0.0 and to make the Combinator Node API consistent for all combinator
types. Please read carefully.
## Summary of Changes
* The way a descendent combinator that isn't a single space character (E.g. `.a .b`) is stored in the AST has changed.
* Named Combinators (E.g. `.a /for/ .b`) are now properly parsed as a combinator.
* It is now possible to look up a node based on the source location of a character in that node and to query nodes if they contain some character.
* Several bug fixes that caused the parser to hang and run out of memory when a `/` was encountered have been fixed.
* The minimum supported version of Node is now `v6.0.0`.
### Changes to the Descendent Combinator
In prior releases, the value of a descendant combinator with multiple spaces included all the spaces.
* `.a .b`: Extra spaces are now stored as space before.
- Old & Busted:
- `combinator.value === " "`
- New hotness:
- `combinator.value === " " && combinator.spaces.before === " "`
* `.a /*comment*/.b`: A comment at the end of the combinator causes extra space to become after space.
- Old & Busted:
- `combinator.value === " "`
- `combinator.raws.value === " /*comment/"`
- New hotness:
- `combinator.value === " "`
- `combinator.spaces.after === " "`
- `combinator.raws.spaces.after === " /*comment*/"`
* `.a<newline>.b`: whitespace that doesn't start or end with a single space character is stored as a raw value.
- Old & Busted:
- `combinator.value === "\n"`
- `combinator.raws.value === undefined`
- New hotness:
- `combinator.value === " "`
- `combinator.raws.value === "\n"`
### Support for "Named Combinators"
Although, nonstandard and unlikely to ever become a standard, combinators like `/deep/` and `/for/` are now properly supported.
Because they've been taken off the standardization track, there is no spec-official name for combinators of the form `/<ident>/`. However, I talked to [Tab Atkins](https://twitter.com/tabatkins) and we agreed to call them "named combinators" so now they are called that.
Before this release such named combinators were parsed without intention and generated three nodes of type `"tag"` where the first and last nodes had a value of `"/"`.
* `.a /for/ .b` is parsed as a combinator.
- Old & Busted:
- `root.nodes[0].nodes[1].type === "tag"`
- `root.nodes[0].nodes[1].value === "/"`
- New hotness:
- `root.nodes[0].nodes[1].type === "combinator"`
- `root.nodes[0].nodes[1].value === "/for/"`
* `.a /F\6fR/ .b` escapes are handled and uppercase is normalized.
- Old & Busted:
- `root.nodes[0].nodes[2].type === "tag"`
- `root.nodes[0].nodes[2].value === "F\\6fR"`
- New hotness:
- `root.nodes[0].nodes[1].type === "combinator"`
- `root.nodes[0].nodes[1].value === "/for/"`
- `root.nodes[0].nodes[1].raws.value === "/F\\6fR/"`
### Source position checks and lookups
A new API was added to look up a node based on the source location.
```js
const selectorParser = require("postcss-selector-parser");
// You can find the most specific node for any given character
let combinator = selectorParser.astSync(".a > .b").atPosition(1,4);
combinator.toString() === " > ";
// You can check if a node includes a specific character
// Whitespace surrounding the node that is owned by that node
// is included in the check.
[2,3,4,5,6].map(column => combinator.isAtPosition(1, column));
// => [false, true, true, true, false]
```
# 4.0.0
This release has **BREAKING CHANGES** that were required to fix bugs regarding values with escape sequences. Please read carefully.
* **Identifiers with escapes** - CSS escape sequences are now hidden from the public API by default.
The normal value of a node like a class name or ID, or an aspect of a node such as attribute
selector's value, is unescaped. Escapes representing Non-ascii characters are unescaped into
unicode characters. For example: `bu\tton, .\31 00, #i\2764\FE0Fu, [attr="value is \"quoted\""]`
will parse respectively to the values `button`, `100`, `i❤u`, `value is "quoted"`.
The original escape sequences for these values can be found in the corresponding property name
in `node.raws`. Where possible, deprecation warnings were added, but the nature
of escape handling makes it impossible to detect what is escaped or not. Our expectation is
that most users are neither expecting nor handling escape sequences in their use of this library,
and so for them, this is a bug fix. Users who are taking care to handle escapes correctly can
now update their code to remove the escape handling and let us do it for them.
* **Mutating values with escapes** - When you make an update to a node property that has escape handling
The value is assumed to be unescaped, and any special characters are escaped automatically and
the corresponding `raws` value is immediately updated. This can result in changes to the original
escape format. Where the exact value of the escape sequence is important there are methods that
allow both values to be set in conjunction. There are a number of new convenience methods for
manipulating values that involve escapes, especially for attributes values where the quote mark
is involved. See https://github.com/postcss/postcss-selector-parser/pull/133 for an extensive
write-up on these changes.
**Upgrade/API Example**
In `3.x` there was no unescape handling and internal consistency of several properties was the caller's job to maintain. It was very easy for the developer
to create a CSS file that did not parse correctly when some types of values
were in use.
```js
const selectorParser = require("postcss-selector-parser");
let attr = selectorParser.attribute({attribute: "id", operator: "=", value: "a-value"});
attr.value; // => "a-value"
attr.toString(); // => [id=a-value]
// Add quotes to an attribute's value.
// All these values have to be set by the caller to be consistent:
// no internal consistency is maintained.
attr.raws.unquoted = attr.value
attr.value = "'" + attr.value + "'";
attr.value; // => "'a-value'"
attr.quoted = true;
attr.toString(); // => "[id='a-value']"
```
In `4.0` there is a convenient API for setting and mutating values
that may need escaping. Especially for attributes.
```js
const selectorParser = require("postcss-selector-parser");
// The constructor requires you specify the exact escape sequence
let className = selectorParser.className({value: "illegal class name", raws: {value: "illegal\\ class\\ name"}});
className.toString(); // => '.illegal\\ class\\ name'
// So it's better to set the value as a property
className = selectorParser.className();
// Most properties that deal with identifiers work like this
className.value = "escape for me";
className.value; // => 'escape for me'
className.toString(); // => '.escape\\ for\\ me'
// emoji and all non-ascii are escaped to ensure it works in every css file.
className.value = "😱🦄😍";
className.value; // => '😱🦄😍'
className.toString(); // => '.\\1F631\\1F984\\1F60D'
// you can control the escape sequence if you want, or do bad bad things
className.setPropertyAndEscape('value', 'xxxx', 'yyyy');
className.value; // => "xxxx"
className.toString(); // => ".yyyy"
// Pass a value directly through to the css output without escaping it.
className.setPropertyWithoutEscape('value', '$REPLACE_ME$');
className.value; // => "$REPLACE_ME$"
className.toString(); // => ".$REPLACE_ME$"
// The biggest changes are to the Attribute class
// passing quoteMark explicitly is required to avoid a deprecation warning.
let attr = selectorParser.attribute({attribute: "id", operator: "=", value: "a-value", quoteMark: null});
attr.toString(); // => "[id=a-value]"
// Get the value with quotes on it and any necessary escapes.
// This is the same as reading attr.value in 3.x.
attr.getQuotedValue(); // => "a-value";
attr.quoteMark; // => null
// Add quotes to an attribute's value.
attr.quoteMark = "'"; // This is all that's required.
attr.toString(); // => "[id='a-value']"
attr.quoted; // => true
// The value is still the same, only the quotes have changed.
attr.value; // => a-value
attr.getQuotedValue(); // => "'a-value'";
// deprecated assignment, no warning because there's no escapes
attr.value = "new-value";
// no quote mark is needed so it is removed
attr.getQuotedValue(); // => "new-value";
// deprecated assignment,
attr.value = "\"a 'single quoted' value\"";
// > (node:27859) DeprecationWarning: Assigning an attribute a value containing characters that might need to be escaped is deprecated. Call attribute.setValue() instead.
attr.getQuotedValue(); // => '"a \'single quoted\' value"';
// quote mark inferred from first and last characters.
attr.quoteMark; // => '"'
// setValue takes options to make manipulating the value simple.
attr.setValue('foo', {smart: true});
// foo doesn't require any escapes or quotes.
attr.toString(); // => '[id=foo]'
attr.quoteMark; // => null
// An explicit quote mark can be specified
attr.setValue('foo', {quoteMark: '"'});
attr.toString(); // => '[id="foo"]'
// preserves quote mark by default
attr.setValue('bar');
attr.toString(); // => '[id="bar"]'
attr.quoteMark = null;
attr.toString(); // => '[id=bar]'
// with no arguments, it preserves quote mark even when it's not a great idea
attr.setValue('a value \n that should be quoted');
attr.toString(); // => '[id=a\\ value\\ \\A\\ that\\ should\\ be\\ quoted]'
// smart preservation with a specified default
attr.setValue('a value \n that should be quoted', {smart: true, preferCurrentQuoteMark: true, quoteMark: "'"});
// => "[id='a value \\A that should be quoted']"
attr.quoteMark = '"';
// => '[id="a value \\A that should be quoted"]'
// this keeps double quotes because it wants to quote the value and the existing value has double quotes.
attr.setValue('this should be quoted', {smart: true, preferCurrentQuoteMark: true, quoteMark: "'"});
// => '[id="this should be quoted"]'
// picks single quotes because the value has double quotes
attr.setValue('a "double quoted" value', {smart: true, preferCurrentQuoteMark: true, quoteMark: "'"});
// => "[id='a "double quoted" value']"
// setPropertyAndEscape lets you do anything you want. Even things that are a bad idea and illegal.
attr.setPropertyAndEscape('value', 'xxxx', 'the password is 42');
attr.value; // => "xxxx"
attr.toString(); // => "[id=the password is 42]"
// Pass a value directly through to the css output without escaping it.
attr.setPropertyWithoutEscape('value', '$REPLACEMENT$');
attr.value; // => "$REPLACEMENT$"
attr.toString(); // => "[id=$REPLACEMENT$]"
```
# 3.1.2
* Fix: Removed dot-prop dependency since it's no longer written in es5.
# 3.1.1
* Fix: typescript definitions weren't in the published package.
# 3.1.0
* Fixed numerous bugs in attribute nodes relating to the handling of comments
and whitespace. There's significant changes to `attrNode.spaces` and `attrNode.raws` since the `3.0.0` release.
* Added `Attribute#offsetOf(part)` to get the offset location of
attribute parts like `"operator"` and `"value"`. This is most
often added to `Attribute#sourceIndex` for error reporting.
# 3.0.0
## Breaking changes
* Some tweaks to the tokenizer/attribute selector parsing mean that whitespace
locations might be slightly different to the 2.x code.
* Better attribute selector parsing with more validation; postcss-selector-parser
no longer uses regular expressions to parse attribute selectors.
* Added an async API (thanks to @jacobp100); the default `process` API is now
async, and the sync API is now accessed through `processSync` instead.
* `process()` and `processSync()` now return a string instead of the Processor
instance.
* Tweaks handling of Less interpolation (thanks to @jwilsson).
* Removes support for Node 0.12.
## Other changes
* `ast()` and `astSync()` methods have been added to the `Processor`. These
return the `Root` node of the selectors after processing them.
* `transform()` and `transformSync()` methods have been added to the
`Processor`. These return the value returned by the processor callback
after processing the selectors.
* Set the parent when inserting a node (thanks to @chriseppstein).
* Correctly adjust indices when using insertBefore/insertAfter (thanks to @tivac).
* Fixes handling of namespaces with qualified tag selectors.
* `process`, `ast` and `transform` (and their sync variants) now accept a
`postcss` rule node. When provided, better errors are generated and selector
processing is automatically set back to the rule selector (unless the `updateSelector` option is set to `false`.)
* Now more memory efficient when tokenizing selectors.
### Upgrade hints
The pattern of:
`rule.selector = processor.process(rule.selector).result.toString();`
is now:
`processor.processSync(rule)`
# 2.2.3
* Resolves an issue where the parser would not reduce multiple spaces between an
ampersand and another simple selector in lossy mode (thanks to @adam-26).
# 2.2.2
* No longer hangs on an unescaped semicolon; instead the parser will throw
an exception for these cases.
# 2.2.1
* Allows a consumer to specify whitespace tokens when creating a new Node
(thanks to @Semigradsky).
# 2.2.0
* Added a new option to normalize whitespace when parsing the selector string
(thanks to @adam-26).
# 2.1.1
* Better unquoted value handling within attribute selectors
(thanks to @evilebottnawi).
# 2.1.0
* Added: Use string constants for all node types & expose them on the main
parser instance (thanks to @Aweary).
# 2.0.0
This release contains the following breaking changes:
* Renamed all `eachInside` iterators to `walk`. For example, `eachTag` is now
`walkTags`, and `eachInside` is now `walk`.
* Renamed `Node#removeSelf()` to `Node#remove()`.
* Renamed `Container#remove()` to `Container#removeChild()`.
* Renamed `Node#raw` to `Node#raws` (thanks to @davidtheclark).
* Now parses `&` as the *nesting* selector, rather than a *tag* selector.
* Fixes misinterpretation of Sass interpolation (e.g. `#{foo}`) as an
id selector (thanks to @davidtheclark).
and;
* Fixes parsing of attribute selectors with equals signs in them
(e.g. `[data-attr="foo=bar"]`) (thanks to @montmanu).
* Adds `quoted` and `raw.unquoted` properties to attribute nodes
(thanks to @davidtheclark).
# 1.3.3
* Fixes an infinite loop on `)` and `]` tokens when they had no opening pairs.
Now postcss-selector-parser will throw when it encounters these lone tokens.
# 1.3.2
* Now uses plain integers rather than `str.charCodeAt(0)` for compiled builds.
# 1.3.1
* Update flatten to v1.x (thanks to @shinnn).
# 1.3.0
* Adds a new node type, `String`, to fix a crash on selectors such as
`foo:bar("test")`.
# 1.2.1
* Fixes a crash when the parser encountered a trailing combinator.
# 1.2.0
* A more descriptive error is thrown when the parser expects to find a
pseudo-class/pseudo-element (thanks to @ashelley).
* Adds support for line/column locations for selector nodes, as well as a
`Node#sourceIndex` method (thanks to @davidtheclark).
# 1.1.4
* Fixes a crash when a selector started with a `>` combinator. The module will
now no longer throw if a selector has a leading/trailing combinator node.
# 1.1.3
* Fixes a crash on `@` tokens.
# 1.1.2
* Fixes an infinite loop caused by using parentheses in a non-pseudo element
context.
# 1.1.1
* Fixes a crash when a backslash ended a selector string.
# 1.1.0
* Adds support for replacing multiple nodes at once with `replaceWith`
(thanks to @jonathantneal).
* Parser no longer throws on sequential IDs and trailing commas, to support
parsing of selector hacks.
# 1.0.1
* Fixes using `insertAfter` and `insertBefore` during iteration.
# 1.0.0
* Adds `clone` and `replaceWith` methods to nodes.
* Adds `insertBefore` and `insertAfter` to containers.
* Stabilises API.
# 0.0.5
* Fixes crash on extra whitespace inside a pseudo selector's parentheses.
* Adds sort function to the container class.
* Enables the parser to pass its input through without transforming.
* Iteration-safe `each` and `eachInside`.
# 0.0.4
* Tidy up redundant duplication.
* Fixes a bug where the parser would loop infinitely on universal selectors
inside pseudo selectors.
* Adds `length` getter and `eachInside`, `map`, `reduce` to the container class.
* When a selector has been removed from the tree, the root node will no longer
cast it to a string.
* Adds node type iterators to the container class (e.g. `eachComment`).
* Adds filter function to the container class.
* Adds split function to the container class.
* Create new node types by doing `parser.id(opts)` etc.
* Adds support for pseudo classes anywhere in the selector.
# 0.0.3
* Adds `next` and `prev` to the node class.
* Adds `first` and `last` getters to the container class.
* Adds `every` and `some` iterators to the container class.
* Add `empty` alias for `removeAll`.
* Combinators are now types of node.
* Fixes the at method so that it is not an alias for `index`.
* Tidy up creation of new nodes in the parser.
* Refactors how namespaces are handled for consistency & less redundant code.
* Refactors AST to use `nodes` exclusively, and eliminates excessive nesting.
* Fixes nested pseudo parsing.
* Fixes whitespace parsing.
# 0.0.2
* Adds support for namespace selectors.
* Adds support for selectors joined by escaped spaces - such as `.\31\ 0`.
# 0.0.1
* Initial release.

View File

@@ -0,0 +1,22 @@
Copyright (c) Ben Briggs <beneb.info@gmail.com> (http://beneb.info)
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.

View File

@@ -0,0 +1,49 @@
# postcss-selector-parser [![Build Status](https://travis-ci.org/postcss/postcss-selector-parser.svg?branch=master)](https://travis-ci.org/postcss/postcss-selector-parser)
> Selector parser with built in methods for working with selector strings.
## Install
With [npm](https://npmjs.com/package/postcss-selector-parser) do:
```
npm install postcss-selector-parser
```
## Quick Start
```js
const parser = require('postcss-selector-parser');
const transform = selectors => {
selectors.walk(selector => {
// do something with the selector
console.log(String(selector))
});
};
const transformed = parser(transform).processSync('h1, h2, h3');
```
To normalize selector whitespace:
```js
const parser = require('postcss-selector-parser');
const normalized = parser().processSync('h1, h2, h3', {lossless: false});
// -> h1,h2,h3
```
Async support is provided through `parser.process` and will resolve a Promise
with the resulting selector string.
## API
Please see [API.md](API.md).
## Credits
* Huge thanks to Andrey Sitnik (@ai) for work on PostCSS which helped
accelerate this module's development.
## License
MIT

View File

@@ -0,0 +1,489 @@
"use strict";
var _process = _interopRequireDefault(require("process"));
var _attribute = _interopRequireDefault(require("../selectors/attribute"));
var _helpers = require("./util/helpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
_process["default"].throwDeprecation = true;
(0, _helpers.test)('attribute selector', '[href]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
t.falsy(tree.nodes[0].nodes[0].quoted);
});
(0, _helpers.test)('attribute selector spaces (before)', '[ href]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.before, ' ');
t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
t.falsy(tree.nodes[0].nodes[0].quoted);
});
(0, _helpers.test)('attribute selector spaces (after)', '[href ]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.after, ' ');
t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
t.falsy(tree.nodes[0].nodes[0].quoted);
});
(0, _helpers.test)('attribute selector spaces with namespace (both)', '[ foo|bar ]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].ns, 'foo');
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'bar');
t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.before, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.after, ' ');
t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
t.falsy(tree.nodes[0].nodes[0].quoted);
});
(0, _helpers.test)('attribute selector spaces (both)', '[ href ]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.before, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.after, ' ');
t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
t.falsy(tree.nodes[0].nodes[0].quoted);
});
(0, _helpers.test)('multiple attribute selectors', '[href][class][name]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[1].attribute, 'class');
t.deepEqual(tree.nodes[0].nodes[2].attribute, 'name');
});
(0, _helpers.test)('select elements with or without a namespace', '[*|href]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, '*');
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
});
(0, _helpers.test)('namespace with escapes', '[\\31 \\#\\32 |href]', function (t, tree) {
var attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.namespace, '1#2');
t.deepEqual(attr.raws.namespace, '\\31 \\#\\32 ');
attr.namespace = "foo";
t.deepEqual(attr.namespace, 'foo');
t.deepEqual(attr.raws.namespace, undefined);
attr.namespace = "1";
t.deepEqual(attr.namespace, '1');
t.deepEqual(attr.raws.namespace, '\\31');
});
(0, _helpers.test)('attribute selector with a empty value', '[href=""]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, '');
t["true"](tree.nodes[0].nodes[0].quoted);
});
(0, _helpers.test)('attribute selector with a value', '[name=james]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'name');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'james');
t.falsy(tree.nodes[0].nodes[0].quoted);
});
(0, _helpers.test)('attribute selector with quoted value', '[name="james"]', function (t, tree) {
var attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.attribute, 'name');
t.deepEqual(attr.operator, '=');
t.deepEqual(attr.value, 'james');
t.deepEqual(attr.quoteMark, '"');
t.truthy(attr.quoted);
t.deepEqual(attr.getQuotedValue(), '"james"');
});
(0, _helpers.test)('attribute selector with escaped quote', '[title="Something \\"weird\\""]', function (t, tree) {
var attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.value, 'Something "weird"');
t.deepEqual(attr.getQuotedValue(), '\"Something \\"weird\\"\"');
t.deepEqual(attr.getQuotedValue({
smart: true
}), '\'Something "weird"\'');
t.deepEqual(attr.getQuotedValue({
quoteMark: null
}), 'Something\\ \\"weird\\"');
t.deepEqual(attr.quoteMark, '"');
t.truthy(attr.quoted);
t.deepEqual(attr.raws.value, '"Something \\"weird\\""');
t.deepEqual(tree.toString(), '[title="Something \\"weird\\""]');
});
(0, _helpers.test)('attribute selector with escaped colon', '[ng\\:cloak]', function (t, tree) {
t.deepEqual(tree.toString(), '[ng\\:cloak]');
var attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.raws.attribute, 'ng\\:cloak');
t.deepEqual(attr.attribute, 'ng:cloak');
});
(0, _helpers.test)('attribute selector with short hex escape', '[ng\\3a cloak]', function (t, tree) {
t.deepEqual(tree.toString(), '[ng\\3a cloak]');
var attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.raws.attribute, 'ng\\3a cloak');
t.deepEqual(attr.attribute, 'ng:cloak');
});
(0, _helpers.test)('attribute selector with hex escape', '[ng\\00003acloak]', function (t, tree) {
t.deepEqual(tree.toString(), '[ng\\00003acloak]');
var attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.raws.attribute, 'ng\\00003acloak');
t.deepEqual(attr.attribute, 'ng:cloak');
});
(0, _helpers.test)('assign attribute name requiring escape', '[ng\\:cloak]', function (t, tree) {
var attr = tree.nodes[0].nodes[0];
attr.attribute = "ng:foo";
t.deepEqual(attr.raws.attribute, 'ng\\:foo');
t.deepEqual(attr.attribute, 'ng:foo');
t.deepEqual(tree.toString(), '[ng\\:foo]');
});
(0, _helpers.test)('multiple attribute selectors + combinator', '[href][class][name] h1 > h2', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[2].attribute, 'name');
t.deepEqual(tree.nodes[0].nodes[3].value, ' ');
t.deepEqual(tree.nodes[0].nodes[5].value, '>');
t.deepEqual(tree.nodes[0].nodes[6].value, 'h2');
});
(0, _helpers.test)('attribute, class, combinator', '[href] > h2.test', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[1].value, '>');
t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
t.deepEqual(tree.nodes[0].nodes[3].value, 'test');
});
(0, _helpers.test)('attribute selector with quoted value & combinator', '[name="james"] > h1', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'james');
t.deepEqual(tree.nodes[0].nodes[0].quoteMark, '"');
t.deepEqual(tree.nodes[0].nodes[1].value, '>');
t.deepEqual(tree.nodes[0].nodes[2].value, 'h1');
});
(0, _helpers.test)('multiple quoted attribute selectors', '[href*="test.com"][rel=\'external\'][id][class~="test"] > [name]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].value, 'test.com');
t.is(tree.nodes[0].nodes[0].quoteMark, '"');
t.deepEqual(tree.nodes[0].nodes[1].attribute, 'rel');
t.deepEqual(tree.nodes[0].nodes[1].value, 'external');
t.is(tree.nodes[0].nodes[1].quoteMark, "'");
t.deepEqual(tree.nodes[0].nodes[2].attribute, 'id');
t.falsy(tree.nodes[0].nodes[2].value, 'should not have a value');
t.is(tree.nodes[0].nodes[2].quoteMark, undefined, 'should not have a quoteMark set');
t.deepEqual(tree.nodes[0].nodes[3].attribute, 'class');
t.deepEqual(tree.nodes[0].nodes[3].value, 'test');
t.deepEqual(tree.nodes[0].nodes[3].quoteMark, '"');
t.deepEqual(tree.nodes[0].nodes[4].value, '>');
t.deepEqual(tree.nodes[0].nodes[5].attribute, 'name');
t.falsy(tree.nodes[0].nodes[5].value, 'should not have a value');
t.is(tree.nodes[0].nodes[5].quoteMark, undefined, 'should not have a quoteMark set');
});
(0, _helpers.test)('more attribute operators', '[href*=test],[href^=test],[href$=test],[href|=test]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
});
(0, _helpers.test)('attribute selector with quoted value containing "="', '[data-weird-attr="Something=weird"]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'Something=weird');
t.is(tree.nodes[0].nodes[0].quoteMark, '"');
t.deepEqual(tree.nodes[0].nodes[0].getQuotedValue(), '"Something=weird"');
});
var selector = '[data-weird-attr*="Something=weird"],' + '[data-weird-attr^="Something=weird"],' + '[data-weird-attr$="Something=weird"],' + '[data-weird-attr|="Something=weird"]';
(0, _helpers.test)('more attribute selector with quoted value containing "="', selector, function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'Something=weird');
t.deepEqual(tree.nodes[1].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
t.deepEqual(tree.nodes[1].nodes[0].value, 'Something=weird');
t.deepEqual(tree.nodes[2].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
t.deepEqual(tree.nodes[2].nodes[0].value, 'Something=weird');
t.deepEqual(tree.nodes[3].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
t.deepEqual(tree.nodes[3].nodes[0].value, 'Something=weird');
});
(0, _helpers.test)('attribute selector with quoted value containing multiple "="', '[data-weird-attr="Something=weird SomethingElse=weirder"]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'Something=weird SomethingElse=weirder');
});
selector = '[data-weird-attr*="Something=weird SomethingElse=weirder"],' + '[data-weird-attr^="Something=weird SomethingElse=weirder"],' + '[data-weird-attr$="Something=weird SomethingElse=weirder"],' + '[data-weird-attr|="Something=weird SomethingElse=weirder"]';
(0, _helpers.test)('more attribute selector with quoted value containing multiple "="', selector, function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'Something=weird SomethingElse=weirder');
t.deepEqual(tree.nodes[1].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
t.deepEqual(tree.nodes[1].nodes[0].value, 'Something=weird SomethingElse=weirder');
t.deepEqual(tree.nodes[2].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
t.deepEqual(tree.nodes[2].nodes[0].value, 'Something=weird SomethingElse=weirder');
t.deepEqual(tree.nodes[3].nodes[0].attribute, 'data-weird-attr');
t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
t.deepEqual(tree.nodes[3].nodes[0].value, 'Something=weird SomethingElse=weirder');
});
(0, _helpers.test)('multiple attribute selectors with quoted value containing "="', '[data-weird-foo="foo=weird"][data-weird-bar="bar=weird"]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo=weird');
t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[0].nodes[1].operator, '=');
t.deepEqual(tree.nodes[0].nodes[1].value, 'bar=weird');
});
(0, _helpers.test)('multiple attribute selectors with value containing escaped "="', '[data-weird-foo=foo\\=weird][data-weird-bar=bar\\3d weird]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo=weird');
t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[0].nodes[1].operator, '=');
t.deepEqual(tree.nodes[0].nodes[1].value, 'bar=weird');
});
selector = '[data-weird-foo*="foo2=weirder"][data-weird-bar*="bar2=weirder"],' + '[data-weird-foo^="foo2=weirder"][data-weird-bar^="bar2=weirder"],' + '[data-weird-foo$="foo2=weirder"][data-weird-bar$="bar2=weirder"],' + '[data-weird-foo|="foo2=weirder"][data-weird-bar|="bar2=weirder"]';
(0, _helpers.test)('more multiple attribute selectors with quoted value containing "="', selector, function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo2=weirder');
t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[0].nodes[1].operator, '*=');
t.deepEqual(tree.nodes[0].nodes[1].value, 'bar2=weirder');
t.deepEqual(tree.nodes[1].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
t.deepEqual(tree.nodes[1].nodes[0].value, 'foo2=weirder');
t.deepEqual(tree.nodes[1].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[1].nodes[1].operator, '^=');
t.deepEqual(tree.nodes[1].nodes[1].value, 'bar2=weirder');
t.deepEqual(tree.nodes[2].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
t.deepEqual(tree.nodes[2].nodes[0].value, 'foo2=weirder');
t.deepEqual(tree.nodes[2].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[2].nodes[1].operator, '$=');
t.deepEqual(tree.nodes[2].nodes[1].value, 'bar2=weirder');
t.deepEqual(tree.nodes[3].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
t.deepEqual(tree.nodes[3].nodes[0].value, 'foo2=weirder');
t.deepEqual(tree.nodes[3].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[3].nodes[1].operator, '|=');
t.deepEqual(tree.nodes[3].nodes[1].value, 'bar2=weirder');
});
(0, _helpers.test)('multiple attribute selectors with quoted value containing multiple "="', '[data-weird-foo="foo1=weirder foo2=weirder"][data-weird-bar="bar1=weirder bar2=weirder"]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo1=weirder foo2=weirder');
t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[0].nodes[1].operator, '=');
t.deepEqual(tree.nodes[0].nodes[1].value, 'bar1=weirder bar2=weirder');
});
selector = '[data-weird-foo*="foo1=weirder foo2=weirder"][data-weird-bar*="bar1=weirder bar2=weirder"],' + '[data-weird-foo^="foo1=weirder foo2=weirder"][data-weird-bar^="bar1=weirder bar2=weirder"],' + '[data-weird-foo$="foo1=weirder foo2=weirder"][data-weird-bar$="bar1=weirder bar2=weirder"],' + '[data-weird-foo|="foo1=weirder foo2=weirder"][data-weird-bar|="bar1=weirder bar2=weirder"]';
(0, _helpers.test)('more multiple attribute selectors with quoted value containing multiple "="', selector, function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo1=weirder foo2=weirder');
t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[0].nodes[1].operator, '*=');
t.deepEqual(tree.nodes[0].nodes[1].value, 'bar1=weirder bar2=weirder');
t.deepEqual(tree.nodes[1].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
t.deepEqual(tree.nodes[1].nodes[0].value, 'foo1=weirder foo2=weirder');
t.deepEqual(tree.nodes[1].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[1].nodes[1].operator, '^=');
t.deepEqual(tree.nodes[1].nodes[1].value, 'bar1=weirder bar2=weirder');
t.deepEqual(tree.nodes[2].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
t.deepEqual(tree.nodes[2].nodes[0].value, 'foo1=weirder foo2=weirder');
t.deepEqual(tree.nodes[2].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[2].nodes[1].operator, '$=');
t.deepEqual(tree.nodes[2].nodes[1].value, 'bar1=weirder bar2=weirder');
t.deepEqual(tree.nodes[3].nodes[0].attribute, 'data-weird-foo');
t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
t.deepEqual(tree.nodes[3].nodes[0].value, 'foo1=weirder foo2=weirder');
t.deepEqual(tree.nodes[3].nodes[1].attribute, 'data-weird-bar');
t.deepEqual(tree.nodes[3].nodes[1].operator, '|=');
t.deepEqual(tree.nodes[3].nodes[1].value, 'bar1=weirder bar2=weirder');
});
(0, _helpers.test)('spaces in attribute selectors', 'h1[ href *= "test" ]', function (t, tree) {
var attr = tree.nodes[0].nodes[1];
t.deepEqual(attr.attribute, 'href');
t.deepEqual(attr.spaces.attribute.before, ' ');
t.deepEqual(attr.spaces.attribute.after, ' ');
t.deepEqual(attr.operator, '*=');
t.deepEqual(attr.spaces.operator.after, ' ');
t.deepEqual(attr.value, 'test');
t.deepEqual(attr.spaces.value.after, ' ');
t.truthy(tree.nodes[0].nodes[1].quoted);
});
(0, _helpers.test)('insensitive attribute selector 1', '[href="test" i]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
});
(0, _helpers.test)('insensitive attribute selector with a empty value', '[href="" i]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, '');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
t["true"](tree.nodes[0].nodes[0].quoted);
});
(0, _helpers.test)('insensitive attribute selector 2', '[href=TEsT i ]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'TEsT');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
t.deepEqual(tree.nodes[0].nodes[0].spaces.value.after, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.insensitive.after, ' ');
});
(0, _helpers.test)('insensitive attribute selector 3', '[href=test i]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
});
(0, _helpers.test)('capitalized insensitive attribute selector 3', '[href=test I]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
});
(0, _helpers.test)('extraneous non-combinating whitespace', ' [href] , [class] ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.after, ' ');
t.deepEqual(tree.nodes[1].nodes[0].attribute, 'class');
t.deepEqual(tree.nodes[1].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[1].nodes[0].spaces.after, ' ');
});
(0, _helpers.test)('newline in attribute selector', '[class="woop \\\nwoop woop"]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'class');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'woop \nwoop woop');
t["true"](tree.nodes[0].nodes[0].quoted);
});
(0, _helpers.test)('comments within attribute selectors', '[href/* wow */=/* wow */test]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].raws.attribute, 'href/* wow */');
t.deepEqual(tree.nodes[0].nodes[0].raws.operator, '=/* wow */');
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
});
(0, _helpers.test)('comments within attribute selectors (2)', '[/* wow */href=test/* wow */]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].raws.spaces.attribute.before, '/* wow */');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'test/* wow */');
});
(0, _helpers.test)('comments within attribute selectors (3)', '[href=test/* wow */i]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].value, 'testi');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'test/* wow */i');
t.falsy(tree.nodes[0].nodes[0].insensitive);
});
(0, _helpers.test)('comments within attribute selectors (4)', '[ /*before*/ href /* after-attr */ = /* after-operator */ te/*inside-value*/st/* wow */ /*omg*/i/*bbq*/ /*whodoesthis*/]', function (t, tree) {
var attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.attribute, 'href');
t.deepEqual(attr.value, 'test');
t.deepEqual(attr.getQuotedValue(), 'test');
t.deepEqual(attr.raws.value, 'te/*inside-value*/st');
t.deepEqual(attr.raws.spaces.value.after, '/* wow */ /*omg*/');
t.truthy(attr.insensitive);
t.deepEqual(attr.offsetOf("attribute"), 13);
t.deepEqual(attr.offsetOf("operator"), 35);
t.deepEqual(attr.offsetOf("insensitive"), 95);
t.deepEqual(attr.raws.spaces.insensitive.after, '/*bbq*/ /*whodoesthis*/');
attr.value = "foo";
t.is(attr.raws.value, undefined);
});
(0, _helpers.test)('non standard modifiers', '[href="foo" y]', function (t, tree) {
var attr = tree.atPosition(1, 13);
t.deepEqual(attr.insensitive, false);
t.deepEqual(attr.insensitiveFlag, '');
t.deepEqual(attr.raws.insensitiveFlag, 'y');
t.deepEqual(tree.toString(), '[href="foo" y]');
});
(0, _helpers.test)('comment after insensitive(non space)', '[href="foo" i/**/]', function (t, tree) {
// https://github.com/postcss/postcss-selector-parser/issues/150
var attr = tree.atPosition(1, 13);
t.deepEqual(attr.insensitive, true);
t.deepEqual(attr.insensitiveFlag, 'i');
t.is(attr.raws.insensitiveFlag, undefined);
t.deepEqual(attr.raws.spaces.insensitive.after, '/**/');
t.deepEqual(tree.toString(), '[href="foo" i/**/]');
});
(0, _helpers.test)('comment after insensitive(space after)', '[href="foo" i/**/ ]', function (t, tree) {
var attr = tree.atPosition(1, 13);
t.deepEqual(attr.insensitive, true);
t.deepEqual(attr.insensitiveFlag, 'i');
t.deepEqual(attr.raws.spaces.insensitive.after, '/**/ ');
t.deepEqual(tree.toString(), '[href="foo" i/**/ ]');
});
(0, _helpers.test)('comment after insensitive(space before)', '[href="foo" i /**/]', function (t, tree) {
var attr = tree.atPosition(1, 13);
t.deepEqual(attr.insensitive, true);
t.deepEqual(attr.insensitiveFlag, 'i');
t.deepEqual(attr.raws.spaces.insensitive.after, ' /**/');
t.deepEqual(tree.toString(), '[href="foo" i /**/]');
});
var testDeprecation = (0, _helpers.nodeVersionAtLeast)('7.0.0') || (0, _helpers.nodeVersionBefore)('6.0.0') ? _helpers.test : _helpers.test.skip;
testDeprecation('deprecated constructor', '', function (t) {
t["throws"](function () {
return new _attribute["default"]({
value: '"foo"',
attribute: "data-bar"
});
}, {
message: "Constructing an Attribute selector with a value without specifying quoteMark is deprecated. Note: The value should be unescaped now."
});
});
testDeprecation('deprecated get of raws.unquoted ', '', function (t) {
t["throws"](function () {
var attr = new _attribute["default"]({
value: 'foo',
quoteMark: '"',
attribute: "data-bar"
});
return attr.raws.unquoted;
}, {
message: "attr.raws.unquoted is deprecated. Call attr.value instead."
});
});
testDeprecation('deprecated set of raws.unquoted ', '', function (t) {
t["throws"](function () {
var attr = new _attribute["default"]({
value: 'foo',
quoteMark: '"',
attribute: "data-bar"
});
attr.raws.unquoted = 'fooooo';
}, {
message: "Setting attr.raws.unquoted is deprecated and has no effect. attr.value is unescaped by default now."
});
});
testDeprecation('smart quotes', '[data-foo=bar]', function (t, tree) {
var attr = tree.nodes[0].nodes[0];
attr.setValue('changed', {
quoteMark: '"'
});
t.deepEqual(attr.toString(), '[data-foo="changed"]');
attr.setValue('changed again', {
quoteMark: "'",
preferCurrentQuoteMark: true
});
t.deepEqual(attr.toString(), '[data-foo="changed again"]');
attr.setValue('smart-ident', {
smart: true
});
t.deepEqual(attr.toString(), '[data-foo=smart-ident]');
attr.setValue('smart quoted', {
smart: true
});
t.deepEqual(attr.toString(), '[data-foo=smart\\ quoted]');
attr.setValue('smart quoted three spaces', {
smart: true
});
t.deepEqual(attr.toString(), '[data-foo="smart quoted three spaces"]');
attr.setValue('smart quoted three spaces', {
smart: true,
quoteMark: "'"
});
t.deepEqual(attr.toString(), "[data-foo='smart quoted three spaces']");
attr.setValue("smart with 'single quotes'", {
smart: true
});
t.deepEqual(attr.toString(), "[data-foo=\"smart with 'single quotes'\"]");
attr.setValue('smart with "double quotes"', {
smart: true
});
t.deepEqual(attr.toString(), "[data-foo='smart with \"double quotes\"']");
});
testDeprecation('set Attribute#quoteMark', '[data-foo=bar]', function (t, tree) {
var attr = tree.nodes[0].nodes[0];
attr.quoteMark = '"';
t.deepEqual(attr.toString(), '[data-foo="bar"]');
attr.quoteMark = "'";
t.deepEqual(attr.toString(), "[data-foo='bar']");
attr.quoteMark = null;
t.deepEqual(attr.toString(), "[data-foo=bar]");
attr.value = "has space";
t.deepEqual(attr.toString(), "[data-foo=has\\ space]");
attr.quoteMark = '"';
t.deepEqual(attr.toString(), '[data-foo="has space"]');
});

View File

@@ -0,0 +1,226 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('class name', '.one', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'one');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
});
(0, _helpers.test)('multiple class names', '.one.two.three', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'one');
t.deepEqual(tree.nodes[0].nodes[1].value, 'two');
t.deepEqual(tree.nodes[0].nodes[2].value, 'three');
});
(0, _helpers.test)('qualified class', 'button.btn-primary', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[1].type, 'class');
});
(0, _helpers.test)('escaped numbers in class name', '.\\31\\ 0', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].value, '1 0');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\31\\ 0');
});
(0, _helpers.test)('extraneous non-combinating whitespace', ' .h1 , .h2 ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.after, ' ');
t.deepEqual(tree.nodes[1].nodes[0].value, 'h2');
t.deepEqual(tree.nodes[1].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[1].nodes[0].spaces.after, ' ');
});
(0, _helpers.test)('Less interpolation within a class', '.foo@{bar}', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes.length, 1);
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo@{bar}');
});
(0, _helpers.test)('ClassName#set value', ".fo\\o", function (t, selectors) {
var className = selectors.first.first;
t.deepEqual(className.raws, {
value: "fo\\o"
});
className.value = "bar";
t.deepEqual(className.raws, {});
});
(0, _helpers.test)('escaped dot in class name', '.foo\\.bar', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo.bar');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'foo\\.bar');
});
(0, _helpers.test)('class selector with escaping', '.♥', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '♥');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
});
(0, _helpers.test)('class selector with escaping (1)', '.©', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '©');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
});
(0, _helpers.test)('class selector with escaping (2)', '.“‘’”', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '“‘’”');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
});
(0, _helpers.test)('class selector with escaping (3)', '.☺☃', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '☺☃');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
});
(0, _helpers.test)('class selector with escaping (4)', '.⌘⌥', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '⌘⌥');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
});
(0, _helpers.test)('class selector with escaping (5)', '.𝄞♪♩♫♬', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '𝄞♪♩♫♬');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
});
(0, _helpers.test)('class selector with escaping (6)', '.💩', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '💩');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
});
(0, _helpers.test)('class selector with escaping (7)', '.\\?', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '?');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\?');
});
(0, _helpers.test)('class selector with escaping (8)', '.\\@', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '@');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\@');
});
(0, _helpers.test)('class selector with escaping (9)', '.\\.', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '.');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\.');
});
(0, _helpers.test)('class selector with escaping (10)', '.\\3A \\)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ':)');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\3A \\)');
});
(0, _helpers.test)('class selector with escaping (11)', '.\\3A \\`\\(', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ':`(');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\3A \\`\\(');
});
(0, _helpers.test)('class selector with escaping (12)', '.\\31 23', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '123');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\31 23');
});
(0, _helpers.test)('class selector with escaping (13)', '.\\31 a2b3c', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '1a2b3c');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\31 a2b3c');
});
(0, _helpers.test)('class selector with escaping (14)', '.\\<p\\>', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '<p>');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\<p\\>');
});
(0, _helpers.test)('class selector with escaping (15)', '.\\<\\>\\<\\<\\<\\>\\>\\<\\>', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '<><<<>><>');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\<\\>\\<\\<\\<\\>\\>\\<\\>');
});
(0, _helpers.test)('class selector with escaping (16)', '.\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\[\\>\\+\\+\\+\\+\\+\\+\\+\\>\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\>\\+\\+\\+\\>\\+\\<\\<\\<\\<\\-\\]\\>\\+\\+\\.\\>\\+\\.\\+\\+\\+\\+\\+\\+\\+\\.\\.\\+\\+\\+\\.\\>\\+\\+\\.\\<\\<\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\.\\>\\.\\+\\+\\+\\.\\-\\-\\-\\-\\-\\-\\.\\-\\-\\-\\-\\-\\-\\-\\-\\.\\>\\+\\.\\>\\.', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\[\\>\\+\\+\\+\\+\\+\\+\\+\\>\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\>\\+\\+\\+\\>\\+\\<\\<\\<\\<\\-\\]\\>\\+\\+\\.\\>\\+\\.\\+\\+\\+\\+\\+\\+\\+\\.\\.\\+\\+\\+\\.\\>\\+\\+\\.\\<\\<\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\.\\>\\.\\+\\+\\+\\.\\-\\-\\-\\-\\-\\-\\.\\-\\-\\-\\-\\-\\-\\-\\-\\.\\>\\+\\.\\>\\.');
});
(0, _helpers.test)('class selector with escaping (17)', '.\\#', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '#');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\#');
});
(0, _helpers.test)('class selector with escaping (18)', '.\\#\\#', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '##');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\#\\#');
});
(0, _helpers.test)('class selector with escaping (19)', '.\\#\\.\\#\\.\\#', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '#.#.#');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\#\\.\\#\\.\\#');
});
(0, _helpers.test)('class selector with escaping (20)', '.\\_', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '_');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\_');
});
(0, _helpers.test)('class selector with escaping (21)', '.\\{\\}', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '{}');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\{\\}');
});
(0, _helpers.test)('class selector with escaping (22)', '.\\#fake\\-id', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '#fake-id');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\#fake\\-id');
});
(0, _helpers.test)('class selector with escaping (23)', '.foo\\.bar', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo.bar');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'foo\\.bar');
});
(0, _helpers.test)('class selector with escaping (24)', '.\\3A hover', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ':hover');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\3A hover');
});
(0, _helpers.test)('class selector with escaping (25)', '.\\3A hover\\3A focus\\3A active', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ':hover:focus:active');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\3A hover\\3A focus\\3A active');
});
(0, _helpers.test)('class selector with escaping (26)', '.\\[attr\\=value\\]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '[attr=value]');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\[attr\\=value\\]');
});
(0, _helpers.test)('class selector with escaping (27)', '.f\\/o\\/o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f/o/o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\/o\\/o');
});
(0, _helpers.test)('class selector with escaping (28)', '.f\\\\o\\\\o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f\\o\\o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\\\o\\\\o');
});
(0, _helpers.test)('class selector with escaping (29)', '.f\\*o\\*o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f*o*o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\*o\\*o');
});
(0, _helpers.test)('class selector with escaping (30)', '.f\\!o\\!o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f!o!o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\!o\\!o');
});
(0, _helpers.test)('class selector with escaping (31)', '.f\\\'o\\\'o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f\'o\'o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\\'o\\\'o');
});
(0, _helpers.test)('class selector with escaping (32)', '.f\\~o\\~o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f~o~o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\~o\\~o');
});
(0, _helpers.test)('class selector with escaping (33)', '.f\\+o\\+o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f+o+o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\+o\\+o');
});
(0, _helpers.test)('class selector with escaping (34)', '.\\1D306', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '𝌆');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\1D306');
});
(0, _helpers.test)('class selector with escaping (35)', '.not-pseudo\\:focus', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'not-pseudo:focus');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'not-pseudo\\:focus');
});
(0, _helpers.test)('class selector with escaping (36)', '.not-pseudo\\:\\:focus', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'not-pseudo::focus');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'not-pseudo\\:\\:focus');
});

View File

@@ -0,0 +1,148 @@
"use strict";
var _types = require("../selectors/types");
var _helpers = require("./util/helpers");
(0, _helpers.test)('multiple combinating spaces', 'h1 h2', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[1].value, ' ');
t.deepEqual(tree.nodes[0].nodes[1].toString(), ' ');
t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
});
(0, _helpers.test)('column combinator', '.selected||td', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'selected');
t.deepEqual(tree.nodes[0].nodes[1].value, '||');
t.deepEqual(tree.nodes[0].nodes[2].value, 'td');
});
(0, _helpers.test)('column combinator (2)', '.selected || td', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'selected');
t.deepEqual(tree.nodes[0].nodes[1].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[1].value, '||');
t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
t.deepEqual(tree.nodes[0].nodes[2].value, 'td');
});
(0, _helpers.test)('descendant combinator', 'h1 h2', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[1].value, ' ');
t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
});
(0, _helpers.test)('multiple descendant combinators', 'h1 h2 h3 h4', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].value, ' ', 'should have a combinator');
t.deepEqual(tree.nodes[0].nodes[3].value, ' ', 'should have a combinator');
t.deepEqual(tree.nodes[0].nodes[5].value, ' ', 'should have a combinator');
});
(0, _helpers.test)('adjacent sibling combinator', 'h1~h2', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[1].value, '~');
t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
});
(0, _helpers.test)('adjacent sibling combinator (2)', 'h1 ~h2', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[1].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[1].value, '~');
t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
});
(0, _helpers.test)('adjacent sibling combinator (3)', 'h1~ h2', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[1].value, '~');
t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
});
(0, _helpers.test)('adjacent sibling combinator (4)', 'h1 ~ h2', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[1].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[1].value, '~');
t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
});
(0, _helpers.test)('adjacent sibling combinator (5)', 'h1~h2~h3', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[1].value, '~');
t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
t.deepEqual(tree.nodes[0].nodes[3].value, '~');
t.deepEqual(tree.nodes[0].nodes[4].value, 'h3');
});
(0, _helpers.test)('piercing combinator', '.a >>> .b', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'a');
t.deepEqual(tree.nodes[0].nodes[1].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[1].value, '>>>');
t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
t.deepEqual(tree.nodes[0].nodes[2].value, 'b');
});
(0, _helpers.test)('named combinators', 'a /deep/ b', function (t, tree) {
var nodes = tree.nodes[0].nodes;
t.deepEqual(nodes[0].value, 'a');
t.deepEqual(nodes[1].type, _types.COMBINATOR);
t.deepEqual(nodes[1].toString(), ' /deep/ ');
t.deepEqual(nodes[1].value, '/deep/');
t.deepEqual(nodes[2].value, 'b');
});
(0, _helpers.test)('named combinators with escapes', 'a /dee\\p/ b', function (t, tree) {
var nodes = tree.nodes[0].nodes;
t.deepEqual(nodes[0].value, 'a');
t.deepEqual(nodes[1].type, _types.COMBINATOR);
t.deepEqual(nodes[1].toString(), ' /dee\\p/ ');
t.deepEqual(nodes[1].value, '/deep/');
t.deepEqual(nodes[2].value, 'b');
});
(0, _helpers.test)('named combinators with escapes and uppercase', 'a /DeE\\p/ b', function (t, tree) {
var nodes = tree.nodes[0].nodes;
t.deepEqual(nodes[0].value, 'a');
t.deepEqual(nodes[1].type, _types.COMBINATOR);
t.deepEqual(nodes[1].toString(), ' /DeE\\p/ ');
t.deepEqual(nodes[1].value, '/deep/');
t.deepEqual(nodes[2].value, 'b');
});
(0, _helpers.test)('multiple combinators', 'h1~h2>h3', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].value, '~', 'should have a combinator');
t.deepEqual(tree.nodes[0].nodes[3].value, '>', 'should have a combinator');
});
(0, _helpers.test)('multiple combinators with whitespaces', 'h1 + h2 > h3', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].value, '+', 'should have a combinator');
t.deepEqual(tree.nodes[0].nodes[3].value, '>', 'should have a combinator');
});
(0, _helpers.test)('multiple combinators with whitespaces (2)', 'h1+ h2 >h3', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].value, '+', 'should have a combinator');
t.deepEqual(tree.nodes[0].nodes[3].value, '>', 'should have a combinator');
});
(0, _helpers.test)('trailing combinator & spaces', 'p + ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'p', 'should be a paragraph');
t.deepEqual(tree.nodes[0].nodes[1].value, '+', 'should have a combinator');
});
(0, _helpers.test)('trailing sibling combinator', 'p ~', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'p', 'should be a paragraph');
t.deepEqual(tree.nodes[0].nodes[1].value, '~', 'should have a combinator');
});
(0, _helpers.test)('ending in comment has no trailing combinator', ".bar /* comment 3 */", function (t, tree) {
var nodeTypes = tree.nodes[0].map(function (n) {
return n.type;
});
t.deepEqual(nodeTypes, ["class"]);
});
(0, _helpers.test)('The combinating space is not a space character', ".bar\n.baz", function (t, tree) {
var nodeTypes = tree.nodes[0].map(function (n) {
return n.type;
});
t.deepEqual(nodeTypes, ["class", "combinator", "class"]);
t.deepEqual(tree.nodes[0].nodes[1].value, ' ', 'should have a combinator');
t.deepEqual(tree.nodes[0].nodes[1].raws.value, '\n', 'should have a raw combinator value');
});
(0, _helpers.test)('with spaces and a comment has only one combinator', ".bar /* comment 3 */ > .foo", function (t, tree) {
var nodeTypes = tree.nodes[0].map(function (n) {
return n.type;
});
t.deepEqual(nodeTypes, ["class", "combinator", "class"]);
});
(0, _helpers.test)('with a meaningful comment in the middle of a compound selector', "div/* wtf */.foo", function (t, tree) {
var nodeTypes = tree.nodes[0].map(function (n) {
return n.type;
});
t.deepEqual(nodeTypes, ["tag", "comment", "class"]);
});
(0, _helpers.test)('with a comment in the middle of a descendant selector', "div/* wtf */ .foo", function (t, tree) {
var nodeTypes = tree.nodes[0].map(function (n) {
return n.type;
});
t.deepEqual(nodeTypes, ["tag", "comment", "combinator", "class"]);
});

View File

@@ -0,0 +1,81 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('comments', '/*test comment*/h2', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '/*test comment*/');
t.deepEqual(tree.nodes[0].nodes[1].value, 'h2');
});
(0, _helpers.test)('comments (2)', '.a /*test comment*/label', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[1].type, 'combinator');
t.deepEqual(tree.nodes[0].nodes[1].value, ' ');
t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
t.deepEqual(tree.nodes[0].nodes[1].rawSpaceAfter, ' /*test comment*/');
t.deepEqual(tree.nodes[0].nodes[2].type, 'tag');
});
(0, _helpers.test)('comments (3)', '.a /*test comment*/ label', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[1].type, 'combinator');
t.deepEqual(tree.nodes[0].nodes[1].value, ' ');
t.deepEqual(tree.nodes[0].nodes[1].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[1].rawSpaceBefore, ' /*test comment*/ ');
t.deepEqual(tree.nodes[0].nodes[2].type, 'tag');
});
(0, _helpers.test)('multiple comments and other things', 'h1/*test*/h2/*test*/.test/*test*/', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag', 'should have a tag');
t.deepEqual(tree.nodes[0].nodes[1].type, 'comment', 'should have a comment');
t.deepEqual(tree.nodes[0].nodes[2].type, 'tag', 'should have a tag');
t.deepEqual(tree.nodes[0].nodes[3].type, 'comment', 'should have a comment');
t.deepEqual(tree.nodes[0].nodes[4].type, 'class', 'should have a class name');
t.deepEqual(tree.nodes[0].nodes[5].type, 'comment', 'should have a comment');
});
(0, _helpers.test)('ending in comment', ".bar /* comment 3 */", function (t, tree) {
var classname = tree.nodes[0].nodes[0];
t.deepEqual(classname.type, 'class', 'should have a tag');
t.deepEqual(classname.spaces.after, ' ');
t.deepEqual(classname.raws.spaces.after, ' /* comment 3 */');
});
(0, _helpers.test)('comments in selector list', 'h2, /*test*/ h4', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].value, 'h2');
t.deepEqual(tree.nodes[1].nodes[0].rawSpaceBefore, ' ');
t.deepEqual(tree.nodes[1].nodes[0].type, 'comment');
t.deepEqual(tree.nodes[1].nodes[0].value, '/*test*/');
t.deepEqual(tree.nodes[1].nodes[1].rawSpaceBefore, ' ');
t.deepEqual(tree.nodes[1].nodes[1].type, 'tag');
t.deepEqual(tree.nodes[1].nodes[1].value, 'h4');
});
(0, _helpers.test)('comments in selector list (2)', 'h2,/*test*/h4', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].value, 'h2');
t.deepEqual(tree.nodes[1].nodes[0].rawSpaceBefore, '');
t.deepEqual(tree.nodes[1].nodes[0].type, 'comment');
t.deepEqual(tree.nodes[1].nodes[0].value, '/*test*/');
t.deepEqual(tree.nodes[1].nodes[1].type, 'tag');
t.deepEqual(tree.nodes[1].nodes[1].value, 'h4');
t.deepEqual(tree.nodes[1].nodes[1].rawSpaceBefore, '');
});
(0, _helpers.test)('comments in selector list (3)', 'h2/*test*/, h4', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].value, 'h2');
t.deepEqual(tree.nodes[0].nodes[1].rawSpaceBefore, '');
t.deepEqual(tree.nodes[0].nodes[1].type, 'comment');
t.deepEqual(tree.nodes[0].nodes[1].value, '/*test*/');
t.deepEqual(tree.nodes[1].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[1].nodes[0].value, 'h4');
t.deepEqual(tree.nodes[1].nodes[0].rawSpaceBefore, ' ');
});
(0, _helpers.test)('comments in selector list (4)', 'h2, /*test*/ /*test*/ h4', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].value, 'h2');
t.deepEqual(tree.nodes[1].nodes[0].rawSpaceBefore, ' ');
t.deepEqual(tree.nodes[1].nodes[0].type, 'comment');
t.deepEqual(tree.nodes[1].nodes[0].value, '/*test*/');
t.deepEqual(tree.nodes[1].nodes[1].rawSpaceBefore, ' ');
t.deepEqual(tree.nodes[1].nodes[1].type, 'comment');
t.deepEqual(tree.nodes[1].nodes[1].value, '/*test*/');
t.deepEqual(tree.nodes[1].nodes[2].rawSpaceBefore, ' ');
t.deepEqual(tree.nodes[1].nodes[2].type, 'tag');
t.deepEqual(tree.nodes[1].nodes[2].value, 'h4');
});

View File

@@ -0,0 +1,393 @@
"use strict";
var _ava = _interopRequireDefault(require("ava"));
var _ = _interopRequireDefault(require(".."));
var _helpers = require("./util/helpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
(0, _ava["default"])('container#append', function (t) {
var out = (0, _helpers.parse)('h1', function (selectors) {
var selector = selectors.first;
var clone = selector.first.clone({
value: 'h2'
});
selectors.append(clone);
});
t.deepEqual(out, 'h1,h2');
});
(0, _ava["default"])('container#prepend', function (t) {
var out = (0, _helpers.parse)('h2', function (selectors) {
var selector = selectors.first;
var clone = selector.first.clone({
value: 'h1'
});
selectors.prepend(clone);
});
t.deepEqual(out, 'h1,h2');
});
(0, _ava["default"])('container#each', function (t) {
var str = '';
(0, _helpers.parse)('h1, h2:not(h3, h4)', function (selectors) {
selectors.each(function (selector) {
if (selector.first.type === 'tag') {
str += selector.first.value;
}
});
});
t.deepEqual(str, 'h1h2');
});
(0, _ava["default"])('container#each (safe iteration)', function (t) {
var out = (0, _helpers.parse)('.x, .y', function (selectors) {
selectors.each(function (selector) {
selector.parent.insertBefore(selector, _["default"].className({
value: 'b'
}));
selector.parent.insertAfter(selector, _["default"].className({
value: 'a'
}));
});
});
t.deepEqual(out, '.b,.x,.a,.b, .y,.a');
});
(0, _ava["default"])('container#each (early exit)', function (t) {
var str = '';
(0, _helpers.parse)('h1, h2, h3, h4', function (selectors) {
var eachReturn = selectors.each(function (selector) {
var tag = selector.first.value;
str += tag;
return tag !== 'h2';
});
t["false"](eachReturn);
});
t.deepEqual(str, 'h1h2');
});
(0, _ava["default"])('container#walk', function (t) {
var str = '';
(0, _helpers.parse)('h1, h2:not(h3, h4)', function (selectors) {
selectors.walk(function (selector) {
if (selector.type === 'tag') {
str += selector.value;
}
});
});
t.deepEqual(str, 'h1h2h3h4');
});
(0, _ava["default"])('container#walk (safe iteration)', function (t) {
var out = (0, _helpers.parse)('[class] + *[href] *:not(*.green)', function (selectors) {
selectors.walkUniversals(function (selector) {
var next = selector.next();
if (next && next.type !== 'combinator') {
selector.remove();
}
});
});
t.deepEqual(out, '[class] + [href] :not(.green)');
});
(0, _ava["default"])('container#walk (early exit)', function (t) {
var str = '';
(0, _helpers.parse)('h1, h2:not(h3, h4)', function (selectors) {
var walkReturn = selectors.walk(function (selector) {
if (selector.type === 'tag') {
var tag = selector.value;
str += tag;
return tag !== 'h3';
}
});
t["false"](walkReturn);
});
t.deepEqual(str, 'h1h2h3');
});
(0, _ava["default"])('container#walkAttribute', function (t) {
var out = (0, _helpers.parse)('[href][class].class', function (selectors) {
selectors.walkAttributes(function (attr) {
if (attr.attribute === 'class') {
attr.remove();
}
});
});
t.deepEqual(out, '[href].class');
});
(0, _ava["default"])('container#walkClass', function (t) {
var out = (0, _helpers.parse)('.one, .two, .three:not(.four, .five)', function (selectors) {
selectors.walkClasses(function (className) {
className.value = className.value.slice(0, 1);
});
});
t.deepEqual(out, '.o, .t, .t:not(.f, .f)');
});
(0, _ava["default"])('container#walkCombinator', function (t) {
var out = (0, _helpers.parse)('h1 h2 h3 h4', function (selectors) {
selectors.walkCombinators(function (comment) {
comment.remove();
});
});
t.deepEqual(out, 'h1h2h3h4');
});
(0, _ava["default"])('container#walkComment', function (t) {
var out = (0, _helpers.parse)('.one/*test*/.two', function (selectors) {
selectors.walkComments(function (comment) {
comment.remove();
});
});
t.deepEqual(out, '.one.two');
});
(0, _ava["default"])('container#walkId', function (t) {
var out = (0, _helpers.parse)('h1#one, h2#two', function (selectors) {
selectors.walkIds(function (id) {
id.value = id.value.slice(0, 1);
});
});
t.deepEqual(out, 'h1#o, h2#t');
});
(0, _ava["default"])('container#walkNesting', function (t) {
var out = (0, _helpers.parse)('& h1', function (selectors) {
selectors.walkNesting(function (node) {
node.replaceWith(_["default"].tag({
value: 'body'
}));
});
});
t.deepEqual(out, 'body h1');
});
(0, _ava["default"])('container#walkPseudo', function (t) {
var out = (0, _helpers.parse)('a:before, a:after', function (selectors) {
selectors.walkPseudos(function (pseudo) {
pseudo.value = pseudo.value.slice(0, 2);
});
});
t.deepEqual(out, 'a:b, a:a');
});
(0, _ava["default"])('container#walkTag', function (t) {
var out = (0, _helpers.parse)('1 2 3', function (selectors) {
selectors.walkTags(function (tag) {
tag.value = 'h' + tag.value;
});
});
t.deepEqual(out, 'h1 h2 h3');
});
(0, _ava["default"])('container#walkUniversal', function (t) {
var out = (0, _helpers.parse)('*.class,*.class,*.class', function (selectors) {
selectors.walkUniversals(function (universal) {
universal.remove();
});
});
t.deepEqual(out, '.class,.class,.class');
});
(0, _ava["default"])('container#map', function (t) {
(0, _helpers.parse)('1 2 3', function (selectors) {
var arr = selectors.first.map(function (selector) {
if (/[0-9]/.test(selector.value)) {
return 'h' + selector.value;
}
return selector.value;
});
t.deepEqual(arr, ['h1', ' ', 'h2', ' ', 'h3']);
});
});
(0, _ava["default"])('container#every', function (t) {
(0, _helpers.parse)('.one.two.three', function (selectors) {
var allClasses = selectors.first.every(function (selector) {
return selector.type === 'class';
});
t.truthy(allClasses);
});
});
(0, _ava["default"])('container#some', function (t) {
(0, _helpers.parse)('one#two.three', function (selectors) {
var someClasses = selectors.first.some(function (selector) {
return selector.type === 'class';
});
t.truthy(someClasses);
});
});
(0, _ava["default"])('container#reduce', function (t) {
(0, _helpers.parse)('h1, h2, h3, h4', function (selectors) {
var str = selectors.reduce(function (memo, selector) {
if (selector.first.type === 'tag') {
memo += selector.first.value;
}
return memo;
}, '');
t.deepEqual(str, 'h1h2h3h4');
});
});
(0, _ava["default"])('container#filter', function (t) {
(0, _helpers.parse)('h1, h2, c1, c2', function (selectors) {
var ast = selectors.filter(function (selector) {
return ~selector.first.value.indexOf('h');
});
t.deepEqual(String(ast), 'h1, h2');
});
});
(0, _ava["default"])('container#split', function (t) {
(0, _helpers.parse)('h1 h2 >> h3', function (selectors) {
var list = selectors.first.split(function (selector) {
return selector.value === '>>';
}).map(function (group) {
return group.map(String);
});
t.deepEqual(list, [['h1', ' ', 'h2', ' >> '], ['h3']]);
t.deepEqual(list.length, 2);
});
});
(0, _ava["default"])('container#sort', function (t) {
var out = (0, _helpers.parse)('h2,h3,h1,h4', function (selectors) {
selectors.sort(function (a, b) {
return a.first.value.slice(-1) - b.first.value.slice(-1);
});
});
t.deepEqual(out, 'h1,h2,h3,h4');
});
(0, _ava["default"])('container#at', function (t) {
(0, _helpers.parse)('h1, h2, h3', function (selectors) {
t.deepEqual(selectors.at(1).first.value, 'h2');
});
});
(0, _ava["default"])('container#first, container#last', function (t) {
(0, _helpers.parse)('h1, h2, h3, h4', function (selectors) {
t.deepEqual(selectors.first.first.value, 'h1');
t.deepEqual(selectors.last.last.value, 'h4');
});
});
(0, _ava["default"])('container#index', function (t) {
(0, _helpers.parse)('h1 h2 h3', function (selectors) {
var middle = selectors.first.at(1);
t.deepEqual(selectors.first.index(middle), 1);
});
});
(0, _ava["default"])('container#length', function (t) {
(0, _helpers.parse)('h1, h2, h3', function (selectors) {
t.deepEqual(selectors.length, 3);
});
});
(0, _ava["default"])('container#removeChild', function (t) {
var out = (0, _helpers.parse)('h1.class h2.class h3.class', function (selectors) {
selectors.walk(function (selector) {
if (selector.type === 'class') {
selector.parent.removeChild(selector);
}
});
});
t.deepEqual(out, 'h1 h2 h3');
});
(0, _ava["default"])('container#removeAll, container#empty', function (t) {
var wipe = function wipe(method) {
return function (selectors) {
return selectors[method]();
};
};
var out1 = (0, _helpers.parse)('h1 h2, h2 h3, h3 h4', wipe('empty'));
var out2 = (0, _helpers.parse)('h1 h2, h2 h3, h3 h4', wipe('removeAll'));
t.deepEqual(out1, '');
t.deepEqual(out2, '');
});
(0, _ava["default"])('container#insertBefore', function (t) {
var out = (0, _helpers.parse)('h2', function (selectors) {
var selector = selectors.first;
var clone = selector.first.clone({
value: 'h1'
});
selectors.insertBefore(selector, clone);
});
t.deepEqual(out, 'h1,h2');
});
(0, _ava["default"])('container#insertBefore and node#remove', function (t) {
var out = (0, _helpers.parse)('h2', function (selectors) {
var selector = selectors.first;
var newSel = _["default"].tag({
value: 'h1'
});
selectors.insertBefore(selector, newSel);
newSel.remove();
});
t.deepEqual(out, 'h2');
});
(0, _ava["default"])('container#insertAfter', function (t) {
var out = (0, _helpers.parse)('h1', function (selectors) {
var selector = selectors.first;
var clone = selector.first.clone({
value: 'h2'
});
selectors.insertAfter(selector, clone);
});
t.deepEqual(out, 'h1,h2');
});
(0, _ava["default"])('container#insertAfter and node#remove', function (t) {
var out = (0, _helpers.parse)('h2', function (selectors) {
var selector = selectors.first;
var newSel = _["default"].tag({
value: 'h1'
});
selectors.insertAfter(selector, newSel);
newSel.remove();
});
t.deepEqual(out, 'h2');
});
(0, _ava["default"])('container#insertAfter (during iteration)', function (t) {
var out = (0, _helpers.parse)('h1, h2, h3', function (selectors) {
selectors.walkTags(function (selector) {
var attribute = _["default"].attribute({
attribute: 'class'
});
selector.parent.insertAfter(selector, attribute);
});
});
t.deepEqual(out, 'h1[class], h2[class], h3[class]');
});
(0, _ava["default"])('Container#atPosition first pseudo', function (t) {
(0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
var node = root.atPosition(1, 1);
t.deepEqual(node.type, "pseudo");
t.deepEqual(node.toString(), ":not(.foo)");
});
});
(0, _ava["default"])('Container#atPosition class in pseudo', function (t) {
(0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
var node = root.atPosition(1, 6);
t.deepEqual(node.type, "class");
t.deepEqual(node.toString(), ".foo");
});
});
(0, _ava["default"])('Container#atPosition id in second selector', function (t) {
(0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
var node = root.atPosition(2, 1);
t.deepEqual(node.type, "id");
t.deepEqual(node.toString(), "\n#foo");
});
});
(0, _ava["default"])('Container#atPosition combinator in second selector', function (t) {
(0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
var node = root.atPosition(2, 6);
t.deepEqual(node.type, "combinator");
t.deepEqual(node.toString(), " > ");
var nodeSpace = root.atPosition(2, 5);
t.deepEqual(nodeSpace.type, "selector");
t.deepEqual(nodeSpace.toString(), "\n#foo > :matches(ol, ul)");
});
});
(0, _ava["default"])('Container#atPosition tag in second selector pseudo', function (t) {
(0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
var node = root.atPosition(2, 17);
t.deepEqual(node.type, "tag");
t.deepEqual(node.toString(), "ol");
});
});
(0, _ava["default"])('Container#atPosition comma in second selector pseudo', function (t) {
(0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
var node = root.atPosition(2, 19);
t.deepEqual(node.type, "pseudo");
t.deepEqual(node.toString(), ":matches(ol, ul)");
});
});

View File

@@ -0,0 +1,19 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('escaped semicolon in class', '.\\;', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ';');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\;');
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
});
(0, _helpers.test)('escaped semicolon in id', '#\\;', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ';');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\;');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
}); // This is a side-effect of allowing media queries to be parsed. Not sure it shouldn't just be an error.
(0, _helpers.test)('bare parens capture contents as a string', '(h1)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '(h1)');
t.deepEqual(tree.nodes[0].nodes[0].type, 'string');
});

View File

@@ -0,0 +1,24 @@
"use strict";
var _helpers = require("./util/helpers");
// Unclosed elements
(0, _helpers["throws"])('unclosed string', 'a[href="wow]');
(0, _helpers["throws"])('unclosed comment', '/* oops');
(0, _helpers["throws"])('unclosed pseudo element', 'button::');
(0, _helpers["throws"])('unclosed pseudo class', 'a:');
(0, _helpers["throws"])('unclosed attribute selector', '[name="james"][href');
(0, _helpers["throws"])('no opening parenthesis', ')');
(0, _helpers["throws"])('no opening parenthesis (2)', ':global.foo)');
(0, _helpers["throws"])('no opening parenthesis (3)', 'h1:not(h2:not(h3)))');
(0, _helpers["throws"])('no opening square bracket', ']');
(0, _helpers["throws"])('no opening square bracket (2)', ':global.foo]');
(0, _helpers["throws"])('no opening square bracket (3)', '[global]]');
(0, _helpers["throws"])('bad pseudo element', 'button::"after"');
(0, _helpers["throws"])('missing closing parenthesis in pseudo', ':not([attr="test"]:not([attr="test"])');
(0, _helpers["throws"])('bad syntax', '-moz-osx-font-smoothing: grayscale');
(0, _helpers["throws"])('bad syntax (2)', '! .body');
(0, _helpers["throws"])('missing backslash for semicolon', '.;');
(0, _helpers["throws"])('missing backslash for semicolon (2)', '.\;');
(0, _helpers["throws"])('unexpected / foo', '-Option\/root', "Unexpected '/'. Escaping special characters with \\ may help.");
(0, _helpers["throws"])('bang in selector', '.foo !optional', "Unexpected '!'. Escaping special characters with \\ may help.");

View File

@@ -0,0 +1,127 @@
"use strict";
var _ = _interopRequireDefault(require("../"));
var _helpers = require("./util/helpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var node = function node(tree, n) {
if (n === void 0) {
n = 0;
}
return tree.nodes[0].nodes[n];
};
(0, _helpers.test)('attribute guard', '[foo]', function (t, tree) {
var n = node(tree);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isAttribute(undefined));
t["true"](_["default"].isAttribute(n));
t["false"](_["default"].isContainer(n));
t["true"](_["default"].isNamespace(n));
});
(0, _helpers.test)('className guard', '.foo', function (t, tree) {
var n = node(tree);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isClassName(undefined));
t["true"](_["default"].isClassName(n));
t["false"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
(0, _helpers.test)('combinator guard', '.foo > .bar', function (t, tree) {
var n = node(tree, 1);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isCombinator(undefined));
t["true"](_["default"].isCombinator(n));
t["false"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
(0, _helpers.test)('comment guard', '/* foo */.foo > .bar', function (t, tree) {
var n = node(tree);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isComment(undefined));
t["true"](_["default"].isComment(n));
t["false"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
(0, _helpers.test)('id guard', '#ident', function (t, tree) {
var n = node(tree);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isIdentifier(undefined));
t["true"](_["default"].isIdentifier(n));
t["false"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
(0, _helpers.test)('nesting guard', '&.foo', function (t, tree) {
var n = node(tree);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isNesting(undefined));
t["true"](_["default"].isNesting(n));
t["false"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
(0, _helpers.test)('pseudo class guard', ':hover', function (t, tree) {
var n = node(tree);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isPseudo(undefined));
t["true"](_["default"].isPseudo(n));
t["true"](_["default"].isPseudoClass(n));
t["false"](_["default"].isPseudoElement(n));
t["true"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
(0, _helpers.test)('pseudo element guard', '::first-line', function (t, tree) {
var n = node(tree);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isPseudo(undefined));
t["true"](_["default"].isPseudo(n));
t["false"](_["default"].isPseudoClass(n));
t["true"](_["default"].isPseudoElement(n));
t["true"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
(0, _helpers.test)('special pseudo element guard', ':before:after', function (t, tree) {
[node(tree), node(tree, 1)].forEach(function (n) {
t["true"](_["default"].isPseudo(n));
t["false"](_["default"].isPseudoClass(n));
t["true"](_["default"].isPseudoElement(n));
t["true"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
});
(0, _helpers.test)('special pseudo element guard (uppercase)', ':BEFORE:AFTER', function (t, tree) {
[node(tree), node(tree, 1)].forEach(function (n) {
t["true"](_["default"].isPseudo(n));
t["false"](_["default"].isPseudoClass(n));
t["true"](_["default"].isPseudoElement(n));
t["true"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
});
(0, _helpers.test)('string guard', '"string"', function (t, tree) {
var n = node(tree);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isString(undefined));
t["true"](_["default"].isString(n));
t["false"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});
(0, _helpers.test)('tag guard', 'h1', function (t, tree) {
var n = node(tree);
t["false"](_["default"].isNode(undefined));
t["true"](_["default"].isNode(n));
t["false"](_["default"].isTag(undefined));
t["true"](_["default"].isTag(n));
t["false"](_["default"].isContainer(n));
t["true"](_["default"].isNamespace(n));
});
(0, _helpers.test)('universal guard', '*', function (t, tree) {
var n = node(tree);
t["true"](_["default"].isNode(n));
t["false"](_["default"].isUniversal(undefined));
t["true"](_["default"].isUniversal(n));
t["false"](_["default"].isContainer(n));
t["false"](_["default"].isNamespace(n));
});

View File

@@ -0,0 +1,235 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('id selector', '#one', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'one');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
});
(0, _helpers.test)('id selector with universal', '*#z98y ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
t.deepEqual(tree.nodes[0].nodes[0].type, 'universal');
t.deepEqual(tree.nodes[0].nodes[1].value, 'z98y');
t.deepEqual(tree.nodes[0].nodes[1].type, 'id');
});
(0, _helpers.test)('id hack', '#one#two', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[1].type, 'id');
});
(0, _helpers.test)('id and class names mixed', '#one.two.three', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'one');
t.deepEqual(tree.nodes[0].nodes[1].value, 'two');
t.deepEqual(tree.nodes[0].nodes[2].value, 'three');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[1].type, 'class');
t.deepEqual(tree.nodes[0].nodes[2].type, 'class');
});
(0, _helpers.test)('qualified id', 'button#one', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[1].type, 'id');
});
(0, _helpers.test)('qualified id & class name', 'h1#one.two', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[1].type, 'id');
t.deepEqual(tree.nodes[0].nodes[2].type, 'class');
});
(0, _helpers.test)('extraneous non-combinating whitespace', ' #h1 , #h2 ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.after, ' ');
t.deepEqual(tree.nodes[1].nodes[0].value, 'h2');
t.deepEqual(tree.nodes[1].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[1].nodes[0].spaces.after, ' ');
});
(0, _helpers.test)('Sass interpolation within a class', '.#{foo}', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes.length, 1);
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
t.deepEqual(tree.nodes[0].nodes[0].value, '#{foo}');
});
(0, _helpers.test)('Sass interpolation within an id', '#foo#{bar}', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes.length, 1);
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo#{bar}');
});
(0, _helpers.test)('Less interpolation within an id', '#foo@{bar}', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes.length, 1);
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo@{bar}');
});
(0, _helpers.test)('id selector with escaping', '#\\#test', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '#test');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\#test');
});
(0, _helpers.test)('id selector with escaping (2)', '#-a-b-c-', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '-a-b-c-');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
});
(0, _helpers.test)('id selector with escaping (3)', '#u-m\\00002b', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'u-m+');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'u-m\\00002b');
});
(0, _helpers.test)('id selector with escaping (4)', '#♥', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '♥');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
});
(0, _helpers.test)('id selector with escaping (5)', '#©', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '©');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
});
(0, _helpers.test)('id selector with escaping (6)', '#“‘’”', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '“‘’”');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
});
(0, _helpers.test)('id selector with escaping (7)', '#☺☃', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '☺☃');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
});
(0, _helpers.test)('id selector with escaping (8)', '#⌘⌥', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '⌘⌥');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
});
(0, _helpers.test)('id selector with escaping (9)', '#𝄞♪♩♫♬', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '𝄞♪♩♫♬');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
});
(0, _helpers.test)('id selector with escaping (10)', '#💩', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '💩');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
});
(0, _helpers.test)('id selector with escaping (11)', '#\\?', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '?');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\?');
});
(0, _helpers.test)('id selector with escaping (12)', '#\\@', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '@');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\@');
});
(0, _helpers.test)('id selector with escaping (13)', '#\\.', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '.');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\.');
});
(0, _helpers.test)('id selector with escaping (14)', '#\\3A \\)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ':)');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\3A \\)');
});
(0, _helpers.test)('id selector with escaping (15)', '#\\3A \\`\\(', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ':`(');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\3A \\`\\(');
});
(0, _helpers.test)('id selector with escaping (16)', '#\\31 23', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '123');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\31 23');
});
(0, _helpers.test)('id selector with escaping (17)', '#\\31 a2b3c', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '1a2b3c');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\31 a2b3c');
});
(0, _helpers.test)('id selector with escaping (18)', '#\\<p\\>', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '<p>');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\<p\\>');
});
(0, _helpers.test)('id selector with escaping (19)', '#\\<\\>\\<\\<\\<\\>\\>\\<\\>', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '<><<<>><>');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\<\\>\\<\\<\\<\\>\\>\\<\\>');
});
(0, _helpers.test)('id selector with escaping (20)', '#\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\[\\>\\+\\+\\+\\+\\+\\+\\+\\>\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\>\\+\\+\\+\\>\\+\\<\\<\\<\\<\\-\\]\\>\\+\\+\\.\\>\\+\\.\\+\\+\\+\\+\\+\\+\\+\\.\\.\\+\\+\\+\\.\\>\\+\\+\\.\\<\\<\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\.\\>\\.\\+\\+\\+\\.\\-\\-\\-\\-\\-\\-\\.\\-\\-\\-\\-\\-\\-\\-\\-\\.\\>\\+\\.\\>\\.', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\[\\>\\+\\+\\+\\+\\+\\+\\+\\>\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\>\\+\\+\\+\\>\\+\\<\\<\\<\\<\\-\\]\\>\\+\\+\\.\\>\\+\\.\\+\\+\\+\\+\\+\\+\\+\\.\\.\\+\\+\\+\\.\\>\\+\\+\\.\\<\\<\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\+\\.\\>\\.\\+\\+\\+\\.\\-\\-\\-\\-\\-\\-\\.\\-\\-\\-\\-\\-\\-\\-\\-\\.\\>\\+\\.\\>\\.');
});
(0, _helpers.test)('id selector with escaping (21)', '#\\#', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '#');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\#');
});
(0, _helpers.test)('id selector with escaping (22)', '#\\#\\#', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '##');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\#\\#');
});
(0, _helpers.test)('id selector with escaping (23)', '#\\#\\.\\#\\.\\#', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '#.#.#');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\#\\.\\#\\.\\#');
});
(0, _helpers.test)('id selector with escaping (24)', '#\\_', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '_');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\_');
});
(0, _helpers.test)('id selector with escaping (25)', '#\\{\\}', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '{}');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\{\\}');
});
(0, _helpers.test)('id selector with escaping (26)', '#\\.fake\\-class', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '.fake-class');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\.fake\\-class');
});
(0, _helpers.test)('id selector with escaping (27)', '#foo\\.bar', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo.bar');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'foo\\.bar');
});
(0, _helpers.test)('id selector with escaping (28)', '#\\3A hover', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ':hover');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\3A hover');
});
(0, _helpers.test)('id selector with escaping (29)', '#\\3A hover\\3A focus\\3A active', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ':hover:focus:active');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\3A hover\\3A focus\\3A active');
});
(0, _helpers.test)('id selector with escaping (30)', '#\\[attr\\=value\\]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '[attr=value]');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\[attr\\=value\\]');
});
(0, _helpers.test)('id selector with escaping (31)', '#f\\/o\\/o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f/o/o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\/o\\/o');
});
(0, _helpers.test)('id selector with escaping (32)', '#f\\\\o\\\\o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f\\o\\o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\\\o\\\\o');
});
(0, _helpers.test)('id selector with escaping (33)', '#f\\*o\\*o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f*o*o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\*o\\*o');
});
(0, _helpers.test)('id selector with escaping (34)', '#f\\!o\\!o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f!o!o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\!o\\!o');
});
(0, _helpers.test)('id selector with escaping (35)', '#f\\\'o\\\'o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f\'o\'o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\\'o\\\'o');
});
(0, _helpers.test)('id selector with escaping (36)', '#f\\~o\\~o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f~o~o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\~o\\~o');
});
(0, _helpers.test)('id selector with escaping (37)', '#f\\+o\\+o', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'f+o+o');
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'f\\+o\\+o');
});

View File

@@ -0,0 +1,88 @@
"use strict";
exports.__esModule = true;
exports.testLossy = exports.parse = void 0;
var _ava = _interopRequireDefault(require("ava"));
var _index = _interopRequireDefault(require("../index"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var parse = function parse(input, options, transform) {
return (0, _index["default"])(transform).processSync(input, options);
};
exports.parse = parse;
var testLossy = function testLossy(t, input, expected) {
var result = parse(input, {
lossless: false
});
t.deepEqual(result, expected);
};
exports.testLossy = testLossy;
(0, _ava["default"])('combinator, descendant - single', testLossy, '.one .two', '.one .two');
(0, _ava["default"])('combinator, descendant - multiple', testLossy, '.one .two', '.one .two');
(0, _ava["default"])('combinator, child - space before', testLossy, '.one >.two', '.one>.two');
(0, _ava["default"])('combinator, child - space after', testLossy, '.one> .two', '.one>.two');
(0, _ava["default"])('combinator, sibling - space before', testLossy, '.one ~.two', '.one~.two');
(0, _ava["default"])('combinator, sibling - space after', testLossy, '.one~ .two', '.one~.two');
(0, _ava["default"])('combinator, adj sibling - space before', testLossy, '.one +.two', '.one+.two');
(0, _ava["default"])('combinator, adj sibling - space after', testLossy, '.one+ .two', '.one+.two');
(0, _ava["default"])('classes, extraneous spaces', testLossy, ' .h1 , .h2 ', '.h1,.h2');
(0, _ava["default"])('ids, extraneous spaces', testLossy, ' #h1 , #h2 ', '#h1,#h2');
(0, _ava["default"])('attribute, spaces in selector', testLossy, 'h1[ href *= "test" ]', 'h1[href*="test"]');
(0, _ava["default"])('attribute, insensitive flag 1', testLossy, '[href="test" i ]', '[href="test"i]');
(0, _ava["default"])('attribute, insensitive flag 2', testLossy, '[href=TEsT i ]', '[href=TEsT i]');
(0, _ava["default"])('attribute, insensitive flag 3', testLossy, '[href=test i ]', '[href=test i]');
(0, _ava["default"])('attribute, extreneous whitespace', testLossy, ' [href] , [class] ', '[href],[class]');
(0, _ava["default"])('namespace, space before', testLossy, ' postcss|button', 'postcss|button');
(0, _ava["default"])('namespace, space after', testLossy, 'postcss|button ', 'postcss|button');
(0, _ava["default"])('namespace - all elements, space before', testLossy, ' postcss|*', 'postcss|*');
(0, _ava["default"])('namespace - all elements, space after', testLossy, 'postcss|* ', 'postcss|*');
(0, _ava["default"])('namespace - all namespaces, space before', testLossy, ' *|button', '*|button');
(0, _ava["default"])('namespace - all namespaces, space after', testLossy, '*|button ', '*|button');
(0, _ava["default"])('namespace - all elements in all namespaces, space before', testLossy, ' *|*', '*|*');
(0, _ava["default"])('namespace - all elements in all namespaces, space after', testLossy, '*|* ', '*|*');
(0, _ava["default"])('namespace - all elements without namespace, space before', testLossy, ' |*', '|*');
(0, _ava["default"])('namespace - all elements without namespace, space after', testLossy, '|* ', '|*');
(0, _ava["default"])('namespace - tag with no namespace, space before', testLossy, ' |button', '|button');
(0, _ava["default"])('namespace - tag with no namespace, space after', testLossy, '|button ', '|button');
(0, _ava["default"])('namespace - inside attribute, space before', testLossy, ' [ postcss|href=test]', '[postcss|href=test]');
(0, _ava["default"])('namespace - inside attribute, space after', testLossy, '[postcss|href= test ] ', '[postcss|href=test]');
(0, _ava["default"])('namespace - inside attribute (2), space before', testLossy, ' [ postcss|href]', '[postcss|href]');
(0, _ava["default"])('namespace - inside attribute (2), space after', testLossy, '[postcss|href ] ', '[postcss|href]');
(0, _ava["default"])('namespace - inside attribute (3), space before', testLossy, ' [ *|href=test]', '[*|href=test]');
(0, _ava["default"])('namespace - inside attribute (3), space after', testLossy, '[*|href= test ] ', '[*|href=test]');
(0, _ava["default"])('namespace - inside attribute (4), space after', testLossy, '[|href= test ] ', '[|href=test]');
(0, _ava["default"])('tag - extraneous whitespace', testLossy, ' h1 , h2 ', 'h1,h2');
(0, _ava["default"])('tag - trailing comma', testLossy, 'h1, ', 'h1,');
(0, _ava["default"])('tag - trailing comma (1)', testLossy, 'h1,', 'h1,');
(0, _ava["default"])('tag - trailing comma (2)', testLossy, 'h1', 'h1');
(0, _ava["default"])('tag - trailing slash (1)', testLossy, 'h1\\ ', 'h1\\ ');
(0, _ava["default"])('tag - trailing slash (2)', testLossy, 'h1\\ h2\\', 'h1\\ h2\\');
(0, _ava["default"])('universal - combinator', testLossy, ' * + * ', '*+*');
(0, _ava["default"])('universal - extraneous whitespace', testLossy, ' * , * ', '*,*');
(0, _ava["default"])('universal - qualified universal selector', testLossy, '*[href] *:not(*.green)', '*[href] *:not(*.green)');
(0, _ava["default"])('nesting - spacing before', testLossy, ' &.class', '&.class');
(0, _ava["default"])('nesting - spacing after', testLossy, '&.class ', '&.class');
(0, _ava["default"])('nesting - spacing between', testLossy, '& .class ', '& .class');
(0, _ava["default"])('pseudo (single) - spacing before', testLossy, ' :after', ':after');
(0, _ava["default"])('pseudo (single) - spacing after', testLossy, ':after ', ':after');
(0, _ava["default"])('pseudo (double) - spacing before', testLossy, ' ::after', '::after');
(0, _ava["default"])('pseudo (double) - spacing after', testLossy, '::after ', '::after');
(0, _ava["default"])('pseudo - multiple', testLossy, ' *:target::before , a:after ', '*:target::before,a:after');
(0, _ava["default"])('pseudo - negated', testLossy, 'h1:not( .heading )', 'h1:not(.heading)');
(0, _ava["default"])('pseudo - negated with combinators (1)', testLossy, 'h1:not(.heading > .title) > h1', 'h1:not(.heading>.title)>h1');
(0, _ava["default"])('pseudo - negated with combinators (2)', testLossy, '.foo:nth-child(2n + 1)', '.foo:nth-child(2n+1)');
(0, _ava["default"])('pseudo - extra whitespace', testLossy, 'a:not( h2 )', 'a:not(h2)');
(0, _ava["default"])('comments - comment inside descendant selector', testLossy, "div /* wtf */.foo", "div /* wtf */.foo");
(0, _ava["default"])('comments - comment inside complex selector', testLossy, "div /* wtf */ > .foo", "div/* wtf */>.foo");
(0, _ava["default"])('comments - comment inside compound selector with space', testLossy, "div /* wtf */ .foo", "div /* wtf */.foo");
(0, _ava["default"])('@words - space before', testLossy, ' @media', '@media');
(0, _ava["default"])('@words - space after', testLossy, '@media ', '@media');
(0, _ava["default"])('@words - maintains space between', testLossy, '@media (min-width: 700px) and (orientation: landscape)', '@media (min-width: 700px) and (orientation: landscape)');
(0, _ava["default"])('@words - extraneous space between', testLossy, '@media (min-width: 700px) and (orientation: landscape)', '@media (min-width: 700px) and (orientation: landscape)');
(0, _ava["default"])('@words - multiple', testLossy, '@media (min-width: 700px), (min-height: 400px)', '@media (min-width: 700px),(min-height: 400px)');

View File

@@ -0,0 +1,66 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('match tags in the postcss namespace', 'postcss|button', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, 'postcss');
t.deepEqual(tree.nodes[0].nodes[0].value, 'button');
});
(0, _helpers.test)('match everything in the postcss namespace', 'postcss|*', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, 'postcss');
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
});
(0, _helpers.test)('match any namespace', '*|button', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, '*');
t.deepEqual(tree.nodes[0].nodes[0].value, 'button');
});
(0, _helpers.test)('match all elements within the postcss namespace', 'postcss|*', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, 'postcss');
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
});
(0, _helpers.test)('match all elements in all namespaces', '*|*', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, '*');
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
});
(0, _helpers.test)('match all elements without a namespace', '|*', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, true);
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
});
(0, _helpers.test)('match tags with no namespace', '|button', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, true);
t.deepEqual(tree.nodes[0].nodes[0].value, 'button');
});
(0, _helpers.test)('match namespace inside attribute selector', '[postcss|href=test]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, 'postcss');
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
});
(0, _helpers.test)('match namespace inside attribute selector (2)', '[postcss|href]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, 'postcss');
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
});
(0, _helpers.test)('match namespace inside attribute selector (3)', '[*|href]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, '*');
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
});
(0, _helpers.test)('match default namespace inside attribute selector', '[|href]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, true);
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
});
(0, _helpers.test)('match default namespace inside attribute selector with spaces', '[ |href ]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, true);
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
});
(0, _helpers.test)('namespace with qualified id selector', 'ns|h1#foo', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, 'ns');
});
(0, _helpers.test)('namespace with qualified class selector', 'ns|h1.foo', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].namespace, 'ns');
});
(0, _helpers.test)('ns alias for namespace', 'f\\oo|h1.foo', function (t, tree) {
var tag = tree.nodes[0].nodes[0];
t.deepEqual(tag.namespace, 'foo');
t.deepEqual(tag.ns, 'foo');
tag.ns = "bar";
t.deepEqual(tag.namespace, 'bar');
t.deepEqual(tag.ns, 'bar');
});

View File

@@ -0,0 +1,40 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('nesting selector', '&', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '&');
t.deepEqual(tree.nodes[0].nodes[0].type, 'nesting');
});
(0, _helpers.test)('nesting selector followed by a class', '& .class', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '&');
t.deepEqual(tree.nodes[0].nodes[0].type, 'nesting');
t.deepEqual(tree.nodes[0].nodes[1].value, ' ');
t.deepEqual(tree.nodes[0].nodes[1].type, 'combinator');
t.deepEqual(tree.nodes[0].nodes[2].value, 'class');
t.deepEqual(tree.nodes[0].nodes[2].type, 'class');
});
(0, _helpers.test)('&foo', '&foo', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '&');
t.deepEqual(tree.nodes[0].nodes[0].type, 'nesting');
t.deepEqual(tree.nodes[0].nodes[1].value, 'foo');
t.deepEqual(tree.nodes[0].nodes[1].type, 'tag');
});
(0, _helpers.test)('&-foo', '&-foo', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '&');
t.deepEqual(tree.nodes[0].nodes[0].type, 'nesting');
t.deepEqual(tree.nodes[0].nodes[1].value, '-foo');
t.deepEqual(tree.nodes[0].nodes[1].type, 'tag');
});
(0, _helpers.test)('&_foo', '&_foo', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '&');
t.deepEqual(tree.nodes[0].nodes[0].type, 'nesting');
t.deepEqual(tree.nodes[0].nodes[1].value, '_foo');
t.deepEqual(tree.nodes[0].nodes[1].type, 'tag');
});
(0, _helpers.test)('&|foo', '&|foo', function (t, tree) {
var element = tree.nodes[0].nodes[0];
t.deepEqual(element.value, 'foo');
t.deepEqual(element.type, 'tag');
t.deepEqual(element.namespace, '&');
});

View File

@@ -0,0 +1,139 @@
"use strict";
var _ava = _interopRequireDefault(require("ava"));
var _ = _interopRequireDefault(require(".."));
var _helpers = require("./util/helpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
(0, _ava["default"])('node#clone', function (t) {
(0, _helpers.parse)('[href="test"]', function (selectors) {
var selector = selectors.first.first;
var clone = selector.clone();
delete selector.parent;
t.deepEqual(clone, selectors.first.first);
});
});
(0, _ava["default"])('node#clone of attribute', function (t) {
(0, _helpers.parse)('[href=test]', function (selectors) {
var selector = selectors.first.first;
var clone = selector.clone();
delete selector.parent;
t.deepEqual(clone, selectors.first.first);
});
});
(0, _ava["default"])('node#replaceWith', function (t) {
var out = (0, _helpers.parse)('[href="test"]', function (selectors) {
var attr = selectors.first.first;
var id = _["default"].id({
value: 'test'
});
var className = _["default"].className({
value: 'test'
});
attr.replaceWith(id, className);
});
t.deepEqual(out, '#test.test');
});
(0, _ava["default"])('Node#appendToPropertyAndEscape', function (t) {
var out = (0, _helpers.parse)('.fo\\o', function (selectors) {
var className = selectors.first.first;
t.deepEqual(className.raws, {
value: "fo\\o"
});
className.appendToPropertyAndEscape("value", "bar", "ba\\r");
t.deepEqual(className.raws, {
value: "fo\\oba\\r"
});
});
t.deepEqual(out, '.fo\\oba\\r');
});
(0, _ava["default"])('Node#setPropertyAndEscape with existing raws', function (t) {
var out = (0, _helpers.parse)('.fo\\o', function (selectors) {
var className = selectors.first.first;
t.deepEqual(className.raws, {
value: "fo\\o"
});
className.setPropertyAndEscape("value", "bar", "ba\\r");
t.deepEqual(className.raws, {
value: "ba\\r"
});
});
t.deepEqual(out, '.ba\\r');
});
(0, _ava["default"])('Node#setPropertyAndEscape without existing raws', function (t) {
var out = (0, _helpers.parse)('.foo', function (selectors) {
var className = selectors.first.first;
t.deepEqual(className.raws, undefined);
className.setPropertyAndEscape("value", "bar", "ba\\r");
t.deepEqual(className.raws, {
value: "ba\\r"
});
});
t.deepEqual(out, '.ba\\r');
});
(0, _ava["default"])('Node#setPropertyWithoutEscape with existing raws', function (t) {
var out = (0, _helpers.parse)('.fo\\o', function (selectors) {
var className = selectors.first.first;
t.deepEqual(className.raws, {
value: "fo\\o"
});
className.setPropertyWithoutEscape("value", "w+t+f");
t.deepEqual(className.raws, {});
});
t.deepEqual(out, '.w+t+f');
});
(0, _ava["default"])('Node#setPropertyWithoutEscape without existing raws', function (t) {
var out = (0, _helpers.parse)('.foo', function (selectors) {
var className = selectors.first.first;
t.deepEqual(className.raws, undefined);
className.setPropertyWithoutEscape("value", "w+t+f");
t.deepEqual(className.raws, {});
t.deepEqual(className.value, "w+t+f");
});
t.deepEqual(out, '.w+t+f');
});
(0, _ava["default"])('Node#isAtPosition', function (t) {
(0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
t.deepEqual(root.isAtPosition(1, 1), true);
t.deepEqual(root.isAtPosition(1, 10), true);
t.deepEqual(root.isAtPosition(2, 23), true);
t.deepEqual(root.isAtPosition(2, 24), false);
var selector = root.first;
t.deepEqual(selector.isAtPosition(1, 1), true);
t.deepEqual(selector.isAtPosition(1, 10), true);
t.deepEqual(selector.isAtPosition(1, 11), false);
var pseudoNot = selector.first;
t.deepEqual(pseudoNot.isAtPosition(1, 1), true);
t.deepEqual(pseudoNot.isAtPosition(1, 7), true);
t.deepEqual(pseudoNot.isAtPosition(1, 10), true);
t.deepEqual(pseudoNot.isAtPosition(1, 11), false);
var notSelector = pseudoNot.first;
t.deepEqual(notSelector.isAtPosition(1, 1), false);
t.deepEqual(notSelector.isAtPosition(1, 4), false);
t.deepEqual(notSelector.isAtPosition(1, 5), true);
t.deepEqual(notSelector.isAtPosition(1, 6), true);
t.deepEqual(notSelector.isAtPosition(1, 9), true);
t.deepEqual(notSelector.isAtPosition(1, 10), true);
t.deepEqual(notSelector.isAtPosition(1, 11), false);
var notClass = notSelector.first;
t.deepEqual(notClass.isAtPosition(1, 5), false);
t.deepEqual(notClass.isAtPosition(1, 6), true);
t.deepEqual(notClass.isAtPosition(1, 9), true);
t.deepEqual(notClass.isAtPosition(1, 10), false);
var secondSel = root.at(1);
t.deepEqual(secondSel.isAtPosition(1, 11), false);
t.deepEqual(secondSel.isAtPosition(2, 1), true);
t.deepEqual(secondSel.isAtPosition(2, 23), true);
t.deepEqual(secondSel.isAtPosition(2, 24), false);
var combinator = secondSel.at(1);
t.deepEqual(combinator.isAtPosition(2, 5), false);
t.deepEqual(combinator.isAtPosition(2, 6), true);
t.deepEqual(combinator.isAtPosition(2, 7), false);
});
});

View File

@@ -0,0 +1,46 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('non-standard selector', '.icon.is-$(network)', function (t, tree) {
var class1 = tree.nodes[0].nodes[0];
t.deepEqual(class1.value, 'icon');
t.deepEqual(class1.type, 'class');
var class2 = tree.nodes[0].nodes[1];
t.deepEqual(class2.value, 'is-$(network)');
t.deepEqual(class2.type, 'class');
});
(0, _helpers.test)('at word in selector', 'em@il.com', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'em@il');
t.deepEqual(tree.nodes[0].nodes[1].value, 'com');
});
(0, _helpers.test)('leading combinator', '> *', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '>');
t.deepEqual(tree.nodes[0].nodes[1].value, '*');
});
(0, _helpers.test)('sass escapes', '.#{$classname}', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, "class");
t.deepEqual(tree.nodes[0].nodes[0].value, "#{$classname}");
});
(0, _helpers.test)('sass escapes (2)', '[lang=#{$locale}]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, "attribute");
t.deepEqual(tree.nodes[0].nodes[0].attribute, "lang");
t.deepEqual(tree.nodes[0].nodes[0].operator, "=");
t.deepEqual(tree.nodes[0].nodes[0].value, "#{$locale}");
});
(0, _helpers.test)('placeholder', '%foo', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, "tag");
t.deepEqual(tree.nodes[0].nodes[0].value, "%foo");
});
(0, _helpers.test)('styled selector', '${Step}', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, "tag");
t.deepEqual(tree.nodes[0].nodes[0].value, "${Step}");
});
(0, _helpers.test)('styled selector (2)', '${Step}:nth-child(odd)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, "tag");
t.deepEqual(tree.nodes[0].nodes[0].value, "${Step}");
t.deepEqual(tree.nodes[0].nodes[1].type, "pseudo");
t.deepEqual(tree.nodes[0].nodes[1].value, ":nth-child");
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].type, "tag");
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, "odd");
});

View File

@@ -0,0 +1,259 @@
"use strict";
var _ava = _interopRequireDefault(require("ava"));
var _index = _interopRequireDefault(require("../index"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
// Node creation
var nodeTypes = [['attribute', '[href]', {
attribute: 'href'
}], ['className', '.classy', {
value: 'classy'
}], ['combinator', ' >> ', {
value: '>>',
spaces: {
before: ' ',
after: ' '
}
}], ['comment', '/* comment */', {
value: '/* comment */'
}], ['id', '#test', {
value: 'test'
}], ['nesting', '&'], ['pseudo', '::before', {
value: '::before'
}], ['string', '"wow"', {
value: '"wow"'
}], ['tag', 'button', {
value: 'button'
}], ['universal', '*']];
nodeTypes.forEach(function (type) {
(0, _ava["default"])("parser#" + type[0], function (t) {
var node = _index["default"][type[0]](type[2] || {});
t.deepEqual(String(node), type[1]);
});
});
(0, _ava["default"])('string constants', function (t) {
t.truthy(_index["default"].TAG);
t.truthy(_index["default"].STRING);
t.truthy(_index["default"].SELECTOR);
t.truthy(_index["default"].ROOT);
t.truthy(_index["default"].PSEUDO);
t.truthy(_index["default"].NESTING);
t.truthy(_index["default"].ID);
t.truthy(_index["default"].COMMENT);
t.truthy(_index["default"].COMBINATOR);
t.truthy(_index["default"].CLASS);
t.truthy(_index["default"].ATTRIBUTE);
t.truthy(_index["default"].UNIVERSAL);
});
(0, _ava["default"])('construct a whole tree', function (t) {
var root = _index["default"].root();
var selector = _index["default"].selector();
selector.append(_index["default"].id({
value: 'tree'
}));
root.append(selector);
t.deepEqual(String(root), '#tree');
});
(0, _ava["default"])('no operation', function (t) {
t.notThrows(function () {
return (0, _index["default"])().processSync('h1 h2 h3');
});
});
(0, _ava["default"])('empty selector string', function (t) {
t.notThrows(function () {
return (0, _index["default"])(function (selectors) {
selectors.walk(function (selector) {
selector.type = 'tag';
});
}).processSync('');
});
});
(0, _ava["default"])('async parser', function (t) {
return (0, _index["default"])(function (selectors) {
return new Promise(function (res) {
setTimeout(function () {
selectors.first.nodes[0].value = 'bar';
res();
}, 1);
});
}).process('foo').then(function (result) {
t.deepEqual(result, 'bar');
});
});
(0, _ava["default"])('parse errors with the async parser', function (t) {
return (0, _index["default"])(function (selectors) {
return new Promise(function (res) {
setTimeout(function () {
selectors.first.nodes[0].value = 'bar';
res();
}, 1);
});
}).process('a b: c')["catch"](function (err) {
return t.truthy(err);
});
});
(0, _ava["default"])('parse errors within the async processor', function (t) {
return (0, _index["default"])(function (selectors) {
return new Promise(function (res, rej) {
setTimeout(function () {
rej(selectors.error("async error"));
}, 1);
});
}).process('.foo')["catch"](function (err) {
return t.truthy(err);
});
});
(0, _ava["default"])('parse errors within the async processor before the promise returns', function (t) {
return (0, _index["default"])(function (selectors) {
throw selectors.error("async error");
}).process('.foo')["catch"](function (err) {
return t.truthy(err);
});
});
(0, _ava["default"])('returning a promise to the sync processor fails', function (t) {
t["throws"](function () {
return (0, _index["default"])(function () {
return new Promise(function (res) {
setTimeout(function () {
res();
}, 1);
});
}).processSync('.foo');
});
});
(0, _ava["default"])('Passing a rule works async', function (t) {
var rule = {
selector: '.foo'
};
return (0, _index["default"])(function (root) {
return new Promise(function (res) {
setTimeout(function () {
root.walkClasses(function (node) {
node.value = "bar";
});
res();
}, 1);
});
}).process(rule).then(function (newSel) {
t.deepEqual(newSel, ".bar");
t.deepEqual(rule.selector, ".bar");
});
});
(0, _ava["default"])('Passing a rule with mutation disabled works async', function (t) {
var rule = {
selector: '.foo'
};
return (0, _index["default"])(function (root) {
return new Promise(function (res) {
setTimeout(function () {
root.walkClasses(function (node) {
node.value = "bar";
});
res();
}, 1);
});
}).process(rule, {
updateSelector: false
}).then(function (newSel) {
t.deepEqual(newSel, ".bar");
t.deepEqual(rule.selector, ".foo");
});
});
(0, _ava["default"])('Passing a rule with mutation works sync', function (t) {
var rule = {
selector: '.foo'
};
var newSel = (0, _index["default"])(function (root) {
root.walkClasses(function (node) {
node.value = "bar";
});
}).processSync(rule, {
updateSelector: true
});
t.deepEqual(newSel, ".bar");
t.deepEqual(rule.selector, ".bar");
});
(0, _ava["default"])('Transform a selector synchronously', function (t) {
var rule = {
selector: '.foo'
};
var count = (0, _index["default"])(function (root) {
var classCount = 0;
root.walkClasses(function (node) {
classCount++;
node.value = "bar";
});
return classCount;
}).transformSync(rule, {
updateSelector: true
});
t.deepEqual(count, 1);
t.deepEqual(rule.selector, ".bar");
});
(0, _ava["default"])('Transform a selector asynchronously', function (t) {
var rule = {
selector: '.foo'
};
return (0, _index["default"])(function (root) {
return new Promise(function (res) {
setTimeout(function () {
var classCount = 0;
root.walkClasses(function (node) {
classCount++;
node.value = "bar";
});
res(classCount);
}, 1);
});
}).transform(rule, {
updateSelector: true
}).then(function (count) {
t.deepEqual(count, 1);
t.deepEqual(rule.selector, ".bar");
});
});
(0, _ava["default"])('get AST of a selector synchronously', function (t) {
var rule = {
selector: '.foo'
};
var ast = (0, _index["default"])(function (root) {
var classCount = 0;
root.walkClasses(function (node) {
classCount++;
node.value = "bar";
});
return classCount;
}).astSync(rule, {
updateSelector: true
});
t.deepEqual(ast.nodes[0].nodes[0].value, "bar");
t.deepEqual(rule.selector, ".bar");
});
(0, _ava["default"])('get AST a selector asynchronously', function (t) {
var rule = {
selector: '.foo'
};
return (0, _index["default"])(function (root) {
return new Promise(function (res) {
setTimeout(function () {
var classCount = 0;
root.walkClasses(function (node) {
classCount++;
node.value = "bar";
});
res(classCount);
}, 1);
});
}).ast(rule, {
updateSelector: true
}).then(function (ast) {
t.deepEqual(ast.nodes[0].nodes[0].value, "bar");
t.deepEqual(rule.selector, ".bar");
});
});

View File

@@ -0,0 +1,46 @@
"use strict";
var _ava = _interopRequireDefault(require("ava"));
var _postcss = _interopRequireDefault(require("postcss"));
var _helpers = require("./util/helpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var cse = 'CssSyntaxError';
function showCode(t, selector) {
var rule = _postcss["default"].parse(selector).first;
try {
(0, _helpers.parse)(rule);
} catch (e) {
if (e.name !== cse) {
return;
} // Removes ANSI codes from snapshot tests as it makes them illegible.
// The formatting of this error is otherwise identical to e.toString()
t.snapshot(cse + ": " + e.message + "\n\n" + e.showSourceCode(false) + "\n");
}
}
(0, _ava["default"])('missing open square bracket', showCode, 'a b c] {}');
(0, _ava["default"])('missing open parenthesis', showCode, 'a b c) {}');
(0, _ava["default"])('missing pseudo class or pseudo element', showCode, 'a b c: {}');
(0, _ava["default"])('space in between colon and word (incorrect pseudo)', showCode, 'a b: c {}');
(0, _ava["default"])('string after colon (incorrect pseudo)', showCode, 'a b:"wow" {}'); // attribute selectors
(0, _ava["default"])('bad string attribute', showCode, '["hello"] {}');
(0, _ava["default"])('bad string attribute with value', showCode, '["foo"=bar] {}');
(0, _ava["default"])('bad parentheses', showCode, '[foo=(bar)] {}');
(0, _ava["default"])('bad lonely asterisk', showCode, '[*] {}');
(0, _ava["default"])('bad lonely pipe', showCode, '[|] {}');
(0, _ava["default"])('bad lonely caret', showCode, '[^] {}');
(0, _ava["default"])('bad lonely dollar', showCode, '[$] {}');
(0, _ava["default"])('bad lonely tilde', showCode, '[~] {}');
(0, _ava["default"])('bad lonely equals', showCode, '[=] {}');
(0, _ava["default"])('bad lonely operator', showCode, '[*=] {}');
(0, _ava["default"])('bad lonely operator (2)', showCode, '[|=] {}');
(0, _ava["default"])('bad doubled operator', showCode, '[href=foo=bar] {}');

View File

@@ -0,0 +1,165 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('pseudo element (single colon)', 'h1:after', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[1].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].value, ':after');
});
(0, _helpers.test)('pseudo element (double colon)', 'h1::after', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[1].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].value, '::after');
});
(0, _helpers.test)('multiple pseudo elements', '*:target::before, a:after', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
t.deepEqual(tree.nodes[0].nodes[1].value, ':target');
t.deepEqual(tree.nodes[0].nodes[2].value, '::before');
t.deepEqual(tree.nodes[1].nodes[1].value, ':after');
});
(0, _helpers.test)('negation pseudo element', 'h1:not(.heading)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].value, ':not');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, 'heading');
});
(0, _helpers.test)('negation pseudo element (2)', 'h1:not(.heading, .title, .content)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].value, ':not');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, 'heading');
t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].value, 'title');
t.deepEqual(tree.nodes[0].nodes[1].nodes[2].nodes[0].value, 'content');
});
(0, _helpers.test)('negation pseudo element (3)', 'h1:not(.heading > .title) > h1', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, 'heading');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[1].value, '>');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[2].value, 'title');
t.deepEqual(tree.nodes[0].nodes[2].value, '>');
t.deepEqual(tree.nodes[0].nodes[3].value, 'h1');
});
(0, _helpers.test)('negation pseudo element (4)', 'h1:not(h2:not(h3))', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[1].nodes[0].nodes[0].value, 'h3');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[1].nodes[0].nodes[0].parent.type, 'selector');
});
(0, _helpers.test)('pseudo class in the middle of a selector', 'a:link.external', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].value, 'a');
t.deepEqual(tree.nodes[0].nodes[1].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].value, ':link');
t.deepEqual(tree.nodes[0].nodes[2].type, 'class');
t.deepEqual(tree.nodes[0].nodes[2].value, 'external');
});
(0, _helpers.test)('extra whitespace inside parentheses', 'a:not( h2 )', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, 'h2');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].spaces.after, ' ');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].spaces.before, ' ');
});
(0, _helpers.test)('escaped numbers in class name with pseudo', 'a:before.\\31\\ 0', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[2].type, 'class');
t.deepEqual(tree.nodes[0].nodes[2].value, '1 0');
t.deepEqual(tree.nodes[0].nodes[2].raws.value, '\\31\\ 0');
});
(0, _helpers.test)('nested pseudo', '.btn-group>.btn:last-child:not(:first-child)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[4].value, ':not');
});
(0, _helpers.test)('extraneous non-combinating whitespace', ' h1:after , h2:after ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[1].value, ':after');
t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[1].nodes[1].value, ':after');
t.deepEqual(tree.nodes[1].nodes[1].spaces.after, ' ');
});
(0, _helpers.test)('negation pseudo element with quotes', 'h1:not(".heading")', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].value, ':not');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, '".heading"');
});
(0, _helpers.test)('negation pseudo element with single quotes', "h1:not('.heading')", function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].value, ':not');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, "'.heading'");
});
(0, _helpers.test)('Issue #116', "svg:not(:root)", function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].value, ':not');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, ':root');
});
(0, _helpers.test)('alone pseudo class', ':root', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[0].value, ':root');
});
(0, _helpers.test)('non standard pseudo (@custom-selector)', ":--foobar, a", function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, ':--foobar');
t.deepEqual(tree.nodes[0].nodes[0].type, 'pseudo');
t.deepEqual(tree.nodes[1].nodes[0].value, 'a');
t.deepEqual(tree.nodes[1].nodes[0].type, 'tag');
});
(0, _helpers.test)('non standard pseudo (@custom-selector) (1)', "a, :--foobar", function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'a');
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[1].nodes[0].value, ':--foobar');
t.deepEqual(tree.nodes[1].nodes[0].type, 'pseudo');
});
(0, _helpers.test)('current pseudo class', ':current(p, li, dt, dd)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[0].value, ':current');
t.deepEqual(tree.nodes[0].nodes[0].nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].nodes[0].nodes[0].value, 'p');
t.deepEqual(tree.nodes[0].nodes[0].nodes[1].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].nodes[1].nodes[0].value, 'li');
t.deepEqual(tree.nodes[0].nodes[0].nodes[2].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].nodes[2].nodes[0].value, 'dt');
t.deepEqual(tree.nodes[0].nodes[0].nodes[3].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].nodes[3].nodes[0].value, 'dd');
});
(0, _helpers.test)('is pseudo class', ':is(p, li, dt, dd)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[0].value, ':is');
t.deepEqual(tree.nodes[0].nodes[0].nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].nodes[0].nodes[0].value, 'p');
t.deepEqual(tree.nodes[0].nodes[0].nodes[1].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].nodes[1].nodes[0].value, 'li');
t.deepEqual(tree.nodes[0].nodes[0].nodes[2].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].nodes[2].nodes[0].value, 'dt');
t.deepEqual(tree.nodes[0].nodes[0].nodes[3].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].nodes[3].nodes[0].value, 'dd');
});
(0, _helpers.test)('is pseudo class with namespace', '*|*:is(:hover, :focus) ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'universal');
t.deepEqual(tree.nodes[0].nodes[0].namespace, '*');
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
t.deepEqual(tree.nodes[0].nodes[1].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].value, ':is');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, ':hover');
t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].value, ':focus');
});
(0, _helpers.test)('has pseudo class', 'a:has(> img)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].value, 'a');
t.deepEqual(tree.nodes[0].nodes[1].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].value, ':has');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].type, 'combinator');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, '>');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[1].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[1].value, 'img');
});
(0, _helpers.test)('where pseudo class', 'a:where(:not(:hover))', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].value, 'a');
t.deepEqual(tree.nodes[0].nodes[1].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].value, ':where');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, ':not');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[0].nodes[0].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[0].nodes[0].value, ':hover');
});
(0, _helpers.test)('nested pseudo classes', "section:not( :has(h1, h2 ) )", function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[0].value, 'section');
t.deepEqual(tree.nodes[0].nodes[1].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].value, ':not');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].type, 'pseudo');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, ':has');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[1].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[1].nodes[0].value, 'h2');
});

View File

@@ -0,0 +1,229 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('universal selector', '*', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
});
(0, _helpers.test)('lobotomized owl selector', ' * + * ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 2);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 2);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 1);
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 4);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 4);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 3);
t.deepEqual(tree.nodes[0].nodes[2].source.start.column, 6);
t.deepEqual(tree.nodes[0].nodes[2].source.end.column, 6);
t.deepEqual(tree.nodes[0].nodes[2].sourceIndex, 5);
});
(0, _helpers.test)('comment', '/**\n * Hello!\n */', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 3);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
});
(0, _helpers.test)('comment & universal selectors', '*/*test*/*', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 2);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 9);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 1);
t.deepEqual(tree.nodes[0].nodes[2].source.start.column, 10);
t.deepEqual(tree.nodes[0].nodes[2].source.end.column, 10);
t.deepEqual(tree.nodes[0].nodes[2].sourceIndex, 9);
});
(0, _helpers.test)('tag selector', 'h1', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 2);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
});
(0, _helpers.test)('id selector', '#id', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 3);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
});
(0, _helpers.test)('tag selector followed by id selector', 'h1, #id', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 2);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 5);
t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 7);
t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 4);
});
(0, _helpers.test)('multiple id selectors', '#one#two', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 4);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 5);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 8);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 4);
});
(0, _helpers.test)('multiple id selectors (2)', '#one#two#three#four', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[2].source.start.column, 9);
t.deepEqual(tree.nodes[0].nodes[2].source.end.column, 14);
t.deepEqual(tree.nodes[0].nodes[2].sourceIndex, 8);
t.deepEqual(tree.nodes[0].nodes[3].source.start.column, 15);
t.deepEqual(tree.nodes[0].nodes[3].source.end.column, 19);
t.deepEqual(tree.nodes[0].nodes[3].sourceIndex, 14);
});
(0, _helpers.test)('multiple id selectors (3)', '#one#two,#three#four', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 5);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 8);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 4);
t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 16);
t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 20);
t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 15);
});
(0, _helpers.test)('multiple class selectors', '.one.two,.three.four', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 5);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 8);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 4);
t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 16);
t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 20);
t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 15);
});
(0, _helpers.test)('attribute selector', '[name="james"]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 14);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
});
(0, _helpers.test)('multiple attribute selectors', '[name="james"][name="ed"],[name="snakeman"][name="a"]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.line, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 14);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 15);
t.deepEqual(tree.nodes[0].nodes[1].source.end.line, 1);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 25);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 14);
t.deepEqual(tree.nodes[1].nodes[0].source.start.line, 1);
t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 27);
t.deepEqual(tree.nodes[1].nodes[0].source.end.line, 1);
t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 43);
t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 26);
t.deepEqual(tree.nodes[1].nodes[1].source.start.line, 1);
t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 44);
t.deepEqual(tree.nodes[1].nodes[1].source.end.line, 1);
t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 53);
t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 43);
});
(0, _helpers.test)('pseudo-class', 'h1:first-child', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 14);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2);
});
(0, _helpers.test)('pseudo-class with argument', 'h1:not(.strudel, .food)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 23);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2);
});
(0, _helpers.test)('pseudo-element', 'h1::before', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 10);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2);
});
(0, _helpers.test)('multiple pseudos', 'h1:not(.food)::before, a:first-child', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3);
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 13);
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2);
t.deepEqual(tree.nodes[0].nodes[2].source.start.line, 1);
t.deepEqual(tree.nodes[0].nodes[2].source.start.column, 14);
t.deepEqual(tree.nodes[0].nodes[2].source.end.column, 21);
t.deepEqual(tree.nodes[0].nodes[2].sourceIndex, 13);
t.deepEqual(tree.nodes[1].nodes[1].source.start.line, 1);
t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 25);
t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 36);
t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 24);
});
(0, _helpers.test)('combinators', 'div > h1 span', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1, "> start line");
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 5, "> start column");
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 5, "> end column");
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 4, "> sourceIndex");
t.deepEqual(tree.nodes[0].nodes[3].source.start.line, 1, "' ' start line");
t.deepEqual(tree.nodes[0].nodes[3].source.start.column, 9, "' ' start column");
t.deepEqual(tree.nodes[0].nodes[3].source.end.column, 9, "' ' end column");
t.deepEqual(tree.nodes[0].nodes[3].sourceIndex, 8, "' ' sourceIndex");
});
(0, _helpers.test)('combinators surrounded by superfluous spaces', 'div > h1 ~ span a', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1, "> start line");
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 7, "> start column");
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 7, "> end column");
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 6, "> sourceIndex");
t.deepEqual(tree.nodes[0].nodes[3].source.start.line, 1, "~ start line");
t.deepEqual(tree.nodes[0].nodes[3].source.start.column, 13, "~ start column");
t.deepEqual(tree.nodes[0].nodes[3].source.end.column, 13, "~ end column");
t.deepEqual(tree.nodes[0].nodes[3].sourceIndex, 12, "~ sourceIndex");
t.deepEqual(tree.nodes[0].nodes[5].source.start.line, 1, "' ' start line");
t.deepEqual(tree.nodes[0].nodes[5].source.start.column, 21, "' ' start column");
t.deepEqual(tree.nodes[0].nodes[5].source.end.column, 23, "' ' end column");
t.deepEqual(tree.nodes[0].nodes[5].sourceIndex, 20, "' ' sourceIndex");
});
(0, _helpers.test)('multiple id selectors on different lines', '#one,\n#two', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 4);
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
t.deepEqual(tree.nodes[1].nodes[0].source.start.line, 2);
t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 1);
t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 4);
t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 6);
});
(0, _helpers.test)('multiple id selectors on different CRLF lines', '#one,\r\n#two,\r\n#three', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1, '#one start line');
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1, '#one start column');
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 4, '#one end column');
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0, '#one sourceIndex');
t.deepEqual(tree.nodes[1].nodes[0].source.start.line, 2, '#two start line');
t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 1, '#two start column');
t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 4, '#two end column');
t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 7, '#two sourceIndex');
t.deepEqual(tree.nodes[2].nodes[0].source.start.line, 3, '#three start line');
t.deepEqual(tree.nodes[2].nodes[0].source.start.column, 1, '#three start column');
t.deepEqual(tree.nodes[2].nodes[0].source.end.column, 6, '#three end column');
t.deepEqual(tree.nodes[2].nodes[0].sourceIndex, 14, '#three sourceIndex');
});
(0, _helpers.test)('id, tag, pseudo, and class selectors on different lines with indentation', '\t#one,\n\th1:after,\n\t\t.two', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1, '#one start line');
t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 2, '#one start column');
t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 5, '#one end column');
t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 1, '#one sourceIndex');
t.deepEqual(tree.nodes[1].nodes[0].source.start.line, 2, 'h1 start line');
t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 2, 'h1 start column');
t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 3, 'h1 end column');
t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 8, 'h1 sourceIndex');
t.deepEqual(tree.nodes[1].nodes[1].source.start.line, 2, ':after start line');
t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 4, ':after start column');
t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 9, ':after end column');
t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 10, ':after sourceIndex');
t.deepEqual(tree.nodes[2].nodes[0].source.start.line, 3, '.two start line');
t.deepEqual(tree.nodes[2].nodes[0].source.start.column, 3, '.two start column');
t.deepEqual(tree.nodes[2].nodes[0].source.end.column, 6, '.two end column');
t.deepEqual(tree.nodes[2].nodes[0].sourceIndex, 20, '.two sourceIndex');
});
(0, _helpers.test)('pseudo with arguments spanning multiple lines', 'h1:not(\n\t.one,\n\t.two\n)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1, ':not start line');
t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3, ':not start column');
t.deepEqual(tree.nodes[0].nodes[1].source.end.line, 4, ':not end line');
t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 1, ':not end column');
t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2, ':not sourceIndex');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].source.start.line, 2, '.one start line');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].source.start.column, 2, '.one start column');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].source.end.line, 2, '.one end line');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].source.end.column, 5, '.one end column');
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].sourceIndex, 9, '.one sourceIndex');
t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].source.start.line, 3, '.two start line');
t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].source.start.column, 2, '.two start column');
t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].source.end.line, 3, '.two end line');
t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].source.end.column, 5, '.two end column');
t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].sourceIndex, 16, '.two sourceIndex');
});

View File

@@ -0,0 +1,17 @@
"use strict";
var _ava = _interopRequireDefault(require("ava"));
var _stripComments = _interopRequireDefault(require("../../src/util/stripComments"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
(0, _ava["default"])("stripComments()", function (t) {
t.deepEqual((0, _stripComments["default"])("aaa/**/bbb"), "aaabbb");
t.deepEqual((0, _stripComments["default"])("aaa/*bbb"), "aaa");
t.deepEqual((0, _stripComments["default"])("aaa/*xxx*/bbb"), "aaabbb");
t.deepEqual((0, _stripComments["default"])("aaa/*/xxx/*/bbb"), "aaabbb");
t.deepEqual((0, _stripComments["default"])("aaa/*x*/bbb/**/"), "aaabbb");
t.deepEqual((0, _stripComments["default"])("/**/aaa/*x*/bbb/**/"), "aaabbb");
t.deepEqual((0, _stripComments["default"])("/**/"), "");
});

View File

@@ -0,0 +1,35 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('tag selector', 'h1', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
});
(0, _helpers.test)('multiple tag selectors', 'h1, h2', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[1].nodes[0].value, 'h2');
});
(0, _helpers.test)('extraneous non-combinating whitespace', ' h1 , h2 ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
t.deepEqual(tree.nodes[0].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.after, ' ');
t.deepEqual(tree.nodes[1].nodes[0].value, 'h2');
t.deepEqual(tree.nodes[1].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[1].nodes[0].spaces.after, ' ');
});
(0, _helpers.test)('tag with trailing comma', 'h1,', function (t, tree) {
t.deepEqual(tree.trailingComma, true);
});
(0, _helpers.test)('tag with trailing slash', 'h1\\', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'h1\\');
});
(0, _helpers.test)('tag with attribute', 'label[for="email"]', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'label');
t.deepEqual(tree.nodes[0].nodes[0].type, 'tag');
t.deepEqual(tree.nodes[0].nodes[1].value, 'email');
t.deepEqual(tree.nodes[0].nodes[1].attribute, 'for');
t.deepEqual(tree.nodes[0].nodes[1].operator, '=');
t.deepEqual(tree.nodes[0].nodes[1].type, 'attribute');
t.deepEqual(tree.nodes[0].nodes[1].quoteMark, '"');
});

View File

@@ -0,0 +1,42 @@
"use strict";
var _helpers = require("./util/helpers");
(0, _helpers.test)('universal selector', '*', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
t.deepEqual(tree.nodes[0].nodes[0].type, 'universal');
});
(0, _helpers.test)('lobotomized owl', '* + *', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'universal');
t.deepEqual(tree.nodes[0].nodes[1].type, 'combinator');
t.deepEqual(tree.nodes[0].nodes[2].type, 'universal');
});
(0, _helpers.test)('universal selector with descendant combinator', '* *', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'universal');
t.deepEqual(tree.nodes[0].nodes[1].type, 'combinator');
t.deepEqual(tree.nodes[0].nodes[2].type, 'universal');
});
(0, _helpers.test)('universal selector with descendant combinator and extraneous non-combinating whitespace', '* *', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].type, 'universal');
t.deepEqual(tree.nodes[0].nodes[1].type, 'combinator');
t.deepEqual(tree.nodes[0].nodes[2].type, 'universal');
});
(0, _helpers.test)('extraneous non-combinating whitespace', ' * , * ', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
t.deepEqual(tree.nodes[0].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.after, ' ');
t.deepEqual(tree.nodes[1].nodes[0].value, '*');
t.deepEqual(tree.nodes[1].nodes[0].spaces.before, ' ');
t.deepEqual(tree.nodes[1].nodes[0].spaces.after, ' ');
});
(0, _helpers.test)('qualified universal selector', '*[href] *:not(*.green)', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
t.deepEqual(tree.nodes[0].nodes[3].value, '*');
t.deepEqual(tree.nodes[0].nodes[4].nodes[0].nodes[0].value, '*');
});
(0, _helpers.test)('universal selector with pseudo', '*::--webkit-media-controls-play-button', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '*');
t.deepEqual(tree.nodes[0].nodes[0].type, 'universal');
t.deepEqual(tree.nodes[0].nodes[1].value, '::--webkit-media-controls-play-button');
t.deepEqual(tree.nodes[0].nodes[1].type, 'pseudo');
});

View File

@@ -0,0 +1,94 @@
"use strict";
exports.__esModule = true;
exports.test = test;
exports.nodeVersionAtLeast = nodeVersionAtLeast;
exports.nodeVersionBefore = nodeVersionBefore;
exports["throws"] = exports.parse = void 0;
var _process = _interopRequireDefault(require("process"));
var _util = _interopRequireDefault(require("util"));
var _ava = _interopRequireDefault(require("ava"));
var _semver = _interopRequireDefault(require("semver"));
var _index = _interopRequireDefault(require("../../index"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var parse = function parse(input, transform) {
return (0, _index["default"])(transform).processSync(input);
};
exports.parse = parse;
function test(spec, input, callback, only, disabled, serial) {
var _this = this;
if (only === void 0) {
only = false;
}
if (disabled === void 0) {
disabled = false;
}
if (serial === void 0) {
serial = false;
}
var tester = only ? _ava["default"].only : _ava["default"];
tester = disabled ? tester.skip : tester;
tester = serial ? tester.serial : tester;
if (callback) {
tester(spec + " (tree)", function (t) {
var tree = (0, _index["default"])().astSync(input);
var debug = _util["default"].inspect(tree, false, null);
return callback.call(_this, t, tree, debug);
});
}
tester(spec + " (toString)", function (t) {
var result = (0, _index["default"])().processSync(input);
t.deepEqual(result, input);
});
}
test.only = function (spec, input, callback) {
return test(spec, input, callback, true);
};
test.skip = function (spec, input, callback) {
return test(spec, input, callback, false, true);
};
test.serial = function (spec, input, callback) {
return test(spec, input, callback, false, false, true);
};
var _throws = function _throws(spec, input, validator) {
(0, _ava["default"])(spec + " (throws)", function (t) {
t["throws"](function () {
return (0, _index["default"])().processSync(input);
}, validator ? {
message: validator
} : {
instanceOf: Error
});
});
};
exports["throws"] = _throws;
function nodeVersionAtLeast(version) {
return _semver["default"].gte(_process["default"].versions.node, version);
}
function nodeVersionBefore(version) {
return _semver["default"].lt(_process["default"].versions.node, version);
}

View File

@@ -0,0 +1,50 @@
"use strict";
var _helpers = require("../util/helpers");
(0, _helpers.test)('id selector', '#foo', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo');
});
(0, _helpers.test)('escaped special char', '#w\\+', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'w+');
});
(0, _helpers.test)('tailing escape', '#foo\\', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo\\');
});
(0, _helpers.test)('double escape', '#wow\\\\k', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'wow\\k');
});
(0, _helpers.test)('leading numeric', '.\\31 23', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '123');
});
(0, _helpers.test)('emoji', '.\\🐐', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '🐐');
}); // https://www.w3.org/International/questions/qa-escapes#cssescapes
(0, _helpers.test)('hex escape', '.\\E9motion', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'émotion');
});
(0, _helpers.test)('hex escape with space', '.\\E9 dition', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'édition');
});
(0, _helpers.test)('hex escape with hex number', '.\\0000E9dition', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'édition');
});
(0, _helpers.test)('class selector with escaping', '.\\1D306', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '𝌆');
});
(0, _helpers.test)('class selector with escaping with more chars', '.\\1D306k', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, '𝌆k');
});
(0, _helpers.test)('class selector with escaping with more chars with whitespace', '.wow\\1D306 k', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, 'wow𝌆k');
});
(0, _helpers.test)('handles 0 value hex', '\\0', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, String.fromCodePoint(0xFFFD));
});
(0, _helpers.test)('handles lone surrogate value hex', '\\DBFF', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, String.fromCodePoint(0xFFFD));
});
(0, _helpers.test)('handles out of bound values', '\\110000', function (t, tree) {
t.deepEqual(tree.nodes[0].nodes[0].value, String.fromCodePoint(0xFFFD));
});

View File

@@ -0,0 +1,24 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _processor = _interopRequireDefault(require("./processor"));
var selectors = _interopRequireWildcard(require("./selectors"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var parser = function parser(processor) {
return new _processor["default"](processor);
};
Object.assign(parser, selectors);
delete parser.__esModule;
var _default = parser;
exports["default"] = _default;
module.exports = exports.default;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,206 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _parser = _interopRequireDefault(require("./parser"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var Processor = /*#__PURE__*/function () {
function Processor(func, options) {
this.func = func || function noop() {};
this.funcRes = null;
this.options = options;
}
var _proto = Processor.prototype;
_proto._shouldUpdateSelector = function _shouldUpdateSelector(rule, options) {
if (options === void 0) {
options = {};
}
var merged = Object.assign({}, this.options, options);
if (merged.updateSelector === false) {
return false;
} else {
return typeof rule !== "string";
}
};
_proto._isLossy = function _isLossy(options) {
if (options === void 0) {
options = {};
}
var merged = Object.assign({}, this.options, options);
if (merged.lossless === false) {
return true;
} else {
return false;
}
};
_proto._root = function _root(rule, options) {
if (options === void 0) {
options = {};
}
var parser = new _parser["default"](rule, this._parseOptions(options));
return parser.root;
};
_proto._parseOptions = function _parseOptions(options) {
return {
lossy: this._isLossy(options)
};
};
_proto._run = function _run(rule, options) {
var _this = this;
if (options === void 0) {
options = {};
}
return new Promise(function (resolve, reject) {
try {
var root = _this._root(rule, options);
Promise.resolve(_this.func(root)).then(function (transform) {
var string = undefined;
if (_this._shouldUpdateSelector(rule, options)) {
string = root.toString();
rule.selector = string;
}
return {
transform: transform,
root: root,
string: string
};
}).then(resolve, reject);
} catch (e) {
reject(e);
return;
}
});
};
_proto._runSync = function _runSync(rule, options) {
if (options === void 0) {
options = {};
}
var root = this._root(rule, options);
var transform = this.func(root);
if (transform && typeof transform.then === "function") {
throw new Error("Selector processor returned a promise to a synchronous call.");
}
var string = undefined;
if (options.updateSelector && typeof rule !== "string") {
string = root.toString();
rule.selector = string;
}
return {
transform: transform,
root: root,
string: string
};
}
/**
* Process rule into a selector AST.
*
* @param rule {postcss.Rule | string} The css selector to be processed
* @param options The options for processing
* @returns {Promise<parser.Root>} The AST of the selector after processing it.
*/
;
_proto.ast = function ast(rule, options) {
return this._run(rule, options).then(function (result) {
return result.root;
});
}
/**
* Process rule into a selector AST synchronously.
*
* @param rule {postcss.Rule | string} The css selector to be processed
* @param options The options for processing
* @returns {parser.Root} The AST of the selector after processing it.
*/
;
_proto.astSync = function astSync(rule, options) {
return this._runSync(rule, options).root;
}
/**
* Process a selector into a transformed value asynchronously
*
* @param rule {postcss.Rule | string} The css selector to be processed
* @param options The options for processing
* @returns {Promise<any>} The value returned by the processor.
*/
;
_proto.transform = function transform(rule, options) {
return this._run(rule, options).then(function (result) {
return result.transform;
});
}
/**
* Process a selector into a transformed value synchronously.
*
* @param rule {postcss.Rule | string} The css selector to be processed
* @param options The options for processing
* @returns {any} The value returned by the processor.
*/
;
_proto.transformSync = function transformSync(rule, options) {
return this._runSync(rule, options).transform;
}
/**
* Process a selector into a new selector string asynchronously.
*
* @param rule {postcss.Rule | string} The css selector to be processed
* @param options The options for processing
* @returns {string} the selector after processing.
*/
;
_proto.process = function process(rule, options) {
return this._run(rule, options).then(function (result) {
return result.string || result.root.toString();
});
}
/**
* Process a selector into a new selector string synchronously.
*
* @param rule {postcss.Rule | string} The css selector to be processed
* @param options The options for processing
* @returns {string} the selector after processing.
*/
;
_proto.processSync = function processSync(rule, options) {
var result = this._runSync(rule, options);
return result.string || result.root.toString();
};
return Processor;
}();
exports["default"] = Processor;
module.exports = exports.default;

View File

@@ -0,0 +1,515 @@
"use strict";
exports.__esModule = true;
exports.unescapeValue = unescapeValue;
exports["default"] = void 0;
var _cssesc = _interopRequireDefault(require("cssesc"));
var _unesc = _interopRequireDefault(require("../util/unesc"));
var _namespace = _interopRequireDefault(require("./namespace"));
var _types = require("./types");
var _CSSESC_QUOTE_OPTIONS;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var deprecate = require("util-deprecate");
var WRAPPED_IN_QUOTES = /^('|")([^]*)\1$/;
var warnOfDeprecatedValueAssignment = deprecate(function () {}, "Assigning an attribute a value containing characters that might need to be escaped is deprecated. " + "Call attribute.setValue() instead.");
var warnOfDeprecatedQuotedAssignment = deprecate(function () {}, "Assigning attr.quoted is deprecated and has no effect. Assign to attr.quoteMark instead.");
var warnOfDeprecatedConstructor = deprecate(function () {}, "Constructing an Attribute selector with a value without specifying quoteMark is deprecated. Note: The value should be unescaped now.");
function unescapeValue(value) {
var deprecatedUsage = false;
var quoteMark = null;
var unescaped = value;
var m = unescaped.match(WRAPPED_IN_QUOTES);
if (m) {
quoteMark = m[1];
unescaped = m[2];
}
unescaped = (0, _unesc["default"])(unescaped);
if (unescaped !== value) {
deprecatedUsage = true;
}
return {
deprecatedUsage: deprecatedUsage,
unescaped: unescaped,
quoteMark: quoteMark
};
}
function handleDeprecatedContructorOpts(opts) {
if (opts.quoteMark !== undefined) {
return opts;
}
if (opts.value === undefined) {
return opts;
}
warnOfDeprecatedConstructor();
var _unescapeValue = unescapeValue(opts.value),
quoteMark = _unescapeValue.quoteMark,
unescaped = _unescapeValue.unescaped;
if (!opts.raws) {
opts.raws = {};
}
if (opts.raws.value === undefined) {
opts.raws.value = opts.value;
}
opts.value = unescaped;
opts.quoteMark = quoteMark;
return opts;
}
var Attribute = /*#__PURE__*/function (_Namespace) {
_inheritsLoose(Attribute, _Namespace);
function Attribute(opts) {
var _this;
if (opts === void 0) {
opts = {};
}
_this = _Namespace.call(this, handleDeprecatedContructorOpts(opts)) || this;
_this.type = _types.ATTRIBUTE;
_this.raws = _this.raws || {};
Object.defineProperty(_this.raws, 'unquoted', {
get: deprecate(function () {
return _this.value;
}, "attr.raws.unquoted is deprecated. Call attr.value instead."),
set: deprecate(function () {
return _this.value;
}, "Setting attr.raws.unquoted is deprecated and has no effect. attr.value is unescaped by default now.")
});
_this._constructed = true;
return _this;
}
/**
* Returns the Attribute's value quoted such that it would be legal to use
* in the value of a css file. The original value's quotation setting
* used for stringification is left unchanged. See `setValue(value, options)`
* if you want to control the quote settings of a new value for the attribute.
*
* You can also change the quotation used for the current value by setting quoteMark.
*
* Options:
* * quoteMark {'"' | "'" | null} - Use this value to quote the value. If this
* option is not set, the original value for quoteMark will be used. If
* indeterminate, a double quote is used. The legal values are:
* * `null` - the value will be unquoted and characters will be escaped as necessary.
* * `'` - the value will be quoted with a single quote and single quotes are escaped.
* * `"` - the value will be quoted with a double quote and double quotes are escaped.
* * preferCurrentQuoteMark {boolean} - if true, prefer the source quote mark
* over the quoteMark option value.
* * smart {boolean} - if true, will select a quote mark based on the value
* and the other options specified here. See the `smartQuoteMark()`
* method.
**/
var _proto = Attribute.prototype;
_proto.getQuotedValue = function getQuotedValue(options) {
if (options === void 0) {
options = {};
}
var quoteMark = this._determineQuoteMark(options);
var cssescopts = CSSESC_QUOTE_OPTIONS[quoteMark];
var escaped = (0, _cssesc["default"])(this._value, cssescopts);
return escaped;
};
_proto._determineQuoteMark = function _determineQuoteMark(options) {
return options.smart ? this.smartQuoteMark(options) : this.preferredQuoteMark(options);
}
/**
* Set the unescaped value with the specified quotation options. The value
* provided must not include any wrapping quote marks -- those quotes will
* be interpreted as part of the value and escaped accordingly.
*/
;
_proto.setValue = function setValue(value, options) {
if (options === void 0) {
options = {};
}
this._value = value;
this._quoteMark = this._determineQuoteMark(options);
this._syncRawValue();
}
/**
* Intelligently select a quoteMark value based on the value's contents. If
* the value is a legal CSS ident, it will not be quoted. Otherwise a quote
* mark will be picked that minimizes the number of escapes.
*
* If there's no clear winner, the quote mark from these options is used,
* then the source quote mark (this is inverted if `preferCurrentQuoteMark` is
* true). If the quoteMark is unspecified, a double quote is used.
*
* @param options This takes the quoteMark and preferCurrentQuoteMark options
* from the quoteValue method.
*/
;
_proto.smartQuoteMark = function smartQuoteMark(options) {
var v = this.value;
var numSingleQuotes = v.replace(/[^']/g, '').length;
var numDoubleQuotes = v.replace(/[^"]/g, '').length;
if (numSingleQuotes + numDoubleQuotes === 0) {
var escaped = (0, _cssesc["default"])(v, {
isIdentifier: true
});
if (escaped === v) {
return Attribute.NO_QUOTE;
} else {
var pref = this.preferredQuoteMark(options);
if (pref === Attribute.NO_QUOTE) {
// pick a quote mark that isn't none and see if it's smaller
var quote = this.quoteMark || options.quoteMark || Attribute.DOUBLE_QUOTE;
var opts = CSSESC_QUOTE_OPTIONS[quote];
var quoteValue = (0, _cssesc["default"])(v, opts);
if (quoteValue.length < escaped.length) {
return quote;
}
}
return pref;
}
} else if (numDoubleQuotes === numSingleQuotes) {
return this.preferredQuoteMark(options);
} else if (numDoubleQuotes < numSingleQuotes) {
return Attribute.DOUBLE_QUOTE;
} else {
return Attribute.SINGLE_QUOTE;
}
}
/**
* Selects the preferred quote mark based on the options and the current quote mark value.
* If you want the quote mark to depend on the attribute value, call `smartQuoteMark(opts)`
* instead.
*/
;
_proto.preferredQuoteMark = function preferredQuoteMark(options) {
var quoteMark = options.preferCurrentQuoteMark ? this.quoteMark : options.quoteMark;
if (quoteMark === undefined) {
quoteMark = options.preferCurrentQuoteMark ? options.quoteMark : this.quoteMark;
}
if (quoteMark === undefined) {
quoteMark = Attribute.DOUBLE_QUOTE;
}
return quoteMark;
};
_proto._syncRawValue = function _syncRawValue() {
var rawValue = (0, _cssesc["default"])(this._value, CSSESC_QUOTE_OPTIONS[this.quoteMark]);
if (rawValue === this._value) {
if (this.raws) {
delete this.raws.value;
}
} else {
this.raws.value = rawValue;
}
};
_proto._handleEscapes = function _handleEscapes(prop, value) {
if (this._constructed) {
var escaped = (0, _cssesc["default"])(value, {
isIdentifier: true
});
if (escaped !== value) {
this.raws[prop] = escaped;
} else {
delete this.raws[prop];
}
}
};
_proto._spacesFor = function _spacesFor(name) {
var attrSpaces = {
before: '',
after: ''
};
var spaces = this.spaces[name] || {};
var rawSpaces = this.raws.spaces && this.raws.spaces[name] || {};
return Object.assign(attrSpaces, spaces, rawSpaces);
};
_proto._stringFor = function _stringFor(name, spaceName, concat) {
if (spaceName === void 0) {
spaceName = name;
}
if (concat === void 0) {
concat = defaultAttrConcat;
}
var attrSpaces = this._spacesFor(spaceName);
return concat(this.stringifyProperty(name), attrSpaces);
}
/**
* returns the offset of the attribute part specified relative to the
* start of the node of the output string.
*
* * "ns" - alias for "namespace"
* * "namespace" - the namespace if it exists.
* * "attribute" - the attribute name
* * "attributeNS" - the start of the attribute or its namespace
* * "operator" - the match operator of the attribute
* * "value" - The value (string or identifier)
* * "insensitive" - the case insensitivity flag;
* @param part One of the possible values inside an attribute.
* @returns -1 if the name is invalid or the value doesn't exist in this attribute.
*/
;
_proto.offsetOf = function offsetOf(name) {
var count = 1;
var attributeSpaces = this._spacesFor("attribute");
count += attributeSpaces.before.length;
if (name === "namespace" || name === "ns") {
return this.namespace ? count : -1;
}
if (name === "attributeNS") {
return count;
}
count += this.namespaceString.length;
if (this.namespace) {
count += 1;
}
if (name === "attribute") {
return count;
}
count += this.stringifyProperty("attribute").length;
count += attributeSpaces.after.length;
var operatorSpaces = this._spacesFor("operator");
count += operatorSpaces.before.length;
var operator = this.stringifyProperty("operator");
if (name === "operator") {
return operator ? count : -1;
}
count += operator.length;
count += operatorSpaces.after.length;
var valueSpaces = this._spacesFor("value");
count += valueSpaces.before.length;
var value = this.stringifyProperty("value");
if (name === "value") {
return value ? count : -1;
}
count += value.length;
count += valueSpaces.after.length;
var insensitiveSpaces = this._spacesFor("insensitive");
count += insensitiveSpaces.before.length;
if (name === "insensitive") {
return this.insensitive ? count : -1;
}
return -1;
};
_proto.toString = function toString() {
var _this2 = this;
var selector = [this.rawSpaceBefore, '['];
selector.push(this._stringFor('qualifiedAttribute', 'attribute'));
if (this.operator && (this.value || this.value === '')) {
selector.push(this._stringFor('operator'));
selector.push(this._stringFor('value'));
selector.push(this._stringFor('insensitiveFlag', 'insensitive', function (attrValue, attrSpaces) {
if (attrValue.length > 0 && !_this2.quoted && attrSpaces.before.length === 0 && !(_this2.spaces.value && _this2.spaces.value.after)) {
attrSpaces.before = " ";
}
return defaultAttrConcat(attrValue, attrSpaces);
}));
}
selector.push(']');
selector.push(this.rawSpaceAfter);
return selector.join('');
};
_createClass(Attribute, [{
key: "quoted",
get: function get() {
var qm = this.quoteMark;
return qm === "'" || qm === '"';
},
set: function set(value) {
warnOfDeprecatedQuotedAssignment();
}
/**
* returns a single (`'`) or double (`"`) quote character if the value is quoted.
* returns `null` if the value is not quoted.
* returns `undefined` if the quotation state is unknown (this can happen when
* the attribute is constructed without specifying a quote mark.)
*/
}, {
key: "quoteMark",
get: function get() {
return this._quoteMark;
}
/**
* Set the quote mark to be used by this attribute's value.
* If the quote mark changes, the raw (escaped) value at `attr.raws.value` of the attribute
* value is updated accordingly.
*
* @param {"'" | '"' | null} quoteMark The quote mark or `null` if the value should be unquoted.
*/
,
set: function set(quoteMark) {
if (!this._constructed) {
this._quoteMark = quoteMark;
return;
}
if (this._quoteMark !== quoteMark) {
this._quoteMark = quoteMark;
this._syncRawValue();
}
}
}, {
key: "qualifiedAttribute",
get: function get() {
return this.qualifiedName(this.raws.attribute || this.attribute);
}
}, {
key: "insensitiveFlag",
get: function get() {
return this.insensitive ? 'i' : '';
}
}, {
key: "value",
get: function get() {
return this._value;
}
/**
* Before 3.0, the value had to be set to an escaped value including any wrapped
* quote marks. In 3.0, the semantics of `Attribute.value` changed so that the value
* is unescaped during parsing and any quote marks are removed.
*
* Because the ambiguity of this semantic change, if you set `attr.value = newValue`,
* a deprecation warning is raised when the new value contains any characters that would
* require escaping (including if it contains wrapped quotes).
*
* Instead, you should call `attr.setValue(newValue, opts)` and pass options that describe
* how the new value is quoted.
*/
,
set: function set(v) {
if (this._constructed) {
var _unescapeValue2 = unescapeValue(v),
deprecatedUsage = _unescapeValue2.deprecatedUsage,
unescaped = _unescapeValue2.unescaped,
quoteMark = _unescapeValue2.quoteMark;
if (deprecatedUsage) {
warnOfDeprecatedValueAssignment();
}
if (unescaped === this._value && quoteMark === this._quoteMark) {
return;
}
this._value = unescaped;
this._quoteMark = quoteMark;
this._syncRawValue();
} else {
this._value = v;
}
}
}, {
key: "attribute",
get: function get() {
return this._attribute;
},
set: function set(name) {
this._handleEscapes("attribute", name);
this._attribute = name;
}
}]);
return Attribute;
}(_namespace["default"]);
exports["default"] = Attribute;
Attribute.NO_QUOTE = null;
Attribute.SINGLE_QUOTE = "'";
Attribute.DOUBLE_QUOTE = '"';
var CSSESC_QUOTE_OPTIONS = (_CSSESC_QUOTE_OPTIONS = {
"'": {
quotes: 'single',
wrap: true
},
'"': {
quotes: 'double',
wrap: true
}
}, _CSSESC_QUOTE_OPTIONS[null] = {
isIdentifier: true
}, _CSSESC_QUOTE_OPTIONS);
function defaultAttrConcat(attrValue, attrSpaces) {
return "" + attrSpaces.before + attrValue + attrSpaces.after;
}

View File

@@ -0,0 +1,69 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _cssesc = _interopRequireDefault(require("cssesc"));
var _util = require("../util");
var _node = _interopRequireDefault(require("./node"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var ClassName = /*#__PURE__*/function (_Node) {
_inheritsLoose(ClassName, _Node);
function ClassName(opts) {
var _this;
_this = _Node.call(this, opts) || this;
_this.type = _types.CLASS;
_this._constructed = true;
return _this;
}
var _proto = ClassName.prototype;
_proto.valueToString = function valueToString() {
return '.' + _Node.prototype.valueToString.call(this);
};
_createClass(ClassName, [{
key: "value",
get: function get() {
return this._value;
},
set: function set(v) {
if (this._constructed) {
var escaped = (0, _cssesc["default"])(v, {
isIdentifier: true
});
if (escaped !== v) {
(0, _util.ensureObject)(this, "raws");
this.raws.value = escaped;
} else if (this.raws) {
delete this.raws.value;
}
}
this._value = v;
}
}]);
return ClassName;
}(_node["default"]);
exports["default"] = ClassName;
module.exports = exports.default;

View File

@@ -0,0 +1,31 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _node = _interopRequireDefault(require("./node"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Combinator = /*#__PURE__*/function (_Node) {
_inheritsLoose(Combinator, _Node);
function Combinator(opts) {
var _this;
_this = _Node.call(this, opts) || this;
_this.type = _types.COMBINATOR;
return _this;
}
return Combinator;
}(_node["default"]);
exports["default"] = Combinator;
module.exports = exports.default;

View File

@@ -0,0 +1,31 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _node = _interopRequireDefault(require("./node"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Comment = /*#__PURE__*/function (_Node) {
_inheritsLoose(Comment, _Node);
function Comment(opts) {
var _this;
_this = _Node.call(this, opts) || this;
_this.type = _types.COMMENT;
return _this;
}
return Comment;
}(_node["default"]);
exports["default"] = Comment;
module.exports = exports.default;

View File

@@ -0,0 +1,102 @@
"use strict";
exports.__esModule = true;
exports.universal = exports.tag = exports.string = exports.selector = exports.root = exports.pseudo = exports.nesting = exports.id = exports.comment = exports.combinator = exports.className = exports.attribute = void 0;
var _attribute = _interopRequireDefault(require("./attribute"));
var _className = _interopRequireDefault(require("./className"));
var _combinator = _interopRequireDefault(require("./combinator"));
var _comment = _interopRequireDefault(require("./comment"));
var _id = _interopRequireDefault(require("./id"));
var _nesting = _interopRequireDefault(require("./nesting"));
var _pseudo = _interopRequireDefault(require("./pseudo"));
var _root = _interopRequireDefault(require("./root"));
var _selector = _interopRequireDefault(require("./selector"));
var _string = _interopRequireDefault(require("./string"));
var _tag = _interopRequireDefault(require("./tag"));
var _universal = _interopRequireDefault(require("./universal"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var attribute = function attribute(opts) {
return new _attribute["default"](opts);
};
exports.attribute = attribute;
var className = function className(opts) {
return new _className["default"](opts);
};
exports.className = className;
var combinator = function combinator(opts) {
return new _combinator["default"](opts);
};
exports.combinator = combinator;
var comment = function comment(opts) {
return new _comment["default"](opts);
};
exports.comment = comment;
var id = function id(opts) {
return new _id["default"](opts);
};
exports.id = id;
var nesting = function nesting(opts) {
return new _nesting["default"](opts);
};
exports.nesting = nesting;
var pseudo = function pseudo(opts) {
return new _pseudo["default"](opts);
};
exports.pseudo = pseudo;
var root = function root(opts) {
return new _root["default"](opts);
};
exports.root = root;
var selector = function selector(opts) {
return new _selector["default"](opts);
};
exports.selector = selector;
var string = function string(opts) {
return new _string["default"](opts);
};
exports.string = string;
var tag = function tag(opts) {
return new _tag["default"](opts);
};
exports.tag = tag;
var universal = function universal(opts) {
return new _universal["default"](opts);
};
exports.universal = universal;

View File

@@ -0,0 +1,395 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _node = _interopRequireDefault(require("./node"));
var types = _interopRequireWildcard(require("./types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Container = /*#__PURE__*/function (_Node) {
_inheritsLoose(Container, _Node);
function Container(opts) {
var _this;
_this = _Node.call(this, opts) || this;
if (!_this.nodes) {
_this.nodes = [];
}
return _this;
}
var _proto = Container.prototype;
_proto.append = function append(selector) {
selector.parent = this;
this.nodes.push(selector);
return this;
};
_proto.prepend = function prepend(selector) {
selector.parent = this;
this.nodes.unshift(selector);
return this;
};
_proto.at = function at(index) {
return this.nodes[index];
};
_proto.index = function index(child) {
if (typeof child === 'number') {
return child;
}
return this.nodes.indexOf(child);
};
_proto.removeChild = function removeChild(child) {
child = this.index(child);
this.at(child).parent = undefined;
this.nodes.splice(child, 1);
var index;
for (var id in this.indexes) {
index = this.indexes[id];
if (index >= child) {
this.indexes[id] = index - 1;
}
}
return this;
};
_proto.removeAll = function removeAll() {
for (var _iterator = _createForOfIteratorHelperLoose(this.nodes), _step; !(_step = _iterator()).done;) {
var node = _step.value;
node.parent = undefined;
}
this.nodes = [];
return this;
};
_proto.empty = function empty() {
return this.removeAll();
};
_proto.insertAfter = function insertAfter(oldNode, newNode) {
newNode.parent = this;
var oldIndex = this.index(oldNode);
this.nodes.splice(oldIndex + 1, 0, newNode);
newNode.parent = this;
var index;
for (var id in this.indexes) {
index = this.indexes[id];
if (oldIndex <= index) {
this.indexes[id] = index + 1;
}
}
return this;
};
_proto.insertBefore = function insertBefore(oldNode, newNode) {
newNode.parent = this;
var oldIndex = this.index(oldNode);
this.nodes.splice(oldIndex, 0, newNode);
newNode.parent = this;
var index;
for (var id in this.indexes) {
index = this.indexes[id];
if (index <= oldIndex) {
this.indexes[id] = index + 1;
}
}
return this;
};
_proto._findChildAtPosition = function _findChildAtPosition(line, col) {
var found = undefined;
this.each(function (node) {
if (node.atPosition) {
var foundChild = node.atPosition(line, col);
if (foundChild) {
found = foundChild;
return false;
}
} else if (node.isAtPosition(line, col)) {
found = node;
return false;
}
});
return found;
}
/**
* Return the most specific node at the line and column number given.
* The source location is based on the original parsed location, locations aren't
* updated as selector nodes are mutated.
*
* Note that this location is relative to the location of the first character
* of the selector, and not the location of the selector in the overall document
* when used in conjunction with postcss.
*
* If not found, returns undefined.
* @param {number} line The line number of the node to find. (1-based index)
* @param {number} col The column number of the node to find. (1-based index)
*/
;
_proto.atPosition = function atPosition(line, col) {
if (this.isAtPosition(line, col)) {
return this._findChildAtPosition(line, col) || this;
} else {
return undefined;
}
};
_proto._inferEndPosition = function _inferEndPosition() {
if (this.last && this.last.source && this.last.source.end) {
this.source = this.source || {};
this.source.end = this.source.end || {};
Object.assign(this.source.end, this.last.source.end);
}
};
_proto.each = function each(callback) {
if (!this.lastEach) {
this.lastEach = 0;
}
if (!this.indexes) {
this.indexes = {};
}
this.lastEach++;
var id = this.lastEach;
this.indexes[id] = 0;
if (!this.length) {
return undefined;
}
var index, result;
while (this.indexes[id] < this.length) {
index = this.indexes[id];
result = callback(this.at(index), index);
if (result === false) {
break;
}
this.indexes[id] += 1;
}
delete this.indexes[id];
if (result === false) {
return false;
}
};
_proto.walk = function walk(callback) {
return this.each(function (node, i) {
var result = callback(node, i);
if (result !== false && node.length) {
result = node.walk(callback);
}
if (result === false) {
return false;
}
});
};
_proto.walkAttributes = function walkAttributes(callback) {
var _this2 = this;
return this.walk(function (selector) {
if (selector.type === types.ATTRIBUTE) {
return callback.call(_this2, selector);
}
});
};
_proto.walkClasses = function walkClasses(callback) {
var _this3 = this;
return this.walk(function (selector) {
if (selector.type === types.CLASS) {
return callback.call(_this3, selector);
}
});
};
_proto.walkCombinators = function walkCombinators(callback) {
var _this4 = this;
return this.walk(function (selector) {
if (selector.type === types.COMBINATOR) {
return callback.call(_this4, selector);
}
});
};
_proto.walkComments = function walkComments(callback) {
var _this5 = this;
return this.walk(function (selector) {
if (selector.type === types.COMMENT) {
return callback.call(_this5, selector);
}
});
};
_proto.walkIds = function walkIds(callback) {
var _this6 = this;
return this.walk(function (selector) {
if (selector.type === types.ID) {
return callback.call(_this6, selector);
}
});
};
_proto.walkNesting = function walkNesting(callback) {
var _this7 = this;
return this.walk(function (selector) {
if (selector.type === types.NESTING) {
return callback.call(_this7, selector);
}
});
};
_proto.walkPseudos = function walkPseudos(callback) {
var _this8 = this;
return this.walk(function (selector) {
if (selector.type === types.PSEUDO) {
return callback.call(_this8, selector);
}
});
};
_proto.walkTags = function walkTags(callback) {
var _this9 = this;
return this.walk(function (selector) {
if (selector.type === types.TAG) {
return callback.call(_this9, selector);
}
});
};
_proto.walkUniversals = function walkUniversals(callback) {
var _this10 = this;
return this.walk(function (selector) {
if (selector.type === types.UNIVERSAL) {
return callback.call(_this10, selector);
}
});
};
_proto.split = function split(callback) {
var _this11 = this;
var current = [];
return this.reduce(function (memo, node, index) {
var split = callback.call(_this11, node);
current.push(node);
if (split) {
memo.push(current);
current = [];
} else if (index === _this11.length - 1) {
memo.push(current);
}
return memo;
}, []);
};
_proto.map = function map(callback) {
return this.nodes.map(callback);
};
_proto.reduce = function reduce(callback, memo) {
return this.nodes.reduce(callback, memo);
};
_proto.every = function every(callback) {
return this.nodes.every(callback);
};
_proto.some = function some(callback) {
return this.nodes.some(callback);
};
_proto.filter = function filter(callback) {
return this.nodes.filter(callback);
};
_proto.sort = function sort(callback) {
return this.nodes.sort(callback);
};
_proto.toString = function toString() {
return this.map(String).join('');
};
_createClass(Container, [{
key: "first",
get: function get() {
return this.at(0);
}
}, {
key: "last",
get: function get() {
return this.at(this.length - 1);
}
}, {
key: "length",
get: function get() {
return this.nodes.length;
}
}]);
return Container;
}(_node["default"]);
exports["default"] = Container;
module.exports = exports.default;

View File

@@ -0,0 +1,64 @@
"use strict";
exports.__esModule = true;
exports.isNode = isNode;
exports.isPseudoElement = isPseudoElement;
exports.isPseudoClass = isPseudoClass;
exports.isContainer = isContainer;
exports.isNamespace = isNamespace;
exports.isUniversal = exports.isTag = exports.isString = exports.isSelector = exports.isRoot = exports.isPseudo = exports.isNesting = exports.isIdentifier = exports.isComment = exports.isCombinator = exports.isClassName = exports.isAttribute = void 0;
var _types = require("./types");
var _IS_TYPE;
var IS_TYPE = (_IS_TYPE = {}, _IS_TYPE[_types.ATTRIBUTE] = true, _IS_TYPE[_types.CLASS] = true, _IS_TYPE[_types.COMBINATOR] = true, _IS_TYPE[_types.COMMENT] = true, _IS_TYPE[_types.ID] = true, _IS_TYPE[_types.NESTING] = true, _IS_TYPE[_types.PSEUDO] = true, _IS_TYPE[_types.ROOT] = true, _IS_TYPE[_types.SELECTOR] = true, _IS_TYPE[_types.STRING] = true, _IS_TYPE[_types.TAG] = true, _IS_TYPE[_types.UNIVERSAL] = true, _IS_TYPE);
function isNode(node) {
return typeof node === "object" && IS_TYPE[node.type];
}
function isNodeType(type, node) {
return isNode(node) && node.type === type;
}
var isAttribute = isNodeType.bind(null, _types.ATTRIBUTE);
exports.isAttribute = isAttribute;
var isClassName = isNodeType.bind(null, _types.CLASS);
exports.isClassName = isClassName;
var isCombinator = isNodeType.bind(null, _types.COMBINATOR);
exports.isCombinator = isCombinator;
var isComment = isNodeType.bind(null, _types.COMMENT);
exports.isComment = isComment;
var isIdentifier = isNodeType.bind(null, _types.ID);
exports.isIdentifier = isIdentifier;
var isNesting = isNodeType.bind(null, _types.NESTING);
exports.isNesting = isNesting;
var isPseudo = isNodeType.bind(null, _types.PSEUDO);
exports.isPseudo = isPseudo;
var isRoot = isNodeType.bind(null, _types.ROOT);
exports.isRoot = isRoot;
var isSelector = isNodeType.bind(null, _types.SELECTOR);
exports.isSelector = isSelector;
var isString = isNodeType.bind(null, _types.STRING);
exports.isString = isString;
var isTag = isNodeType.bind(null, _types.TAG);
exports.isTag = isTag;
var isUniversal = isNodeType.bind(null, _types.UNIVERSAL);
exports.isUniversal = isUniversal;
function isPseudoElement(node) {
return isPseudo(node) && node.value && (node.value.startsWith("::") || node.value.toLowerCase() === ":before" || node.value.toLowerCase() === ":after");
}
function isPseudoClass(node) {
return isPseudo(node) && !isPseudoElement(node);
}
function isContainer(node) {
return !!(isNode(node) && node.walk);
}
function isNamespace(node) {
return isAttribute(node) || isTag(node);
}

View File

@@ -0,0 +1,37 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _node = _interopRequireDefault(require("./node"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var ID = /*#__PURE__*/function (_Node) {
_inheritsLoose(ID, _Node);
function ID(opts) {
var _this;
_this = _Node.call(this, opts) || this;
_this.type = _types.ID;
return _this;
}
var _proto = ID.prototype;
_proto.valueToString = function valueToString() {
return '#' + _Node.prototype.valueToString.call(this);
};
return ID;
}(_node["default"]);
exports["default"] = ID;
module.exports = exports.default;

View File

@@ -0,0 +1,27 @@
"use strict";
exports.__esModule = true;
var _types = require("./types");
Object.keys(_types).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _types[key]) return;
exports[key] = _types[key];
});
var _constructors = require("./constructors");
Object.keys(_constructors).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _constructors[key]) return;
exports[key] = _constructors[key];
});
var _guards = require("./guards");
Object.keys(_guards).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _guards[key]) return;
exports[key] = _guards[key];
});

View File

@@ -0,0 +1,101 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _cssesc = _interopRequireDefault(require("cssesc"));
var _util = require("../util");
var _node = _interopRequireDefault(require("./node"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Namespace = /*#__PURE__*/function (_Node) {
_inheritsLoose(Namespace, _Node);
function Namespace() {
return _Node.apply(this, arguments) || this;
}
var _proto = Namespace.prototype;
_proto.qualifiedName = function qualifiedName(value) {
if (this.namespace) {
return this.namespaceString + "|" + value;
} else {
return value;
}
};
_proto.valueToString = function valueToString() {
return this.qualifiedName(_Node.prototype.valueToString.call(this));
};
_createClass(Namespace, [{
key: "namespace",
get: function get() {
return this._namespace;
},
set: function set(namespace) {
if (namespace === true || namespace === "*" || namespace === "&") {
this._namespace = namespace;
if (this.raws) {
delete this.raws.namespace;
}
return;
}
var escaped = (0, _cssesc["default"])(namespace, {
isIdentifier: true
});
this._namespace = namespace;
if (escaped !== namespace) {
(0, _util.ensureObject)(this, "raws");
this.raws.namespace = escaped;
} else if (this.raws) {
delete this.raws.namespace;
}
}
}, {
key: "ns",
get: function get() {
return this._namespace;
},
set: function set(namespace) {
this.namespace = namespace;
}
}, {
key: "namespaceString",
get: function get() {
if (this.namespace) {
var ns = this.stringifyProperty("namespace");
if (ns === true) {
return '';
} else {
return ns;
}
} else {
return '';
}
}
}]);
return Namespace;
}(_node["default"]);
exports["default"] = Namespace;
;
module.exports = exports.default;

View File

@@ -0,0 +1,32 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _node = _interopRequireDefault(require("./node"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Nesting = /*#__PURE__*/function (_Node) {
_inheritsLoose(Nesting, _Node);
function Nesting(opts) {
var _this;
_this = _Node.call(this, opts) || this;
_this.type = _types.NESTING;
_this.value = '&';
return _this;
}
return Nesting;
}(_node["default"]);
exports["default"] = Nesting;
module.exports = exports.default;

View File

@@ -0,0 +1,239 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _util = require("../util");
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var cloneNode = function cloneNode(obj, parent) {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
var cloned = new obj.constructor();
for (var i in obj) {
if (!obj.hasOwnProperty(i)) {
continue;
}
var value = obj[i];
var type = typeof value;
if (i === 'parent' && type === 'object') {
if (parent) {
cloned[i] = parent;
}
} else if (value instanceof Array) {
cloned[i] = value.map(function (j) {
return cloneNode(j, cloned);
});
} else {
cloned[i] = cloneNode(value, cloned);
}
}
return cloned;
};
var Node = /*#__PURE__*/function () {
function Node(opts) {
if (opts === void 0) {
opts = {};
}
Object.assign(this, opts);
this.spaces = this.spaces || {};
this.spaces.before = this.spaces.before || '';
this.spaces.after = this.spaces.after || '';
}
var _proto = Node.prototype;
_proto.remove = function remove() {
if (this.parent) {
this.parent.removeChild(this);
}
this.parent = undefined;
return this;
};
_proto.replaceWith = function replaceWith() {
if (this.parent) {
for (var index in arguments) {
this.parent.insertBefore(this, arguments[index]);
}
this.remove();
}
return this;
};
_proto.next = function next() {
return this.parent.at(this.parent.index(this) + 1);
};
_proto.prev = function prev() {
return this.parent.at(this.parent.index(this) - 1);
};
_proto.clone = function clone(overrides) {
if (overrides === void 0) {
overrides = {};
}
var cloned = cloneNode(this);
for (var name in overrides) {
cloned[name] = overrides[name];
}
return cloned;
}
/**
* Some non-standard syntax doesn't follow normal escaping rules for css.
* This allows non standard syntax to be appended to an existing property
* by specifying the escaped value. By specifying the escaped value,
* illegal characters are allowed to be directly inserted into css output.
* @param {string} name the property to set
* @param {any} value the unescaped value of the property
* @param {string} valueEscaped optional. the escaped value of the property.
*/
;
_proto.appendToPropertyAndEscape = function appendToPropertyAndEscape(name, value, valueEscaped) {
if (!this.raws) {
this.raws = {};
}
var originalValue = this[name];
var originalEscaped = this.raws[name];
this[name] = originalValue + value; // this may trigger a setter that updates raws, so it has to be set first.
if (originalEscaped || valueEscaped !== value) {
this.raws[name] = (originalEscaped || originalValue) + valueEscaped;
} else {
delete this.raws[name]; // delete any escaped value that was created by the setter.
}
}
/**
* Some non-standard syntax doesn't follow normal escaping rules for css.
* This allows the escaped value to be specified directly, allowing illegal
* characters to be directly inserted into css output.
* @param {string} name the property to set
* @param {any} value the unescaped value of the property
* @param {string} valueEscaped the escaped value of the property.
*/
;
_proto.setPropertyAndEscape = function setPropertyAndEscape(name, value, valueEscaped) {
if (!this.raws) {
this.raws = {};
}
this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
this.raws[name] = valueEscaped;
}
/**
* When you want a value to passed through to CSS directly. This method
* deletes the corresponding raw value causing the stringifier to fallback
* to the unescaped value.
* @param {string} name the property to set.
* @param {any} value The value that is both escaped and unescaped.
*/
;
_proto.setPropertyWithoutEscape = function setPropertyWithoutEscape(name, value) {
this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
if (this.raws) {
delete this.raws[name];
}
}
/**
*
* @param {number} line The number (starting with 1)
* @param {number} column The column number (starting with 1)
*/
;
_proto.isAtPosition = function isAtPosition(line, column) {
if (this.source && this.source.start && this.source.end) {
if (this.source.start.line > line) {
return false;
}
if (this.source.end.line < line) {
return false;
}
if (this.source.start.line === line && this.source.start.column > column) {
return false;
}
if (this.source.end.line === line && this.source.end.column < column) {
return false;
}
return true;
}
return undefined;
};
_proto.stringifyProperty = function stringifyProperty(name) {
return this.raws && this.raws[name] || this[name];
};
_proto.valueToString = function valueToString() {
return String(this.stringifyProperty("value"));
};
_proto.toString = function toString() {
return [this.rawSpaceBefore, this.valueToString(), this.rawSpaceAfter].join('');
};
_createClass(Node, [{
key: "rawSpaceBefore",
get: function get() {
var rawSpace = this.raws && this.raws.spaces && this.raws.spaces.before;
if (rawSpace === undefined) {
rawSpace = this.spaces && this.spaces.before;
}
return rawSpace || "";
},
set: function set(raw) {
(0, _util.ensureObject)(this, "raws", "spaces");
this.raws.spaces.before = raw;
}
}, {
key: "rawSpaceAfter",
get: function get() {
var rawSpace = this.raws && this.raws.spaces && this.raws.spaces.after;
if (rawSpace === undefined) {
rawSpace = this.spaces.after;
}
return rawSpace || "";
},
set: function set(raw) {
(0, _util.ensureObject)(this, "raws", "spaces");
this.raws.spaces.after = raw;
}
}]);
return Node;
}();
exports["default"] = Node;
module.exports = exports.default;

View File

@@ -0,0 +1,38 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _container = _interopRequireDefault(require("./container"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Pseudo = /*#__PURE__*/function (_Container) {
_inheritsLoose(Pseudo, _Container);
function Pseudo(opts) {
var _this;
_this = _Container.call(this, opts) || this;
_this.type = _types.PSEUDO;
return _this;
}
var _proto = Pseudo.prototype;
_proto.toString = function toString() {
var params = this.length ? '(' + this.map(String).join(',') + ')' : '';
return [this.rawSpaceBefore, this.stringifyProperty("value"), params, this.rawSpaceAfter].join('');
};
return Pseudo;
}(_container["default"]);
exports["default"] = Pseudo;
module.exports = exports.default;

View File

@@ -0,0 +1,60 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _container = _interopRequireDefault(require("./container"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Root = /*#__PURE__*/function (_Container) {
_inheritsLoose(Root, _Container);
function Root(opts) {
var _this;
_this = _Container.call(this, opts) || this;
_this.type = _types.ROOT;
return _this;
}
var _proto = Root.prototype;
_proto.toString = function toString() {
var str = this.reduce(function (memo, selector) {
memo.push(String(selector));
return memo;
}, []).join(',');
return this.trailingComma ? str + ',' : str;
};
_proto.error = function error(message, options) {
if (this._error) {
return this._error(message, options);
} else {
return new Error(message);
}
};
_createClass(Root, [{
key: "errorGenerator",
set: function set(handler) {
this._error = handler;
}
}]);
return Root;
}(_container["default"]);
exports["default"] = Root;
module.exports = exports.default;

View File

@@ -0,0 +1,31 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _container = _interopRequireDefault(require("./container"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Selector = /*#__PURE__*/function (_Container) {
_inheritsLoose(Selector, _Container);
function Selector(opts) {
var _this;
_this = _Container.call(this, opts) || this;
_this.type = _types.SELECTOR;
return _this;
}
return Selector;
}(_container["default"]);
exports["default"] = Selector;
module.exports = exports.default;

View File

@@ -0,0 +1,31 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _node = _interopRequireDefault(require("./node"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var String = /*#__PURE__*/function (_Node) {
_inheritsLoose(String, _Node);
function String(opts) {
var _this;
_this = _Node.call(this, opts) || this;
_this.type = _types.STRING;
return _this;
}
return String;
}(_node["default"]);
exports["default"] = String;
module.exports = exports.default;

View File

@@ -0,0 +1,31 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _namespace = _interopRequireDefault(require("./namespace"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Tag = /*#__PURE__*/function (_Namespace) {
_inheritsLoose(Tag, _Namespace);
function Tag(opts) {
var _this;
_this = _Namespace.call(this, opts) || this;
_this.type = _types.TAG;
return _this;
}
return Tag;
}(_namespace["default"]);
exports["default"] = Tag;
module.exports = exports.default;

View File

@@ -0,0 +1,28 @@
"use strict";
exports.__esModule = true;
exports.UNIVERSAL = exports.ATTRIBUTE = exports.CLASS = exports.COMBINATOR = exports.COMMENT = exports.ID = exports.NESTING = exports.PSEUDO = exports.ROOT = exports.SELECTOR = exports.STRING = exports.TAG = void 0;
var TAG = 'tag';
exports.TAG = TAG;
var STRING = 'string';
exports.STRING = STRING;
var SELECTOR = 'selector';
exports.SELECTOR = SELECTOR;
var ROOT = 'root';
exports.ROOT = ROOT;
var PSEUDO = 'pseudo';
exports.PSEUDO = PSEUDO;
var NESTING = 'nesting';
exports.NESTING = NESTING;
var ID = 'id';
exports.ID = ID;
var COMMENT = 'comment';
exports.COMMENT = COMMENT;
var COMBINATOR = 'combinator';
exports.COMBINATOR = COMBINATOR;
var CLASS = 'class';
exports.CLASS = CLASS;
var ATTRIBUTE = 'attribute';
exports.ATTRIBUTE = ATTRIBUTE;
var UNIVERSAL = 'universal';
exports.UNIVERSAL = UNIVERSAL;

View File

@@ -0,0 +1,32 @@
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _namespace = _interopRequireDefault(require("./namespace"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Universal = /*#__PURE__*/function (_Namespace) {
_inheritsLoose(Universal, _Namespace);
function Universal(opts) {
var _this;
_this = _Namespace.call(this, opts) || this;
_this.type = _types.UNIVERSAL;
_this.value = '*';
return _this;
}
return Universal;
}(_namespace["default"]);
exports["default"] = Universal;
module.exports = exports.default;

View File

@@ -0,0 +1,13 @@
"use strict";
exports.__esModule = true;
exports["default"] = sortAscending;
function sortAscending(list) {
return list.sort(function (a, b) {
return a - b;
});
}
;
module.exports = exports.default;

View File

@@ -0,0 +1,95 @@
"use strict";
exports.__esModule = true;
exports.combinator = exports.word = exports.comment = exports.str = exports.tab = exports.newline = exports.feed = exports.cr = exports.backslash = exports.bang = exports.slash = exports.doubleQuote = exports.singleQuote = exports.space = exports.greaterThan = exports.pipe = exports.equals = exports.plus = exports.caret = exports.tilde = exports.dollar = exports.closeSquare = exports.openSquare = exports.closeParenthesis = exports.openParenthesis = exports.semicolon = exports.colon = exports.comma = exports.at = exports.asterisk = exports.ampersand = void 0;
var ampersand = 38; // `&`.charCodeAt(0);
exports.ampersand = ampersand;
var asterisk = 42; // `*`.charCodeAt(0);
exports.asterisk = asterisk;
var at = 64; // `@`.charCodeAt(0);
exports.at = at;
var comma = 44; // `,`.charCodeAt(0);
exports.comma = comma;
var colon = 58; // `:`.charCodeAt(0);
exports.colon = colon;
var semicolon = 59; // `;`.charCodeAt(0);
exports.semicolon = semicolon;
var openParenthesis = 40; // `(`.charCodeAt(0);
exports.openParenthesis = openParenthesis;
var closeParenthesis = 41; // `)`.charCodeAt(0);
exports.closeParenthesis = closeParenthesis;
var openSquare = 91; // `[`.charCodeAt(0);
exports.openSquare = openSquare;
var closeSquare = 93; // `]`.charCodeAt(0);
exports.closeSquare = closeSquare;
var dollar = 36; // `$`.charCodeAt(0);
exports.dollar = dollar;
var tilde = 126; // `~`.charCodeAt(0);
exports.tilde = tilde;
var caret = 94; // `^`.charCodeAt(0);
exports.caret = caret;
var plus = 43; // `+`.charCodeAt(0);
exports.plus = plus;
var equals = 61; // `=`.charCodeAt(0);
exports.equals = equals;
var pipe = 124; // `|`.charCodeAt(0);
exports.pipe = pipe;
var greaterThan = 62; // `>`.charCodeAt(0);
exports.greaterThan = greaterThan;
var space = 32; // ` `.charCodeAt(0);
exports.space = space;
var singleQuote = 39; // `'`.charCodeAt(0);
exports.singleQuote = singleQuote;
var doubleQuote = 34; // `"`.charCodeAt(0);
exports.doubleQuote = doubleQuote;
var slash = 47; // `/`.charCodeAt(0);
exports.slash = slash;
var bang = 33; // `!`.charCodeAt(0);
exports.bang = bang;
var backslash = 92; // '\\'.charCodeAt(0);
exports.backslash = backslash;
var cr = 13; // '\r'.charCodeAt(0);
exports.cr = cr;
var feed = 12; // '\f'.charCodeAt(0);
exports.feed = feed;
var newline = 10; // '\n'.charCodeAt(0);
exports.newline = newline;
var tab = 9; // '\t'.charCodeAt(0);
// Expose aliases primarily for readability.
exports.tab = tab;
var str = singleQuote; // No good single character representation!
exports.str = str;
var comment = -1;
exports.comment = comment;
var word = -2;
exports.word = word;
var combinator = -3;
exports.combinator = combinator;

View File

@@ -0,0 +1,271 @@
"use strict";
exports.__esModule = true;
exports["default"] = tokenize;
exports.FIELDS = void 0;
var t = _interopRequireWildcard(require("./tokenTypes"));
var _unescapable, _wordDelimiters;
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var unescapable = (_unescapable = {}, _unescapable[t.tab] = true, _unescapable[t.newline] = true, _unescapable[t.cr] = true, _unescapable[t.feed] = true, _unescapable);
var wordDelimiters = (_wordDelimiters = {}, _wordDelimiters[t.space] = true, _wordDelimiters[t.tab] = true, _wordDelimiters[t.newline] = true, _wordDelimiters[t.cr] = true, _wordDelimiters[t.feed] = true, _wordDelimiters[t.ampersand] = true, _wordDelimiters[t.asterisk] = true, _wordDelimiters[t.bang] = true, _wordDelimiters[t.comma] = true, _wordDelimiters[t.colon] = true, _wordDelimiters[t.semicolon] = true, _wordDelimiters[t.openParenthesis] = true, _wordDelimiters[t.closeParenthesis] = true, _wordDelimiters[t.openSquare] = true, _wordDelimiters[t.closeSquare] = true, _wordDelimiters[t.singleQuote] = true, _wordDelimiters[t.doubleQuote] = true, _wordDelimiters[t.plus] = true, _wordDelimiters[t.pipe] = true, _wordDelimiters[t.tilde] = true, _wordDelimiters[t.greaterThan] = true, _wordDelimiters[t.equals] = true, _wordDelimiters[t.dollar] = true, _wordDelimiters[t.caret] = true, _wordDelimiters[t.slash] = true, _wordDelimiters);
var hex = {};
var hexChars = "0123456789abcdefABCDEF";
for (var i = 0; i < hexChars.length; i++) {
hex[hexChars.charCodeAt(i)] = true;
}
/**
* Returns the last index of the bar css word
* @param {string} css The string in which the word begins
* @param {number} start The index into the string where word's first letter occurs
*/
function consumeWord(css, start) {
var next = start;
var code;
do {
code = css.charCodeAt(next);
if (wordDelimiters[code]) {
return next - 1;
} else if (code === t.backslash) {
next = consumeEscape(css, next) + 1;
} else {
// All other characters are part of the word
next++;
}
} while (next < css.length);
return next - 1;
}
/**
* Returns the last index of the escape sequence
* @param {string} css The string in which the sequence begins
* @param {number} start The index into the string where escape character (`\`) occurs.
*/
function consumeEscape(css, start) {
var next = start;
var code = css.charCodeAt(next + 1);
if (unescapable[code]) {// just consume the escape char
} else if (hex[code]) {
var hexDigits = 0; // consume up to 6 hex chars
do {
next++;
hexDigits++;
code = css.charCodeAt(next + 1);
} while (hex[code] && hexDigits < 6); // if fewer than 6 hex chars, a trailing space ends the escape
if (hexDigits < 6 && code === t.space) {
next++;
}
} else {
// the next char is part of the current word
next++;
}
return next;
}
var FIELDS = {
TYPE: 0,
START_LINE: 1,
START_COL: 2,
END_LINE: 3,
END_COL: 4,
START_POS: 5,
END_POS: 6
};
exports.FIELDS = FIELDS;
function tokenize(input) {
var tokens = [];
var css = input.css.valueOf();
var _css = css,
length = _css.length;
var offset = -1;
var line = 1;
var start = 0;
var end = 0;
var code, content, endColumn, endLine, escaped, escapePos, last, lines, next, nextLine, nextOffset, quote, tokenType;
function unclosed(what, fix) {
if (input.safe) {
// fyi: this is never set to true.
css += fix;
next = css.length - 1;
} else {
throw input.error('Unclosed ' + what, line, start - offset, start);
}
}
while (start < length) {
code = css.charCodeAt(start);
if (code === t.newline) {
offset = start;
line += 1;
}
switch (code) {
case t.space:
case t.tab:
case t.newline:
case t.cr:
case t.feed:
next = start;
do {
next += 1;
code = css.charCodeAt(next);
if (code === t.newline) {
offset = next;
line += 1;
}
} while (code === t.space || code === t.newline || code === t.tab || code === t.cr || code === t.feed);
tokenType = t.space;
endLine = line;
endColumn = next - offset - 1;
end = next;
break;
case t.plus:
case t.greaterThan:
case t.tilde:
case t.pipe:
next = start;
do {
next += 1;
code = css.charCodeAt(next);
} while (code === t.plus || code === t.greaterThan || code === t.tilde || code === t.pipe);
tokenType = t.combinator;
endLine = line;
endColumn = start - offset;
end = next;
break;
// Consume these characters as single tokens.
case t.asterisk:
case t.ampersand:
case t.bang:
case t.comma:
case t.equals:
case t.dollar:
case t.caret:
case t.openSquare:
case t.closeSquare:
case t.colon:
case t.semicolon:
case t.openParenthesis:
case t.closeParenthesis:
next = start;
tokenType = code;
endLine = line;
endColumn = start - offset;
end = next + 1;
break;
case t.singleQuote:
case t.doubleQuote:
quote = code === t.singleQuote ? "'" : '"';
next = start;
do {
escaped = false;
next = css.indexOf(quote, next + 1);
if (next === -1) {
unclosed('quote', quote);
}
escapePos = next;
while (css.charCodeAt(escapePos - 1) === t.backslash) {
escapePos -= 1;
escaped = !escaped;
}
} while (escaped);
tokenType = t.str;
endLine = line;
endColumn = start - offset;
end = next + 1;
break;
default:
if (code === t.slash && css.charCodeAt(start + 1) === t.asterisk) {
next = css.indexOf('*/', start + 2) + 1;
if (next === 0) {
unclosed('comment', '*/');
}
content = css.slice(start, next + 1);
lines = content.split('\n');
last = lines.length - 1;
if (last > 0) {
nextLine = line + last;
nextOffset = next - lines[last].length;
} else {
nextLine = line;
nextOffset = offset;
}
tokenType = t.comment;
line = nextLine;
endLine = nextLine;
endColumn = next - nextOffset;
} else if (code === t.slash) {
next = start;
tokenType = code;
endLine = line;
endColumn = start - offset;
end = next + 1;
} else {
next = consumeWord(css, start);
tokenType = t.word;
endLine = line;
endColumn = next - offset;
}
end = next + 1;
break;
} // Ensure that the token structure remains consistent
tokens.push([tokenType, // [0] Token type
line, // [1] Starting line
start - offset, // [2] Starting column
endLine, // [3] Ending line
endColumn, // [4] Ending column
start, // [5] Start position / Source index
end // [6] End position
]); // Reset offset for the next token
if (nextOffset) {
offset = nextOffset;
nextOffset = null;
}
start = end;
}
return tokens;
}

View File

@@ -0,0 +1,22 @@
"use strict";
exports.__esModule = true;
exports["default"] = ensureObject;
function ensureObject(obj) {
for (var _len = arguments.length, props = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
props[_key - 1] = arguments[_key];
}
while (props.length > 0) {
var prop = props.shift();
if (!obj[prop]) {
obj[prop] = {};
}
obj = obj[prop];
}
}
module.exports = exports.default;

View File

@@ -0,0 +1,24 @@
"use strict";
exports.__esModule = true;
exports["default"] = getProp;
function getProp(obj) {
for (var _len = arguments.length, props = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
props[_key - 1] = arguments[_key];
}
while (props.length > 0) {
var prop = props.shift();
if (!obj[prop]) {
return undefined;
}
obj = obj[prop];
}
return obj;
}
module.exports = exports.default;

View File

@@ -0,0 +1,22 @@
"use strict";
exports.__esModule = true;
exports.stripComments = exports.ensureObject = exports.getProp = exports.unesc = void 0;
var _unesc = _interopRequireDefault(require("./unesc"));
exports.unesc = _unesc["default"];
var _getProp = _interopRequireDefault(require("./getProp"));
exports.getProp = _getProp["default"];
var _ensureObject = _interopRequireDefault(require("./ensureObject"));
exports.ensureObject = _ensureObject["default"];
var _stripComments = _interopRequireDefault(require("./stripComments"));
exports.stripComments = _stripComments["default"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

View File

@@ -0,0 +1,27 @@
"use strict";
exports.__esModule = true;
exports["default"] = stripComments;
function stripComments(str) {
var s = "";
var commentStart = str.indexOf("/*");
var lastEnd = 0;
while (commentStart >= 0) {
s = s + str.slice(lastEnd, commentStart);
var commentEnd = str.indexOf("*/", commentStart + 2);
if (commentEnd < 0) {
return s;
}
lastEnd = commentEnd + 2;
commentStart = str.indexOf("/*", lastEnd);
}
s = s + str.slice(lastEnd);
return s;
}
module.exports = exports.default;

View File

@@ -0,0 +1,93 @@
"use strict";
exports.__esModule = true;
exports["default"] = unesc;
// Many thanks for this post which made this migration much easier.
// https://mathiasbynens.be/notes/css-escapes
/**
*
* @param {string} str
* @returns {[string, number]|undefined}
*/
function gobbleHex(str) {
var lower = str.toLowerCase();
var hex = '';
var spaceTerminated = false;
for (var i = 0; i < 6 && lower[i] !== undefined; i++) {
var code = lower.charCodeAt(i); // check to see if we are dealing with a valid hex char [a-f|0-9]
var valid = code >= 97 && code <= 102 || code >= 48 && code <= 57; // https://drafts.csswg.org/css-syntax/#consume-escaped-code-point
spaceTerminated = code === 32;
if (!valid) {
break;
}
hex += lower[i];
}
if (hex.length === 0) {
return undefined;
}
var codePoint = parseInt(hex, 16);
var isSurrogate = codePoint >= 0xD800 && codePoint <= 0xDFFF; // Add special case for
// "If this number is zero, or is for a surrogate, or is greater than the maximum allowed code point"
// https://drafts.csswg.org/css-syntax/#maximum-allowed-code-point
if (isSurrogate || codePoint === 0x0000 || codePoint > 0x10FFFF) {
return ["\uFFFD", hex.length + (spaceTerminated ? 1 : 0)];
}
return [String.fromCodePoint(codePoint), hex.length + (spaceTerminated ? 1 : 0)];
}
var CONTAINS_ESCAPE = /\\/;
function unesc(str) {
var needToProcess = CONTAINS_ESCAPE.test(str);
if (!needToProcess) {
return str;
}
var ret = "";
for (var i = 0; i < str.length; i++) {
if (str[i] === "\\") {
var gobbled = gobbleHex(str.slice(i + 1, i + 7));
if (gobbled !== undefined) {
ret += gobbled[0];
i += gobbled[1];
continue;
} // Retain a pair of \\ if double escaped `\\\\`
// https://github.com/postcss/postcss-selector-parser/commit/268c9a7656fb53f543dc620aa5b73a30ec3ff20e
if (str[i + 1] === "\\") {
ret += "\\";
i++;
continue;
} // if \\ is at the end of the string retain it
// https://github.com/postcss/postcss-selector-parser/commit/01a6b346e3612ce1ab20219acc26abdc259ccefb
if (str.length === i + 1) {
ret += str[i];
}
continue;
}
ret += str[i];
}
return ret;
}
module.exports = exports.default;

View File

@@ -0,0 +1 @@
../../../../../cssesc/bin/cssesc

View File

@@ -0,0 +1,547 @@
// Type definitions for postcss-selector-parser 2.2.3
// Definitions by: Chris Eppstein <chris@eppsteins.net>
/*~ Note that ES6 modules cannot directly export callable functions.
*~ This file should be imported using the CommonJS-style:
*~ import x = require('someLibrary');
*~
*~ Refer to the documentation to understand common
*~ workarounds for this limitation of ES6 modules.
*/
/*~ This declaration specifies that the function
*~ is the exported object from the file
*/
export = parser;
// A type that's T but not U.
type Diff<T, U> = T extends U ? never : T;
// TODO: Conditional types in TS 1.8 will really clean this up.
declare function parser(): parser.Processor<never>;
declare function parser<Transform>(processor: parser.AsyncProcessor<Transform>): parser.Processor<Transform, never>;
declare function parser(processor: parser.AsyncProcessor<void>): parser.Processor<never, never>;
declare function parser<Transform>(processor: parser.SyncProcessor<Transform>): parser.Processor<Transform>;
declare function parser(processor: parser.SyncProcessor<void>): parser.Processor<never>;
declare function parser<Transform>(processor?: parser.SyncProcessor<Transform> | parser.AsyncProcessor<Transform>): parser.Processor<Transform>;
/*~ If you want to expose types from your module as well, you can
*~ place them in this block. Often you will want to describe the
*~ shape of the return type of the function; that type should
*~ be declared in here, as this example shows.
*/
declare namespace parser {
/* copied from postcss -- so we don't need to add a dependency */
type ErrorOptions = {
plugin?: string;
word?: string;
index?: number
};
/* the bits we use of postcss.Rule, copied from postcss -- so we don't need to add a dependency */
type PostCSSRuleNode = {
selector: string
/**
* @returns postcss.CssSyntaxError but it's a complex object, caller
* should cast to it if they have a dependency on postcss.
*/
error(message: string, options?: ErrorOptions): Error;
};
/** Accepts a string */
type Selectors = string | PostCSSRuleNode
type ProcessorFn<ReturnType = void> = (root: parser.Root) => ReturnType;
type SyncProcessor<Transform = void> = ProcessorFn<Transform>;
type AsyncProcessor<Transform = void> = ProcessorFn<PromiseLike<Transform>>;
const TAG: "tag";
const STRING: "string";
const SELECTOR: "selector";
const ROOT: "root";
const PSEUDO: "pseudo";
const NESTING: "nesting";
const ID: "id";
const COMMENT: "comment";
const COMBINATOR: "combinator";
const CLASS: "class";
const ATTRIBUTE: "attribute";
const UNIVERSAL: "universal";
interface NodeTypes {
tag: Tag,
string: String,
selector: Selector,
root: Root,
pseudo: Pseudo,
nesting: Nesting,
id: Identifier,
comment: Comment,
combinator: Combinator,
class: ClassName,
attribute: Attribute,
universal: Universal
}
type Node = NodeTypes[keyof NodeTypes];
function isNode(node: any): node is Node;
interface Options {
/**
* Preserve whitespace when true. Default: false;
*/
lossless: boolean;
/**
* When true and a postcss.Rule is passed, set the result of
* processing back onto the rule when done. Default: false.
*/
updateSelector: boolean;
}
class Processor<
TransformType = never,
SyncSelectorsType extends Selectors | never = Selectors
> {
res: Root;
readonly result: String;
ast(selectors: Selectors, options?: Partial<Options>): Promise<Root>;
astSync(selectors: SyncSelectorsType, options?: Partial<Options>): Root;
transform(selectors: Selectors, options?: Partial<Options>): Promise<TransformType>;
transformSync(selectors: SyncSelectorsType, options?: Partial<Options>): TransformType;
process(selectors: Selectors, options?: Partial<Options>): Promise<string>;
processSync(selectors: SyncSelectorsType, options?: Partial<Options>): string;
}
interface ParserOptions {
css: string;
error: (message: string, options: ErrorOptions) => Error;
options: Options;
}
class Parser {
input: ParserOptions;
lossy: boolean;
position: number;
root: Root;
selectors: string;
current: Selector;
constructor(input: ParserOptions);
/**
* Raises an error, if the processor is invoked on
* a postcss Rule node, a better error message is raised.
*/
error(message: string, options?: ErrorOptions): void;
}
interface NodeSource {
start?: {
line: number,
column: number
},
end?: {
line: number,
column: number
}
}
interface SpaceAround {
before: string;
after: string;
}
interface Spaces extends SpaceAround {
[spaceType: string]: string | Partial<SpaceAround> | undefined;
}
interface NodeOptions<Value = string> {
value: Value;
spaces?: Partial<Spaces>;
source?: NodeSource;
sourceIndex?: number;
}
interface Base<
Value extends string | undefined = string,
ParentType extends Container | undefined = Container | undefined
> {
type: keyof NodeTypes;
parent: ParentType;
value: Value;
spaces: Spaces;
source?: NodeSource;
sourceIndex: number;
rawSpaceBefore: string;
rawSpaceAfter: string;
remove(): Node;
replaceWith(...nodes: Node[]): Node;
next(): Node;
prev(): Node;
clone(opts: {[override: string]:any}): Node;
/**
* Return whether this node includes the character at the position of the given line and column.
* Returns undefined if the nodes lack sufficient source metadata to determine the position.
* @param line 1-index based line number relative to the start of the selector.
* @param column 1-index based column number relative to the start of the selector.
*/
isAtPosition(line: number, column: number): boolean | undefined;
/**
* Some non-standard syntax doesn't follow normal escaping rules for css,
* this allows the escaped value to be specified directly, allowing illegal characters to be
* directly inserted into css output.
* @param name the property to set
* @param value the unescaped value of the property
* @param valueEscaped optional. the escaped value of the property.
*/
setPropertyAndEscape(name: string, value: any, valueEscaped: string): void;
/**
* When you want a value to passed through to CSS directly. This method
* deletes the corresponding raw value causing the stringifier to fallback
* to the unescaped value.
* @param name the property to set.
* @param value The value that is both escaped and unescaped.
*/
setPropertyWithoutEscape(name: string, value: any): void;
/**
* Some non-standard syntax doesn't follow normal escaping rules for css.
* This allows non standard syntax to be appended to an existing property
* by specifying the escaped value. By specifying the escaped value,
* illegal characters are allowed to be directly inserted into css output.
* @param {string} name the property to set
* @param {any} value the unescaped value of the property
* @param {string} valueEscaped optional. the escaped value of the property.
*/
appendToPropertyAndEscape(name: string, value: any, valueEscaped: string): void;
toString(): string;
}
interface ContainerOptions extends NodeOptions {
nodes?: Array<Node>;
}
interface Container<
Value extends string | undefined = string,
Child extends Node = Node
> extends Base<Value> {
nodes: Array<Child>;
append(selector: Selector): this;
prepend(selector: Selector): this;
at(index: number): Child;
/**
* Return the most specific node at the line and column number given.
* The source location is based on the original parsed location, locations aren't
* updated as selector nodes are mutated.
*
* Note that this location is relative to the location of the first character
* of the selector, and not the location of the selector in the overall document
* when used in conjunction with postcss.
*
* If not found, returns undefined.
* @param line The line number of the node to find. (1-based index)
* @param col The column number of the node to find. (1-based index)
*/
atPosition(line: number, column: number): Child;
index(child: Child): number;
readonly first: Child;
readonly last: Child;
readonly length: number;
removeChild(child: Child): this;
removeAll(): Container;
empty(): Container;
insertAfter(oldNode: Child, newNode: Child): this;
insertBefore(oldNode: Child, newNode: Child): this;
each(callback: (node: Child) => boolean | void): boolean | undefined;
walk(
callback: (node: Node) => boolean | void
): boolean | undefined;
walkAttributes(
callback: (node: Attribute) => boolean | void
): boolean | undefined;
walkClasses(
callback: (node: ClassName) => boolean | void
): boolean | undefined;
walkCombinators(
callback: (node: Combinator) => boolean | void
): boolean | undefined;
walkComments(
callback: (node: Comment) => boolean | void
): boolean | undefined;
walkIds(
callback: (node: Identifier) => boolean | void
): boolean | undefined;
walkNesting(
callback: (node: Nesting) => boolean | void
): boolean | undefined;
walkPseudos(
callback: (node: Pseudo) => boolean | void
): boolean | undefined;
walkTags(callback: (node: Tag) => boolean | void): boolean | undefined;
split(callback: (node: Child) => boolean): [Child[], Child[]];
map<T>(callback: (node: Child) => T): T[];
reduce(
callback: (
previousValue: Child,
currentValue: Child,
currentIndex: number,
array: readonly Child[]
) => Child
): Child;
reduce(
callback: (
previousValue: Child,
currentValue: Child,
currentIndex: number,
array: readonly Child[]
) => Child,
initialValue: Child
): Child;
reduce<T>(
callback: (
previousValue: T,
currentValue: Child,
currentIndex: number,
array: readonly Child[]
) => T,
initialValue: T
): T;
every(callback: (node: Child) => boolean): boolean;
some(callback: (node: Child) => boolean): boolean;
filter(callback: (node: Child) => boolean): Child[];
sort(callback: (nodeA: Child, nodeB: Child) => number): Child[];
toString(): string;
}
function isContainer(node: any): node is Root | Selector | Pseudo;
interface NamespaceOptions<Value extends string | undefined = string> extends NodeOptions<Value> {
namespace?: string | true;
}
interface Namespace<Value extends string | undefined = string> extends Base<Value> {
/** alias for namespace */
ns: string | true;
/**
* namespace prefix.
*/
namespace: string | true;
/**
* If a namespace exists, prefix the value provided with it, separated by |.
*/
qualifiedName(value: string): string;
/**
* A string representing the namespace suitable for output.
*/
readonly namespaceString: string;
}
function isNamespace(node: any): node is Attribute | Tag;
interface Root extends Container<undefined, Selector> {
type: "root";
/**
* Raises an error, if the processor is invoked on
* a postcss Rule node, a better error message is raised.
*/
error(message: string, options?: ErrorOptions): Error;
nodeAt(line: number, column: number): Node
}
function root(opts: ContainerOptions): Root;
function isRoot(node: any): node is Root;
interface _Selector<S> extends Container<string, Diff<Node, S>> {
type: "selector";
}
type Selector = _Selector<Selector>;
function selector(opts: ContainerOptions): Selector;
function isSelector(node: any): node is Selector;
interface Combinator extends Base {
type: "combinator"
}
function combinator(opts: NodeOptions): Combinator;
function isCombinator(node: any): node is Combinator;
interface ClassName extends Base {
type: "class";
}
function className(opts: NamespaceOptions): ClassName;
function isClassName(node: any): node is ClassName;
type AttributeOperator = "=" | "~=" | "|=" | "^=" | "$=" | "*=";
type QuoteMark = '"' | "'" | null;
interface PreferredQuoteMarkOptions {
quoteMark?: QuoteMark;
preferCurrentQuoteMark?: boolean;
}
interface SmartQuoteMarkOptions extends PreferredQuoteMarkOptions {
smart?: boolean;
}
interface AttributeOptions extends NamespaceOptions<string | undefined> {
attribute: string;
operator?: AttributeOperator;
insensitive?: boolean;
quoteMark?: QuoteMark;
/** @deprecated Use quoteMark instead. */
quoted?: boolean;
spaces?: {
before?: string;
after?: string;
attribute?: Partial<SpaceAround>;
operator?: Partial<SpaceAround>;
value?: Partial<SpaceAround>;
insensitive?: Partial<SpaceAround>;
}
raws: {
unquoted?: string;
attribute?: string;
operator?: string;
value?: string;
insensitive?: string;
spaces?: {
attribute?: Partial<Spaces>;
operator?: Partial<Spaces>;
value?: Partial<Spaces>;
insensitive?: Partial<Spaces>;
}
};
}
interface Attribute extends Namespace<string | undefined> {
type: "attribute";
attribute: string;
operator?: AttributeOperator;
insensitive?: boolean;
quoteMark: QuoteMark;
quoted?: boolean;
spaces: {
before: string;
after: string;
attribute?: Partial<Spaces>;
operator?: Partial<Spaces>;
value?: Partial<Spaces>;
insensitive?: Partial<Spaces>;
}
raws: {
/** @deprecated The attribute value is unquoted, use that instead.. */
unquoted?: string;
attribute?: string;
operator?: string;
/** The value of the attribute with quotes and escapes. */
value?: string;
insensitive?: string;
spaces?: {
attribute?: Partial<Spaces>;
operator?: Partial<Spaces>;
value?: Partial<Spaces>;
insensitive?: Partial<Spaces>;
}
};
/**
* The attribute name after having been qualified with a namespace.
*/
readonly qualifiedAttribute: string;
/**
* The case insensitivity flag or an empty string depending on whether this
* attribute is case insensitive.
*/
readonly insensitiveFlag : 'i' | '';
/**
* Returns the attribute's value quoted such that it would be legal to use
* in the value of a css file. The original value's quotation setting
* used for stringification is left unchanged. See `setValue(value, options)`
* if you want to control the quote settings of a new value for the attribute or
* `set quoteMark(mark)` if you want to change the quote settings of the current
* value.
*
* You can also change the quotation used for the current value by setting quoteMark.
**/
getQuotedValue(options?: SmartQuoteMarkOptions): string;
/**
* Set the unescaped value with the specified quotation options. The value
* provided must not include any wrapping quote marks -- those quotes will
* be interpreted as part of the value and escaped accordingly.
* @param value
*/
setValue(value: string, options?: SmartQuoteMarkOptions): void;
/**
* Intelligently select a quoteMark value based on the value's contents. If
* the value is a legal CSS ident, it will not be quoted. Otherwise a quote
* mark will be picked that minimizes the number of escapes.
*
* If there's no clear winner, the quote mark from these options is used,
* then the source quote mark (this is inverted if `preferCurrentQuoteMark` is
* true). If the quoteMark is unspecified, a double quote is used.
**/
smartQuoteMark(options: PreferredQuoteMarkOptions): QuoteMark;
/**
* Selects the preferred quote mark based on the options and the current quote mark value.
* If you want the quote mark to depend on the attribute value, call `smartQuoteMark(opts)`
* instead.
*/
preferredQuoteMark(options: PreferredQuoteMarkOptions): QuoteMark
/**
* returns the offset of the attribute part specified relative to the
* start of the node of the output string.
*
* * "ns" - alias for "namespace"
* * "namespace" - the namespace if it exists.
* * "attribute" - the attribute name
* * "attributeNS" - the start of the attribute or its namespace
* * "operator" - the match operator of the attribute
* * "value" - The value (string or identifier)
* * "insensitive" - the case insensitivity flag;
* @param part One of the possible values inside an attribute.
* @returns -1 if the name is invalid or the value doesn't exist in this attribute.
*/
offsetOf(part: "ns" | "namespace" | "attribute" | "attributeNS" | "operator" | "value" | "insensitive"): number;
}
function attribute(opts: AttributeOptions): Attribute;
function isAttribute(node: any): node is Attribute;
interface Pseudo extends Container<string, Selector> {
type: "pseudo";
}
function pseudo(opts: ContainerOptions): Pseudo;
/**
* Checks wether the node is the Psuedo subtype of node.
*/
function isPseudo(node: any): node is Pseudo;
/**
* Checks wether the node is, specifically, a pseudo element instead of
* pseudo class.
*/
function isPseudoElement(node: any): node is Pseudo;
/**
* Checks wether the node is, specifically, a pseudo class instead of
* pseudo element.
*/
function isPseudoClass(node: any): node is Pseudo;
interface Tag extends Namespace {
type: "tag";
}
function tag(opts: NamespaceOptions): Tag;
function isTag(node: any): node is Tag;
interface Comment extends Base {
type: "comment";
}
function comment(opts: NodeOptions): Comment;
function isComment(node: any): node is Comment;
interface Identifier extends Base {
type: "id";
}
function id(opts: any): any;
function isIdentifier(node: any): node is Identifier;
interface Nesting extends Base {
type: "nesting";
}
function nesting(opts: any): any;
function isNesting(node: any): node is Nesting;
interface String extends Base {
type: "string";
}
function string(opts: NodeOptions): String;
function isString(node: any): node is String;
interface Universal extends Base {
type: "universal";
}
function universal(opts?: NamespaceOptions): any;
function isUniversal(node: any): node is Universal;
}

View File

@@ -0,0 +1,22 @@
Copyright (c) Bogdan Chadkin <trysound@yandex.ru>
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.

View File

@@ -0,0 +1,263 @@
# postcss-value-parser
[![Travis CI](https://travis-ci.org/TrySound/postcss-value-parser.svg)](https://travis-ci.org/TrySound/postcss-value-parser)
Transforms CSS declaration values and at-rule parameters into a tree of nodes, and provides a simple traversal API.
## Usage
```js
var valueParser = require('postcss-value-parser');
var cssBackgroundValue = 'url(foo.png) no-repeat 40px 73%';
var parsedValue = valueParser(cssBackgroundValue);
// parsedValue exposes an API described below,
// e.g. parsedValue.walk(..), parsedValue.toString(), etc.
```
For example, parsing the value `rgba(233, 45, 66, .5)` will return the following:
```js
{
nodes: [
{
type: 'function',
value: 'rgba',
before: '',
after: '',
nodes: [
{ type: 'word', value: '233' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: '45' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: '66' },
{ type: 'div', value: ',', before: ' ', after: '' },
{ type: 'word', value: '.5' }
]
}
]
}
```
If you wanted to convert each `rgba()` value in `sourceCSS` to a hex value, you could do so like this:
```js
var valueParser = require('postcss-value-parser');
var parsed = valueParser(sourceCSS);
// walk() will visit all the of the nodes in the tree,
// invoking the callback for each.
parsed.walk(function (node) {
// Since we only want to transform rgba() values,
// we can ignore anything else.
if (node.type !== 'function' && node.value !== 'rgba') return;
// We can make an array of the rgba() arguments to feed to a
// convertToHex() function
var color = node.nodes.filter(function (node) {
return node.type === 'word';
}).map(function (node) {
return Number(node.value);
}); // [233, 45, 66, .5]
// Now we will transform the existing rgba() function node
// into a word node with the hex value
node.type = 'word';
node.value = convertToHex(color);
})
parsed.toString(); // #E92D42
```
## Nodes
Each node is an object with these common properties:
- **type**: The type of node (`word`, `string`, `div`, `space`, `comment`, or `function`).
Each type is documented below.
- **value**: Each node has a `value` property; but what exactly `value` means
is specific to the node type. Details are documented for each type below.
- **sourceIndex**: The starting index of the node within the original source
string. For example, given the source string `10px 20px`, the `word` node
whose value is `20px` will have a `sourceIndex` of `5`.
### word
The catch-all node type that includes keywords (e.g. `no-repeat`),
quantities (e.g. `20px`, `75%`, `1.5`), and hex colors (e.g. `#e6e6e6`).
Node-specific properties:
- **value**: The "word" itself.
### string
A quoted string value, e.g. `"something"` in `content: "something";`.
Node-specific properties:
- **value**: The text content of the string.
- **quote**: The quotation mark surrounding the string, either `"` or `'`.
- **unclosed**: `true` if the string was not closed properly. e.g. `"unclosed string `.
### div
A divider, for example
- `,` in `animation-duration: 1s, 2s, 3s`
- `/` in `border-radius: 10px / 23px`
- `:` in `(min-width: 700px)`
Node-specific properties:
- **value**: The divider character. Either `,`, `/`, or `:` (see examples above).
- **before**: Whitespace before the divider.
- **after**: Whitespace after the divider.
### space
Whitespace used as a separator, e.g. ` ` occurring twice in `border: 1px solid black;`.
Node-specific properties:
- **value**: The whitespace itself.
### comment
A CSS comment starts with `/*` and ends with `*/`
Node-specific properties:
- **value**: The comment value without `/*` and `*/`
- **unclosed**: `true` if the comment was not closed properly. e.g. `/* comment without an end `.
### function
A CSS function, e.g. `rgb(0,0,0)` or `url(foo.bar)`.
Function nodes have nodes nested within them: the function arguments.
Additional properties:
- **value**: The name of the function, e.g. `rgb` in `rgb(0,0,0)`.
- **before**: Whitespace after the opening parenthesis and before the first argument,
e.g. ` ` in `rgb( 0,0,0)`.
- **after**: Whitespace before the closing parenthesis and after the last argument,
e.g. ` ` in `rgb(0,0,0 )`.
- **nodes**: More nodes representing the arguments to the function.
- **unclosed**: `true` if the parentheses was not closed properly. e.g. `( unclosed-function `.
Media features surrounded by parentheses are considered functions with an
empty value. For example, `(min-width: 700px)` parses to these nodes:
```js
[
{
type: 'function', value: '', before: '', after: '',
nodes: [
{ type: 'word', value: 'min-width' },
{ type: 'div', value: ':', before: '', after: ' ' },
{ type: 'word', value: '700px' }
]
}
]
```
`url()` functions can be parsed a little bit differently depending on
whether the first character in the argument is a quotation mark.
`url( /gfx/img/bg.jpg )` parses to:
```js
{ type: 'function', sourceIndex: 0, value: 'url', before: ' ', after: ' ', nodes: [
{ type: 'word', sourceIndex: 5, value: '/gfx/img/bg.jpg' }
] }
```
`url( "/gfx/img/bg.jpg" )`, on the other hand, parses to:
```js
{ type: 'function', sourceIndex: 0, value: 'url', before: ' ', after: ' ', nodes: [
type: 'string', sourceIndex: 5, quote: '"', value: '/gfx/img/bg.jpg' },
] }
```
### unicode-range
The unicode-range CSS descriptor sets the specific range of characters to be
used from a font defined by @font-face and made available
for use on the current page (`unicode-range: U+0025-00FF`).
Node-specific properties:
- **value**: The "unicode-range" itself.
## API
```
var valueParser = require('postcss-value-parser');
```
### valueParser.unit(quantity)
Parses `quantity`, distinguishing the number from the unit. Returns an object like the following:
```js
// Given 2rem
{
number: '2',
unit: 'rem'
}
```
If the `quantity` argument cannot be parsed as a number, returns `false`.
*This function does not parse complete values*: you cannot pass it `1px solid black` and expect `px` as
the unit. Instead, you should pass it single quantities only. Parse `1px solid black`, then pass it
the stringified `1px` node (a `word` node) to parse the number and unit.
### valueParser.stringify(nodes[, custom])
Stringifies a node or array of nodes.
The `custom` function is called for each `node`; return a string to override the default behaviour.
### valueParser.walk(nodes, callback[, bubble])
Walks each provided node, recursively walking all descendent nodes within functions.
Returning `false` in the `callback` will prevent traversal of descendent nodes (within functions).
You can use this feature to for shallow iteration, walking over only the *immediate* children.
*Note: This only applies if `bubble` is `false` (which is the default).*
By default, the tree is walked from the outermost node inwards.
To reverse the direction, pass `true` for the `bubble` argument.
The `callback` is invoked with three arguments: `callback(node, index, nodes)`.
- `node`: The current node.
- `index`: The index of the current node.
- `nodes`: The complete nodes array passed to `walk()`.
Returns the `valueParser` instance.
### var parsed = valueParser(value)
Returns the parsed node tree.
### parsed.nodes
The array of nodes.
### parsed.toString()
Stringifies the node tree.
### parsed.walk(callback[, bubble])
Walks each node inside `parsed.nodes`. See the documentation for `valueParser.walk()` above.
# License
MIT © [Bogdan Chadkin](mailto:trysound@yandex.ru)

View File

@@ -0,0 +1,172 @@
declare namespace postcssValueParser {
interface BaseNode {
/**
* The offset inside the CSS value at which the node starts
*/
sourceIndex: number;
/**
* The node's characteristic value
*/
value: string;
}
interface ClosableNode {
/**
* Whether the parsed CSS value ended before the node was properly closed
*/
unclosed?: true;
}
interface AdjacentAwareNode {
/**
* The token at the start of the node
*/
before: string;
/**
* The token at the end of the node
*/
after: string;
}
interface CommentNode extends BaseNode, ClosableNode {
type: "comment";
}
interface DivNode extends BaseNode, AdjacentAwareNode {
type: "div";
}
interface FunctionNode extends BaseNode, ClosableNode, AdjacentAwareNode {
type: "function";
/**
* Nodes inside the function
*/
nodes: Node[];
}
interface SpaceNode extends BaseNode {
type: "space";
}
interface StringNode extends BaseNode, ClosableNode {
type: "string";
/**
* The quote type delimiting the string
*/
quote: '"' | "'";
}
interface UnicodeRangeNode extends BaseNode {
type: "unicode-range";
}
interface WordNode extends BaseNode {
type: "word";
}
/**
* Any node parsed from a CSS value
*/
type Node =
| CommentNode
| DivNode
| FunctionNode
| SpaceNode
| StringNode
| UnicodeRangeNode
| WordNode;
interface CustomStringifierCallback {
/**
* @param node The node to stringify
* @returns The serialized CSS representation of the node
*/
(nodes: Node): string | undefined;
}
interface WalkCallback {
/**
* @param node The currently visited node
* @param index The index of the node in the series of parsed nodes
* @param nodes The series of parsed nodes
* @returns Returning `false` will prevent traversal of descendant nodes (only applies if `bubble` was set to `true` in the `walk()` call)
*/
(node: Node, index: number, nodes: Node[]): void | boolean;
}
/**
* A CSS dimension, decomposed into its numeric and unit parts
*/
interface Dimension {
number: string;
unit: string;
}
/**
* A wrapper around a parsed CSS value that allows for inspecting and walking nodes
*/
interface ParsedValue {
/**
* The series of parsed nodes
*/
nodes: Node[];
/**
* Walk all parsed nodes, applying a callback
*
* @param callback A visitor callback that will be executed for each node
* @param bubble When set to `true`, walking will be done inside-out instead of outside-in
*/
walk(callback: WalkCallback, bubble?: boolean): this;
}
interface ValueParser {
/**
* Decompose a CSS dimension into its numeric and unit part
*
* @param value The dimension to decompose
* @returns An object representing `number` and `unit` part of the dimension or `false` if the decomposing fails
*/
unit(value: string): Dimension | false;
/**
* Serialize a series of nodes into a CSS value
*
* @param nodes The nodes to stringify
* @param custom A custom stringifier callback
* @returns The generated CSS value
*/
stringify(nodes: Node | Node[], custom?: CustomStringifierCallback): string;
/**
* Walk a series of nodes, applying a callback
*
* @param nodes The nodes to walk
* @param callback A visitor callback that will be executed for each node
* @param bubble When set to `true`, walking will be done inside-out instead of outside-in
*/
walk(nodes: Node[], callback: WalkCallback, bubble?: boolean): void;
/**
* Parse a CSS value into a series of nodes to operate on
*
* @param value The value to parse
*/
new (value: string): ParsedValue;
/**
* Parse a CSS value into a series of nodes to operate on
*
* @param value The value to parse
*/
(value: string): ParsedValue;
}
}
declare const postcssValueParser: postcssValueParser.ValueParser;
export = postcssValueParser;

View File

@@ -0,0 +1,28 @@
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);
}
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.unit = require("./unit");
ValueParser.walk = walk;
ValueParser.stringify = stringify;
module.exports = ValueParser;

View File

@@ -0,0 +1,304 @@
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 uLower = "u".charCodeAt(0);
var uUpper = "U".charCodeAt(0);
var plus = "+".charCodeAt(0);
var isUnicodeRange = /^[a-f0-9?-]+$/i;
module.exports = function(input) {
var tokens = [];
var value = input;
var next,
quote,
prev,
token,
escape,
escapePos,
whitespacePos,
parenthesesOpenPos;
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 = "";
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 &&
(!parent ||
(parent && parent.type === "function" && parent.value !== "calc")))
) {
before = token;
} else {
tokens.push({
type: "space",
sourceIndex: pos,
value: token
});
}
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);
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
};
next = value.indexOf("*/", pos);
if (next === -1) {
token.unclosed = true;
next = value.length;
}
token.value = value.slice(pos + 2, next);
tokens.push(token);
pos = next + 2;
code = value.charCodeAt(pos);
// Operation within calc
} else if (
(code === slash || code === star) &&
parent &&
parent.type === "function" &&
parent.value === "calc"
) {
token = value[pos];
tokens.push({
type: "word",
sourceIndex: pos - before.length,
value: token
});
pos += 1;
code = value.charCodeAt(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 = "";
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);
parenthesesOpenPos = pos;
token = {
type: "function",
sourceIndex: pos - name.length,
value: name,
before: value.slice(parenthesesOpenPos + 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 (parenthesesOpenPos < whitespacePos) {
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);
}
} else {
token.after = "";
token.nodes = [];
}
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);
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 === star &&
parent &&
parent.type === "function" &&
parent.value === "calc") ||
(code === slash &&
parent.type === "function" &&
parent.value === "calc") ||
(code === closeParentheses && balanced)
)
);
token = value.slice(pos, next);
if (openParentheses === code) {
name = token;
} else if (
(uLower === token.charCodeAt(0) || uUpper === token.charCodeAt(0)) &&
plus === token.charCodeAt(1) &&
isUnicodeRange.test(token.slice(2))
) {
tokens.push({
type: "unicode-range",
sourceIndex: pos,
value: token
});
} else {
tokens.push({
type: "word",
sourceIndex: pos,
value: token
});
}
pos = next;
}
}
for (pos = stack.length - 1; pos; pos -= 1) {
stack[pos].unclosed = true;
}
return stack[0].nodes;
};

View File

@@ -0,0 +1,48 @@
function stringifyNode(node, custom) {
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, custom);
if (type !== "function") {
return buf;
}
return (
value +
"(" +
(node.before || "") +
buf +
(node.after || "") +
(node.unclosed ? "" : ")")
);
}
return value;
}
function stringify(nodes, custom) {
var result, i;
if (Array.isArray(nodes)) {
result = "";
for (i = nodes.length - 1; ~i; i -= 1) {
result = stringifyNode(nodes[i], custom) + result;
}
return result;
}
return stringifyNode(nodes, custom);
}
module.exports = stringify;

View File

@@ -0,0 +1,120 @@
var minus = "-".charCodeAt(0);
var plus = "+".charCodeAt(0);
var dot = ".".charCodeAt(0);
var exp = "e".charCodeAt(0);
var EXP = "E".charCodeAt(0);
// Check if three code points would start a number
// https://www.w3.org/TR/css-syntax-3/#starts-with-a-number
function likeNumber(value) {
var code = value.charCodeAt(0);
var nextCode;
if (code === plus || code === minus) {
nextCode = value.charCodeAt(1);
if (nextCode >= 48 && nextCode <= 57) {
return true;
}
var nextNextCode = value.charCodeAt(2);
if (nextCode === dot && nextNextCode >= 48 && nextNextCode <= 57) {
return true;
}
return false;
}
if (code === dot) {
nextCode = value.charCodeAt(1);
if (nextCode >= 48 && nextCode <= 57) {
return true;
}
return false;
}
if (code >= 48 && code <= 57) {
return true;
}
return false;
}
// Consume a number
// https://www.w3.org/TR/css-syntax-3/#consume-number
module.exports = function(value) {
var pos = 0;
var length = value.length;
var code;
var nextCode;
var nextNextCode;
if (length === 0 || !likeNumber(value)) {
return false;
}
code = value.charCodeAt(pos);
if (code === plus || code === minus) {
pos++;
}
while (pos < length) {
code = value.charCodeAt(pos);
if (code < 48 || code > 57) {
break;
}
pos += 1;
}
code = value.charCodeAt(pos);
nextCode = value.charCodeAt(pos + 1);
if (code === dot && nextCode >= 48 && nextCode <= 57) {
pos += 2;
while (pos < length) {
code = value.charCodeAt(pos);
if (code < 48 || code > 57) {
break;
}
pos += 1;
}
}
code = value.charCodeAt(pos);
nextCode = value.charCodeAt(pos + 1);
nextNextCode = value.charCodeAt(pos + 2);
if (
(code === exp || code === EXP) &&
((nextCode >= 48 && nextCode <= 57) ||
((nextCode === plus || nextCode === minus) &&
nextNextCode >= 48 &&
nextNextCode <= 57))
) {
pos += nextCode === plus || nextCode === minus ? 3 : 2;
while (pos < length) {
code = value.charCodeAt(pos);
if (code < 48 || code > 57) {
break;
}
pos += 1;
}
}
return {
number: value.slice(0, pos),
unit: value.slice(pos)
};
};

View File

@@ -0,0 +1,22 @@
module.exports = function walk(nodes, cb, bubble) {
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);
}
if (
result !== false &&
node.type === "function" &&
Array.isArray(node.nodes)
) {
walk(node.nodes, cb, bubble);
}
if (bubble) {
cb(node, i, nodes);
}
}
};

View File

@@ -0,0 +1,668 @@
# Change Log
This project adheres to [Semantic Versioning](http://semver.org/).
## 7.0.35
* Add migration guide link to PostCSS 8 error text.
## 7.0.34
* Fix compatibility with `postcss-scss` 2.
## 7.0.33
* Add error message for PostCSS 8 plugins.
## 7.0.32
* Fix error message (by @admosity).
## 7.0.31
* Use only the latest source map annotation (by Emmanouil Zoumpoulakis).
## 7.0.30
* Fix TypeScript definition (by Natalie Weizenbaum).
## 7.0.29
* Update `Processor#version`.
## 7.0.28
* Fix TypeScript definition (by Natalie Weizenbaum).
## 7.0.27
* Fix TypeScript definition (by Natalie Weizenbaum).
## 7.0.26
* Fix TypeScript definition (by Natalie Weizenbaum).
## 7.0.25
* Fix absolute path support for Windows (by Tom Raviv).
## 7.0.24
* Fix TypeScript definition (by Keith Cirkel).
## 7.0.23
* Update `Processor#version`.
## 7.0.22
* Add funding link for `npm fund`.
## 7.0.21
* Revert passing `nodes` property to node constructor.
## 7.0.20
* Allow to pass PostCSSs nodes in `nodes` property to node constructor.
## 7.0.19
* Fix passing `nodes` property to node constructor.
## 7.0.18
* Fix TypeScript type definitions (by Jan Buschtöns).
## 7.0.17
* Fix TypeScript type definitions (by Bob Matcuk and Jan Buschtöns).
## 7.0.16
* Revert Custom Properties fix until PostCSS 8.0.
## 7.0.15
* Fix Custom Properties support (by Ivan Solovev).
## 7.0.14
* Fix tokenizer for `postcss-less` (by Matt Lyons).
## 7.0.13
* Fix parsing regression in 7.0.12 for comments between property and value.
## 7.0.12
* Fix parsing broken CSS with two words in declaration property.
## 7.0.11
* Fix source maps on declaration semicolon (by Niklas Mischkulnig).
## 7.0.10
* Fix source maps (by Niklas Mischkulnig).
## 7.0.9
* Increase stringifing performance for non-raws AST.
## 7.0.8
* Fix TypeScript definitions (by Ankur Oberoi).
* Use `support-colors` 6.0.
## 7.0.7
* Extend `Error` in `CssSyntaxError`.
## 7.0.6
* Fix parsing files with BOM (by Veniamin Krol).
## 7.0.5
* Reduce npm package size (by Gilad Peleg).
## 7.0.4
* Fix safe parser regression.
## 7.0.3
* Fix tokenizer extendability (by Andrew Powell).
* Reduce npm package size.
## 7.0.2
* Fix warning text (by Rui Pedro M Lima).
## 7.0.1
* Fix JSDoc (by Steven Lambert).
## 7.0 “President Amy”
* Remove Node.js 9 and Node.js 4 support.
* Remove IE and “dead” browsers support for client-side Babel transpiling.
* Add CSS position on error happened inside `walk()` (by Nikhil Gaba).
* Add `LazyResult#finally` (by Igor Kamyshev).
* Add warning on calling PostCSS without plugins and syntax options.
* Reduce client-side size.
## 6.0.23
* Fix parsing nested at-rules without semicolon, params, and spaces.
* Fix docs (by Kevin Schiffer and Pat Cavit).
## 6.0.22
* Fix `Node#prev` and `Node#next` on missed parent.
## 6.0.21
* Rename Chinese docs to fix `yarnpkg.com` issue.
## 6.0.20
* Better error message on `null` as input CSS.
## 6.0.19
* Fix TypeScript definitions for source maps (by Oleh Kuchuk).
* Fix `source` field in TypeScript definitions (by Sylvain Pollet-Villard).
## 6.0.18
* Use primitive object in TypeScript definitions (by Sylvain Pollet-Villard).
## 6.0.17
* Fix parsing comment in selector between word tokens (by Oleh Kuchuk).
## 6.0.16
* Fix warning text (by Michael Keller).
## 6.0.15
* Add warning about missed `from` option on `process().then()` call.
* Add IE 10 support.
## 6.0.14
* Fix TypeScript definitions (by Jed Mao).
## 6.0.13
* Fix TypeScript definitions for case of multiple PostCSS versions
in `node_modules` (by Chris Eppstein).
* Use `source-map` 0.6.
## 6.0.12
* Dont copy `*` hack to declaration indent.
## 6.0.11
* Add upper case `!IMPORTANT` support.
## 6.0.10
* Reduce PostCSS size in webpack bundle.
## 6.0.9
* Improve error message for plugin with old PostCSS (by Igor Adamenko).
## 6.0.8
* Fix Node.js 4.2.2 support.
## 6.0.7
* Fix base64 decoding for old Node.js and browser.
## 6.0.6
* Fix `end` position in at-rule without semicolon (by Oleh Kuchuk).
## 6.0.5
* Move Babel config from `package.json` for `node_modules` compiling cases.
## 6.0.4
* Fix parsing `;;` after rules.
* Use Chalk 2.0.
## 6.0.3
* Fix escape sequences parsing (by Oleh Kuchuk).
* Added ability to force disable colors with an environment variable.
* Improved color detection of some terminal apps.
## 6.0.2
* Keep `raws.before` on moving `Root` children to new `Root`.
## 6.0.1
* Fix parser extensibility to use it in Safe Parser.
## 6.0 “Marquis Orias”
* Remove node.js 0.12 support.
* Remove deprecated method from PostCSS 4.
* Insert methods remove child from previous parent, instead of closing.
* Insert methods and cloning doesnt clean `raws` anymore.
* Methods `moveTo`, `moveAfter`, `moveBefore` were deprecated.
* Options was changed in `Plugin#process(css, processOptions, pluginOptions)`.
* Add stream parser to reduce memory usage (by Oleh Kuchuk).
* Add `before()`/`after()` shortcuts for `node.parent.insertBefore(node, x)`.
* Add `Rule#raws.ownSemicolon` for semicolon after templates for `@apply`.
* Use `babel-preset-env` to compile npm package.
* Remove `js-base64` from dependencies (by Roman Dvornov).
* Fix error message on single `:` in CSS.
* Move tests to Jest.
* Clean up test (by Gabriel Kalani).
## 5.2.18
* Fix TypeScript definitions for case of multiple PostCSS versions
in `node_modules` (by Chris Eppstein).
## 5.2.17
* Add `postcss-sass` suggestion to syntax error on `.sass` input.
## 5.2.16
* Better error on wrong argument in node constructor.
## 5.2.15
* Fix TypeScript definitions (by bumbleblym).
## 5.2.14
* Fix browser bundle building in webpack (by janschoenherr).
## 5.2.13
* Do not add comment to important raws.
* Fix JSDoc (by Dmitry Semigradsky).
## 5.2.12
* Fix typo in deprecation message (by Garet McKinley).
## 5.2.11
* Fix TypeScript definitions (by Jed Mao).
## 5.2.10
* Fix TypeScript definitions (by Jed Mao).
## 5.2.9
* Update TypeScript definitions (by Jed Mao).
## 5.2.8
* Fix error message (by Ben Briggs).
## 5.2.7
* Better error message on syntax object in plugins list.
## 5.2.6
* Fix `postcss.vendor` for values with spaces (by 刘祺).
## 5.2.5
* Better error message on unclosed string (by Ben Briggs).
## 5.2.4
* Improve terminal CSS syntax highlight (by Simon Lydell).
## 5.2.3
* Better color highlight in syntax error code frame.
* Fix color highlight support in old systems.
## 5.2.2
* Update `Processor#version`.
## 5.2.1
* Fix source map path for CSS without `from` option (by Michele Locati).
## 5.2 “Duke Vapula”
* Add syntax highlight to code frame in syntax error (by Andrey Popp).
* Use Babel code frame style and size in syntax error.
* Add `[` and `]` tokens to parse `[attr=;] {}` correctly.
* Add `ignoreErrors` options to tokenizer (by Andrey Popp).
* Fix error position on tab indent (by Simon Lydell).
## 5.1.2
* Suggests SCSS/Less parsers on parse errors depends on file extension.
## 5.1.1
* Fix TypeScript definitions (by Efremov Alexey).
## 5.1 “King and President Zagan”
* Add URI in source map support (by Mark Finger).
* Add `map.from` option (by Mark Finger).
* Add `<no source>` mappings for nodes without source (by Bogdan Chadkin).
* Add function value support to `map.prev` option (by Chris Montoro).
* Add declaration value type check in shortcut creating (by 刘祺).
* `Result#warn` now returns new created warning.
* Dont call plugin creator in `postcss.plugin` call.
* Add source maps to PostCSS ES5 build.
* Add JSDoc to PostCSS classes.
* Clean npm package from unnecessary docs.
## 5.0.21
* Fix support with input source mao with `utf8` encoding name.
## 5.0.20
* Fix between raw value parsing (by David Clark).
* Update TypeScript definitions (by Jed Mao).
* Clean fake node.source after `append(string)`.
## 5.0.19
* Fix indent-based syntaxes support.
## 5.0.18
* Parse new lines according W3C CSS syntax specification.
## 5.0.17
* Fix options argument in `Node#warn` (by Ben Briggs).
* Fix TypeScript definitions (by Jed Mao).
## 5.0.16
* Fix CSS syntax error position on unclosed quotes.
## 5.0.15
* Fix `Node#clone()` on `null` value somewhere in node.
## 5.0.14
* Allow to use PostCSS in webpack bundle without JSON loader.
## 5.0.13
* Fix `index` and `word` options in `Warning#toString` (by Bogdan Chadkin).
* Fix input source content loading in errors.
* Fix map options on using `LazyResult` as input CSS.
* 100% test coverage.
* Use Babel 6.
## 5.0.12
* Allow passing a previous map with no mappings (by Andreas Lind).
## 5.0.11
* Increase plugins performance by 1.5 times.
## 5.0.10
* Fix warning from nodes without source.
## 5.0.9
* Fix source map type detection (by @asan).
## 5.0.8
* Fixed a missed step in `5.0.7` that caused the module to be published as
ES6 code.
## 5.0.7
* PostCSS now requires that node 0.12 is installed via the engines property
in package.json (by Howard Zuo).
## 5.0.6
* Fix parsing nested at-rule without semicolon (by Matt Drake).
* Trim `Declaration#value` (by Bogdan Chadkin).
## 5.0.5
* Fix multi-tokens property parsing (by Matt Drake).
## 5.0.4
* Fix start position in `Root#source`.
* Fix source map annotation, when CSS uses `\r\n` (by Mohammad Younes).
## 5.0.3
* Fix `url()` parsing.
* Fix using `selectors` in `Rule` constructor.
* Add start source to `Root` node.
## 5.0.2
* Fix `remove(index)` to be compatible with 4.x plugin.
## 5.0.1
* Fix PostCSS 4.x plugins compatibility.
* Fix type definition loading (by Jed Mao).
## 5.0 “President Valac”
* Remove `safe` option. Move Safe Parser to separate project.
* `Node#toString` does not include `before` for root nodes.
* Remove plugin returning `Root` API.
* Remove Promise polyfill for node.js 0.10.
* Deprecate `eachInside`, `eachDecl`, `eachRule`, `eachAtRule` and `eachComment`
in favor of `walk`, `walkDecls`, `walkRules`, `walkAtRules` and `walkComments`
(by Jed Mao).
* Deprecate `Container#remove` and `Node#removeSelf`
in favor of `Container#removeChild` and `Node#remove` (by Ben Briggs).
* Deprecate `Node#replace` in favor of `replaceWith` (by Ben Briggs).
* Deprecate raw properties in favor of `Node#raws` object.
* Deprecate `Node#style` in favor of `raw`.
* Deprecate `CssSyntaxError#generated` in favor of `input`.
* Deprecate `Node#cleanStyles` in favor of `cleanRaws`.
* Deprecate `Root#prevMap` in favor of `Root.source.input.map`.
* Add `syntax`, `parser` and `stringifier` options for Custom Syntaxes.
* Add stringifier option to `Node#toString`.
* Add `Result#content` alias for non-CSS syntaxes.
* Add `plugin.process(css)` shortcut to every plugin function (by Ben Briggs).
* Add multiple nodes support to insert methods (by Jonathan Neal).
* Add `Node#warn` shortcut (by Ben Briggs).
* Add `word` and `index` options to errors and warnings (by David Clark).
* Add `line`, `column` properties to `Warning`.
* Use `supports-color` library to detect color support in error output.
* Add type definitions for TypeScript plugin developers (by Jed Mao).
* `Rule#selectors` setter detects separators.
* Add `postcss.stringify` method.
* Throw descriptive errors for incorrectly formatted plugins.
* Add docs to npm release.
* Fix `url()` parsing.
* Fix Windows support (by Jed Mao).
## 4.1.16
* Fix errors without stack trace.
## 4.1.15
* Allow asynchronous plugins to change processor plugins list (by Ben Briggs).
## 4.1.14
* Fix for plugins packs defined by `postcss.plugin`.
## 4.1.13
* Fix input inlined source maps with UTF-8 encoding.
## 4.1.12
* Update Promise polyfill.
## 4.1.11
* Fix error message on wrong plugin format.
## 4.1.10
* Fix Promise behavior on sync plugin errors.
* Automatically fill `plugin` field in `CssSyntaxError`.
* Fix warning message (by Ben Briggs).
## 4.1.9
* Speed up `node.clone()`.
## 4.1.8
* Accepts `Processor` instance in `postcss()` constructor too.
## 4.1.7
* Speed up `postcss.list` (by Bogdan Chadkin).
## 4.1.6
* Fix Promise behavior on parsing error.
## 4.1.5
* Parse at-words in declaration values.
## 4.1.4
* Fix Promise polyfill dependency (by Anton Yakushev and Matija Marohnić).
## 4.1.3
* Add Promise polyfill for node.js 0.10 and IE.
## 4.1.2
* List helpers can be accessed independently `var space = postcss.list.space`.
## 4.1.1
* Show deprecated message only once.
## 4.1 “Marquis Andras”
* Asynchronous plugin support.
* Add warnings from plugins and `Result#messages`.
* Add `postcss.plugin()` to create plugins with a standard API.
* Insert nodes by CSS string.
* Show version warning message on error from an outdated plugin.
* Send `Result` instance to plugins as the second argument.
* Add `CssSyntaxError#plugin`.
* Add `CssSyntaxError#showSourceCode()`.
* Add `postcss.list` and `postcss.vendor` aliases.
* Add `Processor#version`.
* Parse wrong closing bracket.
* Parse `!important` statement with spaces and comments inside (by Ben Briggs).
* Throw an error on declaration without `prop` or `value` (by Philip Peterson).
* Fix source map mappings position.
* Add indexed source map support.
* Always set `error.generated`.
* Clean all source map annotation comments.
## 4.0.6
* Remove `babel` from released package dependencies (by Andres Suarez).
## 4.0.5
* Fix error message on double colon in declaration.
## 4.0.4
* Fix indent detection in some rare cases.
## 4.0.3
* Faster API with 6to5 Loose mode.
* Fix indexed source maps support.
## 4.0.2
* Do not copy IE hacks to code style.
## 4.0.1
* Add `source.input` to `Root` too.
## 4.0 “Duke Flauros”
* Rename `Container#childs` to `nodes`.
* Rename `PostCSS#processors` to `plugins`.
* Add `Node#replaceValues()` method.
* Add `Node#moveTo()`, `moveBefore()` and `moveAfter()` methods.
* Add `Node#cloneBefore()` and `cloneAfter()` shortcuts.
* Add `Node#next()`, `prev()` and `root()` shortcuts.
* Add `Node#replaceWith()` method.
* Add `Node#error()` method.
* Add `Container#removeAll()` method.
* Add filter argument to `eachDecl()` and `eachAtRule()`.
* Add `Node#source.input` and move `source.file` or `source.id` to `input`.
* Change code indent, when node was moved.
* Better fix code style on `Rule`, `AtRule` and `Comment` nodes changes.
* Allow to create rules and at-rules by hash shortcut in append methods.
* Add class name to CSS syntax error output.
## 3.0.7
* Fix IE filter parsing with multiple commands.
* Safer way to consume PostCSS object as plugin (by Maxime Thirouin).
## 3.0.6
* Fix missing semicolon when comment comes after last declaration.
* Fix Safe Mode declaration parsing on unclosed blocks.
## 3.0.5
* Fix parser to support difficult cases with backslash escape and brackets.
* Add `CssSyntaxError#stack` (by Maxime Thirouin).
## 3.0.4
* Fix Safe Mode on unknown word before declaration.
## 3.0.3
* Increase tokenizer speed (by Roman Dvornov).
## 3.0.2
* Fix empty comment parsing.
* Fix `Root#normalize` in some inserts.
## 3.0.1
* Fix Rhino JS runtime support.
* Typo in deprecated warning (by Maxime Thirouin).
## 3.0 “Marquis Andrealphus”
* New parser, which become the fastest ever CSS parser written in JavaScript.
* Parser can now parse declarations and rules in one parent (like in `@page`)
and nested declarations for plugins like `postcss-nested`.
* Child nodes array is now in `childs` property, instead of `decls` and `rules`.
* `map.inline` and `map.sourcesContent` options are now `true` by default.
* Fix iterators (`each`, `insertAfter`) on children array changes.
* Use previous source map to show origin source of CSS syntax error.
* Use 6to5 ES6 compiler, instead of ES6 Transpiler.
* Use code style for manually added rules from existing rules.
* Use `from` option from previous source map `file` field.
* Set `to` value to `from` if `to` option is missing.
* Use better node source name when missing `from` option.
* Show a syntax error when `;` is missed between declarations.
* Allow to pass `PostCSS` instance or list of plugins to `use()` method.
* Allow to pass `Result` instance to `process()` method.
* Trim Unicode BOM on source maps parsing.
* Parse at-rules without spaces like `@import"file"`.
* Better previous `sourceMappingURL` annotation comment cleaning.
* Do not remove previous `sourceMappingURL` comment on `map.annotation: false`.
* Parse nameless at-rules in Safe Mode.
* Fix source map generation for nodes without source.
* Fix next child `before` if `Root` first child got removed.
## 2.2.6
* Fix map generation for nodes without source (by Josiah Savary).
## 2.2.5
* Fix source map with BOM marker support (by Mohammad Younes).
* Fix source map paths (by Mohammad Younes).
## 2.2.4
* Fix `prepend()` on empty `Root`.
## 2.2.3
* Allow to use object shortcut in `use()` with functions like `autoprefixer`.
## 2.2.2
* Add shortcut to set processors in `use()` via object with `.postcss` property.
## 2.2.1
* Send `opts` from `Processor#process(css, opts)` to processors.
## 2.2 “Marquis Cimeies”
* Use GNU style syntax error messages.
* Add `Node#replace` method.
* Add `CssSyntaxError#reason` property.
## 2.1.2
* Fix UTF-8 support in inline source map.
* Fix source map `sourcesContent` if there is no `from` and `to` options.
## 2.1.1
* Allow to miss `to` and `from` options for inline source maps.
* Add `Node#source.id` if file name is unknown.
* Better detect splitter between rules in CSS concatenation tools.
* Automatically clone node in insert methods.
## 2.1 “King Amdusias”
* Change Traceur ES6 compiler to ES6 Transpiler.
* Show broken CSS line in syntax error.
## 2.0 “King Belial”
* Project was rewritten from CoffeeScript to ES6.
* Add Safe Mode to works with live input or with hacks from legacy code.
* More safer parser to pass all hacks from Browserhacks.com.
* Use real properties instead of magic getter/setter for raw properties.
## 1.0 “Marquis Decarabia”
* Save previous source map for each node to support CSS concatenation
with multiple previous maps.
* Add `map.sourcesContent` option to add origin content to `sourcesContent`
inside map.
* Allow to set different place of output map in annotation comment.
* Allow to use arrays and `Root` in `Container#append` and same methods.
* Add `Root#prevMap` with information about previous map.
* Allow to use latest PostCSS from GitHub by npm.
* `Result` now is lazy and it will generate output CSS only if you use `css`
or `map` property.
* Use separated `map.prev` option to set previous map.
* Rename `inlineMap` option to `map.inline`.
* Rename `mapAnnotation` option to `map.annotation`.
* `Result#map` now return `SourceMapGenerator` object, instead of string.
* Run previous map autodetect only if input CSS contains annotation comment.
* Add `map: 'inline'` shortcut for `map: { inline: true }` option.
* `Node#source.file` now will contains absolute path.
* Clean `Declaration#between` style on node clone.
## 0.3.5
* Allow to use `Root` or `Result` as first argument in `process()`.
* Save parsed AST to `Result#root`.
## 0.3.4
* Better space symbol detect to read UTF-8 BOM correctly.
## 0.3.3
* Remove source map hacks by using new Mozillas `source-map` (by Simon Lydell).
## 0.3.2
* Add URI encoding support for inline source maps.
## 0.3.1
* Fix relative paths from previous source map.
* Safer space split in `Rule#selectors` (by Simon Lydell).
## 0.3 “Prince Seere”
* Add `Comment` node for comments between declarations or rules.
* Add source map annotation comment to output CSS.
* Allow to inline source map to annotation comment by data:uri.
* Fix source maps on Windows.
* Fix source maps for subdirectory (by Dmitry Nikitenko and Simon Lydell).
* Autodetect previous source map.
* Add `first` and `last` shortcuts to container nodes.
* Parse `!important` to separated property in `Declaration`.
* Allow to break iteration by returning `false`.
* Copy code style to new nodes.
* Add `eachInside` method to recursively iterate all nodes.
* Add `selectors` shortcut to get selectors array.
* Add `toResult` method to `Rule` to simplify work with several input files.
* Clean declarations `value`, rules `selector` and at-rules `params`
by storing spaces in `between` property.
## 0.2 “Duke Dantalion”
* Add source map support.
* Add shortcuts to create nodes.
* Method `process()` now returns object with `css` and `map` keys.
* Origin CSS file option was renamed from `file` to `from`.
* Rename `Node#remove()` method to `removeSelf()` to fix name conflict.
* Node source was moved to `source` property with origin file
and node end position.
* You can set own CSS generate function.
## 0.1 “Count Andromalius”
* Initial release.

View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright 2013 Andrey Sitnik <andrey@sitnik.ru>
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.

View File

@@ -0,0 +1,491 @@
# PostCSS [![Gitter][chat-img]][chat]
<img align="right" width="95" height="95"
alt="Philosophers stone, logo of PostCSS"
src="http://postcss.github.io/postcss/logo.svg">
[chat-img]: https://img.shields.io/badge/Gitter-Join_the_PostCSS_chat-brightgreen.svg
[chat]: https://gitter.im/postcss/postcss
PostCSS is a tool for transforming styles with JS plugins.
These plugins can lint your CSS, support variables and mixins,
transpile future CSS syntax, inline images, and more.
PostCSS is used by industry leaders including Wikipedia, Twitter, Alibaba,
and JetBrains. The [Autoprefixer] PostCSS plugin is one of the most popular
CSS processors.
PostCSS takes a CSS file and provides an API to analyze and modify its rules
(by transforming them into an [Abstract Syntax Tree]).
This API can then be used by [plugins] to do a lot of useful things,
e.g. to find errors automatically insert vendor prefixes.
**Support / Discussion:** [Gitter](https://gitter.im/postcss/postcss)<br>
**Twitter account:** [@postcss](https://twitter.com/postcss)<br>
**VK.com page:** [postcss](https://vk.com/postcss)<br>
**中文翻译**: [`README-cn.md`](./README-cn.md)
For PostCSS commercial support (consulting, improving the front-end culture
of your company, PostCSS plugins), contact [Evil Martians]
at <surrender@evilmartians.com>.
[Abstract Syntax Tree]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
[Evil Martians]: https://evilmartians.com/?utm_source=postcss
[Autoprefixer]: https://github.com/postcss/autoprefixer
[plugins]: https://github.com/postcss/postcss#plugins
<a href="https://evilmartians.com/?utm_source=postcss">
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
alt="Sponsored by Evil Martians" width="236" height="54">
</a>
## Sponsorship
PostCSS needs your support. We are accepting donations
[at Open Collective](https://opencollective.com/postcss/).
<a href="https://tailwindcss.com/">
<img src="https://refactoringui.nyc3.cdn.digitaloceanspaces.com/tailwind-logo.svg"
alt="Sponsored by Tailwind CSS" width="273" height="64">
</a>
## Plugins
Currently, PostCSS has more than 200 plugins. You can find all of the plugins
in the [plugins list] or in the [searchable catalog]. Below is a list
of our favorite plugins — the best demonstrations of what can be built
on top of PostCSS.
If you have any new ideas, [PostCSS plugin development] is really easy.
[searchable catalog]: http://postcss.parts
[plugins list]: https://github.com/postcss/postcss/blob/master/docs/plugins.md
### Solve Global CSS Problem
* [`postcss-use`] allows you to explicitly set PostCSS plugins within CSS
and execute them only for the current file.
* [`postcss-modules`] and [`react-css-modules`] automatically isolate
selectors within components.
* [`postcss-autoreset`] is an alternative to using a global reset
that is better for isolatable components.
* [`postcss-initial`] adds `all: initial` support, which resets
all inherited styles.
* [`cq-prolyfill`] adds container query support, allowing styles that respond
to the width of the parent.
### Use Future CSS, Today
* [`autoprefixer`] adds vendor prefixes, using data from Can I Use.
* [`postcss-preset-env`] allows you to use future CSS features today.
### Better CSS Readability
* [`precss`] contains plugins for Sass-like features, like variables, nesting,
and mixins.
* [`postcss-sorting`] sorts the content of rules and at-rules.
* [`postcss-utilities`] includes the most commonly used shortcuts and helpers.
* [`short`] adds and extends numerous shorthand properties.
### Images and Fonts
* [`postcss-assets`] inserts image dimensions and inlines files.
* [`postcss-sprites`] generates image sprites.
* [`font-magician`] generates all the `@font-face` rules needed in CSS.
* [`postcss-inline-svg`] allows you to inline SVG and customize its styles.
* [`postcss-write-svg`] allows you to write simple SVG directly in your CSS.
### Linters
* [`stylelint`] is a modular stylesheet linter.
* [`stylefmt`] is a tool that automatically formats CSS
according `stylelint` rules.
* [`doiuse`] lints CSS for browser support, using data from Can I Use.
* [`colorguard`] helps you maintain a consistent color palette.
### Other
* [`postcss-rtl`] combines both-directional (left-to-right and right-to-left) styles in one CSS file.
* [`cssnano`] is a modular CSS minifier.
* [`lost`] is a feature-rich `calc()` grid system.
* [`rtlcss`] mirrors styles for right-to-left locales.
[PostCSS plugin development]: https://github.com/postcss/postcss/blob/master/docs/writing-a-plugin.md
[`postcss-inline-svg`]: https://github.com/TrySound/postcss-inline-svg
[`postcss-preset-env`]: https://github.com/jonathantneal/postcss-preset-env
[`react-css-modules`]: https://github.com/gajus/react-css-modules
[`postcss-autoreset`]: https://github.com/maximkoretskiy/postcss-autoreset
[`postcss-write-svg`]: https://github.com/jonathantneal/postcss-write-svg
[`postcss-utilities`]: https://github.com/ismamz/postcss-utilities
[`postcss-initial`]: https://github.com/maximkoretskiy/postcss-initial
[`postcss-sprites`]: https://github.com/2createStudio/postcss-sprites
[`postcss-modules`]: https://github.com/outpunk/postcss-modules
[`postcss-sorting`]: https://github.com/hudochenkov/postcss-sorting
[`postcss-assets`]: https://github.com/assetsjs/postcss-assets
[`font-magician`]: https://github.com/jonathantneal/postcss-font-magician
[`autoprefixer`]: https://github.com/postcss/autoprefixer
[`cq-prolyfill`]: https://github.com/ausi/cq-prolyfill
[`postcss-rtl`]: https://github.com/vkalinichev/postcss-rtl
[`postcss-use`]: https://github.com/postcss/postcss-use
[`css-modules`]: https://github.com/css-modules/css-modules
[`colorguard`]: https://github.com/SlexAxton/css-colorguard
[`stylelint`]: https://github.com/stylelint/stylelint
[`stylefmt`]: https://github.com/morishitter/stylefmt
[`cssnano`]: http://cssnano.co
[`precss`]: https://github.com/jonathantneal/precss
[`doiuse`]: https://github.com/anandthakker/doiuse
[`rtlcss`]: https://github.com/MohammadYounes/rtlcss
[`short`]: https://github.com/jonathantneal/postcss-short
[`lost`]: https://github.com/peterramsing/lost
## Syntaxes
PostCSS can transform styles in any syntax, not just CSS.
If there is not yet support for your favorite syntax,
you can write a parser and/or stringifier to extend PostCSS.
* [`sugarss`] is a indent-based syntax like Sass or Stylus.
* [`postcss-syntax`] switch syntax automatically by file extensions.
* [`postcss-html`] parsing styles in `<style>` tags of HTML-like files.
* [`postcss-markdown`] parsing styles in code blocks of Markdown files.
* [`postcss-jsx`] parsing CSS in template / object literals of source files.
* [`postcss-styled`] parsing CSS in template literals of source files.
* [`postcss-scss`] allows you to work with SCSS
*(but does not compile SCSS to CSS)*.
* [`postcss-sass`] allows you to work with Sass
*(but does not compile Sass to CSS)*.
* [`postcss-less`] allows you to work with Less
*(but does not compile LESS to CSS)*.
* [`postcss-less-engine`] allows you to work with Less
*(and DOES compile LESS to CSS using true Less.js evaluation)*.
* [`postcss-js`] allows you to write styles in JS or transform
React Inline Styles, Radium or JSS.
* [`postcss-safe-parser`] finds and fixes CSS syntax errors.
* [`midas`] converts a CSS string to highlighted HTML.
[`postcss-less-engine`]: https://github.com/Crunch/postcss-less
[`postcss-safe-parser`]: https://github.com/postcss/postcss-safe-parser
[`postcss-syntax`]: https://github.com/gucong3000/postcss-syntax
[`postcss-html`]: https://github.com/gucong3000/postcss-html
[`postcss-markdown`]: https://github.com/gucong3000/postcss-markdown
[`postcss-jsx`]: https://github.com/gucong3000/postcss-jsx
[`postcss-styled`]: https://github.com/gucong3000/postcss-styled
[`postcss-scss`]: https://github.com/postcss/postcss-scss
[`postcss-sass`]: https://github.com/AleshaOleg/postcss-sass
[`postcss-less`]: https://github.com/webschik/postcss-less
[`postcss-js`]: https://github.com/postcss/postcss-js
[`sugarss`]: https://github.com/postcss/sugarss
[`midas`]: https://github.com/ben-eb/midas
## Articles
* [Some things you may think about PostCSS… and you might be wrong](http://julian.io/some-things-you-may-think-about-postcss-and-you-might-be-wrong)
* [What PostCSS Really Is; What It Really Does](http://davidtheclark.com/its-time-for-everyone-to-learn-about-postcss)
* [PostCSS Guides](http://webdesign.tutsplus.com/series/postcss-deep-dive--cms-889)
More articles and videos you can find on [awesome-postcss](https://github.com/jjaderg/awesome-postcss) list.
## Books
* [Mastering PostCSS for Web Design](https://www.packtpub.com/web-development/mastering-postcss-web-design) by Alex Libby, Packt. (June 2016)
## Usage
You can start using PostCSS in just two steps:
1. Find and add PostCSS extensions for your build tool.
2. [Select plugins] and add them to your PostCSS process.
[Select plugins]: http://postcss.parts
### CSS-in-JS
The best way to use PostCSS with CSS-in-JS is [`astroturf`].
Add its loader to your `webpack.config.js`:
```js
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'postcss-loader'],
},
{
test: /\.jsx?$/,
use: ['babel-loader', 'astroturf/loader'],
}
]
}
}
```
Then create `postcss.config.js`:
```js
module.exports = {
plugins: [
require('autoprefixer'),
require('postcss-nested')
]
}
```
[`astroturf`]: https://github.com/4Catalyzer/astroturf
### Parcel
[Parcel] has built-in PostCSS support. It already uses Autoprefixer
and cssnano. If you want to change plugins, create `postcss.config.js`
in projects root:
```js
module.exports = {
plugins: [
require('autoprefixer'),
require('postcss-nested')
]
}
```
Parcel will even automatically install these plugins for you.
> Please, be aware of [the several issues in Version 1](https://github.com/parcel-bundler/parcel/labels/CSS%20Preprocessing). Notice, [Version 2](https://github.com/parcel-bundler/parcel/projects/5) may resolve the issues via [issue #2157](https://github.com/parcel-bundler/parcel/issues/2157).
[Parcel]: https://parceljs.org
### Webpack
Use [`postcss-loader`] in `webpack.config.js`:
```js
module.exports = {
module: {
rules: [
{
test: /\.css$/,
exclude: /node_modules/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
}
},
{
loader: 'postcss-loader'
}
]
}
]
}
}
```
Then create `postcss.config.js`:
```js
module.exports = {
plugins: [
require('precss'),
require('autoprefixer')
]
}
```
[`postcss-loader`]: https://github.com/postcss/postcss-loader
### Gulp
Use [`gulp-postcss`] and [`gulp-sourcemaps`].
```js
gulp.task('css', () => {
const postcss = require('gulp-postcss')
const sourcemaps = require('gulp-sourcemaps')
return gulp.src('src/**/*.css')
.pipe( sourcemaps.init() )
.pipe( postcss([ require('precss'), require('autoprefixer') ]) )
.pipe( sourcemaps.write('.') )
.pipe( gulp.dest('build/') )
})
```
[`gulp-sourcemaps`]: https://github.com/floridoo/gulp-sourcemaps
[`gulp-postcss`]: https://github.com/postcss/gulp-postcss
### npm run / CLI
To use PostCSS from your command-line interface or with npm scripts
there is [`postcss-cli`].
```sh
postcss --use autoprefixer -c options.json -o main.css css/*.css
```
[`postcss-cli`]: https://github.com/postcss/postcss-cli
### Browser
If you want to compile CSS string in browser (for instance, in live edit
tools like CodePen), just use [Browserify] or [webpack]. They will pack
PostCSS and plugins files into a single file.
To apply PostCSS plugins to React Inline Styles, JSS, Radium
and other [CSS-in-JS], you can use [`postcss-js`] and transforms style objects.
```js
var postcss = require('postcss-js')
var prefixer = postcss.sync([ require('autoprefixer') ])
prefixer({ display: 'flex' }) //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }
```
[`postcss-js`]: https://github.com/postcss/postcss-js
[Browserify]: http://browserify.org/
[CSS-in-JS]: https://github.com/MicheleBertoli/css-in-js
[webpack]: https://webpack.github.io/
### Runners
* **Grunt**: [`grunt-postcss`](https://github.com/nDmitry/grunt-postcss)
* **HTML**: [`posthtml-postcss`](https://github.com/posthtml/posthtml-postcss)
* **Stylus**: [`poststylus`](https://github.com/seaneking/poststylus)
* **Rollup**: [`rollup-plugin-postcss`](https://github.com/egoist/rollup-plugin-postcss)
* **Brunch**: [`postcss-brunch`](https://github.com/brunch/postcss-brunch)
* **Broccoli**: [`broccoli-postcss`](https://github.com/jeffjewiss/broccoli-postcss)
* **Meteor**: [`postcss`](https://atmospherejs.com/juliancwirko/postcss)
* **ENB**: [`enb-postcss`](https://github.com/awinogradov/enb-postcss)
* **Taskr**: [`taskr-postcss`](https://github.com/lukeed/taskr/tree/master/packages/postcss)
* **Start**: [`start-postcss`](https://github.com/start-runner/postcss)
* **Connect/Express**: [`postcss-middleware`](https://github.com/jedmao/postcss-middleware)
### JS API
For other environments, you can use the JS API:
```js
const autoprefixer = require('autoprefixer')
const postcss = require('postcss')
const precss = require('precss')
const fs = require('fs')
fs.readFile('src/app.css', (err, css) => {
postcss([precss, autoprefixer])
.process(css, { from: 'src/app.css', to: 'dest/app.css' })
.then(result => {
fs.writeFile('dest/app.css', result.css, () => true)
if ( result.map ) {
fs.writeFile('dest/app.css.map', result.map, () => true)
}
})
})
```
Read the [PostCSS API documentation] for more details about the JS API.
All PostCSS runners should pass [PostCSS Runner Guidelines].
[PostCSS Runner Guidelines]: https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md
[PostCSS API documentation]: http://api.postcss.org/postcss.html
### Options
Most PostCSS runners accept two parameters:
* An array of plugins.
* An object of options.
Common options:
* `syntax`: an object providing a syntax parser and a stringifier.
* `parser`: a special syntax parser (for example, [SCSS]).
* `stringifier`: a special syntax output generator (for example, [Midas]).
* `map`: [source map options].
* `from`: the input file name (most runners set it automatically).
* `to`: the output file name (most runners set it automatically).
[source map options]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md
[Midas]: https://github.com/ben-eb/midas
[SCSS]: https://github.com/postcss/postcss-scss
### Treat Warnings as Errors
In some situations it might be helpful to fail the build on any warning
from PostCSS or one of its plugins. This guarantees that no warnings
go unnoticed, and helps to avoid bugs. While there is no option to enable
treating warnings as errors, it can easily be done
by adding `postcss-fail-on-warn` plugin in the end of PostCSS plugins:
```js
module.exports = {
plugins: [
require('autoprefixer'),
require('postcss-fail-on-warn')
]
}
```
## Contributing
[Our contributing guidelines](./CONTRIBUTING.md) will help you
with making pull request to this project.
## Editors & IDE Integration
### VS Code
* [`csstools.postcss`] adds support for PostCSS, `postcss-preset-env`
and CSS Modules.
[`csstools.postcss`]: https://marketplace.visualstudio.com/items?itemName=csstools.postcss
### Atom
* [`language-postcss`] adds PostCSS and [SugarSS] highlight.
* [`source-preview-postcss`] previews your output CSS in a separate, live pane.
[SugarSS]: https://github.com/postcss/sugarss
### Sublime Text
* [`Syntax-highlighting-for-PostCSS`] adds PostCSS highlight.
[`Syntax-highlighting-for-PostCSS`]: https://github.com/hudochenkov/Syntax-highlighting-for-PostCSS
[`source-preview-postcss`]: https://atom.io/packages/source-preview-postcss
[`language-postcss`]: https://atom.io/packages/language-postcss
### Vim
* [`postcss.vim`] adds PostCSS highlight.
[`postcss.vim`]: https://github.com/stephenway/postcss.vim
### WebStorm
WebStorm 2016.3 [has] built-in PostCSS support.
[has]: https://blog.jetbrains.com/webstorm/2016/08/webstorm-2016-3-early-access-preview/
## Security Contact
To report a security vulnerability, please use the [Tidelift security contact].
Tidelift will coordinate the fix and disclosure.
[Tidelift security contact]: https://tidelift.com/security
## For Enterprise
Available as part of the Tidelift Subscription.
The maintainers of `postcss` and thousands of other packages are working
with Tidelift to deliver commercial support and maintenance for the open source
dependencies you use to build your applications. Save time, reduce risk,
and improve code health, while paying the maintainers of the exact dependencies
you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-postcss?utm_source=npm-postcss&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)

View File

@@ -0,0 +1,156 @@
## PostCSS Architecture
General overview of the PostCSS architecture.
It can be useful for everyone who wishes to contribute to the core or develop a better understanding of the tool.
**Table of Contents**
- [Overview](#overview)
- [Workflow](#workflow)
- [Core Structures](#core-structures)
* [Tokenizer](#tokenizer--libtokenizees6-)
* [Parser](#parser--libparsees6-libparseres6-)
* [Processor](#processor--libprocessores6-)
* [Stringifier](#stringifier--libstringifyes6-libstringifieres6-)
- [API](#api-reference)
### Overview
> This section describes ideas lying behind PostCSS
Before diving deeper into the development of PostCSS let's briefly describe what is PostCSS and what is not.
**PostCSS**
- *is **NOT** a style preprocessor like `Sass` or `Less`.*
It does not define a custom syntax and semantics, it's not actually a language.
PostCSS works with CSS and can be easily integrated with the tools described above. That being said any valid CSS can be processed by PostCSS.
- *is a tool for CSS syntax transformations*
It allows you to define custom CSS like syntax that could be understandable and transformed by plugins. That being said PostCSS is not strictly about CSS spec but about syntax definition manner of CSS. In such a way you can define custom syntax constructs like at-rule, that could be very helpful for tools build around PostCSS. PostCSS plays the role of a framework for building outstanding tools for CSS manipulations.
- *is a big player in CSS ecosystem*
A Large amount of lovely tools like `Autoprefixer`, `Stylelint`, `CSSnano` were built on PostCSS ecosystem. There is a big chance that you already use it implicitly, just check your `node_modules` :smiley:
### Workflow
This is a high-level overview of the whole PostCSS workflow
<img width="300" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/aa/PostCSS_scheme.svg/512px-PostCSS_scheme.svg.png" alt="workflow">
As you can see from the diagram above, PostCSS architecture is pretty straightforward but some parts of it could be misunderstood.
You can see a part called *Parser*, this construct will be described in details later on, just for now think about it as a structure that can understand your CSS like syntax and create an object representation of it.
That being said, there are few ways to write a parser.
- *Write a single file with string to AST transformation*
This method is quite popular, for example, the [Rework analyzer](https://github.com/reworkcss/css/blob/master/lib/parse/index.js) was written in this style. But with a large code base, the code becomes hard to read and pretty slow.
- *Split it into lexical analysis/parsing steps (source string → tokens → AST)*
This is the way of how we do it in PostCSS and also the most popular one.
A lot of parsers like [`@babel/parser` (parser behind Babel)](https://github.com/babel/babel/tree/master/packages/babel-parser), [`CSSTree`](https://github.com/csstree/csstree) were written in such way.
The main reasons to separate tokenization from parsing steps are performance and abstracting complexity.
Let think about why the second way is better for our needs.
First of all, because string to tokens step takes more time than parsing step. We operate on large source string and process it char by char, this is why it is very inefficient operation in terms of performance and we should perform it only once.
But from other side tokens to AST transformation is logically more complex so with such separation we could write very fast tokenizer (but from this comes sometimes hard to read code) and easy to read (but slow) parser.
Summing it up splitting into two steps improve performance and code readability.
So now let's look more closely on structures that play the main role in PostCSS workflow.
### Core Structures
- #### Tokenizer ( [lib/tokenize.es6](https://github.com/postcss/postcss/blob/master/lib/tokenize.es6) )
Tokenizer (aka Lexer) plays important role in syntax analysis.
It accepts CSS string and returns a list of tokens.
Token is a simple structure that describes some part of syntax like `at-rule`, `comment` or `word`. It can also contain positional information for more descriptive errors.
For example, if we consider following CSS
```css
.className { color: #FFF; }
```
corresponding tokens from PostCSS will be
```js
[
["word", ".className", 1, 1, 1, 10]
["space", " "]
["{", "{", 1, 12]
["space", " "]
["word", "color", 1, 14, 1, 18]
[":", ":", 1, 19]
["space", " "]
["word", "#FFF" , 1, 21, 1, 23]
[";", ";", 1, 24]
["space", " "]
["}", "}", 1, 26]
]
```
As you can see from the example above a single token represented as a list and also `space` token doesn't have positional information.
Let's look more closely on single token like `word`. As it was said each token represented as a list and follow such pattern.
```js
const token = [
// represents token type
'word',
// represents matched word
'.className',
// This two numbers represent start position of token.
// It is optional value as we saw in the example above,
// tokens like `space` don't have such information.
// Here the first number is line number and the second one is corresponding column.
1, 1,
// Next two numbers also optional and represent end position for multichar tokens like this one. Numbers follow same rule as was described above
1, 10
]
```
There are many patterns how tokenization could be done, PostCSS motto is performance and simplicity. Tokenization is a complex computing operation and takes a large amount of syntax analysis time ( ~90% ), that why PostCSS' Tokenizer looks dirty but it was optimized for speed. Any high-level constructs like classes could dramatically slow down tokenizer.
PostCSS' Tokenizer uses some sort of streaming/chaining API where you expose [`nextToken()`](https://github.com/postcss/postcss/blob/master/lib/tokenize.es6#L48-L308) method to Parser. In this manner, we provide a clean interface for Parser and reduce memory usage by storing only a few tokens and not the whole list of tokens.
- #### Parser ( [lib/parse.es6](https://github.com/postcss/postcss/blob/master/lib/parse.es6), [lib/parser.es6](https://github.com/postcss/postcss/blob/master/lib/parser.es6) )
Parser is the main structure responsible for [syntax analysis](https://en.wikipedia.org/wiki/Parsing) of incoming CSS. Parser produces a structure called [Abstract Syntax Tree (AST)](https://en.wikipedia.org/wiki/Abstract_syntax_tree) that could then be transformed by plugins later on.
Parser works in common with Tokenizer and operates over tokens, not source string, as it would be a very inefficient operation.
It uses mostly `nextToken` and `back` methods provided by Tokenizer for obtaining single or multiple tokens and then construct part of AST called `Node`.
There are multiple Node types that PostCSS could produce but all of them inherit from base Node [class](https://github.com/postcss/postcss/blob/master/lib/node.es6#L34).
- #### Processor ( [lib/processor.es6](https://github.com/postcss/postcss/blob/master/lib/processor.es6) )
Processor is a very plain structure that initializes plugins and runs syntax transformations. Plugin is just a function registered with [postcss.plugin](https://github.com/postcss/postcss/blob/master/lib/postcss.es6#L109) call.
It exposes only a few public API methods. Description of them could be found on [api.postcss.org/Processor](http://api.postcss.org/Processor.html)
- #### Stringifier ( [lib/stringify.es6](https://github.com/postcss/postcss/blob/master/lib/stringify.es6), [lib/stringifier.es6](https://github.com/postcss/postcss/blob/master/lib/stringifier.es6) )
Stringifier is a base class that translates modified AST to pure CSS string. Stringifier traverses AST starting from provided Node and generates a raw string representation of it calling corresponding methods.
The most essential method is [`Stringifier.stringify`](https://github.com/postcss/postcss/blob/master/lib/stringifier.es6#L25-L27)
that accepts initial Node and semicolon indicator.
You can learn more by checking [stringifier.es6](https://github.com/postcss/postcss/blob/master/lib/stringifier.es6)
### API Reference
More descriptive API documentation could be found [here](http://api.postcss.org/)

View File

@@ -0,0 +1,195 @@
# PostCSS Plugin Guidelines
A PostCSS plugin is a function that receives and, usually,
transforms a CSS AST from the PostCSS parser.
The rules below are *mandatory* for all PostCSS plugins.
See also [ClojureWerkzs recommendations] for open source projects.
[ClojureWerkzs recommendations]: http://blog.clojurewerkz.org/blog/2013/04/20/how-to-make-your-open-source-project-really-awesome/
## 1. API
### 1.1 Clear name with `postcss-` prefix
The plugins purpose should be clear just by reading its name.
If you wrote a transpiler for CSS 4 Custom Media, `postcss-custom-media`
would be a good name. If you wrote a plugin to support mixins,
`postcss-mixins` would be a good name.
The prefix `postcss-` shows that the plugin is part of the PostCSS ecosystem.
This rule is not mandatory for plugins that can run as independent tools,
without the user necessarily knowing that it is powered by
PostCSS — for example, [RTLCSS] and [Autoprefixer].
[Autoprefixer]: https://github.com/postcss/autoprefixer
[RTLCSS]: https://rtlcss.com/
### 1.2. Do one thing, and do it well
Do not create multitool plugins. Several small, one-purpose plugins bundled into
a plugin pack is usually a better solution.
For example, [`postcss-preset-env`] contains many small plugins,
one for each W3C specification. And [`cssnano`] contains a separate plugin
for each of its optimization.
[`postcss-preset-env`]: https://preset-env.cssdb.org/
[`cssnano`]: https://github.com/ben-eb/cssnano
### 1.3. Do not use mixins
Preprocessors libraries like Compass provide an API with mixins.
PostCSS plugins are different.
A plugin cannot be just a set of mixins for [`postcss-mixins`].
To achieve your goal, consider transforming valid CSS
or using custom at-rules and custom properties.
[`postcss-mixins`]: https://github.com/postcss/postcss-mixins
### 1.4. Create plugin by `postcss.plugin`
By wrapping your function in this method,
you are hooking into a common plugin API:
```js
module.exports = postcss.plugin('plugin-name', opts => {
return (root, result) => {
// Plugin code
}
})
```
## 2. Processing
### 2.1. Plugin must be tested
A CI service like [Travis] is also recommended for testing code in
different environments. You should test in (at least) Node.js [active LTS](https://github.com/nodejs/LTS) and current stable version.
[Travis]: https://travis-ci.org/
### 2.2. Use asynchronous methods whenever possible
For example, use `fs.writeFile` instead of `fs.writeFileSync`:
```js
postcss.plugin('plugin-sprite', opts => {
return (root, result) => {
return new Promise((resolve, reject) => {
const sprite = makeSprite()
fs.writeFile(opts.file, sprite, err => {
if (err) return reject(err)
resolve()
})
})
}
})
```
### 2.3. Set `node.source` for new nodes
Every node must have a relevant `source` so PostCSS can generate
an accurate source map.
So if you add a new declaration based on some existing declaration, you should
clone the existing declaration in order to save that original `source`.
```js
if (needPrefix(decl.prop)) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
}
```
You can also set `source` directly, copying from some existing node:
```js
if (decl.prop === 'animation') {
const keyframe = createAnimationByName(decl.value)
keyframes.source = decl.source
decl.root().append(keyframes)
}
```
### 2.4. Use only the public PostCSS API
PostCSS plugins must not rely on undocumented properties or methods,
which may be subject to change in any minor release. The public API
is described in [API docs].
[API docs]: http://api.postcss.org/
## 3. Errors
### 3.1. Use `node.error` on CSS relevant errors
If you have an error because of input CSS (like an unknown name
in a mixin plugin) you should use `node.error` to create an error
that includes source position:
```js
if (typeof mixins[name] === 'undefined') {
throw decl.error('Unknown mixin ' + name, { plugin: 'postcss-mixins' })
}
```
### 3.2. Use `result.warn` for warnings
Do not print warnings with `console.log` or `console.warn`,
because some PostCSS runner may not allow console output.
```js
if (outdated(decl.prop)) {
result.warn(decl.prop + ' is outdated', { node: decl })
}
```
If CSS input is a source of the warning, the plugin must set the `node` option.
## 4. Documentation
### 4.1. Document your plugin in English
PostCSS plugins must have their `README.md` wrote in English. Do not be afraid
of your English skills, as the open source community will fix your errors.
Of course, you are welcome to write documentation in other languages;
just name them appropriately (e.g. `README.ja.md`).
### 4.2. Include input and output examples
The plugin's `README.md` must contain example input and output CSS.
A clear example is the best way to describe how your plugin works.
The first section of the `README.md` is a good place to put examples.
See [postcss-opacity](https://github.com/iamvdo/postcss-opacity) for an example.
Of course, this guideline does not apply if your plugin does not
transform the CSS.
### 4.3. Maintain a changelog
PostCSS plugins must describe the changes of all their releases
in a separate file, such as `CHANGELOG.md`, `History.md`, or [GitHub Releases].
Visit [Keep A Changelog] for more information about how to write one of these.
Of course, you should be using [SemVer].
[Keep A Changelog]: http://keepachangelog.com/
[GitHub Releases]: https://help.github.com/articles/creating-releases/
[SemVer]: http://semver.org/
### 4.4. Include `postcss-plugin` keyword in `package.json`
PostCSS plugins written for npm must have the `postcss-plugin` keyword
in their `package.json`. This special keyword will be useful for feedback about
the PostCSS ecosystem.
For packages not published to npm, this is not mandatory, but is recommended
if the package format can contain keywords.

View File

@@ -0,0 +1,143 @@
# PostCSS Runner Guidelines
A PostCSS runner is a tool that processes CSS through a user-defined list
of plugins; for example, [`postcss-cli`] or [`gulppostcss`].
These rules are mandatory for any such runners.
For single-plugin tools, like [`gulp-autoprefixer`],
these rules are not mandatory but are highly recommended.
See also [ClojureWerkzs recommendations] for open source projects.
[ClojureWerkzs recommendations]: http://blog.clojurewerkz.org/blog/2013/04/20/how-to-make-your-open-source-project-really-awesome/
[`gulp-autoprefixer`]: https://github.com/sindresorhus/gulp-autoprefixer
[`gulppostcss`]: https://github.com/w0rm/gulp-postcss
[`postcss-cli`]: https://github.com/postcss/postcss-cli
## 1. API
### 1.1. Accept functions in plugin parameters
If your runner uses a config file, it must be written in JavaScript, so that
it can support plugins which accept a function, such as [`postcss-assets`]:
```js
module.exports = [
require('postcss-assets')({
cachebuster: function (file) {
return fs.statSync(file).mtime.getTime().toString(16)
}
})
]
```
[`postcss-assets`]: https://github.com/borodean/postcss-assets
## 2. Processing
### 2.1. Set `from` and `to` processing options
To ensure that PostCSS generates source maps and displays better syntax errors,
runners must specify the `from` and `to` options. If your runner does not handle
writing to disk (for example, a gulp transform), you should set both options
to point to the same file:
```js
processor.process({ from: file.path, to: file.path })
```
### 2.2. Use only the asynchronous API
PostCSS runners must use only the asynchronous API.
The synchronous API is provided only for debugging, is slower,
and cant work with asynchronous plugins.
```js
processor.process(opts).then(result => {
// processing is finished
});
```
### 2.3. Use only the public PostCSS API
PostCSS runners must not rely on undocumented properties or methods,
which may be subject to change in any minor release. The public API
is described in [API docs].
[API docs]: http://api.postcss.org/
## 3. Output
### 3.1. Dont show JS stack for `CssSyntaxError`
PostCSS runners must not show a stack trace for CSS syntax errors,
as the runner can be used by developers who are not familiar with JavaScript.
Instead, handle such errors gracefully:
```js
processor.process(opts).catch(error => {
if (error.name === 'CssSyntaxError') {
process.stderr.write(error.message + error.showSourceCode())
} else {
throw error
}
})
```
### 3.2. Display `result.warnings()`
PostCSS runners must output warnings from `result.warnings()`:
```js
result.warnings().forEach(warn => {
process.stderr.write(warn.toString())
})
```
See also [postcss-log-warnings] and [postcss-messages] plugins.
[postcss-log-warnings]: https://github.com/davidtheclark/postcss-log-warnings
[postcss-messages]: https://github.com/postcss/postcss-messages
### 3.3. Allow the user to write source maps to different files
PostCSS by default will inline source maps in the generated file; however,
PostCSS runners must provide an option to save the source map in a different
file:
```js
if (result.map) {
fs.writeFile(opts.to + '.map', result.map.toString())
}
```
## 4. Documentation
### 4.1. Document your runner in English
PostCSS runners must have their `README.md` wrote in English. Do not be afraid
of your English skills, as the open source community will fix your errors.
Of course, you are welcome to write documentation in other languages;
just name them appropriately (e.g. `README.ja.md`).
### 4.2. Maintain a changelog
PostCSS runners must describe changes of all releases in a separate file,
such as `ChangeLog.md`, `History.md`, or with [GitHub Releases].
Visit [Keep A Changelog] for more information on how to write one of these.
Of course, you should use [SemVer].
[Keep A Changelog]: http://keepachangelog.com/
[GitHub Releases]: https://help.github.com/articles/creating-releases/
[SemVer]: http://semver.org/
### 4.3. `postcss-runner` keyword in `package.json`
PostCSS runners written for npm must have the `postcss-runner` keyword
in their `package.json`. This special keyword will be useful for feedback about
the PostCSS ecosystem.
For packages not published to npm, this is not mandatory, but recommended
if the package format is allowed to contain keywords.

View File

@@ -0,0 +1,74 @@
# PostCSS and Source Maps
PostCSS has great [source maps] support. It can read and interpret maps
from previous transformation steps, autodetect the format that you expect,
and output both external and inline maps.
To ensure that you generate an accurate source map, you must indicate the input
and output CSS file paths — using the options `from` and `to`, respectively.
To generate a new source map with the default options, simply set `map: true`.
This will generate an inline source map that contains the source content.
If you dont want the map inlined, you can set `map.inline: false`.
```js
processor
.process(css, {
from: 'app.sass.css',
to: 'app.css',
map: { inline: false }
})
.then(result => {
result.map //=> '{ "version":3,
// "file":"app.css",
// "sources":["app.sass"],
// "mappings":"AAAA,KAAI" }'
})
```
If PostCSS finds source maps from a previous transformation,
it will automatically update that source map with the same options.
## Options
If you want more control over source map generation, you can define the `map`
option as an object with the following parameters:
* `inline` boolean: indicates that the source map should be embedded
in the output CSS as a Base64-encoded comment. By default, it is `true`.
But if all previous maps are external, not inline, PostCSS will not embed
the map even if you do not set this option.
If you have an inline source map, the `result.map` property will be empty,
as the source map will be contained within the text of `result.css`.
* `prev` string, object, boolean or function: source map content from
a previous processing step (for example, Sass compilation).
PostCSS will try to read the previous source map automatically
(based on comments within the source CSS), but you can use this option
to identify it manually. If desired, you can omit the previous map
with `prev: false`.
* `sourcesContent` boolean: indicates that PostCSS should set the origin
content (for example, Sass source) of the source map. By default,
it is `true`. But if all previous maps do not contain sources content,
PostCSS will also leave it out even if you do not set this option.
* `annotation` boolean or string: indicates that PostCSS should add annotation
comments to the CSS. By default, PostCSS will always add a comment with a path
to the source map. PostCSS will not add annotations to CSS files that
do not contain any comments.
By default, PostCSS presumes that you want to save the source map as
`opts.to + '.map'` and will use this path in the annotation comment.
A different path can be set by providing a string value for `annotation`.
If you have set `inline: true`, annotation cannot be disabled.
* `from` string: by default, PostCSS will set the `sources` property of the map
to the value of the `from` option. If you want to override this behaviour, you
can use `map.from` to explicitly set the source map's `sources` property.
Path should be absolute or relative from generated file
(`to` option in `process()` method).
[source maps]: http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/

View File

@@ -0,0 +1,233 @@
# How to Write Custom Syntax
PostCSS can transform styles in any syntax, and is not limited to just CSS.
By writing a custom syntax, you can transform styles in any desired format.
Writing a custom syntax is much harder than writing a PostCSS plugin, but
it is an awesome adventure.
There are 3 types of PostCSS syntax packages:
* **Parser** to parse input string to nodes tree.
* **Stringifier** to generate output string by nodes tree.
* **Syntax** contains both parser and stringifier.
## Syntax
A good example of a custom syntax is [SCSS]. Some users may want to transform
SCSS sources with PostCSS plugins, for example if they need to add vendor
prefixes or change the property order. So this syntax should output SCSS from
an SCSS input.
The syntax API is a very simple plain object, with `parse` & `stringify`
functions:
```js
module.exports = {
parse: require('./parse'),
stringify: require('./stringify')
}
```
[SCSS]: https://github.com/postcss/postcss-scss
## Parser
A good example of a parser is [Safe Parser], which parses malformed/broken CSS.
Because there is no point to generate broken output, this package only provides
a parser.
The parser API is a function which receives a string & returns a [`Root`] node.
The second argument is a function which receives an object with PostCSS options.
```js
const postcss = require('postcss')
module.exports = function parse (css, opts) {
const root = postcss.root()
// Add other nodes to root
return root
}
```
[Safe Parser]: https://github.com/postcss/postcss-safe-parser
[`Root`]: http://api.postcss.org/Root.html
### Main Theory
There are many books about parsers; but do not worry because CSS syntax is
very easy, and so the parser will be much simpler than a programming language
parser.
The default PostCSS parser contains two steps:
1. [Tokenizer] which reads input string character by character and builds a
tokens array. For example, it joins space symbols to a `['space', '\n ']`
token, and detects strings to a `['string', '"\"{"']` token.
2. [Parser] which reads the tokens array, creates node instances and
builds a tree.
[Tokenizer]: https://github.com/postcss/postcss/blob/master/lib/tokenize.es6
[Parser]: https://github.com/postcss/postcss/blob/master/lib/parser.es6
### Performance
Parsing input is often the most time consuming task in CSS processors. So it
is very important to have a fast parser.
The main rule of optimization is that there is no performance without a
benchmark. You can look at [PostCSS benchmarks] to build your own.
Of parsing tasks, the tokenize step will often take the most time, so its
performance should be prioritized. Unfortunately, classes, functions and
high level structures can slow down your tokenizer. Be ready to write dirty
code with repeated statements. This is why it is difficult to extend the
default [PostCSS tokenizer]; copy & paste will be a necessary evil.
Second optimization is using character codes instead of strings.
```js
// Slow
string[i] === '{'
// Fast
const OPEN_CURLY = 123 // `{'
string.charCodeAt(i) === OPEN_CURLY
```
Third optimization is “fast jumps”. If you find open quotes, you can find
next closing quote much faster by `indexOf`:
```js
// Simple jump
next = string.indexOf('"', currentPosition + 1)
// Jump by RegExp
regexp.lastIndex = currentPosion + 1
regexp.test(string)
next = regexp.lastIndex
```
The parser can be a well written class. There is no need in copy-paste and
hardcore optimization there. You can extend the default [PostCSS parser].
[PostCSS benchmarks]: https://github.com/postcss/benchmark
[PostCSS tokenizer]: https://github.com/postcss/postcss/blob/master/lib/tokenize.es6
[PostCSS parser]: https://github.com/postcss/postcss/blob/master/lib/parser.es6
### Node Source
Every node should have `source` property to generate correct source map.
This property contains `start` and `end` properties with `{ line, column }`,
and `input` property with an [`Input`] instance.
Your tokenizer should save the original position so that you can propagate
the values to the parser, to ensure that the source map is correctly updated.
[`Input`]: https://github.com/postcss/postcss/blob/master/lib/input.es6
### Raw Values
A good PostCSS parser should provide all information (including spaces symbols)
to generate byte-to-byte equal output. It is not so difficult, but respectful
for user input and allow integration smoke tests.
A parser should save all additional symbols to `node.raws` object.
It is an open structure for you, you can add additional keys.
For example, [SCSS parser] saves comment types (`/* */` or `//`)
in `node.raws.inline`.
The default parser cleans CSS values from comments and spaces.
It saves the original value with comments to `node.raws.value.raw` and uses it,
if the node value was not changed.
[SCSS parser]: https://github.com/postcss/postcss-scss
### Tests
Of course, all parsers in the PostCSS ecosystem must have tests.
If your parser just extends CSS syntax (like [SCSS] or [Safe Parser]),
you can use the [PostCSS Parser Tests]. It contains unit & integration tests.
[PostCSS Parser Tests]: https://github.com/postcss/postcss-parser-tests
## Stringifier
A style guide generator is a good example of a stringifier. It generates output
HTML which contains CSS components. For this use case, a parser isn't necessary,
so the package should just contain a stringifier.
The Stringifier API is little bit more complicated, than the parser API.
PostCSS generates a source map, so a stringifier cant just return a string.
It must link every substring with its source node.
A Stringifier is a function which receives [`Root`] node and builder callback.
Then it calls builder with every nodes string and node instance.
```js
module.exports = function stringify (root, builder) {
// Some magic
const string = decl.prop + ':' + decl.value + ';'
builder(string, decl)
// Some science
};
```
### Main Theory
PostCSS [default stringifier] is just a class with a method for each node type
and many methods to detect raw properties.
In most cases it will be enough just to extend this class,
like in [SCSS stringifier].
[default stringifier]: https://github.com/postcss/postcss/blob/master/lib/stringifier.es6
[SCSS stringifier]: https://github.com/postcss/postcss-scss/blob/master/lib/scss-stringifier.es6
### Builder Function
A builder function will be passed to `stringify` function as second argument.
For example, the default PostCSS stringifier class saves it
to `this.builder` property.
Builder receives output substring and source node to append this substring
to the final output.
Some nodes contain other nodes in the middle. For example, a rule has a `{`
at the beginning, many declarations inside and a closing `}`.
For these cases, you should pass a third argument to builder function:
`'start'` or `'end'` string:
```js
this.builder(rule.selector + '{', rule, 'start')
// Stringify declarations inside
this.builder('}', rule, 'end')
```
### Raw Values
A good PostCSS custom syntax saves all symbols and provide byte-to-byte equal
output if there were no changes.
This is why every node has `node.raws` object to store space symbol, etc.
All data related to source code and not CSS structure, should be in `Node#raws`. For instance, `postcss-scss` keep in `Comment#raws.inline` boolean marker of inline comment (`// comment` instead of `/* comment */`).
Be careful, because sometimes these raw properties will not be present; some
nodes may be built manually, or may lose their indentation when they are moved
to another parent node.
This is why the default stringifier has a `raw()` method to autodetect raw
properties by other nodes. For example, it will look at other nodes to detect
indent size and them multiply it with the current node depth.
### Tests
A stringifier must have tests too.
You can use unit and integration test cases from [PostCSS Parser Tests].
Just compare input CSS with CSS after your parser and stringifier.
[PostCSS Parser Tests]: https://github.com/postcss/postcss-parser-tests

View File

@@ -0,0 +1,127 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _container = _interopRequireDefault(require("./container"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
/**
* Represents an at-rule.
*
* If its followed in the CSS by a {} block, this node will have
* a nodes property representing its children.
*
* @extends Container
*
* @example
* const root = postcss.parse('@charset "UTF-8"; @media print {}')
*
* const charset = root.first
* charset.type //=> 'atrule'
* charset.nodes //=> undefined
*
* const media = root.last
* media.nodes //=> []
*/
var AtRule = /*#__PURE__*/function (_Container) {
_inheritsLoose(AtRule, _Container);
function AtRule(defaults) {
var _this;
_this = _Container.call(this, defaults) || this;
_this.type = 'atrule';
return _this;
}
var _proto = AtRule.prototype;
_proto.append = function append() {
var _Container$prototype$;
if (!this.nodes) this.nodes = [];
for (var _len = arguments.length, children = new Array(_len), _key = 0; _key < _len; _key++) {
children[_key] = arguments[_key];
}
return (_Container$prototype$ = _Container.prototype.append).call.apply(_Container$prototype$, [this].concat(children));
};
_proto.prepend = function prepend() {
var _Container$prototype$2;
if (!this.nodes) this.nodes = [];
for (var _len2 = arguments.length, children = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
children[_key2] = arguments[_key2];
}
return (_Container$prototype$2 = _Container.prototype.prepend).call.apply(_Container$prototype$2, [this].concat(children));
}
/**
* @memberof AtRule#
* @member {string} name The at-rules name immediately follows the `@`.
*
* @example
* const root = postcss.parse('@media print {}')
* media.name //=> 'media'
* const media = root.first
*/
/**
* @memberof AtRule#
* @member {string} params The at-rules parameters, the values
* that follow the at-rules name but precede
* any {} block.
*
* @example
* const root = postcss.parse('@media print, screen {}')
* const media = root.first
* media.params //=> 'print, screen'
*/
/**
* @memberof AtRule#
* @member {object} raws Information to generate byte-to-byte equal
* node string as it was in the origin input.
*
* Every parser saves its own properties,
* but the default CSS parser uses:
*
* * `before`: the space symbols before the node. It also stores `*`
* and `_` symbols before the declaration (IE hack).
* * `after`: the space symbols after the last child of the node
* to the end of the node.
* * `between`: the symbols between the property and value
* for declarations, selector and `{` for rules, or last parameter
* and `{` for at-rules.
* * `semicolon`: contains true if the last child has
* an (optional) semicolon.
* * `afterName`: the space between the at-rule name and its parameters.
*
* PostCSS cleans at-rule parameters from comments and extra spaces,
* but it stores origin content in raws properties.
* As such, if you dont change a declarations value,
* PostCSS will use the raw value with comments.
*
* @example
* const root = postcss.parse(' @media\nprint {\n}')
* root.first.first.raws //=> { before: ' ',
* // between: ' ',
* // afterName: '\n',
* // after: '\n' }
*/
;
return AtRule;
}(_container.default);
var _default = AtRule;
exports.default = _default;
module.exports = exports.default;
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImF0LXJ1bGUuZXM2Il0sIm5hbWVzIjpbIkF0UnVsZSIsImRlZmF1bHRzIiwidHlwZSIsImFwcGVuZCIsIm5vZGVzIiwiY2hpbGRyZW4iLCJwcmVwZW5kIiwiQ29udGFpbmVyIl0sIm1hcHBpbmdzIjoiOzs7OztBQUFBOzs7Ozs7QUFFQTs7Ozs7Ozs7Ozs7Ozs7Ozs7O0lBa0JNQSxNOzs7QUFDSixrQkFBYUMsUUFBYixFQUF1QjtBQUFBOztBQUNyQixrQ0FBTUEsUUFBTjtBQUNBLFVBQUtDLElBQUwsR0FBWSxRQUFaO0FBRnFCO0FBR3RCOzs7O1NBRURDLE0sR0FBQSxrQkFBcUI7QUFBQTs7QUFDbkIsUUFBSSxDQUFDLEtBQUtDLEtBQVYsRUFBaUIsS0FBS0EsS0FBTCxHQUFhLEVBQWI7O0FBREUsc0NBQVZDLFFBQVU7QUFBVkEsTUFBQUEsUUFBVTtBQUFBOztBQUVuQix5REFBYUYsTUFBYixrREFBdUJFLFFBQXZCO0FBQ0QsRzs7U0FFREMsTyxHQUFBLG1CQUFzQjtBQUFBOztBQUNwQixRQUFJLENBQUMsS0FBS0YsS0FBVixFQUFpQixLQUFLQSxLQUFMLEdBQWEsRUFBYjs7QUFERyx1Q0FBVkMsUUFBVTtBQUFWQSxNQUFBQSxRQUFVO0FBQUE7O0FBRXBCLDBEQUFhQyxPQUFiLG1EQUF3QkQsUUFBeEI7QUFDRDtBQUVEOzs7Ozs7Ozs7O0FBVUE7Ozs7Ozs7Ozs7OztBQVlBOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0VBdENtQkUsa0I7O2VBdUVOUCxNIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IENvbnRhaW5lciBmcm9tICcuL2NvbnRhaW5lcidcblxuLyoqXG4gKiBSZXByZXNlbnRzIGFuIGF0LXJ1bGUuXG4gKlxuICogSWYgaXTigJlzIGZvbGxvd2VkIGluIHRoZSBDU1MgYnkgYSB7fSBibG9jaywgdGhpcyBub2RlIHdpbGwgaGF2ZVxuICogYSBub2RlcyBwcm9wZXJ0eSByZXByZXNlbnRpbmcgaXRzIGNoaWxkcmVuLlxuICpcbiAqIEBleHRlbmRzIENvbnRhaW5lclxuICpcbiAqIEBleGFtcGxlXG4gKiBjb25zdCByb290ID0gcG9zdGNzcy5wYXJzZSgnQGNoYXJzZXQgXCJVVEYtOFwiOyBAbWVkaWEgcHJpbnQge30nKVxuICpcbiAqIGNvbnN0IGNoYXJzZXQgPSByb290LmZpcnN0XG4gKiBjaGFyc2V0LnR5cGUgIC8vPT4gJ2F0cnVsZSdcbiAqIGNoYXJzZXQubm9kZXMgLy89PiB1bmRlZmluZWRcbiAqXG4gKiBjb25zdCBtZWRpYSA9IHJvb3QubGFzdFxuICogbWVkaWEubm9kZXMgICAvLz0+IFtdXG4gKi9cbmNsYXNzIEF0UnVsZSBleHRlbmRzIENvbnRhaW5lciB7XG4gIGNvbnN0cnVjdG9yIChkZWZhdWx0cykge1xuICAgIHN1cGVyKGRlZmF1bHRzKVxuICAgIHRoaXMudHlwZSA9ICdhdHJ1bGUnXG4gIH1cblxuICBhcHBlbmQgKC4uLmNoaWxkcmVuKSB7XG4gICAgaWYgKCF0aGlzLm5vZGVzKSB0aGlzLm5vZGVzID0gW11cbiAgICByZXR1cm4gc3VwZXIuYXBwZW5kKC4uLmNoaWxkcmVuKVxuICB9XG5cbiAgcHJlcGVuZCAoLi4uY2hpbGRyZW4pIHtcbiAgICBpZiAoIXRoaXMubm9kZXMpIHRoaXMubm9kZXMgPSBbXVxuICAgIHJldHVybiBzdXBlci5wcmVwZW5kKC4uLmNoaWxkcmVuKVxuICB9XG5cbiAgLyoqXG4gICAqIEBtZW1iZXJvZiBBdFJ1bGUjXG4gICAqIEBtZW1iZXIge3N0cmluZ30gbmFtZSBUaGUgYXQtcnVsZeKAmXMgbmFtZSBpbW1lZGlhdGVseSBmb2xsb3dzIHRoZSBgQGAuXG4gICAqXG4gICAqIEBleGFtcGxlXG4gICAqIGNvbnN0IHJvb3QgID0gcG9zdGNzcy5wYXJzZSgnQG1lZGlhIHByaW50IHt9JylcbiAgICogbWVkaWEubmFtZSAvLz0+ICdtZWRpYSdcbiAgICogY29uc3QgbWVkaWEgPSByb290LmZpcnN0XG4gICAqL1xuXG4gIC8qKlxuICAgKiBAbWVtYmVyb2YgQXRSdWxlI1xuICAgKiBAbWVtYmVyIHtzdHJpbmd9IHBhcmFtcyBUaGUgYXQtcnVsZeKAmXMgcGFyYW1ldGVycywgdGhlIHZhbHVlc1xuICAgKiAgICAgICAgICAgICAgICAgICAgICAgICB0aGF0IGZvbGxvdyB0aGUgYXQtcnVsZeKAmXMgbmFtZSBidXQgcHJlY2VkZVxuICAgKiAgICAgICAgICAgICAgICAgICAgICAgICBhbnkge30gYmxvY2suXG4gICAqXG4gICAqIEBleGFtcGxlXG4gICAqIGNvbnN0IHJvb3QgID0gcG9zdGNzcy5wYXJzZSgnQG1lZGlhIHByaW50LCBzY3JlZW4ge30nKVxuICAgKiBjb25zdCBtZWRpYSA9IHJvb3QuZmlyc3RcbiAgICogbWVkaWEucGFyYW1zIC8vPT4gJ3ByaW50LCBzY3JlZW4nXG4gICAqL1xuXG4gIC8qKlxuICAgKiBAbWVtYmVyb2YgQXRSdWxlI1xuICAgKiBAbWVtYmVyIHtvYmplY3R9IHJhd3MgSW5mb3JtYXRpb24gdG8gZ2VuZXJhdGUgYnl0ZS10by1ieXRlIGVxdWFsXG4gICAqICAgICAgICAgICAgICAgICAgICAgICAgbm9kZSBzdHJpbmcgYXMgaXQgd2FzIGluIHRoZSBvcmlnaW4gaW5wdXQuXG4gICAqXG4gICAqIEV2ZXJ5IHBhcnNlciBzYXZlcyBpdHMgb3duIHByb3BlcnRpZXMsXG4gICAqIGJ1dCB0aGUgZGVmYXVsdCBDU1MgcGFyc2VyIHVzZXM6XG4gICAqXG4gICAqICogYGJlZm9yZWA6IHRoZSBzcGFjZSBzeW1ib2xzIGJlZm9yZSB0aGUgbm9kZS4gSXQgYWxzbyBzdG9yZXMgYCpgXG4gICAqICAgYW5kIGBfYCBzeW1ib2xzIGJlZm9yZSB0aGUgZGVjbGFyYXRpb24gKElFIGhhY2spLlxuICAgKiAqIGBhZnRlcmA6IHRoZSBzcGFjZSBzeW1ib2xzIGFmdGVyIHRoZSBsYXN0IGNoaWxkIG9mIHRoZSBub2RlXG4gICAqICAgdG8gdGhlIGVuZCBvZiB0aGUgbm9kZS5cbiAgICogKiBgYmV0d2VlbmA6IHRoZSBzeW1ib2xzIGJldHdlZW4gdGhlIHByb3BlcnR5IGFuZCB2YWx1ZVxuICAgKiAgIGZvciBkZWNsYXJhdGlvbnMsIHNlbGVjdG9yIGFuZCBge2AgZm9yIHJ1bGVzLCBvciBsYXN0IHBhcmFtZXRlclxuICAgKiAgIGFuZCBge2AgZm9yIGF0LXJ1bGVzLlxuICAgKiAqIGBzZW1pY29sb25gOiBjb250YWlucyB0cnVlIGlmIHRoZSBsYXN0IGNoaWxkIGhhc1xuICAgKiAgIGFuIChvcHRpb25hbCkgc2VtaWNvbG9uLlxuICAgKiAqIGBhZnRlck5hbWVgOiB0aGUgc3BhY2UgYmV0d2VlbiB0aGUgYXQtcnVsZSBuYW1lIGFuZCBpdHMgcGFyYW1ldGVycy5cbiAgICpcbiAgICogUG9zdENTUyBjbGVhbnMgYXQtcnVsZSBwYXJhbWV0ZXJzIGZyb20gY29tbWVudHMgYW5kIGV4dHJhIHNwYWNlcyxcbiAgICogYnV0IGl0IHN0b3JlcyBvcmlnaW4gY29udGVudCBpbiByYXdzIHByb3BlcnRpZXMuXG4gICAqIEFzIHN1Y2gsIGlmIHlvdSBkb27igJl0IGNoYW5nZSBhIGRlY2xhcmF0aW9u4oCZcyB2YWx1ZSxcbiAgICogUG9zdENTUyB3aWxsIHVzZSB0aGUgcmF3IHZhbHVlIHdpdGggY29tbWVudHMuXG4gICAqXG4gICAqIEBleGFtcGxlXG4gICAqIGNvbnN0IHJvb3QgPSBwb3N0Y3NzLnBhcnNlKCcgIEBtZWRpYVxcbnByaW50IHtcXG59JylcbiAgICogcm9vdC5maXJzdC5maXJzdC5yYXdzIC8vPT4geyBiZWZvcmU6ICcgICcsXG4gICAqICAgICAgICAgICAgICAgICAgICAgICAvLyAgICAgYmV0d2VlbjogJyAnLFxuICAgKiAgICAgICAgICAgICAgICAgICAgICAgLy8gICAgIGFmdGVyTmFtZTogJ1xcbicsXG4gICAqICAgICAgICAgICAgICAgICAgICAgICAvLyAgICAgYWZ0ZXI6ICdcXG4nIH1cbiAgICovXG59XG5cbmV4cG9ydCBkZWZhdWx0IEF0UnVsZVxuIl0sImZpbGUiOiJhdC1ydWxlLmpzIn0=

View File

@@ -0,0 +1,55 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _node = _interopRequireDefault(require("./node"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
/**
* Represents a comment between declarations or statements (rule and at-rules).
*
* Comments inside selectors, at-rule parameters, or declaration values
* will be stored in the `raws` properties explained above.
*
* @extends Node
*/
var Comment = /*#__PURE__*/function (_Node) {
_inheritsLoose(Comment, _Node);
function Comment(defaults) {
var _this;
_this = _Node.call(this, defaults) || this;
_this.type = 'comment';
return _this;
}
/**
* @memberof Comment#
* @member {string} text The comments text.
*/
/**
* @memberof Comment#
* @member {object} raws Information to generate byte-to-byte equal
* node string as it was in the origin input.
*
* Every parser saves its own properties,
* but the default CSS parser uses:
*
* * `before`: the space symbols before the node.
* * `left`: the space symbols between `/*` and the comments text.
* * `right`: the space symbols between the comments text.
*/
return Comment;
}(_node.default);
var _default = Comment;
exports.default = _default;
module.exports = exports.default;
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNvbW1lbnQuZXM2Il0sIm5hbWVzIjpbIkNvbW1lbnQiLCJkZWZhdWx0cyIsInR5cGUiLCJOb2RlIl0sIm1hcHBpbmdzIjoiOzs7OztBQUFBOzs7Ozs7QUFFQTs7Ozs7Ozs7SUFRTUEsTzs7O0FBQ0osbUJBQWFDLFFBQWIsRUFBdUI7QUFBQTs7QUFDckIsNkJBQU1BLFFBQU47QUFDQSxVQUFLQyxJQUFMLEdBQVksU0FBWjtBQUZxQjtBQUd0QjtBQUVEOzs7OztBQUtBOzs7Ozs7Ozs7Ozs7Ozs7RUFYb0JDLGE7O2VBeUJQSCxPIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IE5vZGUgZnJvbSAnLi9ub2RlJ1xuXG4vKipcbiAqIFJlcHJlc2VudHMgYSBjb21tZW50IGJldHdlZW4gZGVjbGFyYXRpb25zIG9yIHN0YXRlbWVudHMgKHJ1bGUgYW5kIGF0LXJ1bGVzKS5cbiAqXG4gKiBDb21tZW50cyBpbnNpZGUgc2VsZWN0b3JzLCBhdC1ydWxlIHBhcmFtZXRlcnMsIG9yIGRlY2xhcmF0aW9uIHZhbHVlc1xuICogd2lsbCBiZSBzdG9yZWQgaW4gdGhlIGByYXdzYCBwcm9wZXJ0aWVzIGV4cGxhaW5lZCBhYm92ZS5cbiAqXG4gKiBAZXh0ZW5kcyBOb2RlXG4gKi9cbmNsYXNzIENvbW1lbnQgZXh0ZW5kcyBOb2RlIHtcbiAgY29uc3RydWN0b3IgKGRlZmF1bHRzKSB7XG4gICAgc3VwZXIoZGVmYXVsdHMpXG4gICAgdGhpcy50eXBlID0gJ2NvbW1lbnQnXG4gIH1cblxuICAvKipcbiAgICogQG1lbWJlcm9mIENvbW1lbnQjXG4gICAqIEBtZW1iZXIge3N0cmluZ30gdGV4dCBUaGUgY29tbWVudOKAmXMgdGV4dC5cbiAgICovXG5cbiAgLyoqXG4gICAqIEBtZW1iZXJvZiBDb21tZW50I1xuICAgKiBAbWVtYmVyIHtvYmplY3R9IHJhd3MgSW5mb3JtYXRpb24gdG8gZ2VuZXJhdGUgYnl0ZS10by1ieXRlIGVxdWFsXG4gICAqICAgICAgICAgICAgICAgICAgICAgICBub2RlIHN0cmluZyBhcyBpdCB3YXMgaW4gdGhlIG9yaWdpbiBpbnB1dC5cbiAgICpcbiAgICogRXZlcnkgcGFyc2VyIHNhdmVzIGl0cyBvd24gcHJvcGVydGllcyxcbiAgICogYnV0IHRoZSBkZWZhdWx0IENTUyBwYXJzZXIgdXNlczpcbiAgICpcbiAgICogKiBgYmVmb3JlYDogdGhlIHNwYWNlIHN5bWJvbHMgYmVmb3JlIHRoZSBub2RlLlxuICAgKiAqIGBsZWZ0YDogdGhlIHNwYWNlIHN5bWJvbHMgYmV0d2VlbiBgLypgIGFuZCB0aGUgY29tbWVudOKAmXMgdGV4dC5cbiAgICogKiBgcmlnaHRgOiB0aGUgc3BhY2Ugc3ltYm9scyBiZXR3ZWVuIHRoZSBjb21tZW504oCZcyB0ZXh0LlxuICAgKi9cbn1cblxuZXhwb3J0IGRlZmF1bHQgQ29tbWVudFxuIl0sImZpbGUiOiJjb21tZW50LmpzIn0=

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,96 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _node = _interopRequireDefault(require("./node"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
/**
* Represents a CSS declaration.
*
* @extends Node
*
* @example
* const root = postcss.parse('a { color: black }')
* const decl = root.first.first
* decl.type //=> 'decl'
* decl.toString() //=> ' color: black'
*/
var Declaration = /*#__PURE__*/function (_Node) {
_inheritsLoose(Declaration, _Node);
function Declaration(defaults) {
var _this;
_this = _Node.call(this, defaults) || this;
_this.type = 'decl';
return _this;
}
/**
* @memberof Declaration#
* @member {string} prop The declarations property name.
*
* @example
* const root = postcss.parse('a { color: black }')
* const decl = root.first.first
* decl.prop //=> 'color'
*/
/**
* @memberof Declaration#
* @member {string} value The declarations value.
*
* @example
* const root = postcss.parse('a { color: black }')
* const decl = root.first.first
* decl.value //=> 'black'
*/
/**
* @memberof Declaration#
* @member {boolean} important `true` if the declaration
* has an !important annotation.
*
* @example
* const root = postcss.parse('a { color: black !important; color: red }')
* root.first.first.important //=> true
* root.first.last.important //=> undefined
*/
/**
* @memberof Declaration#
* @member {object} raws Information to generate byte-to-byte equal
* node string as it was in the origin input.
*
* Every parser saves its own properties,
* but the default CSS parser uses:
*
* * `before`: the space symbols before the node. It also stores `*`
* and `_` symbols before the declaration (IE hack).
* * `between`: the symbols between the property and value
* for declarations.
* * `important`: the content of the important statement,
* if it is not just `!important`.
*
* PostCSS cleans declaration from comments and extra spaces,
* but it stores origin content in raws properties.
* As such, if you dont change a declarations value,
* PostCSS will use the raw value with comments.
*
* @example
* const root = postcss.parse('a {\n color:black\n}')
* root.first.first.raws //=> { before: '\n ', between: ':' }
*/
return Declaration;
}(_node.default);
var _default = Declaration;
exports.default = _default;
module.exports = exports.default;
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImRlY2xhcmF0aW9uLmVzNiJdLCJuYW1lcyI6WyJEZWNsYXJhdGlvbiIsImRlZmF1bHRzIiwidHlwZSIsIk5vZGUiXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUE7Ozs7OztBQUVBOzs7Ozs7Ozs7OztJQVdNQSxXOzs7QUFDSix1QkFBYUMsUUFBYixFQUF1QjtBQUFBOztBQUNyQiw2QkFBTUEsUUFBTjtBQUNBLFVBQUtDLElBQUwsR0FBWSxNQUFaO0FBRnFCO0FBR3RCO0FBRUQ7Ozs7Ozs7Ozs7QUFVQTs7Ozs7Ozs7OztBQVVBOzs7Ozs7Ozs7OztBQVdBOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7RUFyQ3dCQyxhOztlQStEWEgsVyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBOb2RlIGZyb20gJy4vbm9kZSdcblxuLyoqXG4gKiBSZXByZXNlbnRzIGEgQ1NTIGRlY2xhcmF0aW9uLlxuICpcbiAqIEBleHRlbmRzIE5vZGVcbiAqXG4gKiBAZXhhbXBsZVxuICogY29uc3Qgcm9vdCA9IHBvc3Rjc3MucGFyc2UoJ2EgeyBjb2xvcjogYmxhY2sgfScpXG4gKiBjb25zdCBkZWNsID0gcm9vdC5maXJzdC5maXJzdFxuICogZGVjbC50eXBlICAgICAgIC8vPT4gJ2RlY2wnXG4gKiBkZWNsLnRvU3RyaW5nKCkgLy89PiAnIGNvbG9yOiBibGFjaydcbiAqL1xuY2xhc3MgRGVjbGFyYXRpb24gZXh0ZW5kcyBOb2RlIHtcbiAgY29uc3RydWN0b3IgKGRlZmF1bHRzKSB7XG4gICAgc3VwZXIoZGVmYXVsdHMpXG4gICAgdGhpcy50eXBlID0gJ2RlY2wnXG4gIH1cblxuICAvKipcbiAgICogQG1lbWJlcm9mIERlY2xhcmF0aW9uI1xuICAgKiBAbWVtYmVyIHtzdHJpbmd9IHByb3AgVGhlIGRlY2xhcmF0aW9u4oCZcyBwcm9wZXJ0eSBuYW1lLlxuICAgKlxuICAgKiBAZXhhbXBsZVxuICAgKiBjb25zdCByb290ID0gcG9zdGNzcy5wYXJzZSgnYSB7IGNvbG9yOiBibGFjayB9JylcbiAgICogY29uc3QgZGVjbCA9IHJvb3QuZmlyc3QuZmlyc3RcbiAgICogZGVjbC5wcm9wIC8vPT4gJ2NvbG9yJ1xuICAgKi9cblxuICAvKipcbiAgICogQG1lbWJlcm9mIERlY2xhcmF0aW9uI1xuICAgKiBAbWVtYmVyIHtzdHJpbmd9IHZhbHVlIFRoZSBkZWNsYXJhdGlvbuKAmXMgdmFsdWUuXG4gICAqXG4gICAqIEBleGFtcGxlXG4gICAqIGNvbnN0IHJvb3QgPSBwb3N0Y3NzLnBhcnNlKCdhIHsgY29sb3I6IGJsYWNrIH0nKVxuICAgKiBjb25zdCBkZWNsID0gcm9vdC5maXJzdC5maXJzdFxuICAgKiBkZWNsLnZhbHVlIC8vPT4gJ2JsYWNrJ1xuICAgKi9cblxuICAvKipcbiAgICogQG1lbWJlcm9mIERlY2xhcmF0aW9uI1xuICAgKiBAbWVtYmVyIHtib29sZWFufSBpbXBvcnRhbnQgYHRydWVgIGlmIHRoZSBkZWNsYXJhdGlvblxuICAgKiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaGFzIGFuICFpbXBvcnRhbnQgYW5ub3RhdGlvbi5cbiAgICpcbiAgICogQGV4YW1wbGVcbiAgICogY29uc3Qgcm9vdCA9IHBvc3Rjc3MucGFyc2UoJ2EgeyBjb2xvcjogYmxhY2sgIWltcG9ydGFudDsgY29sb3I6IHJlZCB9JylcbiAgICogcm9vdC5maXJzdC5maXJzdC5pbXBvcnRhbnQgLy89PiB0cnVlXG4gICAqIHJvb3QuZmlyc3QubGFzdC5pbXBvcnRhbnQgIC8vPT4gdW5kZWZpbmVkXG4gICAqL1xuXG4gIC8qKlxuICAgKiBAbWVtYmVyb2YgRGVjbGFyYXRpb24jXG4gICAqIEBtZW1iZXIge29iamVjdH0gcmF3cyBJbmZvcm1hdGlvbiB0byBnZW5lcmF0ZSBieXRlLXRvLWJ5dGUgZXF1YWxcbiAgICogICAgICAgICAgICAgICAgICAgICAgIG5vZGUgc3RyaW5nIGFzIGl0IHdhcyBpbiB0aGUgb3JpZ2luIGlucHV0LlxuICAgKlxuICAgKiBFdmVyeSBwYXJzZXIgc2F2ZXMgaXRzIG93biBwcm9wZXJ0aWVzLFxuICAgKiBidXQgdGhlIGRlZmF1bHQgQ1NTIHBhcnNlciB1c2VzOlxuICAgKlxuICAgKiAqIGBiZWZvcmVgOiB0aGUgc3BhY2Ugc3ltYm9scyBiZWZvcmUgdGhlIG5vZGUuIEl0IGFsc28gc3RvcmVzIGAqYFxuICAgKiAgIGFuZCBgX2Agc3ltYm9scyBiZWZvcmUgdGhlIGRlY2xhcmF0aW9uIChJRSBoYWNrKS5cbiAgICogKiBgYmV0d2VlbmA6IHRoZSBzeW1ib2xzIGJldHdlZW4gdGhlIHByb3BlcnR5IGFuZCB2YWx1ZVxuICAgKiAgIGZvciBkZWNsYXJhdGlvbnMuXG4gICAqICogYGltcG9ydGFudGA6IHRoZSBjb250ZW50IG9mIHRoZSBpbXBvcnRhbnQgc3RhdGVtZW50LFxuICAgKiAgIGlmIGl0IGlzIG5vdCBqdXN0IGAhaW1wb3J0YW50YC5cbiAgICpcbiAgICogUG9zdENTUyBjbGVhbnMgZGVjbGFyYXRpb24gZnJvbSBjb21tZW50cyBhbmQgZXh0cmEgc3BhY2VzLFxuICAgKiBidXQgaXQgc3RvcmVzIG9yaWdpbiBjb250ZW50IGluIHJhd3MgcHJvcGVydGllcy5cbiAgICogQXMgc3VjaCwgaWYgeW91IGRvbuKAmXQgY2hhbmdlIGEgZGVjbGFyYXRpb27igJlzIHZhbHVlLFxuICAgKiBQb3N0Q1NTIHdpbGwgdXNlIHRoZSByYXcgdmFsdWUgd2l0aCBjb21tZW50cy5cbiAgICpcbiAgICogQGV4YW1wbGVcbiAgICogY29uc3Qgcm9vdCA9IHBvc3Rjc3MucGFyc2UoJ2Ege1xcbiAgY29sb3I6YmxhY2tcXG59JylcbiAgICogcm9vdC5maXJzdC5maXJzdC5yYXdzIC8vPT4geyBiZWZvcmU6ICdcXG4gICcsIGJldHdlZW46ICc6JyB9XG4gICAqL1xufVxuXG5leHBvcnQgZGVmYXVsdCBEZWNsYXJhdGlvblxuIl0sImZpbGUiOiJkZWNsYXJhdGlvbi5qcyJ9

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,40 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _parser = _interopRequireDefault(require("./parser"));
var _input = _interopRequireDefault(require("./input"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function parse(css, opts) {
var input = new _input.default(css, opts);
var parser = new _parser.default(input);
try {
parser.parse();
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
if (e.name === 'CssSyntaxError' && opts && opts.from) {
if (/\.scss$/i.test(opts.from)) {
e.message += '\nYou tried to parse SCSS with ' + 'the standard CSS parser; ' + 'try again with the postcss-scss parser';
} else if (/\.sass/i.test(opts.from)) {
e.message += '\nYou tried to parse Sass with ' + 'the standard CSS parser; ' + 'try again with the postcss-sass parser';
} else if (/\.less$/i.test(opts.from)) {
e.message += '\nYou tried to parse Less with ' + 'the standard CSS parser; ' + 'try again with the postcss-less parser';
}
}
}
throw e;
}
return parser.root;
}
var _default = parse;
exports.default = _default;
module.exports = exports.default;
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBhcnNlLmVzNiJdLCJuYW1lcyI6WyJwYXJzZSIsImNzcyIsIm9wdHMiLCJpbnB1dCIsIklucHV0IiwicGFyc2VyIiwiUGFyc2VyIiwiZSIsInByb2Nlc3MiLCJlbnYiLCJOT0RFX0VOViIsIm5hbWUiLCJmcm9tIiwidGVzdCIsIm1lc3NhZ2UiLCJyb290Il0sIm1hcHBpbmdzIjoiOzs7OztBQUFBOztBQUNBOzs7O0FBRUEsU0FBU0EsS0FBVCxDQUFnQkMsR0FBaEIsRUFBcUJDLElBQXJCLEVBQTJCO0FBQ3pCLE1BQUlDLEtBQUssR0FBRyxJQUFJQyxjQUFKLENBQVVILEdBQVYsRUFBZUMsSUFBZixDQUFaO0FBQ0EsTUFBSUcsTUFBTSxHQUFHLElBQUlDLGVBQUosQ0FBV0gsS0FBWCxDQUFiOztBQUNBLE1BQUk7QUFDRkUsSUFBQUEsTUFBTSxDQUFDTCxLQUFQO0FBQ0QsR0FGRCxDQUVFLE9BQU9PLENBQVAsRUFBVTtBQUNWLFFBQUlDLE9BQU8sQ0FBQ0MsR0FBUixDQUFZQyxRQUFaLEtBQXlCLFlBQTdCLEVBQTJDO0FBQ3pDLFVBQUlILENBQUMsQ0FBQ0ksSUFBRixLQUFXLGdCQUFYLElBQStCVCxJQUEvQixJQUF1Q0EsSUFBSSxDQUFDVSxJQUFoRCxFQUFzRDtBQUNwRCxZQUFJLFdBQVdDLElBQVgsQ0FBZ0JYLElBQUksQ0FBQ1UsSUFBckIsQ0FBSixFQUFnQztBQUM5QkwsVUFBQUEsQ0FBQyxDQUFDTyxPQUFGLElBQWEsb0NBQ0EsMkJBREEsR0FFQSx3Q0FGYjtBQUdELFNBSkQsTUFJTyxJQUFJLFVBQVVELElBQVYsQ0FBZVgsSUFBSSxDQUFDVSxJQUFwQixDQUFKLEVBQStCO0FBQ3BDTCxVQUFBQSxDQUFDLENBQUNPLE9BQUYsSUFBYSxvQ0FDQSwyQkFEQSxHQUVBLHdDQUZiO0FBR0QsU0FKTSxNQUlBLElBQUksV0FBV0QsSUFBWCxDQUFnQlgsSUFBSSxDQUFDVSxJQUFyQixDQUFKLEVBQWdDO0FBQ3JDTCxVQUFBQSxDQUFDLENBQUNPLE9BQUYsSUFBYSxvQ0FDQSwyQkFEQSxHQUVBLHdDQUZiO0FBR0Q7QUFDRjtBQUNGOztBQUNELFVBQU1QLENBQU47QUFDRDs7QUFFRCxTQUFPRixNQUFNLENBQUNVLElBQWQ7QUFDRDs7ZUFFY2YsSyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBQYXJzZXIgZnJvbSAnLi9wYXJzZXInXG5pbXBvcnQgSW5wdXQgZnJvbSAnLi9pbnB1dCdcblxuZnVuY3Rpb24gcGFyc2UgKGNzcywgb3B0cykge1xuICBsZXQgaW5wdXQgPSBuZXcgSW5wdXQoY3NzLCBvcHRzKVxuICBsZXQgcGFyc2VyID0gbmV3IFBhcnNlcihpbnB1dClcbiAgdHJ5IHtcbiAgICBwYXJzZXIucGFyc2UoKVxuICB9IGNhdGNoIChlKSB7XG4gICAgaWYgKHByb2Nlc3MuZW52Lk5PREVfRU5WICE9PSAncHJvZHVjdGlvbicpIHtcbiAgICAgIGlmIChlLm5hbWUgPT09ICdDc3NTeW50YXhFcnJvcicgJiYgb3B0cyAmJiBvcHRzLmZyb20pIHtcbiAgICAgICAgaWYgKC9cXC5zY3NzJC9pLnRlc3Qob3B0cy5mcm9tKSkge1xuICAgICAgICAgIGUubWVzc2FnZSArPSAnXFxuWW91IHRyaWVkIHRvIHBhcnNlIFNDU1Mgd2l0aCAnICtcbiAgICAgICAgICAgICAgICAgICAgICAgJ3RoZSBzdGFuZGFyZCBDU1MgcGFyc2VyOyAnICtcbiAgICAgICAgICAgICAgICAgICAgICAgJ3RyeSBhZ2FpbiB3aXRoIHRoZSBwb3N0Y3NzLXNjc3MgcGFyc2VyJ1xuICAgICAgICB9IGVsc2UgaWYgKC9cXC5zYXNzL2kudGVzdChvcHRzLmZyb20pKSB7XG4gICAgICAgICAgZS5tZXNzYWdlICs9ICdcXG5Zb3UgdHJpZWQgdG8gcGFyc2UgU2FzcyB3aXRoICcgK1xuICAgICAgICAgICAgICAgICAgICAgICAndGhlIHN0YW5kYXJkIENTUyBwYXJzZXI7ICcgK1xuICAgICAgICAgICAgICAgICAgICAgICAndHJ5IGFnYWluIHdpdGggdGhlIHBvc3Rjc3Mtc2FzcyBwYXJzZXInXG4gICAgICAgIH0gZWxzZSBpZiAoL1xcLmxlc3MkL2kudGVzdChvcHRzLmZyb20pKSB7XG4gICAgICAgICAgZS5tZXNzYWdlICs9ICdcXG5Zb3UgdHJpZWQgdG8gcGFyc2UgTGVzcyB3aXRoICcgK1xuICAgICAgICAgICAgICAgICAgICAgICAndGhlIHN0YW5kYXJkIENTUyBwYXJzZXI7ICcgK1xuICAgICAgICAgICAgICAgICAgICAgICAndHJ5IGFnYWluIHdpdGggdGhlIHBvc3Rjc3MtbGVzcyBwYXJzZXInXG4gICAgICAgIH1cbiAgICAgIH1cbiAgICB9XG4gICAgdGhyb3cgZVxuICB9XG5cbiAgcmV0dXJuIHBhcnNlci5yb290XG59XG5cbmV4cG9ydCBkZWZhdWx0IHBhcnNlXG4iXSwiZmlsZSI6InBhcnNlLmpzIn0=

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show More