Fix issue with ensureSameOwnerDocument when parent is a document

This fixes the failing tests for IE.