<html>
<body>
<div id="test"><span id="span1">foo<span id="span2">bar</span></span><span id="span3">baz</span></div>
<div id="console"></div>
<script>
var s = window.getSelection();
var testDiv = document.getElementById("test");
var span1 = document.getElementById("span1");
var span2 = document.getElementById("span2");
var span3 = document.getElementById("span3");

function log(str) {
    var li = document.createElement("li");
    li.appendChild(document.createTextNode(str));
    document.getElementById("console").appendChild(li);
}

function shouldBe(expr, expected) {
    var actual = eval(expr);
    if (actual != expected)
        log("Failure: " + expr + " should be " + expected + ", was " + actual + ".");
    else
        log("Success: " + expr + " is " + expected + ".");
}

if (window.testRunner)
    testRunner.dumpAsText();

var r = document.createRange();

// select span2
r.setStart(span1, 1);
r.setEnd(span1, 2);
s.addRange(r);

shouldBe('s.containsNode(testDiv, false)', false);
shouldBe('s.containsNode(testDiv, true)', true);
shouldBe('s.containsNode(span1, false)', false);
shouldBe('s.containsNode(span1, true)', true);
shouldBe('s.containsNode(span2.firstChild, false)', true);
shouldBe('s.containsNode(span2.firstChild, true)', true);
shouldBe('s.containsNode(span3.firstChild, false)', false);
shouldBe('s.containsNode(span3.firstChild, true)', false);
shouldBe('s.containsNode(document, false)', false);
shouldBe('s.containsNode(document.body, true)', true);
shouldBe('s.containsNode(document.getElementById("console"), false)', false);
shouldBe('s.containsNode(document.getElementById("console"), true)', false);
shouldBe('s.containsNode(document.implementation.createDocumentType("name", "", ""), true)', false);

// partially select a text node
r.setStart(span2.firstChild, 1);
r.setEnd(span2.firstChild, 2);
s.removeAllRanges();
s.addRange(r);

shouldBe('s.containsNode(span2.firstChild, false)', false);
shouldBe('s.containsNode(span2.firstChild, true)', true);
shouldBe('s.containsNode(span2, false)', false);
shouldBe('s.containsNode(span2, true)', true);
shouldBe('s.containsNode(span1, false)', false);
shouldBe('s.containsNode(span1, true)', true);

// select several nodes
r.setStart(span1.firstChild, 1);
r.setEnd(span3.firstChild, 2);
s.removeAllRanges();
s.addRange(r);

shouldBe('s.containsNode(span2, false)', true);
shouldBe('s.containsNode(span2.firstChild, false)', true);
shouldBe('s.containsNode(span3, false)', false);
shouldBe('s.containsNode(span3, true)', true);

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