Skip to content

BUG: goto (scroll to selected element) broken when no-shadow enabled #82

@alifeee

Description

@alifeee

Working example:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script
      type="module"
      src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@2/dist/zero-md.min.js"
    ></script>
  </head>

  <body>
    <zero-md
      src="https://raw.githubusercontent.com/zerodevx/zero-md/master/README.md"
    ></zero-md>
  </body>
</html>

Broken example

The difference is that no-shadow is provided to the <zero-md> element.

<!DOCTYPE html>
<html lang="en">
  <head>
    <script
      type="module"
      src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@2/dist/zero-md.min.js"
    ></script>
  </head>

  <body>
    <zero-md
      src="https://raw.githubusercontent.com/zerodevx/zero-md/master/README.md"
      no-shadow
    ></zero-md>
  </body>
</html>

Expected behaviour:

go to https://URL#basic-usage. Page should scroll to that heading.

Actual behaviour

console error:

Uncaught TypeError: this.root.getElementById is not a function
    goto index.js:153
    t index.js:66
    setTimeout handler*t/< index.js:66
    promise callback*t index.js:66
    <anonymous> index.js:330
index.js:153:27

Environment

I am using Firefox.

Code

The problem is with the goto function.

zero-md/src/index.js

Lines 151 to 158 in 5a627f2

goto(id) {
if (id) {
const el = this.root.getElementById(id.substring(1))
if (el) {
el.scrollIntoView()
}
}
}

this.root does not have getElementById if no-shadow is true. this.root is set in the constructor() function:

this.root = this.hasAttribute('no-shadow') ? this : this.attachShadow({ mode: 'open' })

The issue appears to be because getElementById is only defined on the document object. See MDN web docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions