Save and restore the selection when restoring the before and after widget markers. This prevents the caret from jumping around as you type in the vicinity of a marker. Now to figure out why trailing content can get lost...