Code coverage report for juice\lib\selector.js

Statements: 77.5% (31 / 40)      Branches: 80% (16 / 20)      Functions: 100% (5 / 5)      Lines: 75.76% (25 / 33)      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 97 98 99 100 101 102 103          1           1                   1 236 236                 1 243 243                 1 22 22   1 9       9 20       20     20 20     20     20                       9         9                     1 234 234          
 
/**
 * Module dependencies.
 */
 
var parser = require('slick').parse;
 
/**
 * Module exports.
 */
 
module.exports = exports = Selector;
 
/**
 * CSS selector constructor.
 *
 * @param {String} selector text
 * @param {Array} optionally, precalculated specificity
 * @api public
 */
 
function Selector (text, spec) {
  this.text = text;
  this.spec = spec;
}
 
/**
 * 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)
      , spec = [0, 0, 0, 0]
      , nots = []
 
    for (var i = 0; i < expressions.length; i++) {
      var expression = expressions[i]
        , 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
      Iif (pseudos) {
        spec[3] += pseudos.length;
 
        for (var p = 0; p < pseudos.length; p++) {
         if (pseudos[p].key === '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 [];
  }
}