Mutation bug

by Isaac Nygaard

HTML

<!DOCTYPE html>
<div id="a">
	text
	<div id="b"></div>
</div>
<script>
	function process_records(records){
		for (const r of records){
			console.log(`added: ${r.addedNodes.length}; removed: ${r.removedNodes.length}`);
		}
	}
	const observer = new MutationObserver(process_records);
	const a = document.getElementById("a");
	const b = document.getElementById("b");
	const txt = b.previousSibling;
	observer.observe(a,  {
		subtree: true,
		childList: true,
		attributes: true,
		attributeOldValue: true,
		characterData: true, 
		characterDataOldValue: true,
	});
	b.replaceChildren(txt);
	process_records(observer.takeRecords());
	observer.disconnect();
</script>