Source code

Revision control

Copy as Markdown

Other Tools

<!DOCTYPE html>
<title>drag &amp; drop - microdata for selection surrounding nested property items</title>
<style>
body > div {
height: 100px;
width: 200px;
background-color: fuchsia;
position: absolute;
top: 8px;
left: 8px;
}
span * {
display: none;
}
body > div + div {
background-color: navy;
top: 116px;
}
body > div + div + div {
background-color: orange;
top: 224px;
}
p:first-of-type {
margin-top: 350px;
}
</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')[2];
orange.childNodes[1].appendChild( makeEl('span',{itemscope:'itemscope',itemprop:'foo'},'test') );
orange.childNodes[1].lastChild.appendChild( makeEl('span',{itemprop:'bar'},'test') );
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.previousSibling.ondragenter = orange.previousSibling.ondragleave = orange.previousSibling.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.previousSibling.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) {
//items should be represented at the top level only if they do not have the itemprop attribute
var i;
if( !md ) { return 'no microdata'; }
md = JSON.parse(md);
if( !md.items ) { return 'no items'; }
if( md.items.length != 1 ) { return md.items.length+' items instead of 1'; }
if( !md.items[0].properties ) { return 'no items[0].properties'; }
if( !md.items[0].properties.foo ) { return 'no items[0].properties.foo'; }
if( md.items[0].properties.foo.length != 1 ) { return 'items[0].properties.foo length '+md.items[0].properties.foo.length+' instead of 1'; }
if( !md.items[0].properties.foo[0].properties ) { return 'items[1].properties.foo[0].properties <i>'+md.items[0].properties.foo[0].properties+'</i> instead of <i>{bar:[test]}</i>'; }
if( !md.items[0].properties.foo[0].properties.bar ) { return 'items[1].properties.foo[0].properties.bar <i>'+md.items[0].properties.foo[0].properties.bar+'</i> instead of <i>[test]</i>'; }
if( md.items[0].properties.foo[0].properties.bar.length != 1 ) { return 'items[1].properties.foo[0].properties.bar.length <i>'+md.items[0].properties.foo[0].properties.bar.length+'</i> instead of 1'; }
if( md.items[0].properties.foo[0].properties.bar[0] != 'test') { return 'items[1].properties.foo[0].properties.bar[0] <i>'+md.items[0].properties.foo[0].properties.bar[0]+'</i> instead of <i>test</i>'; }
return '';
}
</script>
<div></div><div></div><div>01<span itemscope>23</span>45<span itemscope itemprop="baz">67</span>89</div>
<p>Use your pointing device to select the text substring "12345678" above, drag the selection upwards to the pink box,
then back to the blue box, and release it.</p>
<noscript><p>Enable JavaScript and reload</p></noscript>