• Robert Knight's avatar
    Fix range selector anchoring in XHTML documents · 39a806fc
    Robert Knight authored
    In HTML documents that have been served with an XML mime type,
    `document.evaluate` handles element names differently. In HTML documents
    the XPath segments do not require prefixes on element names. In XHTML
    documents however they do. Since the client always generates the same
    un-prefixed XPaths regardless of document type, evaluation always
    failed. There was a fallback path but it was only executed if
    `document.evaluate` threw an error, not if it returned `null` as in this
    case.
    
    This commit resolves the issue by first attempting to evaluate the XPath
    using custom logic that only handles simple XPaths and then using
    `document.evaluate` only for more complex XPaths. The _simple XPath_
    logic behaves the same in HTML and XML documents and ignores namespaces.
    As a result it works with the XPaths that the client generates
    regardless of document type and also regardless of whether the XPath
    references only HTML elements or elements from other namespaces (eg.
    MathML or SVG).
    
    We could change the way that the client generates XPaths in future in
    XML documents or SVG/MathML content within HTML documents, but the
    client would still need to handle `RangeSelector` selectors on existing
    annotations.
    
    For more details, see https://github.com/hypothesis/client/pull/2590#issuecomment-702132563
    
    Fixes #2592
    39a806fc
xpath.js 2.88 KB