postcssfunctionProcessorclassLazyResultclassResultclassWarningclassCssSyntaxErrorclass- Vendor module
- List module
Inputclass- Nodes common methods
- Containers common methods
RootnodeAtRulenodeRulenodeDeclarationnodeCommentnode
The postcss function is the main entry point for PostCSS.
var postcss = require('postcss');For those using TypeScript, typings are already provided in this package. Simply, import PostCSS as you would normally.
import * as postcss from 'postcss';Returns a new Processor instance that will apply plugins
as CSS processors.
postcss([autoprefixer, cssnext, cssgrace]).process(css).css;Arguments:
plugins (array): list of PostCSS plugins to be included as processors.
Plugins can also be included with the Processor#use method.
See its description below for details about plugin formats.
Parses source css and returns a new Root node, which contains
the source CSS nodes.
// Simple CSS concatenation with source map support
var root1 = postcss.parse(css1, { from: file1 });
var root2 = postcss.parse(css2, { from: file2 });
root1.append(root2).toResult().css;Arguments:
css (string|#toString): String with input CSS or any object withtoString()method, like a Buffer.opts (object) optional: options:from: the path to the source CSS file. You should always setfrom, because it is used in map generation and in syntax error messages.map: an object of source map options. Onlymap.previs used inparse.
Creates a PostCSS plugin with a standard API.
var remove = postcss.plugin('postcss-remove', function (opts) {
opts = opts || {};
var filter = opts.prop || 'z-index';
return function (css, result) {
css.walkDecls(filter, function (decl) {
decl.remove();
});
};
});
postcss([ remove ]) // with default options
postcss([ remove({ prop: 'color' })]) // with optionsArguments:
name (string): PostCSS plugin name. Same as innameproperty inpackage.json. It will be saved inplugin.postcssPluginproperty.initializer (function): will receive plugin options and should return functions to modify nodes in input CSS.
The newly-wrapped function will provide both the name and PostCSS version of the plugin:
var processor = postcss([replace]);
processor.plugins[0].postcssPlugin //=> 'postcss-replace'
processor.plugins[0].postcssVersion //=> '4.1.0'The plugin function receives 2 arguments: Root node and Result instance.
The function should mutate the provided Root node. Alternatively, you can
create a new Root node and override the result.root property.
var cleaner = postcss.plugin('postcss-cleaner', function () {
return function (css, result) {
result.root = postcss.root();
};
});As a convenience, plugins also expose a process method so that you can use
them as standalone tools.
cleaner.process(css, options);
// This is equivalent to:
postcss([ cleaner(options) ]).process(css);Asynchronous plugins should return a Promise instance.
postcss.plugin('postcss-import', function () {
return function (css, result) {
return new Promise(function (resolve, reject) {
fs.readFile('base.css', function (base) {
css.prepend(base);
resolve();
});
});
};
});Add warnings using the Node#warn() method.
postcss.plugin('postcss-caniuse-test', function () {
return function (css, result) {
css.walkDecls(function (decl) {
if ( !caniuse.support(decl.prop) ) {
decl.warn(result,
'Some browsers do not support ' + decl.prop);
}
});
};
});Send data to other plugins using the Result#messages array.
Creates a new Root node.
postcss.root({ after: '\n' }).toString() //=> "\n"Arguments:
props (object) optional: properties for the new root node.
Creates a new AtRule node.
postcss.atRule({ name: 'charset' }).toString() //=> "@charset"Arguments:
props (object) optional: properties for the new at-rule node.
Creates a new Rule node.
postcss.rule({ selector: 'a' }).toString() //=> "a {\n}"Arguments:
props (object) optional: properties for the new rule node.
Creates a new Declaration node.
postcss.decl({ prop: 'color', value: 'black' }).toString() //=> "color: black"Arguments:
props (object) optional: properties for the new declaration node.
Creates a new Comment node.
postcss.comment({ text: 'test' }).toString() //=> "/* test */"Arguments:
props (object) optional: properties for the new comment node.
Contains the Vendor module.
postcss.vendor.unprefixed('-moz-tab') //=> ['tab']Contains the List module.
postcss.list.space('5px calc(10% + 5px)') //=> ['5px', 'calc(10% + 5px)']Default function to convert a node tree into a CSS string.
A Processor instance contains plugins to process CSS. Create
one Processor instance, initialize its plugins, and then use that instance
on numerous CSS files.
var processor = postcss([autoprefixer, cssnext, cssgrace]);
processor.process(css1).css;
processor.process(css2).css;Adds a plugin to be used as a CSS processor.
var processor = postcss();
processor.use(autoprefixer()).use(cssnext()).use(cssgrace());Arguments:
plugin (function|#postcss|Processor): PostCSS plugin. It can be in three formats:- A plugin created by
postcss.plugin()method. - A function. PostCSS will pass the function a
Rootnode as the first argument and currentResultinstance as the second. - An object with a
postcssmethod. PostCSS will use that method as described in #2. - Another
Processorinstance. PostCSS will copy plugins from that instance into this one.
- A plugin created by
Plugins can also be added by passing them as arguments when creating
a postcss instance (see postcss(plugins)).
Asynchronous Plugins should return a Promise instance.
Parses source CSS and returns a LazyResult instance. Because some plugins
can be asynchronous it doesn’t make any transformations. Transformations will
be applied in the LazyResult’s methods.
processor.process(css, { from: 'a.css', to: 'a.out.css' }).then(function (result) {
console.log(result.css);
});Arguments:
css (string|#toString|Result): String with input CSS or any object with atoString()method, like a Buffer. Optionally, send aResultinstance and the processor will take the existing [Root] parser from it.opts (object) optional: options:from: the path of the CSS source file. You should always setfrom, because it is used in source map generation and syntax error messages.to: the path where you’ll put the output CSS file. You should always settoto generate correct source maps.parser: function to generate AST by string.stringifier: class to generate string by AST.syntax: object withparseandstringifyfunctions.map: an object of source map options.
Contains plugins added to this processor.
var processor = postcss([cssnext, cssgrace]);
processor.plugins.length //=> 2Contains the current version of PostCSS.
postcss().version //=> '4.0.5'A promise proxy for the result of PostCSS transformations.
A LazyResult instance is returned by Processor#process(css, opts).
var lazy = postcss([cssnext]).process(css);Processes input CSS through synchronous and asynchronous plugins
and calls onFulfilled with a Result instance. If a plugin throws
an error, the onRejected callback will be executed.
postcss([cssnext]).process(css).then(function(result) {
console.log(result.css);
});This method is a standard Promise method.
Processes input CSS through synchronous and asynchronous plugins
and calls onRejected for each error thrown in any plugin.
postcss([cssnext]).process(css).then(function(result) {
console.log(result.css);
}).catch(function (error) {
console.error(error);
});This method is a standard Promise method.
Alias for the LazyResult#css property.
Processes input CSS through synchronous plugins, converts Root to a CSS
string and returns Result#css.
processor.process(css).css;This property will only work with synchronous plugins. If the processor
contains any asynchronous plugins it will throw an error. In this case,
you should use LazyResult#then() instead.
postcss([cssnext]).then(function (result) {
console.log(result.css);
});An alias for the css property. Use it with syntaxes that generate non-CSS
output.
lazy.css === lazy.content;Processes input CSS through synchronous plugins and returns Result#map.
if ( result.map ) {
fs.writeFileSync(result.opts.to + '.map', result.map.toString());
}This property will only work with synchronous plugins. If the processor
contains any asynchronous plugins it will throw an error. In this case,
you should use LazyResult#then() instead.
postcss([cssnext]).then(function (result) {
if ( result.map ) {
fs.writeFileSync(result.opts.to + '.map', result.map.toString());
}
});Processes input CSS through synchronous plugins and returns
Result#root.
This property will only work with synchronous plugins. If the processor
contains any asynchronous plugins it will throw an error. In this case,
you should use LazyResult#then() instead.
postcss([cssnext]).then(function (result) {
console.log(result.root);
});Processes input CSS through synchronous plugins and calls [Result#warnings()].
postcss([cssnext]).warnings().forEach(function (message) {
console.warn(message.text);
});This property will only work with synchronous plugins. If the processor
contains any asynchronous plugins it will throw an error. In this case,
you should use LazyResult#then() instead.
postcss([cssnext]).then(function (result) {
result.warnings().forEach(function (message) {
console.warn(message.text);
});
});Processes input CSS through synchronous plugins and returns Result#messages.
This property will only work with synchronous plugins. If the processor
contains any asynchronous plugins it will throw an error. In this case,
you should use LazyResult#then() instead.
Returns a Processor instance, which will be used for CSS transformations.
var lazy = postcss([cssnext, cssgrace]).process(css);
lazy.processor.plugins.length //=> 2Options from the Processor#process(css, opts) call that produced
this Result instance.
postcss().process(css, opts).opts == opts;Provides the result of the PostCSS transformations.
A Result instance is returned by Root#toResult(opts)
or LazyResult#then() methods.
postcss([cssnext]).process(css).then(function (result1) {
console.log(result1.css);
});
var result2 = postcss.parse(css).toResult();Alias for Result#css property.
Creates an instance of Warning and adds it to Result#messages.
var plugin = postcss.plugin('postcss-important', function () {
return function (css, result) {
css.walkDecls(function (decl) {
if ( decl.important ) {
result.warn('Try to avoid !important', { node: decl });
}
});
};
});
postcss([plugin]).process(css).then(function (result) {
result.warnings() //=> [{
// plugin: 'postcss-important-warning',
// text: 'Try to avoid !important'
// node: { type: 'decl', … }
// }]
});Arguments:
text (string): warning message. It will be used in thetextproperty of the message object.opts (object) optional: properties to assign to the message object.node: CSS node that was the source of the warning.word (string): word inside a node’s string that should be highlighted as the source of the warning.index(number): index inside a node’s string that should be highlighted as the source of the warning.plugin: name of the plugin that created this warning.Result#warn()will automatically fill it with theplugin.postcssPluginvalue.
Returns warnings from plugins. Filters Warning instances
from [Result#messages].
result.warnings().forEach(function (message) {
console.log(message.toString());
});A CSS string representing this Result’s 'Root instance.
postcss.parse('a{}').toResult().css //=> "a{}"An alias for the css property. Use it with syntaxes that generate non-CSS
output.
result.css === result.content;An instance of the SourceMapGenerator class from the source-map library,
representing changes to the Result’s Root instance.
result.map.toJSON() //=> { version: 3, file: 'a.css', sources: ['a.css'], … }This property will have a value only if the user does not want an inline source
map. By default, PostCSS generates inline source maps, written directly into
the processed CSS. The map property will be empty by default.
An external source map will be generated — and assigned to map —
only if the user has set the map.inline option to false, or if PostCSS
was passed an external input source map.
if ( result.map ) {
fs.writeFileSync(result.opts.to + '.map', result.map.toString());
}Contains the Root node after all transformations.
root.toResult().root == root;Contains messages from plugins (e.g., warnings or custom messages).
Each message should have type and plugin properties.
postcss.plugin('postcss-min-browser', function () {
return function (css, result) {
var browsers = detectMinBrowsersByCanIUse(css);
result.messages.push({
type: 'min-browser',
plugin: 'postcss-min-browser',
browsers: browsers
});
};
});Add a warning using Result#warn() and get all warnings
using the Result#warnings() method.
Returns the Processor instance used for this transformation.
result.processor.plugins.forEach(function (plugin) {
if ( plugin.postcssPlugin == 'postcss-bad' ) {
throw 'postcss-good is incompatible with postcss-bad';
}
});Options from the Processor#process(css, opts) or Root#toResult(opts)
call that produced this Result instance.
root.toResult(opts).opts == opts;Represents a plugin warning. It can be created using Node#warn().
if ( decl.important ) {
decl.warn(result, 'Try to avoid !important');
}Returns a string with the error position and message.
warning.toString() //=> 'postcss-important:a.css:10:4: Try to avoid !important'Contains the warning message.
warning.text //=> 'Try to avoid !important'Contains the name of the plugin that created this warning. When you call
Node#warn() it will fill this property automatically.
warning.plugin //=> 'postcss-important'Contains the CSS node that caused the warning.
warning.node.toString() //=> 'color: white !important'The line in the input file with this warning’s source.
warning.line //=> 5Column in the input file with this warning’s source.
warning.column //=> 4The CSS parser throws this error for broken CSS.
postcss.parse('a{') //=> CssSyntaxErrorCustom parsers can throw this error for broken custom syntax
using the Node#error() method.
throw node.error('Unknown variable', { plugin: 'postcss-vars' });Returns a string with the error position, message and source code of the broken part.
error.toString() //=> CssSyntaxError: app.css:1:1: Unclosed block
// a {
// ^Returns a few lines of CSS source that caused the error.
error.showSourceCode() //=>
// a {
// bad
// ^
// }Arguments:
color (boolean) optional: whether arrow will be colored red by terminal color codes. By default, PostCSS will useprocess.stdout.isTTYandprocess.env.NODE_DISABLE_COLORS.
If the CSS has an input source map without sourceContent, this method will
return an empty string.
Contains full error text in the GNU error format.
error.message //=> 'a.css:1:1: Unclosed block'Contains only the error description.
error.reason //=> 'Unclosed block'Contains the PostCSS plugin name if the error didn’t come from the CSS parser.
error.plugin //=> 'postcss-vars'PostCSS will fill it automatically.
Contains the absolute path to the broken file. If you use it, send the from
option to the parser.
error.file //=> 'a.sass'PostCSS will use the input source map to detect the original error location. If you wrote a Sass file, compiled it to CSS and then parsed it with PostCSS, PostCSS will show the original position in the Sass file.
If you need the position in the PostCSS input (e.g., to debug the previous
compiler), use error.input.file.
error.file //=> 'a.sass'
error.input.file //=> 'a.css'Contains the source line of the error.
error.line //=> 2PostCSS will use the input source map to detect the original error location. If you wrote a Sass file, compiled it to CSS and then parsed it with PostCSS, PostCSS will show the original position in the Sass file.
If you need the position in the PostCSS input (e.g., to debug the previous
compiler), use error.input.file.
error.line //=> 2
error.input.line //=> 4Contains the source column of the error.
error.column //=> 1PostCSS will use the input source map to detect the original error location. If you wrote a Sass file, compiled it to CSS and then parsed it with PostCSS, PostCSS will show the original position in the Sass file.
If you need the position in the PostCSS input (e.g., to debug the previous
compiler), use error.input.file.
error.column //=> 1
error.input.column //=> 4Contains the source code of the broken file.
error.source //=> 'a {} b {'PostCSS will use the input source map to detect the original error location. If you wrote a Sass file, compiled it to CSS and then parsed it with PostCSS, PostCSS will show the original position in the Sass file.
If you need the position in the PostCSS input (e.g., to debug the previous
compiler), use error.input.file.
error.source //=> 'a { b {} }'
error.input.column //=> 'a b { }'Contains helpers for working with vendor prefixes.
var vendor = postcss.vendor;Returns the vendor prefix extracted from an input string.
postcss.vendor.prefix('-moz-tab-size') //=> '-moz-'Returns the input string stripped of its vendor prefix.
postcss.vendor.unprefixed('-moz-tab-size') //=> 'tab-size'Contains helpers for safely splitting lists of CSS values, preserving parentheses and quotes.
var list = postcss.list;Safely splits space-separated values (such as those for background,
border-radius, and other shorthand properties).
postcss.list.space('1px calc(10% + 1px)') //=> ['1px', 'calc(10% + 1px)']Safely splits comma-separated values (such as those
for transition-* and background properties).
postcss.list.comma('black, linear-gradient(white, black)')
//=> ['black', 'linear-gradient(white, black)']Represents the source CSS.
var root = postcss.parse(css, { from: file });
var input = root.source.input;The absolute path to the CSS source file defined with the from option.
var root = postcss.parse(css, { from: 'a.css' });
root.source.input.file //=> '/home/ai/a.css'The unique ID of the CSS source. Used if from
option is not provided (because PostCSS does not know the file path).
var root = postcss.parse(css);
root.source.input.file //=> undefined
root.source.input.id //=> <input css 1>The CSS source identifier. Contains input.file if the user set
the from option, or input.id if they did not.
var root = postcss.parse(css, { from: 'a.css' });
root.source.input.from //=> '/home/ai/a.css'
var root = postcss.parse(css);
root.source.input.from //=> <input css 1>Represents the input source map passed from a compilation step before PostCSS (e.g., from the Sass compiler).
map.consumer() returns an instance of the SourceMapConsumer class
from the source-map library.
root.source.input.map.consumer().sources //=> ['a.sass']Reads the input source map and returns a symbol position in the input source (e.g., in a Sass file that was compiled to CSS before being passed to PostCSS):
root.source.input.origin(1, 1) //=> { file: 'a.css', line: 3, column: 1 }All node classes inherit the following common methods.
Returns a string representing the node’s type.
Possible values are root, atrule, rule, decl, or comment.
postcss.decl({ prop: 'color', value: 'black' }).type //=> 'decl'Returns the node’s parent node.
root.nodes[0].parent == root;Returns the input source of the node, with the following properties:
node.source.input: AnInputinstance.node.source.start: The starting position of the node’s source — line and column.node.source.end: The ending position of the node’s source — line and column.
decl.source.input.from //=> '/home/ai/a.sass'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }The property is used in source map generation.
If you create a node manually (e.g., with postcss.decl()),
that node will not have a source property and will be absent
from the source map. For this reason, the plugin developer should consider
cloning nodes to create new ones (in which case the new node’s source
will reference the original, cloned node) or setting the source property
manually.
// Bad
var prefixed = postcss.decl({ prop: '-moz-' + decl.prop, value: decl.value });
// Good
var prefixed = decl.clone({ prop: '-moz-' + decl.prop });if ( atrule.name == 'add-link' ) {
var rule = postcss.rule({ selector: 'a' }); // Rule has no source
atrule.parent.insertBefore(atrule, rule); // We add it because of atrule
rule.source = atrule.source; // So we copy source from atrule
}Contains 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: containstrueif the last child has an (optional) semicolon.afterName: the space between the at-rule’s name and its parameters.left: the space symbols between/*and the comment’s text.right: the space symbols between the comment’s text and*/.important: the content of the important statement, if it is not just!important.
PostCSS cleans selectors, declaration values and at-rule parameters
from comments and extra spaces, but it stores origin content
in raws properties. As such, if you don’t change a declaration’s value,
PostCSS will use the raw value with comments.
Returns a CSS string representing the node.
postcss.rule({ selector: 'a' }).toString() //=> 'a {}''Arguments:
stringifier (functions|object) optional: a syntax to use in string generation.
Returns a CssSyntaxError instance containing the original position
of the node in the source, showing line and column numbers and also
a small excerpt to facilitate debugging.
If present, an input source map will be used to get the original position of the source, even from a previous compilation step (e.g., from Sass compilation).
This method produces very useful error messages.
if ( !variables[name] ) {
throw decl.error('Unknown variable ' + name, { word: name });
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// a
// color: $black
// ^
// background: white
}Arguments:
message (string): error description.opts (object) optional: options.plugin (string): plugin name that created this error. PostCSS will set it automatically.word (string): a word inside a node’s string that should be highlighted as the source of the error.index(number): an index inside a node’s string that should be highlighted as the source of the error.
This method is provided as a convenience wrapper for Result#warn().
var plugin = postcss.plugin('postcss-deprecated', function () {
return function (css, result) {
css.walkDecls('bad', function (decl) {
decl.warn(result, 'Deprecated property bad');
});
};
});Arguments:
result: TheResultinstance that will receive the warning.text (string): warning message. It will be used in thetextproperty of the message object.opts (object) optional: properties to assign to the message object.word (string): word inside a node’s string that should be highlighted as the source of the warning.index(number): index inside a node’s string that should be highlighted as the source of the warning.plugin: name of the plugin that created this warning.Result#warn()will automatically fill it with theplugin.postcssPluginvalue.
Note that opts.node is automatically passed to Result#warn() for you.
Returns the next/previous child of the node’s parent.
Returns undefined if the current node is the last/first child.
var annotation = decl.prev();
if ( annotation.type == 'comment' ) {
readAnnotation( annotation.text );
}Returns the Root instance of the node’s tree.
root.nodes[0].nodes[0].root() == rootRemoves the node from its parent and cleans the parent properties from the
node and its children.
if ( decl.prop.match(/^-webkit-/) ) {
decl.remove();
}Inserts node(s) before the current node and removes the current node.
if ( atrule.name == 'mixin' ) {
atrule.replaceWith(mixinRules[atrule.params]);
}Returns a clone of the node.
The resulting cloned node and its (cloned) children will have a clean parent
and code style properties.
var cloned = decl.clone({ prop: '-moz-' + decl.prop });
cloned.raws.before //=> undefined
cloned.parent //=> undefined
cloned.toString() //=> -moz-transform: scale(0)Arguments:
props (object) optional: new properties to override in the clone.
Shortcut to clone the node and insert the resulting cloned node before/after the current node.
decl.cloneBefore({ prop: '-moz-' + decl.prop });Arguments:
props (object) optional: new properties to override in the clone.
Removes the node from its current parent and inserts it
at the end of newParent.
This will clean the before and after code style properties from the node
and replace them with the indentation style of newParent. It will also clean
the between property if newParent is in another Root.
atrule.moveTo(atrule.parent.parent);Arguments:
newParent: (Container): container node where the current node will be moved.
Removes the node from its current parent and inserts it into a new parent
before/after otherNode.
This will also clean the node’s code style properties just as it would in
node.moveTo(newParent).
Arguments:
otherNode (Node): node that will be after/before current node after moving.
Returns a code style property value. If the node is missing the code style property (because the node was manually built or cloned), PostCSS will try to autodetect the code style property by looking at other nodes in the tree.
var root = postcss.parse('a { background: white }');
root.nodes[0].append({ prop: 'color', value: 'black' });
root.nodes[0].nodes[1].raws.before //=> ' 'Arguments:
prop (string): name or code style property.defaultType (string): name of default value. It can be easily missed if the value is the same asprop.
The Root, AtRule, and Rule container nodes inherit some common methods
to help work with their children.
Note that all containers can store any content. If you write a rule inside a rule, PostCSS will parse it.
An array containing the container’s children.
var root = postcss.parse('a { color: black }');
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'The container’s first child.
rule.first == rules.nodes[0];The container’s last child.
rule.last == rule.nodes[rule.nodes.length - 1];Returns a child’s index within the container’s nodes array.
rule.index( rule.nodes[2] ) //=> 2Arguments:
child (Node): child of the current container.
Returns true if callback returns true for all of the container’s children.
var noPrefixes = rule.every(function (decl) {
return decl.prop[0] != '-';
});Arguments:
callback (function): iterator. Returns true or false.
Returns true if callback returns true for (at least) one
of the container’s children.
var hasPrefix = rule.some(function (decl) {
return decl.prop[0] == '-';
});Arguments:
callback (function): iterator. Returns true or false.
Iterates through the container’s immediate children, calling callback
for each child.
Returning false in the callback will break iteration.
var color;
rule.each(function (decl) {
if ( decl.prop == 'color' ) {
color = decl.value;
return false;
}
});Arguments:
callback (function): iterator. Receives each node and its index.
Unlike the for {}-cycle or Array#forEach() this iterator is safe
if you are mutating the array of child nodes during iteration.
PostCSS will adjust the current index to match the mutations.
var root = postcss.parse('a { color: black; z-index: 1 }');
var rule = root.first;
for ( var i = 0; i < rule.nodes.length; i++ ) {
var decl = rule.nodes[i];
decl.cloneBefore({ prop: '-webkit-' + decl.prop });
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(function (decl) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop });
// Will be executed only for color and z-index
});container.each() only iterates through the container’s immediate children.
If you need to recursively iterate through all the container’s descendant nodes,
use container.walk().
Traverses the container’s descendant nodes, calling callback for each node.
root.walk(function (node) {
// Traverses all descendant nodes.
});Arguments:
callback (function): iterator. Receives each node and its index.
Like container.each(), this method is safe to use
if you are mutating arrays during iteration.
If you only need to iterate through the container’s immediate children,
use container.each().
Traverses the container’s descendant nodes, calling callback for each
declaration node.
root.walkDecls(function (decl) {
if ( decl.prop.match(/^-webkit-/) ) {
decl.remove();
}
});Arguments:
propFilter: (string|RegExp) optional: string or regular expression to filter declarations by property name.callback (function): iterator. Receives each declaration node and its index.
If you pass a propFilter, iteration will only happen over declarations with
matching properties.
// Make a flat design
root.walkDecls('border-radius', function (decl) {
decl.remove();
});
root.walkDecls(/^background/, function (decl) {
decl.value = takeFirstColorFromGradient(decl.value);
});Like container.each(), this method is safe to use if you are mutating
arrays during iteration.
Traverses the container’s descendant nodes, calling callback for each
at-rule node.
root.walkAtRules(function (rule) {
if ( rule.name.match(/^-webkit-/) ) rule.remove();
});Arguments:
nameFilter: (string|RegExp) optional: string or regular expression to filter at-rules by name.callback (function): iterator. Receives each at-rule and its index.
If you pass a filter, iteration will only happen over at-rules that have
matching names.
var first = false;
root.walkAtRules('charset', function (rule) {
if ( !first ) {
first = true;
} else {
rule.remove();
}
});Like container.each(), this method is safe to use if you are mutating arrays
during iteration.
Traverses the container’s descendant nodes, calling callback for each
rule node.
var selectors = [];
root.walkRules(function (rule) {
selectors.push(rule.selector);
});
console.log('Your CSS uses ' + selectors.length + ' selectors');Arguments:
selectorFilter: (string|RegExp) optional: string or regular expression to filter rules by selector.callback (function): iterator. Receives each rule node and its index.
If you pass a selectorFilter, iteration will only happen over rules with
matching selectors.
Like container.each(), this method is safe to use if you are mutating arrays
during iteration.
Traverses the container’s descendant nodes, calling callback for each
comment node.
root.walkComments(function (comment) {
comment.remove();
});Arguments:
callback (function): iterator. Receives each comment node and its index.
Like container.each(), this method is safe to use if you are mutating arrays
during iteration.
Passes all declaration values within the container that match pattern through
callback, replacing those values with the returned result of callback.
This method is useful if you are using a custom unit or function and need to iterate through all values.
root.replaceValues(/\d+rem/, { fast: 'rem' }, function (string) {
return 15 * parseInt(string) + 'px';
});Arguments:
pattern (string|RegExp): replace pattern.opts (object) optional: options to speed up the search:props: An array of property names. The method will only search for values that matchregexpwithin declarations of listed properties.fast: A string that’s used to narrow down values and speed up the regexp search. Searching every single value with a regexp can be slow. If you pass afaststring, PostCSS will first check whether the value contains thefaststring; and only if it does will PostCSS check that value againstregexp. For example, instead of just checking for/\d+rem/on all values, setfast: 'rem'to first check whether a value has theremunit, and only if it does perform the regexp check.
callback (function|string): string to replacepatternor callback that returns a new value. The callback will receive the same arguments as those passed to a function parameter ofString#replace.
Inserts new nodes to the start/end of the container.
var decl = postcss.decl({ prop: 'color', value: 'black' });
rule.append(decl);var decl1 = postcss.decl({ prop: 'color', value: 'black' });
var decl2 = postcss.decl({ prop: 'background-color', value: 'white' });
rule.prepend(decl1, decl2);Arguments:
node (Node|array|object|string): new node.
Because each node class is identifiable by unique properties, use the following shortcuts to create nodes in insert methods:
root.append({ name: 'charset', params: '"UTF-8"' }); // at-rule
root.append({ selector: 'a' }); // rule
rule.append({ prop: 'color', value: 'black' }); // declaration
rule.append({ text: 'Comment' }) // commentA string containing the CSS of the new element can also be used. This approach is slower than the above shortcuts.
root.append('a {}');
root.first.append('color: black; z-index: 1');Insert newNode before/after oldNode within the container.
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }));Arguments:
oldNode (Node|number): child or child’s index.node (Node|array|object|string): new node.
Removes node from the container and cleans the parent properties from the
node and its children.
rule.nodes.length //=> 5
rule.removeChild(decl);
rule.nodes.length //=> 4
decl.parent //=> undefinedArguments:
node (Node|number): child or child’s index.
Removes all children from the container and cleans their parent properties.
rule.removeAll();
rule.nodes.length //=> 0Represents a CSS file and contains all its parsed nodes.
var root = postcss.parse('a{color:black} b{z-index:2}');
root.type //=> 'root'
root.nodes.length //=> 2Returns a Result instance representing the root’s CSS.
var root1 = postcss.parse(css1, { from: 'a.css' });
var root2 = postcss.parse(css2, { from: 'b.css' });
root1.append(root2);
var result = root1.toResult({ to: 'all.css', map: true });Arguments:
opts (object) optional: options:to: the path where you’ll put the output CSS file. You should always settoto generate correct source maps.map: an object of source map options.
Represents an at-rule.
If it’s followed in the CSS by a {} block, this node will have a nodes
property representing its children.
var root = postcss.parse('@charset "UTF-8"; @media print {}');
var charset = root.first;
charset.type //=> 'atrule'
charset.nodes //=> undefined
var media = root.last;
media.nodes //=> []The at-rule’s name. This is the identifier that immediately follows the @.
var root = postcss.parse('@media print {}');
var media = root.first;
media.name //=> 'media'The at-rule’s parameters. These are the values that follow the at-rule’s name
but precede any {} block. The spec refers to this area
as the at-rule’s “prelude”.
var root = postcss.parse('@media print, screen {}');
var media = root.first;
media.params //=> 'print, screen'Represents a CSS rule: a selector followed by a declaration block.
var root = postcss.parse('a{}');
var rule = root.first;
rule.type //=> 'rule'
rule.toString() //=> 'a{}'The rule’s full selector represented as a string. If there are multiple comma-separated selectors, the entire group will be included.
var root = postcss.parse('a, b { }');
var rule = root.first;
rule.selector //=> 'a, b'An array containing the rule’s individual selectors. Groups of selectors are split at commas.
var root = postcss.parse('a, b { }');
var rule = root.first;
rule.selector //=> 'a, b'
rule.selectors //=> ['a', 'b']
rule.selectors = ['a', 'strong'];
rule.selector //=> 'a, strong'Represents a CSS declaration.
var root = postcss.parse('a { color: black }');
var decl = root.first.first;
decl.type //=> 'decl'
decl.toString() //=> ' color: black'The declaration’s property name.
var root = postcss.parse('a { color: black }');
var decl = root.first.first;
decl.prop //=> 'color'The declaration’s value.
var root = postcss.parse('a { color: black }');
var decl = root.first.first;
decl.value //=> 'black'true if the declaration has an !important annotation.
var root = postcss.parse('a { color: black !important; color: white }');
root.first.first.important //=> true
root.first.last.important //=> undefinedRepresents a comment between declarations or statements (rule and at-rules).
Comments inside selectors, at-rule parameters, or declaration values
will be stored in the Node#raws properties explained above.
var root = postcss.parse('a { color: /* inner */ black; /* outer */ }');
var decl = root.first.first;
var comment = root.first.last;
comment.type //=> 'comment'
decl.between //=> ': /* inner */'The comment’s text.
var root = postcss.parse('/* Empty file */');
var comment = root.first;
var comment.text //=> 'Empty file'