006.html (4155B)
1 <!DOCTYPE html> 2 <title>drag & drop - microdata with type and id</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 * { 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.appendChild( makeEl('div',{itemprop:'foo',itemscope:'itemscope',itemtype:'http://example.com/',id:'id2',itemid:'http://example.com/bar'},'') ); 48 49 orange.ondragstart = function(e) { 50 e.dataTransfer.effectAllowed = 'copy'; 51 e.dataTransfer.setData('Text', 'dummy text'); 52 var err; 53 if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) { 54 fails[fails.length] = e.type + ' ' + err; 55 } 56 }; 57 orange.nextSibling.ondragenter = orange.nextSibling.ondragleave = orange.nextSibling.ondragover = 58 orange.ondrag = orange.ondragend = function(e) { 59 if( e.type == 'dragover' || e.type == 'dragenter' ) { 60 e.preventDefault(); 61 e.dataTransfer.dropEffect = 'copy'; 62 } 63 if( e.dataTransfer.getData('application/microdata+json') ) { 64 fails[fails.length] = e.type + ' unexpectedly had microdata (security restriction)'; 65 } 66 }; 67 orange.nextSibling.ondrop = function(e) { 68 var err; 69 if( err = checkprops(e.dataTransfer.getData('application/microdata+json')) ) { 70 fails[fails.length] = e.type + ' ' + err; 71 } 72 if( e.type != 'drop' ) { return; } 73 if( doneonce ) { return; } 74 doneonce = true; 75 setTimeout(function () { 76 document.getElementsByTagName('p')[0].innerHTML = fails.length ? ( 'FAIL: ' + fails.join('<br>') ) : 'PASS'; 77 fails = []; 78 }, 200 ); 79 }; 80 81 }; 82 function checkprops(md) { 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].type != orange.getAttribute('itemtype') ) { return 'items[0].type <i>'+md.items[0].type+'</i> instead of <i>'+orange.getAttribute('itemtype')+'</i>'; } 89 if( md.items[0].id != orange.getAttribute('itemid') ) { return 'items[0].id <i>'+md.items[0].id+'</i> instead of <i>'+orange.getAttribute('itemid')+'</i>'; } 90 if( !md.items[0].properties ) { return 'no properties'; } 91 if( !md.items[0].properties.foo ) { return 'no properties.foo'; } 92 if( md.items[0].properties.foo.length != 1 ) { return 'properties.foo length '+md.items[0].properties.foo.length+' instead of 1'; } 93 if( !md.items[0].properties.foo[0] ) { return 'properties.foo[0] <i>'+md.items[0].properties.foo[0]+'</i> instead of <i>{...}</i>'; } 94 if( md.items[0].properties.foo[0].type != orange.firstChild.getAttribute('itemtype') ) { return 'items[0].properties.foo[0].type <i>'+md.items[0].properties.foo[0].type+'</i> instead of <i>'+orange.firstChild.getAttribute('itemtype')+'</i>'; } 95 if( md.items[0].properties.foo[0].id != orange.firstChild.getAttribute('itemid') ) { return 'items[0].properties.foo[0].id <i>'+md.items[0].properties.foo[0].id+'</i> instead of <i>'+orange.firstChild.getAttribute('itemid')+'</i>'; } 96 if( !md.items[0].properties.foo[0].properties ) { return 'properties.foo[0].properties <i>'+md.items[0].properties.foo[0].properties+'</i> instead of <i>{}</i>'; } 97 return ''; 98 } 99 100 </script> 101 102 <div draggable='true' itemscope id='id1' itemtype='http://example.org/' itemid='http://example.org/foo'></div><div></div><div></div> 103 104 <p>Use your pointing device to drag the orange box to the pink box, then back to the blue box, and release it.</p> 105 <noscript><p>Enable JavaScript and reload</p></noscript>