mutationobservers.js (2232B)
1 // Compares a mutation record to a predefined one 2 // mutationToCheck is a mutation record from the user agent 3 // expectedRecord is a mutation record minted by the test 4 // for expectedRecord, if properties are omitted, they get default ones 5 function checkRecords(target, mutationToCheck, expectedRecord) { 6 var mr1; 7 var mr2; 8 9 10 function checkField(property, isArray) { 11 var field = mr2[property]; 12 if (isArray === undefined) { 13 isArray = false; 14 } 15 if (field instanceof Function) { 16 field = field(); 17 } else if (field === undefined) { 18 if (isArray) { 19 field = new Array(); 20 } else { 21 field = null; 22 } 23 } 24 if (isArray) { 25 assert_array_equals(mr1[property], field, property + " didn't match"); 26 } else { 27 assert_equals(mr1[property], field, property + " didn't match"); 28 } 29 } 30 31 assert_equals(mutationToCheck.length, expectedRecord.length, "mutation records must match"); 32 for (var item = 0; item < mutationToCheck.length; item++) { 33 mr1 = mutationToCheck[item]; 34 mr2 = expectedRecord[item]; 35 36 if (mr2.target instanceof Function) { 37 assert_equals(mr1.target, mr2.target(), "target node must match"); 38 } else if (mr2.target !== undefined) { 39 assert_equals(mr1.target, mr2.target, "target node must match"); 40 } else { 41 assert_equals(mr1.target, target, "target node must match"); 42 } 43 44 checkField("type"); 45 checkField("addedNodes", true); 46 checkField("removedNodes", true); 47 checkField("previousSibling"); 48 checkField("nextSibling"); 49 checkField("attributeName"); 50 checkField("attributeNamespace"); 51 checkField("oldValue"); 52 }; 53 } 54 55 function runMutationTest(node, mutationObserverOptions, mutationRecordSequence, mutationFunction, description, target) { 56 var test = async_test(description); 57 58 59 function moc(mrl, obs) { 60 test.step( 61 function () { 62 if (target === undefined) target = node; 63 checkRecords(target, mrl, mutationRecordSequence); 64 test.done(); 65 } 66 ); 67 } 68 69 test.step( 70 function () { 71 (new MutationObserver(moc)).observe(node, mutationObserverOptions); 72 mutationFunction(); 73 } 74 ); 75 return mutationRecordSequence.length 76 }