Blame view

node_modules/stylus/lib/functions/selector.js 1.76 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
  }