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