Code coverage report for juice\lib\selector.js

Statements: 97.67% (42 / 43)      Branches: 100% (20 / 20)      Functions: 100% (5 / 5)      Lines: 97.22% (35 / 36)      Ignored: none     

All files » juice\lib\ » selector.js
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96    1   1                   1 393 393                 1 480 480                 1 709 709   1 108 108 108   108 148 148     148     148 148     148     148 17   17 18 2 2           108 2 8     108                     1 394 394          
'use strict';
 
var parser = require('slick').parse;
 
module.exports = exports = Selector;
 
/**
 * CSS selector constructor.
 *
 * @param {String} selector text
 * @param {Array} optionally, precalculated specificity
 * @api public
 */
 
function Selector(text) {
  this.text = text;
  this.spec = undefined;
}
 
/**
 * Get parsed selector.
 *
 * @api public
 */
 
Selector.prototype.parsed = function() {
  if (!this.tokens) { this.tokens = parse(this.text); }
  return this.tokens;
};
 
/**
 * Lazy specificity getter
 *
 * @api public
 */
 
Selector.prototype.specificity = function() {
  if (!this.spec) { this.spec = specificity(this.text, this.parsed()); }
  return this.spec;
 
  function specificity(text, parsed) {
    var expressions = parsed || parse(text);
    var spec = [0, 0, 0, 0];
    var nots = [];
 
    for (var i = 0; i < expressions.length; i++) {
      var expression = expressions[i];
      var pseudos = expression.pseudos;
 
      // id awards a point in the second column
      if (expression.id) { spec[1]++; }
 
      // classes and attributes award a point each in the third column
      if (expression.attributes) { spec[2] += expression.attributes.length; }
      if (expression.classList) { spec[2] += expression.classList.length; }
 
      // tag awards a point in the fourth column
      if (expression.tag && expression.tag !== '*') { spec[3]++; }
 
      // pseudos award a point each in the fourth column
      if (pseudos) {
        spec[3] += pseudos.length;
 
        for (var p = 0; p < pseudos.length; p++) {
          if (pseudos[p].name === 'not') {
            nots.push(pseudos[p].value);
            spec[3]--;
          }
        }
      }
    }
 
    for (var ii = nots.length; ii--;) {
      var not = specificity(nots[ii]);
      for (var jj = 4; jj--;) { spec[jj] += not[jj]; }
    }
 
    return spec;
  }
};
 
/**
 * Parses a selector and returns the tokens.
 *
 * @param {String} selector
 * @api private.
 */
 
function parse(text) {
  try {
    return parser(text)[0];
  } catch (e) {
    return [];
  }
}