Use contains instead of compareDocumentPosition