summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/forms/select-set-length-with-mutation-reorder.html
blob: 441168f6dfe0a70260f927cac8d962407b5d3682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../fast/js/resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description('Tests that setting the .length on an HTMLSelectElement works in the presence of DOM mutation listeners that reorder option elements');

var wrapper = document.createElement('div');
document.body.appendChild(wrapper);
wrapper.innerHTML = '<select id="theSelect">' +
                    '<option id="a">a</option>' +
                    '<option id="b">b</option>' +
                    '<option id="c">c</option>' +
                    '<option id="d">d</option>' +
                    '</select>';

var sel = document.getElementById('theSelect');

var firstRemove = true;
function onRemove(e) {
    if (firstRemove) {
        // remove listener temporarily to avoid lots of nesting
        sel.removeEventListener('DOMNodeRemoved', onRemove, false);
        var lastOption = document.getElementById('d');
        sel.removeChild(lastOption);
        sel.insertBefore(lastOption, document.getElementById('c'));
        firstRemove = false;
        sel.addEventListener('DOMNodeRemoved', onRemove, false);
    }
}

sel.addEventListener('DOMNodeRemoved', onRemove, false);
sel.addEventListener('DOMNodeInserted', function() {}, false);

shouldBe('sel.length', '4');
sel.length = 2;
shouldBe('sel.length', '2');
shouldBe('sel.options.item(0).id', '"a"');
shouldBe('sel.options.item(1).id', '"b"');
</script>
<script src="../../fast/js/resources/js-test-post.js"></script>
</body>
</html>