<!DOCTYPE html>
<html>
<body>
<p>This tests removing a focused element from a document and calling activeElement on a shadow tree in the same document.<br>
WebKit should clear the focused element even if the removed element was focused during removal and should not crash or hit an assertion.</p>
<script>

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

var iframe = document.createElement('iframe');
document.body.appendChild(iframe);

var doc = iframe.contentDocument;
var host = doc.createElement('div');
var shadowRoot = host.attachShadow({mode: 'closed'});
doc.body.appendChild(host);

var input = doc.createElement('input');
doc.body.appendChild(input);
input.focus();

input.addEventListener('DOMNodeRemovedFromDocument', function () {
    input.focus();
});

// 1. ContainerNode::removeChild
document.body.appendChild(input);
shadowRoot.activeElement;

// 2. ContainerNode::removeChildren
doc.body.appendChild(input);
input.focus();
doc.body.innerHTML = '';
document.body.appendChild(input);
shadowRoot.activeElement;


// 3. ContainerNode::removeChild for disconnecting frames
var focusableIframe = document.createElement('iframe');
doc.body.appendChild(host);
doc.body.appendChild(focusableIframe);
focusableIframe.focus();
focusableIframe.contentWindow.addEventListener('unload', function () {
    focusableIframe.focus();
});
document.body.appendChild(focusableIframe);
shadowRoot.activeElement;

// 4. ContainerNode::removeChildren for disconnecting frames
focusableIframe = document.createElement('iframe');
doc.body.appendChild(host);
doc.body.appendChild(focusableIframe);
focusableIframe.focus();
focusableIframe.contentWindow.addEventListener('unload', function () {
    focusableIframe.focus();
});
doc.body.innerHTML = '';
document.body.appendChild(focusableIframe);
shadowRoot.activeElement;

document.write('PASS - did not crash');

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