Attached is a patch to fix this based on the discussion in bug#52237.
Thanks for the patch.
One slightly odd thing is that for context menu functions that put their items
at the top, they place their separator *below* the items. Other functions
place the separator *above* the items.
There is some logic in this: when the menu doesn't contain a separator
at the top, then it is necessary to add a separator below the added items,
e.g. by modifying such a menu:
- existing menu item 1
- existing menu item 2
to
- new item 1
- new item 2
- new separator
- existing menu item 1
- existing menu item 2