Source code
Revision control
Copy as Markdown
Other Tools
<!doctype html>
<html>
<head>
<title>Drag and drop passing over body with cancelling dragenter</title>
<style type="text/css">
div:first-child {
height: 100px;
width: 100px;
background: orange;
display: inline-block;
}
div:first-child + div {
height: 100px;
width: 100px;
margin-left: 200px;
background: blue;
display: inline-block;
}
</style>
<script type="text/javascript">
//this test enforces the following spec statement:
//"if this immediate user selection is not the same as the current target element"
window.onload = function () {
var drag = document.getElementsByTagName('div')[0], sequence = [];
drag.ondragstart = function (e) {
e.dataTransfer.setData('text','hello');
e.dataTransfer.effectAllowed = 'copy';
};
drag.ondragenter = function (e) {
sequence[sequence.length] = 'drag.dragenter';
};
drag.ondragover = function (e) {
if( sequence[sequence.length-1] != 'drag.dragover' ) {
sequence[sequence.length] = 'drag.dragover';
}
};
var drop = document.getElementsByTagName('div')[1];
drop.ondragenter = function (e) {
e.preventDefault();
sequence[sequence.length] = 'drop.dragenter';
};
drop.ondragover = function (e) {
e.preventDefault();
if( sequence[sequence.length-1] != 'drop.dragover' ) {
sequence[sequence.length] = 'drop.dragover';
}
};
drop.ondrop = function (e) {
e.preventDefault();
sequence[sequence.length] = 'drop.ondrop';
};
document.body.ondragenter = function (e) {
sequence[sequence.length] = ( e.target == this ) ? 'body.dragenter' : 'bubble.dragenter';
if( e.target != this ) { return; }
e.preventDefault(); //don't cancel when it bubbles from the child elements
};
document.body.ondragover = function (e) {
if( e.target != this ) { return; }
if( sequence[sequence.length-1] != 'body.dragover' ) {
sequence[sequence.length] = 'body.dragover';
}
};
drag.ondragend = function (e) {
sequence = sequence.join('=>')
var desiredsequence = (['drag.dragenter','bubble.dragenter','body.dragenter','body.dragover','drop.dragenter','bubble.dragenter','drop.dragover','drop.ondrop']).join('=>')
if( sequence == desiredsequence ) {
document.getElementsByTagName('div')[2].innerHTML = 'PASS';
} else {
document.getElementsByTagName('div')[2].innerHTML = 'FAIL, got:<br>'+sequence+'<br>instead of:<br>'+desiredsequence;
}
};
};
</script>
</head>
<body>
<div draggable="true"></div><div></div>
<div> </div>
<p>Drag the orange square onto the blue square and release it.</p>
<noscript><p>Enable JavaScript and reload</p></noscript>
</body>
</html>