<!DOCTYPE HTML>
<html>
<head>
 <title>DOM Traversal: NodeIterator: Mutations (3/x)</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<p id="display"></p>
<div id="content" style="display: none">
  <span id=root><span id=B></span><span id=C></span><span id=D></span><span id=E><span id=E1><span id=E11></span></span></span></span>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
  function removeNode(n) {
    n.remove();
  }
  var initInner = $('content').innerHTML;
  var content = $('content');
  

  function resetContent() {
    content.innerHTML = initInner;
    var checkIt = document.createNodeIterator(content, NodeFilter.SHOW_ELEMENT,
					      testNodeFilter);
    var node;
    while ((node = checkIt.nextNode()) != null) {
      if (node.id) {
        window[node.id] = node;
      }
    }
  }

  function makeSpan(id) {
    var e = document.createElement('span');
    e.id = id;
    return e;
  }

  function testNodeFilter(n) {
    if (n.tagName == 'SPAN')
      return NodeFilter.FILTER_ACCEPT;
    return NodeFilter.FILTER_SKIP;
  }

  function checkseq(it, root, expect) {
    var checkIt = document.createNodeIterator(root, NodeFilter.SHOW_ELEMENT,
					      testNodeFilter);
    var printedPointer = (it.referenceNode == undefined);
    var string = '';
    var node;
    while ((node = checkIt.nextNode()) != null) {
      if (!printedPointer && it.referenceNode == node) {
        printedPointer = true;
	var s = '[' + node.id + '] ';
          if (it.pointerBeforeReferenceNode)
            string += "* " + s;
          else
            string += s + "* ";
      } else {
        string += node.id + " ";
      }
    }
    is(string.slice(0, -1), expect, "sequence check");
  }

  resetContent();
  var it = document.createNodeIterator(E, NodeFilter.SHOW_ELEMENT,
                                       testNodeFilter);
  checkseq(it, root, "root B C D * [E] E1 E11");

  removeNode(C);
  checkseq(it, root, "root B D * [E] E1 E11");

  it.nextNode();
  removeNode(D);
  checkseq(it, root, "root B [E] * E1 E11");

  it.nextNode();
  removeNode(B);
  checkseq(it, root, "root E [E1] * E11");

  it.nextNode();
  checkseq(it, root, "root E E1 [E11] *");

  it.nextNode();
  checkseq(it, root, "root E E1 [E11] *");

  it.previousNode();
  it.previousNode();
  it.previousNode();
  it.previousNode();
  it.previousNode();
  checkseq(it, root, "root * [E] E1 E11");

  resetContent();
  it = document.createNodeIterator(E, NodeFilter.SHOW_ELEMENT,
                                   testNodeFilter);
  checkseq(it, root, "root B C D * [E] E1 E11");

  it.nextNode();
  it.nextNode();
  checkseq(it, root, "root B C D E [E1] * E11");

  it.previousNode();
  it.previousNode();
  checkseq(it, root, "root B C D * [E] E1 E11");

  removeNode(D);
  removeNode(B);
  checkseq(it, root, "root C * [E] E1 E11");

  n = makeSpan('n');
  root.insertBefore(n, E);
  checkseq(it, root, "root C n * [E] E1 E11");

  n2 = makeSpan('n2');
  root.insertBefore(n2, C);
  checkseq(it, root, "root n2 C n * [E] E1 E11");

  resetContent();
  it = document.createNodeIterator(E, NodeFilter.SHOW_ELEMENT,
                                   testNodeFilter);
  checkseq(it, root, "root B C D * [E] E1 E11");

  removeNode(root);
  checkseq(it, root, "root B C D * [E] E1 E11");

  removeNode(B);
  checkseq(it, root, "root C D * [E] E1 E11");

  removeNode(D);
  checkseq(it, root, "root C * [E] E1 E11");

  it.nextNode();
  it.nextNode();
  it.nextNode();
  checkseq(it, root, "root C E E1 [E11] *");

  removeNode(E1);
  checkseq(it, root, "root C [E] *");
  
  n = makeSpan('n');
  root.insertBefore(n, E);
  checkseq(it, root, "root C n [E] *");
  
  n2 = makeSpan('n2');
  E.appendChild(n2);
  checkseq(it, root, "root C n [E] * n2");
  
  it.nextNode();
  checkseq(it, root, "root C n E [n2] *");

  removeNode(E);
  checkseq(it, root, "root C n");

  
</script>
</pre>
</body>
</html>
