- PowCSS
PowCSS 负责解析 source 为节点树, 并拼接编译器的编译结果. 在 PowCSS 中的插件就是 compiler, compiler 负责与 context 配合.
- Compiler
PowCSS 缺省的 Compiler 实现. 所有方法采用原生 js 语法, 要求源码自带嵌套占位符 '...'.
- Context
PowCSS 缺省的 Context 实现. 该实现不分析 CSS 规则的合法性, 只提供结构上的操作和一些辅助方法. 如果要对结果进行再次处理, 推荐使用 walk 方法.
- lineify
lineify 是个非空白行扫描器, 扫描并返回非空白行信息.
- util
辅助方法集合
- Rule :
object 抽象规则
PowCSS 负责解析 source 为节点树, 并拼接编译器的编译结果. 在 PowCSS 中的插件就是 compiler, compiler 负责与 context 配合.
Kind: global class
- PowCSS
- new PowCSS(plugins)
- .use(plugin) ⇒
this - .process(source) ⇒
this - .parse(source) ⇒
object - .format(root) ⇒
string - .compile() ⇒
string - .build(params) ⇒
object - .run(source, params, args) ⇒
object - .walk(nodes, context, callback) ⇒
this
| Param | Type | Description |
|---|---|---|
| plugins | Array.<Compiler> |
编译器数组, 缺省为 [compiler()] |
使用一个编译器插件
Kind: instance method of PowCSS
| Param | Type |
|---|---|
| plugin | Compiler |
包装 this.result = this.parse(source) 并返回 this.
Kind: instance method of PowCSS
| Param | Type | Description |
|---|---|---|
| source | string |
源码 |
解析 source 为节点树. 节点类型:
- root {mode:'root', nodes}
- comment {mode:'comment', source} 只保留非行尾注释
- decl {mode:'decl', source, key, val}
- pending {mode:'', source, nodes}
即所有 !mode 的节点需要通过编译插件进行确认
Kind: instance method of PowCSS
Returns: object - root 节点树
| Param | Type | Description |
|---|---|---|
| source | string |
源码 |
格式化输出 root.nodes
Kind: instance method of PowCSS
Returns: string - CSS 无花括号两空格缩进格式
| Param | Type | Description |
|---|---|---|
| root | object |
解析后的节点树 |
遍历 this.result 所有节点拼接编译插件的编译结果. 未被编译的节点和其子节点被抛弃.
Kind: instance method of PowCSS
Returns: string - body 编译后的函数主体代码;
返回 Function(params, this.compile(this.result + ';return '+ params.split(',')[0]))
Kind: instance method of PowCSS
Returns: object - ctx
| Param | Type | Description |
|---|---|---|
| params | string |
形参, 缺省为 'ctx' |
包装 process, build 并返回执行结果
Kind: instance method of PowCSS
| Param | Type | Description |
|---|---|---|
| source | string |
CSS 源码 |
| params | string |
形参, 缺省为 'ctx' |
| args | array |
实参, 缺省为 [context()] |
使用 nodes.forEach 深度遍历节点树并调用 callback(item, context, index, nodes). 如果 callback 返回非真值, item.nodes 将不参与遍历.
Kind: instance method of PowCSS
| Param | Type | Description |
|---|---|---|
| nodes | array |
|
| context | Object |
上下文 |
| callback | function |
回调函数 |
PowCSS 缺省的 Compiler 实现. 所有方法采用原生 js 语法, 要求源码自带嵌套占位符 '...'.
Kind: global class
构造, 参数用来对 this 进行扩展. 缺省 this.ctx = 'ctx' 表示 context 的形参名
compile 接口
Kind: instance method of Compiler
编译 n.mode === 'comment' 的节点, '/*!' 开头的顶层注释被保留, 其它被抛弃.
Kind: instance method of Compiler
编译 !n.mode 的节点为规则节点
Kind: instance method of Compiler
编译 n.mode === 'decl' 的节点
Kind: instance method of Compiler
if 语句, 原生语法: if(expr) code; if (expr) code;
Kind: instance method of Compiler
each 语句, 原生语法:
each(expr, (val, key)=>{code}); ctx.each(expr, (val, key)=>{code});
Kind: instance method of Compiler
let 语句, 原生语法:
let v1 = expr; let [v1,v2] = [expr, expr]; // ES6 解构赋值 let v1 = expr; code;
Kind: instance method of Compiler
PowCSS 缺省的 Context 实现. 该实现不分析 CSS 规则的合法性, 只提供结构上的操作和一些辅助方法. 如果要对结果进行再次处理, 推荐使用 walk 方法.
Kind: global class
Properties
| Name | Type | Description |
|---|---|---|
| rule | Rule |
当前维护的规则, 初始 null |
| rules | Array.<Rule> |
最终的规则数组 |
| stack | Array.<Rule> |
当前规则的 parent 栈 |
- Context
- new Context()
- .reset() ⇒
this - .each(x, callback) ⇒
this - .open(name) ⇒
this - .close() ⇒
this - .name() ⇒
string - .decl(key, val) ⇒
string - .toCSS() ⇒
string - .walk(context) ⇒
boolean
构造, 参数用来对 this 进行扩展.
重置 .rule, .rules, .stack 为初始状态
Kind: instance method of Context
遍历 x 回调 callback(val, key)
Kind: instance method of Context
| Param | Type | Description |
|---|---|---|
| x | object | array |
|
| callback | function |
参数顺序 (val, key) |
开启一个具名规则并替换 name 中的占位符 '&', 该方法必须与 close 成对使用. 嵌套使用时 this.stack 会增长.
Kind: instance method of Context
| Param | Type |
|---|---|
| name | string |
关闭当前的规则, this.stack 会减少, 该方法必须与 .open 成对使用.
Kind: instance method of Context
返回 this.rule.name
Kind: instance method of Context
返回或设置当前规则的 key 声明
Kind: instance method of Context
| Param | Type |
|---|---|
| key | string |
| val | string |
输出 this.rules 为 CSS 源码
Kind: instance method of Context
Returns: string - css
遍历 this.rules 调用 context 的 open, close, decl 方法. context 的 open, close 返回的对象会用于后续的迭代. 任何一个方法返回非真值会终止遍历.
Kind: instance method of Context
Returns: boolean - finished 是否完全遍历
| Param | Type | Description |
|---|---|---|
| context | object |
实现 open, close, decl 方法的对象 |
lineify 是个非空白行扫描器, 扫描并返回非空白行信息.
Kind: global class
- lineify
- new lineify(source)
- .scan() ⇒
Object|null
| Param | Type | Description |
|---|---|---|
| source | string |
待扫描的字符串 |
返回扫描到的非空白行字符串和位置信息, 并前进. 结构: {source, offset, line, column}
Kind: instance method of lineify
Returns: Object | null - token 返回 null 表示 EOF
辅助方法集合
Kind: global variable
- util
- .info(message, loc, at) ⇒
string - .isObject(x) ⇒
Boolean - .isNumber(x) ⇒
Boolean - .isArray(x) ⇒
Boolean - .isString(x) ⇒
Boolean - .isFunction(x) ⇒
Boolean
- .info(message, loc, at) ⇒
返回包含位置信息的字符串, 常用于出错信息
Kind: static method of util
| Param | Type | Description |
|---|---|---|
| message | string |
自定义信息 |
| loc | object |
含有 line, column 的位置信息 |
| at | string |
缺省为 |
isObject
Kind: static method of util
| Param | Type |
|---|---|
| x | * |
isNumber
Kind: static method of util
| Param | Type |
|---|---|
| x | * |
isArray
Kind: static method of util
| Param | Type |
|---|---|
| x | * |
isString
Kind: static method of util
| Param | Type |
|---|---|
| x | * |
isFunction
Kind: static method of util
| Param | Type |
|---|---|
| x | * |
抽象规则
Kind: global typedef
Properties
| Name | Type | Description |
|---|---|---|
| name | string |
规则名, 也可能是个定义值, 比如 @charset |
| decls | object.<string, (string|Rule)> |
键值声明 |