wy-ei
5/20/2016 - 2:37 AM

style.js

var util = require('./util');
var Style = function(stylesheet) {
    this.stylesheet = stylesheet;
};


Style.prototype.addRule = function(rule) {
    var stylesheet = this.stylesheet;
    util.each(rule, function(style, selector) {
        var styles = [selector + '{'];
        util.each(style, function(value, prop) {
            styles.push(prop + ':');
            styles.push(value + ';');
        });
        styles.push('}');
        styles = styles.join('');
        stylesheet.insertRule(styles, stylesheet.cssRules.length);
    });
};

Style.prototype.deleteRule = function(index) {
    this.stylesheet.deleteRule(index);
};

Style.prototype.disable = function() {
    this.stylesheet.disabled = true;
};

Style.prototype.enable = function() {
    this.stylesheet.disabled = false;
};

Style.prototype.getCSSText = function(index) {
    var cssRule = this.stylesheet.cssRules[index];
    if (cssRule) {
        return cssRule.cssText;
    } else {
        return '';
    }
};

Style.prototype.getStyleSheetContent = function() {
    var cssRules = this.stylesheet.cssRules;
    var content = [];
    util.each(cssRules, function(rule) {
        content.push(rule.cssText);
    });
    content = content.join('');
    return content;
};

module.exports = Style;