Sync DOM to model after gap removal.

Also ensure a valid selection.