<iframe src="empty.html" id="selectors" width=0 height=0 frameborder=0></iframe>
<p>The test below should report no failures, and should say PASS at the end.</p>
<script>
if (window.testRunner) {
    testRunner.dumpAsText();
}
</script>
<script>

  function getTestDocument() {
    var iframe = document.getElementById("selectors");
    var doc = iframe.contentDocument;
    for (var i = doc.documentElement.childNodes.length-1; i >= 0; i -= 1)
      doc.documentElement.removeChild(doc.documentElement.childNodes[i]);
    doc.documentElement.appendChild(doc.createElement('head'));
    doc.documentElement.firstChild.appendChild(doc.createElement('title'));
    doc.documentElement.appendChild(doc.createElement('body'));
    return doc;
  }

var failCount = 0;

  function fail(message) {
    document.write(message.replace("&", "&amp;").replace("<", "&lt;") + "<br>");
    ++failCount;
  }

  function assert(condition, message) {
    if (!condition)
      fail(message);
  }

  function assertEquals(expression, value, message) {
    if (expression != value) {
      expression = (""+expression).replace(/[\r\n]+/g, "\\n");
      value = (""+value).replace(/\r?\n/g, "\\n");
      fail("expected '" + value + "' but got '" + expression + "' - " + message);
    }
  }

      // test 11: Ranges and Comments
      var msg;
      var doc = getTestDocument();
      var c1 = doc.createComment("11111");
      doc.appendChild(c1);
      var r = doc.createRange();
      r.selectNode(c1);
      msg = 'wrong exception raised';
      try {
        r.surroundContents(doc.createElement('a'));
        msg = 'no exception raised';
      } catch (e) {
        if ('code' in e)
          msg += '; code = ' + e.code;
        if (e.code == 3) // HIERARCHY_REQUEST_ERR
          msg = '';
      }
      assert(msg == '', "when inserting <a> into Document with another child: " + msg);
      var c2 = doc.createComment("22222");
      doc.body.appendChild(c2);
      var c3 = doc.createComment("33333");
      doc.body.appendChild(c3);
      r.setStart(c2, 2);
      r.setEnd(c3, 3);
      var msg = 'wrong exception raised';
      try {
        r.surroundContents(doc.createElement('a'));
        msg = 'no exception raised';
      } catch (e) {
// COMMENTED OUT FOR 2011 UPDATE - DOM Core changes the exception from RangeException.BAD_BOUNDARYPOINTS_ERR (1) to DOMException.INVALID_STATE_ERR (11)
//        if ('code' in e)
//          msg += '; code = ' + e.code;
//        if (e.code == 1)
          msg = '';
      }
      assert(msg == '', "when trying to surround two halves of comment: " + msg);
      assertEquals(r.toString(), "", "comments returned text");

if (failCount == 0)
  document.write("PASS<br>");
</script>

