diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/stylus/lib/functions/selector.js | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/stylus/lib/functions/selector.js')
| -rw-r--r-- | node_modules/stylus/lib/functions/selector.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/node_modules/stylus/lib/functions/selector.js b/node_modules/stylus/lib/functions/selector.js new file mode 100644 index 00000000..45a421d6 --- /dev/null +++ b/node_modules/stylus/lib/functions/selector.js @@ -0,0 +1,71 @@ +var utils = require('../utils'); + +/** + * Return the current selector or compile + * selector from a string or a list. + * + * @param {String|Expression} + * @return {String} + * @api public + */ + +(module.exports = function selector(){ + var stack = this.selectorStack + , args = [].slice.call(arguments); + + if (1 == args.length) { + var expr = utils.unwrap(args[0]) + , len = expr.nodes.length; + + // selector('.a') + if (1 == len) { + utils.assertString(expr.first, 'selector'); + var SelectorParser = require('../selector-parser') + , val = expr.first.string + , parsed = new SelectorParser(val).parse().val; + + if (parsed == val) return val; + + stack.push(parse(val)); + } else if (len > 1) { + // selector-list = '.a', '.b', '.c' + // selector(selector-list) + if (expr.isList) { + pushToStack(expr.nodes, stack); + // selector('.a' '.b' '.c') + } else { + stack.push(parse(expr.nodes.map(function(node){ + utils.assertString(node, 'selector'); + return node.string; + }).join(' '))); + } + } + // selector('.a', '.b', '.c') + } else if (args.length > 1) { + pushToStack(args, stack); + } + + return stack.length ? utils.compileSelectors(stack).join(',') : '&'; +}).raw = true; + +function pushToStack(selectors, stack) { + selectors.forEach(function(sel) { + sel = sel.first; + utils.assertString(sel, 'selector'); + stack.push(parse(sel.string)); + }); +} + +function parse(selector) { + var Parser = new require('../parser') + , parser = new Parser(selector) + , nodes; + parser.state.push('selector-parts'); + nodes = parser.selector(); + nodes.forEach(function(node) { + node.val = node.segments.map(function(seg){ + return seg.toString(); + }).join(''); + }); + return nodes; +} |
