<!doctype html>
<html>
  <head>
    <title>MathML roots: adding children</title>

    <!-- This test verifies insertion of children into an <mroot>/<msqrt> element using DOM operations. It should not crash and should render the same as the equivalent static page. -->

    <style type="text/css">
      div { border: 1px solid; margin: .5em; }
    </style>
    <script type="text/javascript">
      var mathmlNS = "http://www.w3.org/1998/Math/MathML";

      function newMn(n)
      {
        var t = document.createTextNode(n.toString());
        var mn = document.createElementNS(mathmlNS, "mn");
        mn.appendChild(t);
        return mn;
      }

      function test1()
      {
        var test1content = document.getElementById("test1content");
        var mroot = document.getElementById("test1").getElementsByTagNameNS(mathmlNS, "mroot");
        var msqrt = document.getElementById("test1").getElementsByTagNameNS(mathmlNS, "msqrt");
        var i;

        // We insert children in normal order.
        for (i = 1; i <= 7; i++) {
          mroot[0].appendChild(newMn(i));
          msqrt[0].appendChild(newMn(i));
        }

        // We insert children in reverse order.
        for (i = 7; i >= 1; i--) {
          mroot[1].insertBefore(newMn(i), mroot[1].firstChild);
          msqrt[1].insertBefore(newMn(i), msqrt[1].firstChild);
        }
      }

      function test2()
      {
        var msqrt = document.getElementById("test2").getElementsByTagNameNS(mathmlNS, "msqrt");
        var mroot = document.getElementById("test2").getElementsByTagNameNS(mathmlNS, "mroot");
        var i;
        for (i = 0; i < msqrt.length; i++)
          msqrt[i].insertBefore(newMn(1), msqrt[i].firstChild);
        for (i = 0; i < mroot.length; i++)
          mroot[i].insertBefore(newMn(1), mroot[i].firstChild);
      }

      function test3()
      {
        var msqrt = document.getElementById("test3").getElementsByTagNameNS(mathmlNS, "msqrt");
        var mroot = document.getElementById("test3").getElementsByTagNameNS(mathmlNS, "mroot");
        var i;
        for (i = 0; i < msqrt.length; i++)
          msqrt[i].appendChild(newMn(msqrt[i].childElementCount+1));
        for (i = 0; i < mroot.length; i++)
          mroot[i].appendChild(newMn(mroot[i].childElementCount+1));
      }

      function test4()
      {
        var msqrt = document.getElementById("test4").getElementsByTagNameNS(mathmlNS, "msqrt");
        var mroot = document.getElementById("test4").getElementsByTagNameNS(mathmlNS, "mroot");
        var i;
        for (i = 0; i < msqrt.length; i++)
          msqrt[i].insertBefore(newMn(2), msqrt[i].firstChild.nextSibling);
        for (i = 0; i < mroot.length; i++)
          mroot[i].insertBefore(newMn(2), mroot[i].firstChild.nextSibling);
      }

      function test5()
      {
        var msqrt = document.getElementById("test5").getElementsByTagNameNS(mathmlNS, "msqrt");
        var mroot = document.getElementById("test5").getElementsByTagNameNS(mathmlNS, "mroot");
        var i;
        for (i = 0; i < msqrt.length; i++)
          msqrt[i].insertBefore(newMn(msqrt[i].childElementCount), msqrt[i].lastChild);
        for (i = 0; i < mroot.length; i++)
          mroot[i].insertBefore(newMn(mroot[i].childElementCount), mroot[i].lastChild);
      }

      function test6()
      {
        var msqrt = document.getElementById("test6").getElementsByTagNameNS(mathmlNS, "msqrt");
        var mroot = document.getElementById("test6").getElementsByTagNameNS(mathmlNS, "mroot");
        var i;
        for (i = 0; i < msqrt.length; i++)
          msqrt[i].insertBefore(newMn(3), msqrt[i].firstChild.nextSibling.nextSibling);
        for (i = 0; i < mroot.length; i++)
          mroot[i].insertBefore(newMn(3), mroot[i].firstChild.nextSibling.nextSibling);
      }

      function test7()
      {
        var msqrt = document.getElementById("test7").getElementsByTagNameNS(mathmlNS, "msqrt");
        var mroot = document.getElementById("test7").getElementsByTagNameNS(mathmlNS, "mroot");
        var i;
        for (i = 0; i < msqrt.length; i++)
          msqrt[i].insertBefore(newMn(msqrt[i].childElementCount-1), msqrt[i].lastChild.previousSibling);
        for (i = 0; i < mroot.length; i++)
          mroot[i].insertBefore(newMn(mroot[i].childElementCount-1), mroot[i].lastChild.previousSibling);
      }

      function run()
      {
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
        test7();
      }
    </script>
  </head>

  <body onload="run()">

    <!-- Test 1: insert in normal and reverse order. -->
    <div id="test1">Test 1:
      <math><mroot></mroot></math>
      <math><msqrt></msqrt></math>
      <math><mroot></mroot></math>
      <math><msqrt></msqrt></math>
    </div>

    <!-- Test 2: insert an <mn> element at the first position in an <mroot>/<msqrt> element with various number of children. -->
    <div id="test2">Test 2:
      <math><msqrt></msqrt></math>
      <math><msqrt><mn>2</mn></msqrt></math>
      <math><msqrt><mn>2</mn><mn>3</mn></msqrt></math>
      <math><msqrt><mn>2</mn><mn>3</mn><mn>4</mn></msqrt></math>
      <math><mroot></mroot></math>
      <math><mroot><mn>2</mn></mroot></math>
      <math><mroot><mn>2</mn><mn>3</mn></mroot></math>
      <math><mroot><mn>2</mn><mn>3</mn><mn>4</mn></mroot></math>
    </div>

    <!-- Test 3: insert an <mn> element at the last position in an <mroot>/<msqrt> element with various number of children. -->
    <div id="test3">Test 3:
      <math><msqrt></msqrt></math>
      <math><msqrt><mn>1</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>2</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>2</mn><mn>3</mn></msqrt></math>
      <math><mroot></mroot></math>
      <math><mroot><mn>1</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>2</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>2</mn><mn>3</mn></mroot></math>
    </div>

    <!-- Test 4: insert an <mn> element at the second position in an <mroot>/<msqrt> element with various number of children. -->
    <div id="test4">Test 4:
      <math><msqrt><mn>1</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>3</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>3</mn><mn>4</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>3</mn><mn>4</mn><mn>5</mn></msqrt></math>
      <math><mroot><mn>1</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>3</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>3</mn><mn>4</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>3</mn><mn>4</mn><mn>5</mn></mroot></math>
    </div>

    <!-- Test 5: insert an <mn> element at the penultimate position in an <mroot>/<msqrt> element with various number of children. -->
    <div id="test5">Test 5:
      <math><msqrt><mn>2</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>3</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>2</mn><mn>4</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>2</mn><mn>3</mn><mn>5</mn></msqrt></math>
      <math><mroot><mn>2</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>3</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>2</mn><mn>4</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>2</mn><mn>3</mn><mn>5</mn></mroot></math>
    </div>

    <!-- Test 6: insert an <mn> element at the third position in an <mroot>/<msqrt> element with various number of children. -->
    <div id="test6">Test 6:
      <math><msqrt><mn>1</mn><mn>2</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>2</mn><mn>4</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>2</mn><mn>4</mn><mn>5</mn></msqrt></math>
      <math><mroot><mn>1</mn><mn>2</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>2</mn><mn>4</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>2</mn><mn>4</mn><mn>5</mn></mroot></math>
    </div>

    <!-- Test 7: insert an <mn> element at the antipenultimate position in an <mroot>/<msqrt> element with various number of children. -->
    <div id="test7">Test 7:
      <math><msqrt><mn>2</mn><mn>3</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>3</mn><mn>4</mn></msqrt></math>
      <math><msqrt><mn>1</mn><mn>2</mn><mn>4</mn><mn>5</mn></msqrt></math>
      <math><mroot><mn>2</mn><mn>3</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>3</mn><mn>4</mn></mroot></math>
      <math><mroot><mn>1</mn><mn>2</mn><mn>4</mn><mn>5</mn></mroot></math>
    </div>

  </body>
</html>
