Anyone working with code in the WordPress TinyMCE WYSIWYG editor will have had their code blown away when switching between “Text” (HTML) and Visual editing modes.
One of the many uses of Advanced Custom Fields plugin is to create a text area to stash code associated with a page or post that the editor can’t touch.
Using: https://www.advancedcustomfields.com/
It’s a similar idea to using the plugin to add custom JavaScript or CSS to a page/post, except the field isn’t exposed in the theme.
This serves to only store the code alongside the post/page in the database.
This will not prevent the editor from modifying the code again in the future if it is pasted back into the editor, but it is a convenient place to copy the code from again.
If you want a more permanent solution you could disable the visual editor, remove the filtering, or other options.