What about when the dom element isn't document?  I tried to fix this case in #4151 -- it might be useful to compare these two pull requests.