# css [![Build Status](https://travis-ci.org/reworkcss/css.svg?branch=master)](https://travis-ci.org/reworkcss/css)
CSS parser / stringifier.
## Installation
$ npm install css
## Usage
```js
var css = require('css');
var obj = css.parse('body { font-size: 12px; }', options);
css.stringify(obj, options);
```
## API
### css.parse(code, [options])
Accepts a CSS string and returns an AST `object`.
`options`:
- silent: silently fail on parse errors.
- source: the path to the file containing `css`. Makes errors and source
maps more helpful, by letting them know where code comes from.
### Example
```js
var ast = css.parse('body { font-size: 12px; }', { source: 'source.css' });
```
### Errors
Errors will have `error.position`, just like [`node.position`](#position). The
error contains the source position in the message. To get the error message
without the position use `error.reason`.
If you create any errors in plugins such as in
[rework](https://github.com/reworkcss/rework), you __must__ set the `position`
as well for consistency.
## AST
### Common properties
All nodes have the following properties.
#### position
Information about the position in the source string that corresponds to
the node.
`Object`:
- start: `Object`:
- line: `Number`.
- column: `Number`.
- end: `Object`:
- line: `Number`.
- column: `Number`.
- source: `String` or `undefined`. The value of `options.source` if passed to
`css.parse`. Otherwise `undefined`.
- content: `String`. The full source string passed to `css.parse`.
The line and column numbers are 1-based: The first line is 1 and the first
column of a line is 1 (not 0).
The `position` property lets you know from which source file the node comes
from (if available), what that file contains, and what part of that file was
parsed into the node.
#### type
`String`. The possible values are the ones listed in the Types section below.
#### parent
A reference to the parent node, or `null` if the node has no parent.
### Types
The available values of `node.type` are listed below, as well as the available
properties of each node (other than the common properties listed above.)
#### stylesheet
The root node returned by `css.parse`.
- stylesheet: `Object`:
- rules: `Array` of nodes with the types `rule`, `comment` and any of the
at-rule types.
#### rule
- selectors: `Array` of `String`s. The list of selectors of the rule, split
on commas. Each selector is trimmed from whitespace and comments.
- declarations: `Array` of nodes with the types `declaration` and `comment`.
#### declaration
- property: `String`. The property name, trimmed from whitespace and
comments. May not be empty.
- value: `String`. The value of the property, trimmed from whitespace and
comments. Empty values are allowed.
#### comment
A rule-level or declaration-level comment. Comments inside selectors,
properties and values etc. are lost.
- comment: `String`. The part between the starting `/*` and the ending `*/`
of the comment, including whitespace.
#### charset
The `@charset` at-rule.
- charset: `String`. The part following `@charset `.
#### custom-media
The `@custom-media` at-rule.
- name: `String`. The `--`-prefixed name.
- media: `String`. The part following the name.
#### document
The `@document` at-rule.
- document: `String`. The part following `@document `.
- vendor: `String` or `undefined`. The vendor prefix in `@document`, or
`undefined` if there is none.
- rules: `Array` of nodes with the types `rule`, `comment` and any of the
at-rule types.
#### font-face
The `@font-face` at-rule.
- declarations: `Array` of nodes with the types `declaration` and `comment`.
#### host
The `@host` at-rule.
- rules: `Array` of nodes with the types `rule`, `comment` and any of the
at-rule types.
#### import
The `@import` at-rule.
- import: `String`. The part following `@import `.
#### keyframes
The `@keyframes` at-rule.
- name: `String`. The name of the keyframes rule.
- vendor: `String` or `undefined`. The vendor prefix in `@keyframes`, or
`undefined` if there is none.
- keyframes: `Array` of nodes with the types `keyframe` and `comment`.
#### keyframe
- values: `Array` of `String`s. The list of “selectors” of the keyframe rule,
split on commas. Each “selector” is trimmed from whitespace.
- declarations: `Array` of nodes with the types `declaration` and `comment`.
#### media
The `@media` at-rule.
- media: `String`. The part following `@media `.
- rules: `Array` of nodes with the types `rule`, `comment` and any of the
at-rule types.
#### namespace
The `@namespace` at-rule.
- namespace: `String`. The part following `@namespace `.
#### page
The `@page` at-rule.
- selectors: `Array` of `String`s. The list of selectors of the rule, split
on commas. Each selector is trimmed from whitespace and comments.
- declarations: `Array` of nodes with the types `declaration` and `comment`.
#### supports
The `@supports` at-rule.
- supports: `String`. The part following `@supports `.
- rules: `Array` of nodes with the types `rule`, `comment` and any of the
at-rule types.
### Example
CSS:
```css
body {
background: #eee;
color: #888;
}
```
Parse tree:
```json
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "rule",
"selectors": [
"body"
],
"declarations": [
{
"type": "declaration",
"property": "background",
"value": "#eee",
"position": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 19
}
}
},
{
"type": "declaration",
"property": "color",
"value": "#888",
"position": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 14
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 4,
"column": 2
}
}
}
]
}
}
```
## License
MIT
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
NativeScript跨平台移动端开发工具 v7.2.1.zip (2000个子文件)
widgets-release.aar 211KB
android-wait-for-emulator 791B
LICENSE.BSD 1KB
generate-android-snapshot.cmd 41B
install-ns-webpack.cmd 34B
ns-webpack-update.cmd 33B
remove-ns-webpack.cmd 33B
ns-verify-bundle.cmd 32B
ns-bundle.cmd 25B
CodeResources 4KB
core.light.css 13KB
app-platform.android.css 2KB
button-border-page.css 1KB
gradient-border-page.css 1KB
label-border-page.css 1KB
layout-border-page.css 1KB
image-border-page.css 1KB
text-view-border-page.css 1KB
text-field-border-page.css 1KB
app.css 1KB
app-platform.ios.css 1KB
margins-paddings-with-percentage-page.css 1KB
non-uniform-radius-page.css 1KB
rounded-images-page.css 900B
row-height.css 687B
app.css 681B
combinators-page.css 679B
scrolling-and-sizing-page.css 645B
scrolling-and-sizing.css 643B
scrolling-and-sizing-page.css 643B
fancy-fonts-page.css 630B
elevation-page.css 614B
missing-background-image-page.css 582B
issue-ng-repo-1599-page.css 494B
flexbox-page.css 487B
font-icons-page.css 476B
font-icons-page.css 476B
custom-tabstrip-page.css 456B
custom-tabstrip-page.css 456B
dynamic-color-change-page.css 454B
dynamic-color-change-page.css 454B
measure-tests-page.css 402B
text-transform-page.css 395B
font-page.css 392B
text-transform-page.css 383B
font-page.css 380B
layouts-border-overlap-page.css 376B
issue-2911-page.css 347B
flexbox-demo-page.css 327B
color-page.css 314B
color-page.css 302B
item-re-layout.css 293B
what-is-new.ios.css 286B
background-color-page.css 263B
issue-3113-page.css 256B
all-non-uniform-border-page.css 252B
background-color-page.css 251B
issue-3007-page.css 233B
item-color-page.css 217B
label-background-image-page.css 201B
flexbox-4834-page.css 190B
main-page-common.css 184B
tab-view-css-page.css 181B
flexbox-4143-page.css 176B
styled-formatted-text-page.css 176B
issue-3654-page.css 171B
test-page.css 168B
issue-4147-page.css 155B
border-playground-page.css 142B
mode-matrix-page.css 141B
page-one.css 133B
all-uniform-border-page.css 123B
page-two.css 119B
transform-decoration-color-page.css 117B
main-page.android.css 114B
font-icons-page.css 114B
font-icons-page.css 114B
text-transform-page.css 113B
bottom-navigation-css-page.css 113B
flexbox-css-page.css 113B
background-page.css 113B
status-bar-css-page.css 113B
background-image-page.css 110B
background-image-page.css 106B
custom-css-file-page.css 92B
list-view-page.css 86B
item-color-page.css 84B
issue-5274-page.css 84B
styles-page.css 82B
transparent-bg-css-page.css 82B
issue-6439-page.css 80B
main-page.ios.css 79B
highlight-4740-page.css 76B
label-tests-wrong-page.css 66B
MyControl.css 63B
issue-4385-page.css 62B
issue-2661-second-page.css 61B
issue-2661-page.css 59B
issue-3211-page.css 58B
customView3-page.css 58B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
芝麻粒儿
- 粉丝: 5w+
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功