Source code

Revision control

Copy as Markdown

Other Tools

<!DOCTYPE html>
<title>drag &amp; drop - microdata with parent itemref loop</title>
<style>
blockquote > div {
height: 200px;
width: 200px;
background-color: orange;
position: absolute;
top: 8px;
left: 8px;
}
blockquote > div *, span {
display: none;
}
blockquote > div + div {
background-color: navy;
left: 250px;
}
blockquote > div + div + div {
background-color: fuchsia;
left: 500px;
}
p:first-of-type {
margin-top: 220px;
}
</style>
<script>
function makeEl(eltype,props,contents) {
var elem = document.createElement(eltype);
for( var i in props ) {
if( props.hasOwnProperty(i) ) {
elem.setAttribute(i,props[i]);
}
}
if( contents ) {
elem.innerHTML = contents;
}
return elem;
}
var orange, fails = [], doneonce = false;
window.onload = function() {
orange = document.getElementsByTagName('div')[0];
orange.ondragstart = function(e) {
e.dataTransfer.effectAllowed = 'copy';
e.dataTransfer.setData('Text', 'dummy text');
var err;
if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) {
fails[fails.length] = e.type + ' ' + err;
}
};
orange.nextSibling.ondragenter = orange.nextSibling.ondragleave = orange.nextSibling.ondragover =
orange.ondrag = orange.ondragend = function(e) {
if( e.type == 'dragover' || e.type == 'dragenter' ) {
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}
if( e.dataTransfer.getData('application/microdata+json') ) {
fails[fails.length] = e.type + ' unexpectedly had microdata (security restriction)';
}
};
orange.nextSibling.ondrop = function(e) {
var err;
if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) {
fails[fails.length] = e.type + ' ' + err;
}
if( e.type != 'drop' ) { return; }
if( doneonce ) { return; }
doneonce = true;
setTimeout(function () {
document.getElementsByTagName('p')[0].innerHTML = fails.length ? ( 'FAIL: ' + fails.join('<br>') ) : 'PASS';
fails = [];
}, 200 );
};
};
function checkprops(md) {
//"check if the element is a top-level microdata item, and if it is"
//as it has itemprop, it is not a top-level item, and should be ignored
var i;
if( !md ) { return 'no microdata'; }
md = JSON.parse(md);
if( !md.items ) { return 'no items'; }
if( md.items.length != 0 ) { return md.items.length+' items instead of 0'; }
return '';
}
</script>
<blockquote id='id1' itemprop='bar' itemscope>
<div draggable='true' itemscope itemprop='foo' itemref='id1'></div><div></div><div></div>
</blockquote>
<p>Use your pointing device to drag the orange box to the pink box, then back to the blue box, and release it.</p>
<noscript><p>Enable JavaScript and reload</p></noscript>