Skip to content
This repository has been archived by the owner on Jun 5, 2018. It is now read-only.

Latest commit

 

History

History
470 lines (331 loc) · 13.1 KB

api.md

File metadata and controls

470 lines (331 loc) · 13.1 KB

Classes

PowCSS

PowCSS 负责解析 source 为节点树, 并拼接编译器的编译结果. 在 PowCSS 中的插件就是 compiler, compiler 负责与 context 配合.

Compiler

PowCSS 缺省的 Compiler 实现. 所有方法采用原生 js 语法, 要求源码自带嵌套占位符 '...'.

Context

PowCSS 缺省的 Context 实现. 该实现不分析 CSS 规则的合法性, 只提供结构上的操作和一些辅助方法. 如果要对结果进行再次处理, 推荐使用 walk 方法.

lineify

lineify 是个非空白行扫描器, 扫描并返回非空白行信息.

Members

util

辅助方法集合

Typedefs

Rule : object

抽象规则

PowCSS

PowCSS 负责解析 source 为节点树, 并拼接编译器的编译结果. 在 PowCSS 中的插件就是 compiler, compiler 负责与 context 配合.

Kind: global class

new PowCSS(plugins)

Param Type Description
plugins Array.<Compiler> 编译器数组, 缺省为 [compiler()]

powCSS.use(plugin) ⇒ this

使用一个编译器插件

Kind: instance method of PowCSS

Param Type
plugin Compiler

powCSS.process(source) ⇒ this

包装 this.result = this.parse(source) 并返回 this.

Kind: instance method of PowCSS

Param Type Description
source string 源码

powCSS.parse(source) ⇒ object

解析 source 为节点树. 节点类型:

  1. root {mode:'root', nodes}
  2. comment {mode:'comment', source} 只保留非行尾注释
  3. decl {mode:'decl', source, key, val}
  4. pending {mode:'', source, nodes}

即所有 !mode 的节点需要通过编译插件进行确认

Kind: instance method of PowCSS
Returns: object - root 节点树

Param Type Description
source string 源码

powCSS.format(root) ⇒ string

格式化输出 root.nodes

Kind: instance method of PowCSS
Returns: string - CSS 无花括号两空格缩进格式

Param Type Description
root object 解析后的节点树

powCSS.compile() ⇒ string

遍历 this.result 所有节点拼接编译插件的编译结果. 未被编译的节点和其子节点被抛弃.

Kind: instance method of PowCSS
Returns: string - body 编译后的函数主体代码;

powCSS.build(params) ⇒ object

返回 Function(params, this.compile(this.result + ';return '+ params.split(',')[0]))

Kind: instance method of PowCSS
Returns: object - ctx

Param Type Description
params string 形参, 缺省为 'ctx'

powCSS.run(source, params, args) ⇒ object

包装 process, build 并返回执行结果

Kind: instance method of PowCSS

Param Type Description
source string CSS 源码
params string 形参, 缺省为 'ctx'
args array 实参, 缺省为 [context()]

powCSS.walk(nodes, context, callback) ⇒ this

使用 nodes.forEach 深度遍历节点树并调用 callback(item, context, index, nodes). 如果 callback 返回非真值, item.nodes 将不参与遍历.

Kind: instance method of PowCSS

Param Type Description
nodes array
context Object 上下文
callback function 回调函数

Compiler

PowCSS 缺省的 Compiler 实现. 所有方法采用原生 js 语法, 要求源码自带嵌套占位符 '...'.

Kind: global class

new Compiler()

构造, 参数用来对 this 进行扩展. 缺省 this.ctx = 'ctx' 表示 context 的形参名

compiler.compile()

compile 接口

Kind: instance method of Compiler

compiler.comment()

编译 n.mode === 'comment' 的节点, '/*!' 开头的顶层注释被保留, 其它被抛弃.

Kind: instance method of Compiler

compiler.rule()

编译 !n.mode 的节点为规则节点

Kind: instance method of Compiler

compiler.decl()

编译 n.mode === 'decl' 的节点

Kind: instance method of Compiler

compiler.if()

if 语句, 原生语法: if(expr) code; if (expr) code;

Kind: instance method of Compiler

compiler.each()

each 语句, 原生语法:

each(expr, (val, key)=>{code}); ctx.each(expr, (val, key)=>{code});

Kind: instance method of Compiler

compiler.let()

let 语句, 原生语法:

let v1 = expr; let [v1,v2] = [expr, expr]; // ES6 解构赋值 let v1 = expr; code;

Kind: instance method of Compiler

Context

PowCSS 缺省的 Context 实现. 该实现不分析 CSS 规则的合法性, 只提供结构上的操作和一些辅助方法. 如果要对结果进行再次处理, 推荐使用 walk 方法.

Kind: global class
Properties

Name Type Description
rule Rule 当前维护的规则, 初始 null
rules Array.<Rule> 最终的规则数组
stack Array.<Rule> 当前规则的 parent 栈

new Context()

构造, 参数用来对 this 进行扩展.

context.reset() ⇒ this

重置 .rule, .rules, .stack 为初始状态

Kind: instance method of Context

context.each(x, callback) ⇒ this

遍历 x 回调 callback(val, key)

Kind: instance method of Context

Param Type Description
x object | array
callback function 参数顺序 (val, key)

context.open(name) ⇒ this

开启一个具名规则并替换 name 中的占位符 '&', 该方法必须与 close 成对使用. 嵌套使用时 this.stack 会增长.

Kind: instance method of Context

Param Type
name string

context.close() ⇒ this

关闭当前的规则, this.stack 会减少, 该方法必须与 .open 成对使用.

Kind: instance method of Context

context.name() ⇒ string

返回 this.rule.name

Kind: instance method of Context

context.decl(key, val) ⇒ string

返回或设置当前规则的 key 声明

Kind: instance method of Context

Param Type
key string
val string

context.toCSS() ⇒ string

输出 this.rules 为 CSS 源码

Kind: instance method of Context
Returns: string - css

context.walk(context) ⇒ boolean

遍历 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

lineify 是个非空白行扫描器, 扫描并返回非空白行信息.

Kind: global class

new lineify(source)

Param Type Description
source string 待扫描的字符串

lineify.scan() ⇒ Object | null

返回扫描到的非空白行字符串和位置信息, 并前进. 结构: {source, offset, line, column}

Kind: instance method of lineify
Returns: Object | null - token 返回 null 表示 EOF

util

辅助方法集合

Kind: global variable

util.info(message, loc, at) ⇒ string

返回包含位置信息的字符串, 常用于出错信息

Kind: static method of util

Param Type Description
message string 自定义信息
loc object 含有 line, column 的位置信息
at string 缺省为

util.isObject(x) ⇒ Boolean

isObject

Kind: static method of util

Param Type
x *

util.isNumber(x) ⇒ Boolean

isNumber

Kind: static method of util

Param Type
x *

util.isArray(x) ⇒ Boolean

isArray

Kind: static method of util

Param Type
x *

util.isString(x) ⇒ Boolean

isString

Kind: static method of util

Param Type
x *

util.isFunction(x) ⇒ Boolean

isFunction

Kind: static method of util

Param Type
x *

Rule : object

抽象规则

Kind: global typedef
Properties

Name Type Description
name string 规则名, 也可能是个定义值, 比如 @charset
decls object.<string, (string|Rule)> 键值声明