diff --git a/.changeset/tricky-buttons-clap.md b/.changeset/tricky-buttons-clap.md new file mode 100644 index 0000000..532f1b0 --- /dev/null +++ b/.changeset/tricky-buttons-clap.md @@ -0,0 +1,5 @@ +--- +"rrweb-cssom": patch +--- + +`index` parameter of `CSSStyleSheet.insertRule` is now optional diff --git a/lib/CSSStyleSheet.js b/lib/CSSStyleSheet.js index f0e0dfc..eaa856e 100644 --- a/lib/CSSStyleSheet.js +++ b/lib/CSSStyleSheet.js @@ -32,11 +32,14 @@ CSSOM.CSSStyleSheet.prototype.constructor = CSSOM.CSSStyleSheet; * -> "img{border:none;}body{margin:0;}" * * @param {string} rule - * @param {number} index + * @param {number} [index=0] * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet-insertRule * @return {number} The index within the style sheet's rule collection of the newly inserted rule. */ CSSOM.CSSStyleSheet.prototype.insertRule = function(rule, index) { + if (index === void 0) { + index = 0; + } if (index < 0 || index > this.cssRules.length) { throw new RangeError("INDEX_SIZE_ERR"); } diff --git a/spec/CSSStyleSheet.spec.js b/spec/CSSStyleSheet.spec.js index 828bc6c..b8dcdbd 100644 --- a/spec/CSSStyleSheet.spec.js +++ b/spec/CSSStyleSheet.spec.js @@ -24,6 +24,15 @@ describe('CSSStyleSheet', function() { s.insertRule("a {color: blue}", 0); expect(s.cssRules[0].parentStyleSheet).toBe(s); }); + + it('should insert in index 0 by default', function () { + var s = new CSSOM.CSSStyleSheet; + s.insertRule("a {color: blue}", 0); + + var insertedIndex = s.insertRule("b {color: black;}"); + expect(insertedIndex).toEqual(0); + expect(s.cssRules[0].cssText).toEqual("b {color: black;}"); + }) }); }); });