示例#1
0
  dom.css = function(selector, string) {
    var match;
    selector = utils.minify_css_selector(selector);

    // start with the last stylesheet
    var stylesheets = _.toArray(document.styleSheets).reverse();

    _.every(stylesheets, function(stylesheet) {
      // ignore disabled stylesheets
      if (stylesheet.disabled) { return true; } // continue

      var rules = stylesheet.cssRules || stylesheet.rules || [];
      match = _.find(rules, function(rule) {
        // find matching minified selector
        return utils.minify_css_selector(rule.selectorText) === selector;
      });

      return !match;
    });

    // always return an object
    if (!match) { return {}; }

    // remove selector and braces, leaving only the CSS properties
    var rule = match.cssText.replace(/^.*{\s*/, '').replace(/\s*}\s*$/, '');

    // convert the property string to an object
    var css = _.chain(rule.split(';')).map(function(property) {
      var map = _.map(property.split(':'), _.str.clean);
      return map.length === 2 ? map : null;
    }).compact().object().value();

    return !string ? css :
      _.reduce(css, function(str, value, prop) {
        return str + _.str.sprintf('%s: %s; ', prop, value);
      }, '').trim();
  };
示例#2
0
 match = _.find(rules, function(rule) {
   // find matching minified selector
   return utils.minify_css_selector(rule.selectorText) === selector;
 });