PostCSS Inherit Parser

0.2.0 · abandoned · verified Tue Apr 21

postcss-inherit-parser is a specialized parser for PostCSS, designed to interpret a custom CSS syntax that facilitates "inherit" declarations, specifically in conjunction with the `postcss-inherit` plugin. This parser enables the processing of CSS where rules can leverage inheritance from other selectors, including those with pseudo-classes, as exemplified by syntax like `.a { inherit: .b:before; }`. The package, currently at version 0.2.0, was last published approximately 8 years ago, suggesting it is no longer actively maintained. Its core functionality is to provide the Abstract Syntax Tree (AST) representation for this non-standard inheritance syntax, allowing subsequent PostCSS plugins to apply transformation logic. A key feature is the configurable `propertyRegExp` option, which allows consumers to define custom keywords for inheritance, such as `extend` instead of `inherit`. Given its age and low version, developers should be aware of potential compatibility issues with newer Node.js or PostCSS versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `postcss-inherit-parser` to parse CSS containing custom `inherit` or `extend` declarations, and then how to process it with the `postcss-inherit` plugin to apply the inheritance logic, including customizing the property name regex.

const postcss = require('postcss');
const inheritParser = require('postcss-inherit-parser');

async function processCssWithInherit() {
  const cssInput = `
    .b:before {
      content: "";
      color: blue;
    }
    .a {
      inherit: .b:before;
      font-size: 16px;
    }
    .c {
      extend: .a;
      border: 1px solid black;
    }
  `;

  // Use the parser with custom propertyRegExp
  const root = await postcss().process(cssInput, {
    from: undefined,
    parser: inheritParser.parse,
    parserOptions: { propertyRegExp: /^(inherit|extend)s?$/i }
  }).root;

  console.log('Parsed AST (first rule selector):', root.nodes[0].selector); // .b:before
  console.log('Parsed AST (second rule inherit property):', root.nodes[1].nodes[0].prop); // inherit
  console.log('Parsed AST (third rule extend property):', root.nodes[2].nodes[0].prop); // extend

  // To apply the actual inheritance, you would typically use postcss-inherit as a plugin:
  const postcssInherit = require('postcss-inherit');
  const result = await postcss([postcssInherit()]).process(cssInput, {
    from: undefined,
    parser: inheritParser.parse,
    parserOptions: { propertyRegExp: /^(inherit|extend)s?$/i }
  });
  console.log('\nCSS after processing with postcss-inherit:\n', result.css);
}

processCssWithInherit();

view raw JSON →