Fix querying detached nodes and document fragments by id

This PR should fix [issue #17699](https://bugs.dojotoolkit.org/ticket/17699).
Just out of curiosity: I was able to make this scenario work merely by changing this one line to use "root" instead of "(root.ownerDocument||root)", which is at least simpler than all the other changes here.  I don't know if that would introduce any other negative consequences though.  (But if you're supplying a context root, why would you ever want to search for ids outside of it?  That's the only thing I can think of that might break with that change.)