012.html (4046B)
1 <!DOCTYPE html> 2 <title>drag & drop - microdata for selection surrounding nested property items</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',{itemscope:'itemscope',itemprop:'foo'},'test') ); 47 orange.childNodes[1].lastChild.appendChild( makeEl('span',{itemprop:'bar'},'test') ); 48 orange.ondragstart = function(e) { 49 e.dataTransfer.effectAllowed = 'copy'; 50 e.dataTransfer.setData('Text', 'dummy text'); 51 var err; 52 if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) { 53 fails[fails.length] = e.type + ' ' + err; 54 } 55 }; 56 orange.previousSibling.ondragenter = orange.previousSibling.ondragleave = orange.previousSibling.ondragover = 57 orange.ondrag = orange.ondragend = function(e) { 58 if( e.type == 'dragover' || e.type == 'dragenter' ) { 59 e.preventDefault(); 60 e.dataTransfer.dropEffect = 'copy'; 61 } 62 if( e.dataTransfer.getData('application/microdata+json') ) { 63 fails[fails.length] = e.type + ' unexpectedly had microdata (security restriction)'; 64 } 65 }; 66 orange.previousSibling.ondrop = function(e) { 67 var err; 68 if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) { 69 fails[fails.length] = e.type + ' ' + err; 70 } 71 if( e.type != 'drop' ) { return; } 72 if( doneonce ) { return; } 73 doneonce = true; 74 setTimeout(function () { 75 document.getElementsByTagName('p')[0].innerHTML = fails.length ? ( 'FAIL: ' + fails.join('<br>') ) : 'PASS'; 76 fails = []; 77 }, 200 ); 78 }; 79 80 }; 81 function checkprops(md) { 82 //items should be represented at the top level only if they do not have the itemprop attribute 83 var i; 84 if( !md ) { return 'no microdata'; } 85 md = JSON.parse(md); 86 if( !md.items ) { return 'no items'; } 87 if( md.items.length != 1 ) { return md.items.length+' items instead of 1'; } 88 if( !md.items[0].properties ) { return 'no items[0].properties'; } 89 if( !md.items[0].properties.foo ) { return 'no items[0].properties.foo'; } 90 if( md.items[0].properties.foo.length != 1 ) { return 'items[0].properties.foo length '+md.items[0].properties.foo.length+' instead of 1'; } 91 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>'; } 92 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>'; } 93 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'; } 94 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>'; } 95 return ''; 96 } 97 98 </script> 99 100 <div></div><div></div><div>01<span itemscope>23</span>45<span itemscope itemprop="baz">67</span>89</div> 101 102 <p>Use your pointing device to select the text substring "12345678" above, drag the selection upwards to the pink box, 103 then back to the blue box, and release it.</p> 104 <noscript><p>Enable JavaScript and reload</p></noscript>