You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
6.0 KiB

1 month ago
  1. <img align="right" width="111" height="111"
  2. alt="CSSTree logo"
  3. src="https://cloud.githubusercontent.com/assets/270491/19243723/6f9136c6-8f21-11e6-82ac-eeeee4c6c452.png"/>
  4. # CSSTree
  5. [![NPM version](https://img.shields.io/npm/v/css-tree.svg)](https://www.npmjs.com/package/css-tree)
  6. [![Build Status](https://travis-ci.org/csstree/csstree.svg?branch=master)](https://travis-ci.org/csstree/csstree)
  7. [![Coverage Status](https://coveralls.io/repos/github/csstree/csstree/badge.svg?branch=master)](https://coveralls.io/github/csstree/csstree?branch=master)
  8. [![NPM Downloads](https://img.shields.io/npm/dm/css-tree.svg)](https://www.npmjs.com/package/css-tree)
  9. [![Twitter](https://img.shields.io/badge/Twitter-@csstree-blue.svg)](https://twitter.com/csstree)
  10. CSSTree is a tool set for CSS: [fast](https://github.com/postcss/benchmark) detailed parser (CSS → AST), walker (AST traversal), generator (AST → CSS) and lexer (validation and matching) based on specs and browser implementations. The main goal is to be efficient and W3C specs compliant, with focus on CSS analyzing and source-to-source transforming tasks.
  11. > NOTE: The library isn't in final shape and needs further improvements (e.g. AST format and API are subjects to change in next major versions). However it's stable enough and used by projects like [CSSO](https://github.com/css/csso) (CSS minifier) and [SVGO](https://github.com/svg/svgo) (SVG optimizer) in production.
  12. ## Features
  13. - **Detailed parsing with an adjustable level of detail**
  14. By default CSSTree parses CSS as detailed as possible, i.e. each single logical part is representing with its own AST node (see [AST format](docs/ast.md) for all possible node types). The parsing detail level can be changed through [parser options](docs/parsing.md#parsesource-options), for example, you can disable parsing of selectors or declaration values for component parts.
  15. - **Tolerant to errors by design**
  16. Parser behaves as [spec says](https://www.w3.org/TR/css-syntax-3/#error-handling): "When errors occur in CSS, the parser attempts to recover gracefully, throwing away only the minimum amount of content before returning to parsing as normal". The only thing the parser departs from the specification is that it doesn't throw away bad content, but wraps it in a special node type (`Raw`) that allows processing it later.
  17. - **Fast and efficient**
  18. CSSTree is created with focus on performance and effective memory consumption. Therefore it's [one of the fastest CSS parsers](https://github.com/postcss/benchmark) at the moment.
  19. - **Syntax validation**
  20. The build-in lexer can test CSS against syntaxes defined by W3C. CSSTree uses [mdn/data](https://github.com/mdn/data/) as a basis for lexer's dictionaries and extends it with vendor specific and legacy syntaxes. Lexer can only check the declaration values currently, but this feature will be extended to other parts of the CSS in the future.
  21. ## Documentation
  22. - [AST format](docs/ast.md)
  23. - [Parsing CSS → AST](docs/parsing.md)
  24. - [parse(source[, options])](docs/parsing.md#parsesource-options)
  25. - [Serialization AST → CSS](docs/generate.md)
  26. - [generate(ast[, options])](docs/generate.md#generateast-options)
  27. - [AST traversal](docs/traversal.md)
  28. - [walk(ast, options)](docs/traversal.md#walkast-options)
  29. - [find(ast, fn)](docs/traversal.md#findast-fn)
  30. - [findLast(ast, fn)](docs/traversal.md#findlastast-fn)
  31. - [findAll(ast, fn)](docs/traversal.md#findallast-fn)
  32. - [Utils for AST](docs/utils.md)
  33. - [property(name)](docs/utils.md#propertyname)
  34. - [keyword(name)](docs/utils.md#keywordname)
  35. - [clone(ast)](docs/utils.md#cloneast)
  36. - [fromPlainObject(object)](docs/utils.md#fromplainobjectobject)
  37. - [toPlainObject(ast)](docs/utils.md#toplainobjectast)
  38. - [Value Definition Syntax](docs/definition-syntax.md)
  39. - [parse(source)](docs/definition-syntax.md#parsesource)
  40. - [walk(node, options, context)](docs/definition-syntax.md#walknode-options-context)
  41. - [generate(node, options)](docs/definition-syntax.md#generatenode-options)
  42. - [AST format](docs/definition-syntax.md#ast-format)
  43. ## Tools
  44. * [AST Explorer](https://astexplorer.net/#/gist/244e2fb4da940df52bf0f4b94277db44/e79aff44611020b22cfd9708f3a99ce09b7d67a8) – explore CSSTree AST format with zero setup
  45. * [CSS syntax reference](https://csstree.github.io/docs/syntax.html)
  46. * [CSS syntax validator](https://csstree.github.io/docs/validator.html)
  47. ## Related projects
  48. * [csstree-validator](https://github.com/csstree/validator) – NPM package to validate CSS
  49. * [stylelint-csstree-validator](https://github.com/csstree/stylelint-validator) – plugin for stylelint to validate CSS
  50. * [Grunt plugin](https://github.com/sergejmueller/grunt-csstree-validator)
  51. * [Gulp plugin](https://github.com/csstree/gulp-csstree)
  52. * [Sublime plugin](https://github.com/csstree/SublimeLinter-contrib-csstree)
  53. * [VS Code plugin](https://github.com/csstree/vscode-plugin)
  54. * [Atom plugin](https://github.com/csstree/atom-plugin)
  55. ## Usage
  56. Install with npm:
  57. ```
  58. > npm install css-tree
  59. ```
  60. Basic usage:
  61. ```js
  62. var csstree = require('css-tree');
  63. // parse CSS to AST
  64. var ast = csstree.parse('.example { world: "!" }');
  65. // traverse AST and modify it
  66. csstree.walk(ast, function(node) {
  67. if (node.type === 'ClassSelector' && node.name === 'example') {
  68. node.name = 'hello';
  69. }
  70. });
  71. // generate CSS from AST
  72. console.log(csstree.generate(ast));
  73. // .hello{world:"!"}
  74. ```
  75. Syntax matching:
  76. ```js
  77. // parse CSS to AST as a declaration value
  78. var ast = csstree.parse('red 1px solid', { context: 'value' });
  79. // match to syntax of `border` property
  80. var matchResult = csstree.lexer.matchProperty('border', ast);
  81. // check first value node is a <color>
  82. console.log(matchResult.isType(ast.children.first(), 'color'));
  83. // true
  84. // get a type list matched to a node
  85. console.log(matchResult.getTrace(ast.children.first()));
  86. // [ { type: 'Property', name: 'border' },
  87. // { type: 'Type', name: 'color' },
  88. // { type: 'Type', name: 'named-color' },
  89. // { type: 'Keyword', name: 'red' } ]
  90. ```
  91. ## Top level API
  92. ![API map](https://cdn.rawgit.com/csstree/csstree/1.0/docs/api-map.svg)
  93. ## License
  94. MIT