Fix handling ownerDocument, fix Range type test

node.ownerDocument can of course be null if and (I suppose!) only if the
node is itself the Document. Handle correctly.

To check if an object is a Range, test for .collapsed instead of
.commonAncestorContainer so we can support StaticRange too.
diff --git a/packages/dom/src/highlight-range.ts b/packages/dom/src/highlight-range.ts
index 7f76e40..e18a6a4 100644
--- a/packages/dom/src/highlight-range.ts
+++ b/packages/dom/src/highlight-range.ts
@@ -79,9 +79,8 @@
   }
 
   // Collect the text nodes.
-  if (!range.startContainer.ownerDocument)
-    throw new TypeError('range.startContainer lacks an ownerDocument');
-  const walker = range.startContainer.ownerDocument.createTreeWalker(
+  const document = range.startContainer.ownerDocument || range.startContainer as Document;
+  const walker = document.createTreeWalker(
     range.commonAncestorContainer,
     NodeFilter.SHOW_TEXT,
     {
@@ -116,13 +115,13 @@
 }
 
 // Replace [node] with <tagName ...attributes>[node]</tagName>
-function wrapNodeInHighlight(node: Node, tagName: string, attributes: Record<string, string>): HTMLElement {
-  if (!node.ownerDocument) throw new TypeError('Node to be highlighted lacks an ownerDocument');
-  const highlightElement = node.ownerDocument.createElement(tagName);
+function wrapNodeInHighlight(node: ChildNode, tagName: string, attributes: Record<string, string>): HTMLElement {
+  const document = node.ownerDocument as Document;
+  const highlightElement = document.createElement(tagName);
   Object.keys(attributes).forEach(key => {
     highlightElement.setAttribute(key, attributes[key]);
   });
-  const tempRange = node.ownerDocument.createRange();
+  const tempRange = document.createRange();
   tempRange.selectNode(node);
   tempRange.surroundContents(highlightElement);
   return highlightElement;
diff --git a/packages/dom/src/scope.ts b/packages/dom/src/scope.ts
index 24c9b26..e7aec59 100644
--- a/packages/dom/src/scope.ts
+++ b/packages/dom/src/scope.ts
@@ -21,25 +21,21 @@
 import { DomScope } from './types';
 
 export function ownerDocument(scope: DomScope): Document {
-  let document;
-  if ('commonAncestorContainer' in scope)
-    document = scope.commonAncestorContainer.ownerDocument;
-  else
-    document = scope.ownerDocument;
-  if (!document) throw new TypeError('scope lacks an ownerDocument');
-  return document;
+  const node = isRange(scope)
+    ? scope.commonAncestorContainer
+    : scope;
+  return node.ownerDocument || node as Document;
 }
 
 export function rangeFromScope(scope: DomScope): Range {
-  if ('commonAncestorContainer' in scope) {
+  if (isRange(scope)) {
     return scope;
   }
-
-  const document = scope.ownerDocument;
-  if (!document) throw new TypeError('scope lacks an ownerDocument');
-  const range = document.createRange();
-
+  const range = ownerDocument(scope).createRange();
   range.selectNodeContents(scope);
-
   return range;
 }
+
+function isRange(scope: DomScope): scope is Range {
+  return 'collapsed' in scope;
+}