Thanks for the bug report. Please try the attached, more-general patch, which I've installed on master. I suppose I should backport it to the emacs-26 branch if it works for you.