Source code
Revision control
Copy as Markdown
Other Tools
<?xml version="1.0" encoding="utf-8"?>
<head>
<title>dataTransfer.items during block element drag and drop</title>
<style type="text/css">
div
{height:100px;
width:100px;
padding:20px;
background-color:green;}
p + div
{background-color:gray;}
</style>
<script type="application/ecmascript">
<![CDATA[
var dataTypes = ['text/plain', 'text/uri-list', 'application/xml', 'application/xhtml+xml', 'application/mathml+xml', 'image/svg+xml', 'text/html', 'text/x-example'],
data = ['PASS', 'data:text/plain,1', '<result>PASS</result>', '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Data store item</title></head><body><p>PASS</p></body></html>', '<math xmlns="http://www.w3.org/1998/Math/MathML"><mn>1</mn></math>', '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100px" height="50px" viewBox="0 0 100 50"><text x="0" y="40" font-size="40" fill="green">PASS</text></svg>', '<!DOCTYPE html><html><head><title>Data store item</title></head><body><p>PASS</p></body></html>', 'PASS'],
e = 0, result = true;
function start(event)
{event.dataTransfer.effectAllowed = 'copy';
for(var i = 0; i != dataTypes.length; i++)
{event.dataTransfer.setData(dataTypes[i], data[i]);}
for(var i = event.dataTransfer.items.length; i != 0; i--)
{if(dataTypes.indexOf(event.dataTransfer.items[i-1].type) == -1)
{delete event.dataTransfer.items[i-1]}
}
if(event.dataTransfer.items.length != dataTypes.length)
{say('items.length (ondragstart) : FAIL (items.length should be' + dataTypes.length + ')')}
for(var i = 0; i != event.dataTransfer.items.length; i++)
{if(event.dataTransfer.items[i].kind != 'string')
{say('Item kind (ondragstart): FAIL (items[' + i + '].kind should be string)')}
if(event.dataTransfer.items[i].type != dataTypes[i])
{say('Item type (ondragstart): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
event.dataTransfer.items[i].getAsString(
function ()
{if(arguments[0] != data[e++])
{say('getAsString : FAIL (items[' + i + '].getAsString should pass' + data[e] + ')')}
}
);
}
}
function dragElement(event)
{event.dataTransfer.items.clear();
if(event.dataTransfer.items.length != dataTypes.length)
{say('items.length (ondrag) : FAIL (items.length should be' + dataTypes.length + ')')}
for(var i = 0; i != event.dataTransfer.items.length; i++)
{if(event.dataTransfer.items[i].kind != 'string')
{say('Item kind (ondrag): FAIL (items[' + i + '].kind should be string)')}
if(event.dataTransfer.items[i].type != dataTypes[i])
{say('Item type (ondrag): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
event.dataTransfer.items[i].getAsString(
function ()
{if(arguments[0])
{say('getAsString : FAIL (items[' + i + '].getAsString should not reveal data to drag event handler)')}
}
);
}
}
function enterElement(event)
{event.preventDefault();
event.dataTransfer.items.clear();
if(event.dataTransfer.items.length != dataTypes.length)
{say('items.length (ondragenter) : FAIL (items.length should be' + dataTypes.length + ')')}
for(var i = 0; i != event.dataTransfer.items.length; i++)
{delete event.dataTransfer.items[i];
if(event.dataTransfer.items[i].kind != 'string')
{say('Item kind (ondragenter): FAIL (items[' + i + '].kind should be string)')}
if(event.dataTransfer.items[i].type != dataTypes[i])
{say('Item type (ondragenter): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
event.dataTransfer.items[i].getAsString(
function ()
{if(arguments[0])
{say('getAsString : FAIL (items[' + i + '].getAsString should not reveal data to drag event handler)')}
}
);
}
}
function overElement(event)
{event.preventDefault();
event.dataTransfer.items.clear();
if(event.dataTransfer.items.length != dataTypes.length)
{say('items.length (ondragover) : FAIL (items.length should be' + dataTypes.length + ')')}
for(var i = 0; i != event.dataTransfer.items.length; i++)
{if(event.dataTransfer.items[i].kind != 'string')
{say('Item kind (ondragover): FAIL (items[' + i + '].kind should be string)')}
if(event.dataTransfer.items[i].type != dataTypes[i])
{say('Item type (ondragover): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
event.dataTransfer.items[i].getAsString(
function ()
{if(arguments[0])
{say('getAsString : FAIL (items[' + i + '].getAsString should not reveal data to dragover event handler)')}
}
);
}
}
function dataDrop(event)
{e = 0;
event.dataTransfer.items.clear();
if(event.dataTransfer.items.length != dataTypes.length)
{say('items.length (ondrop) : FAIL (items.length should be' + dataTypes.length + ')')}
for(var i = 0; i != event.dataTransfer.items.length; i++)
{delete event.dataTransfer.items[i];
if(event.dataTransfer.items[i].kind != 'string')
{say('Item kind (ondrop): FAIL (items[' + i + '].kind should be string)')}
if(event.dataTransfer.items[i].type != dataTypes[i])
{say('Item type (ondrop): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
event.dataTransfer.items[i].getAsString(
function ()
{if(arguments[0] != data[e++])
{say('getAsString : FAIL (items[' + i + '].getAsString should pass' + data[e] + ')')}
}
);
}
document.querySelector('p + div').setAttribute('style','background-color:' + (result?'green':'red'));}
function say(it)
{result = false;
document.querySelector('pre').appendChild(document.createTextNode(it + '\n'));}
]]>
</script>
</head>
<body>
<div draggable="true" ondragstart="start(event)" ondrag="dragElement(event)"/>
<p>Drag green box above to the gray box below and drop it. Gray box should turn green.</p>
<div ondragenter="enterElement(event)" ondragover="overElement(event)" ondrop="dataDrop(event)"/>
<pre/>
</body>
</html>