Thanks for reporting that. I installed the first attached patch into Emacs master and am marking this bug as done. I am not sure whether a simpler patch should be installed into the emacs-26 branch, but the second attached patch should do the trick for that if needed, not that it's easy to test this sort of thing.