<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet' title='Bootstrap-3.3.7'>
<link href='https://glpjt.s3-us-west-1.amazonaws.com/ir/cssom.css' rel="stylesheet" title='CSSOM'>
<style title='Tag'>
body {max-width:96%;visibility:hidden;font:400 16px/1.2 Verdana}
ul.x-list.x-list {margin-left: 0; padding-left: 1em; text-indent: -1em;list-style-type:none;}
li.x-item.x-item {list-style:none; line-height:1.5;}
.x-flex.x-flex {display:flex;justify-content:center;align-items:center;}
.x-col {display:flex;flex-direction:column}
option::before {content:attr(name)}
#idx {max-width:6ch;}
label {margin:10px auto 10px -15px}
#add {position:fixed; z-index:1; top:50px; left:20px}
</style>
</head>

<body>
<main class='container' style='padding:50px 0 20px 20px'>
 

  <form id='cssom' class='row x-col'>
    <div class='btn-group col-sm-12'>
      <input id='add' class='btn-lg btn-success' type='button' value='Add Set'>
      </div>
      <section class='row'>
         <!--=================================[0]-->
         <fieldset class='set0 col-sm-12'>
         <hr>
            <div class='row x-flex'>
               <!--=================================[1]-->
               <label class='form-control-label col-sm-2'>CSS Rule</label>
               <textarea id='rul0' class='form-control col-sm-10' rows='2'>li.x-item.x-item::before {content: '\1f539\00a0';list-style:none;font-size:small;position:relative;bottom:2px}</textarea>
            </div>
            <div class='form-inline row'>
               <label class='form-control-label col-sm-2'>Stylesheet</label>
               <select id='sht0' class='form-control col-sm-4'>
                 <optgroup label="LINK">
                  <!--================================[3]-->
                  <option value='0' selected name='Bootstrap-3.3.7'> [0]</option>
                  <option value='1' name='CSSOM'> [1]</option>
                </optgroup>
                <optgroup label="STYLE">
                  <option value='2' name='Tag'> [2]</option>
                </optgroup>
               </select>
               <label class='form-control-label col-sm-1'>Rule Index</label>
               <input id='idx0' class='form-control col-sm-1' type='number' min='-1' value='0'>
               <!--==========[4]--> 
            
            <div class="btn-group col-sm-4"> 
    
               <!--=====[5]-->
               <input id='ins0' class='btn btn-primary' type='button' value='Insert Rule' onclick='modRule(this)'>
               <!--======[6]-->
               <input id='del0' class='btn btn-danger' type='button' value='Delete Rule' onclick='modRule(this)'>
            </div>
            </div>
         </fieldset>
         </section>
         <hr><br>
         
      </form>

 
   <hgroup class='x-inline'> 
      <!--====================================[hgroup.x-inline]-->
      <h1 class='h1'>CSSStyleSheet</h1>
      <h2 class='h2'>.insertRule()</h2>
   </hgroup>
   <article class='text-primary'>
      <blockquote class='blockquote'>
         <h3 id="Restrictions" class='h3'>Restrictions</h3>
         <p>CSS stylesheet rule-lists have a number of intuitive and not-so-intuitive <a class="external" href="https://drafts.csswg.org/cssom/#insert-a-css-rule">restrictions</a> affecting how and where rules can be inserted. Violating these will likely
            cause an error raised as a <a title="The DOMException interface represents an abnormal event (called an exception) which occurs as a result of calling a method or accessing a property of a web API." href="https://developer.mozilla.org/en-US/docs/Web/API/DOMException"><code>DOMException</code></a> </p>
         
         <!--==========================[ul.x-list | li.x-fade]-->
         <ul id='list' class='list-group-flush x-list'>
            <li class='list-group-item-text fade x-fade x-item'>If index &gt; number of rules in the stylesheet (the <a title="A CSSRuleList is an (indirect-modify only) array-like object containing an ordered collection of CSSRule objects." href="https://developer.mozilla.org/en-US/docs/Web/API/CSSRuleList"><code>CSSRuleList</code></a>.length),
               then aborts with IndexSizeError.</li>
            <li class='list-group-item-warning x-item'>If rule cannot be inserted at index 0 due to some CSS constraint, then aborts with HierarchyRequestError.</li>
            <li class='list-group-item-danger x-item'>If more than one rule is given in the rule parameter, then aborts with SyntaxError</li>
            <li class='list-group-item-text x-fade x-item'>If trying to insert an @import at-rule after a style rule, then aborts with HierarchyRequestError.</li>
            <li class='list-group-item-text x-fade x-item'>If rule is @namespace at-rule and list has more than just @import at-rules and/or @namespace at-rules, then aborts with InvalidStateError.</li>
         </ul>
      </blockquote>
      <footer class='blockquote-footer'> 
         
         <!--===============================[[cite.x-cite]--> 
         <cite class='x-cite'><a href='https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule#Restrictions'>CSSStyleSheet.insertRule() - Wed APIs #Restrictions | MDN</a></cite> </footer>
   </article>
</main>
<script>
var cnt = 0;   
var form = document.forms[0];
var add = document.getElementById('add');

function modRule(ID) {
	var e = window.event;
	var i = ID.id.split('').pop();
	console.log('ruleIndex: '+i);
	
	var sheets = document.styleSheets;
  var sheet = document.getElementById('sht'+i);
	var rulez = document.getElementById('rul'+i);
	var index = document.getElementById('idx'+i);
	var vSht = parseInt(sheet.value, 10);
	var vIdx = parseInt(index.value, 10);
	var vRul = rulez.value;
	
	if(e.target.value === 'Delete Rule') {
		switch(vSht) {
			case 0:
		  sheets[0].deleteRule(vIdx);
			break;
			case 1:
		  sheets[1].deleteRule(vIdx);
			break;
			case 2:
		  sheets[2].deleteRule(vIdx);
			break;
			default:			
		  sheets[0].deleteRule(vIdx);
			break;
		}
			
	}
	else if(e.target.value === 'Insert Rule') {
	  switch(vSht) {
			case 0:
		  sheets[0].insertRule(vRul, vIdx);
			break;
			case 1:
		  sheets[1].insertRule(vRul, vIdx);
			break;
			case 2:
		  sheets[2].insertRule(vRul, vIdx);
			break;
			default:			
		  sheets[0].insertRule(vRul, vIdx);
			break;
		}
	}
  else {
		return;
	}
}

add.addEventListener('click', addSet, false);

function addSet(e) {
	cnt++;
	var set = document.querySelector('.set0');
	var opt = document.options
	var dupe = set.cloneNode(true);
	dupe.className = 'set'+cnt;
	var fields = Array.from(dupe.querySelectorAll('[id]'));
	var ids = fields.map(function(ID, idx) {
	  var zero = ID.id.lastIndexOf("0");
    ID.id = ID.id.slice(0, zero);
	  ID.id = ID.id + cnt;
		if(ID.id === 'rul'+cnt) {
			ID.textContent = 'p {color:red}';
		}
    console.log('id: '+ID.id+' val: '+ID.value);
	  return ID.value;
  });
	form.appendChild(dupe);
}

 
  </script>
</body>
</html>
/*0*/
hgroup.x-inline {display:flex; justify-content:center; align-items: center;}
/*1*/
ul.x-list.x-list {list-style: none;}
/*2*/
li.x-fade.x-fade {color:rgba(0,0,0,.3);}
/*3*/
cite.x-cite.x-cite {position:relative; left:60%}
/*4*/
cite.x-cite.x-cite::before {content:"\2014 \00A0"}
/*5*/
.blockquote-footer::before {content: '';}
/*6*/
li.list-group-item {line-height:1.5}
/*7*/
a {text-shadow: 2px 5px 3px rgba(192,192,192,.8)} 
/* Fonts from Google Fonts - more at https://fonts.google.com */
@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,700');
@import url('https://fonts.googleapis.com/css?family=Merriweather:400,700');

body {
  background-color: white;
  font-family: "Open Sans", sans-serif;
  padding: 5px 25px;
  font-size: 18px;
  margin: 0;
  color: #444;
}

h1 {
  font-family: "Merriweather", serif;
  font-size: 32px;
}