009.html (3197B)
1 <!DOCTYPE html> 2 <title>drag & drop - microdata for selection surrounding one item</title> 3 <style> 4 body > div { 5 height: 100px; 6 width: 200px; 7 background-color: fuchsia; 8 position: absolute; 9 top: 8px; 10 left: 8px; 11 } 12 span * { 13 display: none; 14 } 15 body > div + div { 16 background-color: navy; 17 top: 116px; 18 } 19 body > div + div + div { 20 background-color: orange; 21 top: 224px; 22 } 23 p:first-of-type { 24 margin-top: 350px; 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')[2]; 46 orange.childNodes[1].appendChild( makeEl('span',{itemprop:'foo'},'test') ); 47 orange.ondragstart = function(e) { 48 e.dataTransfer.effectAllowed = 'copy'; 49 e.dataTransfer.setData('Text', 'dummy text'); 50 var err; 51 if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) { 52 fails[fails.length] = e.type + ' ' + err; 53 } 54 }; 55 orange.previousSibling.ondragenter = orange.previousSibling.ondragleave = orange.previousSibling.ondragover = 56 orange.ondrag = orange.ondragend = function(e) { 57 if( e.type == 'dragover' || e.type == 'dragenter' ) { 58 e.preventDefault(); 59 e.dataTransfer.dropEffect = 'copy'; 60 } 61 if( e.dataTransfer.getData('application/microdata+json') ) { 62 fails[fails.length] = e.type + ' unexpectedly had microdata (security restriction)'; 63 } 64 }; 65 orange.previousSibling.ondrop = function(e) { 66 var err; 67 if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) { 68 fails[fails.length] = e.type + ' ' + err; 69 } 70 if( e.type != 'drop' ) { return; } 71 if( doneonce ) { return; } 72 doneonce = true; 73 setTimeout(function () { 74 document.getElementsByTagName('p')[0].innerHTML = fails.length ? ( 'FAIL: ' + fails.join('<br>') ) : 'PASS'; 75 fails = []; 76 }, 200 ); 77 }; 78 79 }; 80 function checkprops(md) { 81 var i; 82 if( !md ) { return 'no microdata'; } 83 md = JSON.parse(md); 84 if( !md.items || md.items.length != 1 ) { return 'no items'; } 85 if( !md.items[0].properties ) { return 'no properties'; } 86 if( !md.items[0].properties.foo ) { return 'no properties.foo'; } 87 if( md.items[0].properties.foo.length != 1 ) { return 'properties.foo length '+md.items[0].properties.foo.length+' instead of 1'; } 88 if( md.items[0].properties.foo[0] != orange.childNodes[1].properties.namedItem('foo').getValues()[0] ) { return 'properties.foo[0] <i>'+md.items[0].properties.foo[0]+'</i> instead of <i>'+orange.childNodes[1].properties.namedItem('foo').getValues()[0]+'</i>'; } 89 return ''; 90 } 91 92 </script> 93 94 <div></div><div></div><div>01<span itemscope>23</span>45<span>67</span>89</div> 95 96 <p>Use your pointing device to select the text substring "12345678" above, drag the selection upwards to the pink box, 97 then back to the blue box, and release it.</p> 98 <noscript><p>Enable JavaScript and reload</p></noscript>