Code coverage report for juice\lib\utils.js

Statements: 96.36% (106 / 110)      Branches: 86.21% (50 / 58)      Functions: 93.75% (15 / 16)      Lines: 96.19% (101 / 105)      Ignored: none     

All files » juice\lib\ » utils.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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249            1 1 1 1 1 1 1   1 1 1                   1 208 208 208   208 2567   2567 252 252   2315 197 197   2118 2118         208 207     208                       1 65 65   65 207 199 199   199 386         65       1 5 5 6 6 6 6   5                     1 20 20   20               72 2 2 2 2   2 2       72 2 2   2   2 3   3   3   3 3       2 2       20                 1 57 57 57     1 100     1 57 3       1 56 3       1 57     1 56                 1                                     1 65 204 55 39     10     1 128 384 384     128     1 50                       50   50    
'use strict';
 
/**
 * Module dependencies.
 */
 
var cssom = require('cssom');
var cheerio = require('cheerio');
var own = {}.hasOwnProperty;
var os = require('os');
var deprecate = require('util-deprecate');
var Selector = require('./selector');
var Property = require('./property');
 
exports.Selector = Selector;
exports.Property = Property;
exports.styleSelector = new Selector('<style attribute>');
 
/**
 * Returns an array of the selectors.
 *
 * @license Sizzle CSS Selector Engine - MIT
 * @param {String} selectorText from cssom
 * @api public
 */
 
exports.extract = function extract(selectorText) {
  var attr = 0;
  var sels = [];
  var sel = '';
 
  for (var i = 0, l = selectorText.length; i < l; i++) {
    var c = selectorText.charAt(i);
 
    if (attr) {
      if (']' === c || ')' === c) { attr--; }
      sel += c;
    } else {
      if (',' === c) {
        sels.push(sel);
        sel = '';
      } else {
        if ('[' === c || '(' === c) { attr++; }
        if (sel.length || (c !== ',' && c !== '\n' && c !== ' ')) { sel += c; }
      }
    }
  }
 
  if (sel.length) {
    sels.push(sel);
  }
 
  return sels;
};
 
/**
 * Returns a parse tree for a CSS source.
 * If it encounters multiple selectors separated by a comma, it splits the
 * tree.
 *
 * @param {String} css source
 * @api public
 */
 
exports.parseCSS = function(css) {
  var rules = cssom.parse(css).cssRules || [];
  var ret = [];
 
  for (var i = 0, l = rules.length; i < l; i++) {
    if (rules[i].selectorText) { // media queries don't have selectorText
      var rule = rules[i];
      var selectors = exports.extract(rule.selectorText);
 
      for (var ii = 0, ll = selectors.length; ii < ll; ii++) {
        ret.push([selectors[ii], rule.style]);
      }
    }
  }
 
  return ret;
};
 
 
var getStringifiedStyles = function(rule) {
  var styles = [];
  for (var style = 0; style < rule.style.length; style++) {
    var property = rule.style[style];
    var value = rule.style[property];
    var important = rule.style._importants[property] ? ' !important' : '';
    styles.push('    ' + property + ': ' + value + important + ';');
  }
  return styles;
};
 
/**
 * Returns preserved text for a CSS source.
 *
 * @param {String} css source
 * @param {Object} options
 * @api public
 */
 
exports.getPreservedText = function(css, options) {
  var rules = cssom.parse(css).cssRules || [];
  var preserved = [];
 
  for (var i = 0, l = rules.length; i < l; i++) {
    /* CSS types
      STYLE: 1,
      IMPORT: 3,
      MEDIA: 4,
      FONT_FACE: 5,
    */
 
    if (options.fontFaces && rules[i].type === cssom.CSSFontFaceRule.prototype.type) {
      var fontFace = [ '' ];
      fontFace.push('@font-face {');
      fontFace = fontFace.concat(getStringifiedStyles(rules[i]));
      fontFace.push('}');
 
      Eif (fontFace.length) {
        preserved.push(fontFace.length ? fontFace.join(os.EOL) + os.EOL : '');
      }
    }
 
    if (options.mediaQueries && rules[i].type === cssom.CSSMediaRule.prototype.type) {
      var query = rules[i];
      var queryString = [];
 
      queryString.push(os.EOL + '@media ' + query.media[0] + ' {');
 
      for (var ii = 0, ll = query.cssRules.length; ii < ll; ii++) {
        var rule = query.cssRules[ii];
 
        Eif (rule.type === cssom.CSSStyleRule.prototype.type
            || rule.type === cssom.CSSFontFaceRule.prototype.type) {
          queryString.push('  '
            + (rule.type === cssom.CSSStyleRule.prototype.type ? rule.selectorText : '@font-face') + ' {');
          queryString = queryString.concat(getStringifiedStyles(rule));
          queryString.push('  }');
        }
      }
 
      queryString.push('}');
      preserved.push(queryString.length ? queryString.join(os.EOL) + os.EOL : '');
    }
  }
 
  return preserved.join(os.EOL);
};
 
/**
 * Returns a Cheerio object
 *
 * api public
 */
 
exports.cheerio = function(html, options) {
  options = exports.extend({decodeEntities: false}, options || {});
  html = exports.encodeEntities(html);
  return cheerio.load(html,options);
};
 
exports.normalizeLineEndings = function(text) {
  return text.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n');
};
 
exports.encodeEJS = function(html) {
  return html.replace(/<%((.|\s)*?)%>/g, function(match, subMatch) {
    return '<!--EJS <%' + subMatch + '%> -->';
  });
};
 
exports.decodeEJS = function(html) {
  return html.replace(/<!--EJS <%((.|\s)*?)%> -->/g, function(match, subMatch) {
    return '<%' + subMatch + '%>';
  });
};
 
exports.encodeEntities = function(html) {
  return exports.encodeEJS(html);
};
 
exports.decodeEntities = function(html) {
  return exports.decodeEJS(html);
};
 
/**
 * Converts to array
 *
 * @api public
 */
 
exports.toArray = deprecate(function(arr) {
  var ret = [];
 
  for (var i = 0, l = arr.length; i < l; i++) {
    ret.push(arr[i]);
  }
 
  return ret;
}, 'utils.toArray: Will be removed in a future version');
 
/**
 * Compares two specificity vectors, returning the winning one.
 *
 * @param {Array} vector a
 * @param {Array} vector b
 * @return {Array}
 * @api public
 */
 
exports.compare = function(a, b) {
  for (var i = 0; i < 4; i++) {
    if (a[i] === b[i]) { continue; }
    if (a[i] > b[i]) { return a; }
    return b;
  }
 
  return b;
};
 
exports.extend = function(obj, src) {
  for (var key in src) {
    Eif (own.call(src, key)) {
      obj[key] = src[key];
    }
  }
  return obj;
};
 
exports.getDefaultOptions = function(options) {
  var result = exports.extend({
    extraCss: '',
    applyStyleTags: true,
    removeStyleTags: true,
    preserveMediaQueries: false,
    preserveFontFaces: false,
    applyWidthAttributes: false,
    applyHeightAttributes: false,
    applyAttributesTableElements: false,
    url: ''
  }, options);
 
  result.webResources = result.webResources || {};
 
  return result;
}