Merge similar elements after undoToRange. Improved hasSameClasses test.