Skip to content

catchNavigation click causes error when using hash for routing #21

@chrishoage

Description

@chrishoage

// Attempt to match the selector from that root. querySelector' doesn't support IDs that start with a digit, so work around that limitation
const elementMatch = hash.match(ID_WITH_LEADING_DIGIT_REGEXP) != null ? root.getElementById(hash.slice(1)) : root.querySelector(hash);

hash from an anchor is passed directly to root.querySelector with our regard for what might be contained in the hash.

Hash-routing libraries store the route in the hash. For example an anchor tag may have #!/foo/bar as the value for the href.

Passing this value to root.querySelector causes an error, for example SyntaxError: The string did not match the expected pattern. in safari or Uncaught DOMException: Document.querySelector: '#!/foo' is not a valid selector in FIrefox.

This could be solved in two ways:

  1. a simple try/catch
  2. further expand the regex to allow for detecting a valid query selector string before passing it to querySelector

@wessberg if you have a preference between the two approaches (or have an alternative suggestion) I am happy to open a PR that fixes the bad assumption around what can live in a hash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions