Skip to content

Instantly share code, notes, and snippets.

@Frontenda
Forked from yckart/jquery.addrule.js
Created April 20, 2016 08:36
Show Gist options
  • Save Frontenda/d0d5a9c7073ab6f128ed385d781c28c1 to your computer and use it in GitHub Desktop.
Save Frontenda/d0d5a9c7073ab6f128ed385d781c28c1 to your computer and use it in GitHub Desktop.

Revisions

  1. @yckart yckart revised this gist Nov 23, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions jquery.addrule.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    /*!
    * jquery.addrule.js 0.0.1 - https://gist.github.com/yckart/5563717/
    * jquery.addrule.js 0.0.2 - https://gist.github.com/yckart/5563717/
    * Add css-rules to an existing stylesheet.
    *
    * @see http://stackoverflow.com/a/16507264/1250044
    *
    * Copyright (c) 2013 Yannick Albert (http://yckart.com)
    * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
    * 2013/05/12
    * 2013/11/23
    **/

    (function ($) {
  2. @yckart yckart revised this gist Nov 23, 2013. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions jquery.addrule.js
    Original file line number Diff line number Diff line change
    @@ -16,11 +16,11 @@
    styles = (function (styles) {
    if (typeof styles === "string") return styles;
    var clone = "";
    for (var p in styles) {
    if (styles.hasOwnProperty(p)) {
    var val = styles[p];
    p = p.replace(/([A-Z])/g, "-$1").toLowerCase(); // convert to dash-case
    clone += p + ":" + (p === "content" ? '"' + val + '"' : val) + "; ";
    for (var prop in styles) {
    if (styles.hasOwnProperty(prop)) {
    var val = styles[prop];
    prop = prop.replace(/([A-Z])/g, "-$1").toLowerCase(); // convert to dash-case
    clone += prop + ":" + (prop === "content" ? '"' + val + '"' : val) + "; ";
    }
    }
    return clone;
    @@ -39,4 +39,4 @@
    return this;
    };

    }(window.jQuery));
    }(this.jQuery || this.Zepto));
  3. Yannick Albert revised this gist May 15, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery.addrule.js
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@
    }
    return clone;
    }(styles));
    sheet = sheet || document.styleSheets[0];
    sheet = sheet || document.styleSheets[document.styleSheets.length - 1];

    if (sheet.insertRule) sheet.insertRule(selector + " {" + styles + "}", sheet.cssRules.length);
    else if (sheet.addRule) sheet.addRule(selector, styles);
  4. Yannick Albert revised this gist May 15, 2013. 1 changed file with 15 additions and 16 deletions.
    31 changes: 15 additions & 16 deletions jquery.addrule.js
    Original file line number Diff line number Diff line change
    @@ -13,17 +13,18 @@

    window.addRule = function (selector, styles, sheet) {

    if (typeof styles !== "string") {
    styles = (function (styles) {
    if (typeof styles === "string") return styles;
    var clone = "";
    for (var style in styles) {
    var val = styles[style];

    style = style.replace(/([A-Z])/g, "-$1").toLowerCase(); // convert to dash-case
    clone += style + ":" + (style === "content" ? '"' + val + '"' : val) + "; ";
    for (var p in styles) {
    if (styles.hasOwnProperty(p)) {
    var val = styles[p];
    p = p.replace(/([A-Z])/g, "-$1").toLowerCase(); // convert to dash-case
    clone += p + ":" + (p === "content" ? '"' + val + '"' : val) + "; ";
    }
    }
    styles = clone;
    }

    return clone;
    }(styles));
    sheet = sheet || document.styleSheets[0];

    if (sheet.insertRule) sheet.insertRule(selector + " {" + styles + "}", sheet.cssRules.length);
    @@ -33,11 +34,9 @@

    };

    if ($) {
    $.fn.addRule = function (styles, sheet) {
    addRule(this.selector, styles, sheet);
    return this;
    };
    }
    if ($) $.fn.addRule = function (styles, sheet) {
    addRule(this.selector, styles, sheet);
    return this;
    };

    }(this.jQuery));
    }(window.jQuery));
  5. Yannick Albert revised this gist May 12, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery.addrule.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /*!
    * jquery.addrule.js 0.0.1 - https://gist.github.com/yckart/5563717/
    * Adds css-rules to an existing stylesheet.
    * Add css-rules to an existing stylesheet.
    *
    * @see http://stackoverflow.com/a/16507264/1250044
    *
  6. Yannick Albert revised this gist May 12, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions jquery.addrule.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@
    * jquery.addrule.js 0.0.1 - https://gist.github.com/yckart/5563717/
    * Adds css-rules to an existing stylesheet.
    *
    * @see http://stackoverflow.com/a/16507264/1250044
    *
    * Copyright (c) 2013 Yannick Albert (http://yckart.com)
    * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
    * 2013/05/12
  7. Yannick Albert revised this gist May 12, 2013. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions jquery.addrule.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    /*!
    * jquery.addrule.js 0.0.1 - https://gist.github.com/yckart/5563717/
    * Adds css-rules to an existing stylesheet.
    *
    * Copyright (c) 2013 Yannick Albert (http://yckart.com)
    * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
    * 2013/05/12
    **/

    (function ($) {

    window.addRule = function (selector, styles, sheet) {
  8. Yannick Albert revised this gist May 12, 2013. 1 changed file with 0 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    // add some styles as object notation
    $("body:after").addRule({
    content: "foo",
    color: "red"
    });

    // or as string (note, you've to write the property in dash-case)
    $("body:after").addRule("font-size: 23px");

    // or plain, without jquery
    addRule("body:after", "font-style: italic");
  9. Yannick Albert revised this gist May 12, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@ $("body:after").addRule({
    content: "foo",
    color: "red"
    });

    // or as string (note, you've to write the property in dash-case)
    $("body:after").addRule("font-size: 23px");

    // or plain, without jquery
    addRule("body:after", "font-style: italic");
  10. Yannick Albert revised this gist May 12, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery.addrule.js
    Original file line number Diff line number Diff line change
    @@ -29,4 +29,4 @@
    };
    }

    }(window.jQuery));
    }(this.jQuery));
  11. Yannick Albert created this gist May 12, 2013.
    11 changes: 11 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    // add some styles as object notation
    $("body:after").addRule({
    content: "foo",
    color: "red"
    });

    // or as string (note, you've to write the property in dash-case)
    $("body:after").addRule("font-size: 23px");

    // or plain, without jquery
    addRule("body:after", "font-style: italic");
    32 changes: 32 additions & 0 deletions jquery.addrule.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    (function ($) {

    window.addRule = function (selector, styles, sheet) {

    if (typeof styles !== "string") {
    var clone = "";
    for (var style in styles) {
    var val = styles[style];

    style = style.replace(/([A-Z])/g, "-$1").toLowerCase(); // convert to dash-case
    clone += style + ":" + (style === "content" ? '"' + val + '"' : val) + "; ";
    }
    styles = clone;
    }

    sheet = sheet || document.styleSheets[0];

    if (sheet.insertRule) sheet.insertRule(selector + " {" + styles + "}", sheet.cssRules.length);
    else if (sheet.addRule) sheet.addRule(selector, styles);

    return this;

    };

    if ($) {
    $.fn.addRule = function (styles, sheet) {
    addRule(this.selector, styles, sheet);
    return this;
    };
    }

    }(window.jQuery));