002.html (3780B)
1 <!DOCTYPE html> 2 <title>drag & drop - microdata with itemref</title> 3 <style> 4 body > div { 5 height: 200px; 6 width: 200px; 7 background-color: orange; 8 position: absolute; 9 top: 8px; 10 left: 8px; 11 } 12 body > div *, span { 13 display: none; 14 } 15 body > div + div { 16 background-color: navy; 17 left: 250px; 18 } 19 body > div + div + div { 20 background-color: fuchsia; 21 left: 500px; 22 } 23 p:first-of-type { 24 margin-top: 220px; 25 } 26 </style> 27 28 <script> 29 30 function makeEl(eltype,props,contents) { 31 var elem = document.createElement(eltype); 32 for( var i in props ) { 33 if( props.hasOwnProperty(i) ) { 34 elem.setAttribute(i,props[i]); 35 } 36 } 37 if( contents ) { 38 elem.innerHTML = contents; 39 } 40 return elem; 41 } 42 43 var orange, fails = [], doneonce = false; 44 window.onload = function() { 45 orange = document.getElementsByTagName('div')[0]; 46 47 orange.parentNode.insertBefore( makeEl('span',{itemprop:'foo',id:'id1'},'dummytext1 '), orange ); 48 orange.parentNode.appendChild( makeEl('span',{itemprop:'bar',id:'id2'},'dummytext2 ') ); 49 orange.parentNode.appendChild( makeEl('span',{itemprop:'foo',id:'id3'},'dummytext3 ') ); 50 51 orange.ondragstart = function(e) { 52 e.dataTransfer.effectAllowed = 'copy'; 53 e.dataTransfer.setData('Text', 'dummy text'); 54 var err; 55 if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) { 56 fails[fails.length] = e.type + ' ' + err; 57 } 58 }; 59 orange.nextSibling.ondragenter = orange.nextSibling.ondragleave = orange.nextSibling.ondragover = 60 orange.ondrag = orange.ondragend = function(e) { 61 if( e.type == 'dragover' || e.type == 'dragenter' ) { 62 e.preventDefault(); 63 e.dataTransfer.dropEffect = 'copy'; 64 } 65 if( e.dataTransfer.getData('application/microdata+json') ) { 66 fails[fails.length] = e.type + ' unexpectedly had microdata (security restriction)'; 67 } 68 }; 69 orange.nextSibling.ondrop = function(e) { 70 var err; 71 if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) { 72 fails[fails.length] = e.type + ' ' + err; 73 } 74 if( e.type != 'drop' ) { return; } 75 if( doneonce ) { return; } 76 doneonce = true; 77 setTimeout(function () { 78 document.getElementsByTagName('p')[0].innerHTML = fails.length ? ( 'FAIL: ' + fails.join('<br>') ) : 'PASS'; 79 fails = []; 80 }, 200 ); 81 }; 82 83 }; 84 function checkprops(md) { 85 var i; 86 if( !md ) { return 'no microdata'; } 87 md = JSON.parse(md); 88 if( !md.items || md.items.length != 1 ) { return 'no items'; } 89 if( !md.items[0].properties ) { return 'no properties'; } 90 if( !md.items[0].properties.foo ) { return 'no properties.foo'; } 91 if( !md.items[0].properties.bar ) { return 'no properties.bar'; } 92 if( md.items[0].properties.foo.length != 2 ) { return 'properties.foo length '+md.items[0].properties.foo.length+' instead of 2'; } 93 if( md.items[0].properties.bar.length != 1 ) { return 'properties.bar length '+md.items[0].properties.bar.length+' instead of 1'; } 94 for( i = 0; i < 2; i++ ) { 95 if( md.items[0].properties.foo[i] != orange.properties.namedItem('foo').getValues()[i] ) { return 'properties.foo['+i+'] <i>'+md.items[0].properties.foo[i]+'</i> instead of <i>'+orange.properties.namedItem('foo').getValues()[i]+'</i>'; } 96 } 97 if( md.items[0].properties.bar[0] != orange.properties.namedItem('bar').getValues()[0] ) { return 'properties.bar[0] <i>'+md.items[0].properties.bar[i]+'</i> instead of <i>'+orange.properties.namedItem('bar').getValues()[0]+'</i>'; } 98 return ''; 99 } 100 101 </script> 102 103 <div draggable='true' itemscope itemref='id3 id2 id1'></div><div></div><div></div> 104 105 <p>Use your pointing device to drag the orange box to the pink box, then back to the blue box, and release it.</p> 106 <noscript><p>Enable JavaScript and reload</p></noscript>