Problem/Motivation

bs_lib_page_attachments() attaches drupalSettings.bs_lib.anchor_scroll
only when anchor_scroll.enable is set in bs_lib.settings. However, the
bs_lib/anchor_scroll library can also reach the page as a dependency of other
libraries (for example a theme TOC library that needs BSLib.getFixedElementsOffset()).
In that case js/anchor-scroll.js loads without its settings and throws:

  • window.BSLib.getFixedElementsOffset() reads
    drupalSettings.bs_lib.anchor_scroll unguarded and throws
    TypeError: Cannot read
      properties of undefined (reading 'anchor_scroll')

    for every caller.

  • Drupal.behaviors.BSLibAnchorScroll.attach() reads
    drupalSettings.bs_lib.anchor_scroll.exclude_links unguarded and throws on every
    behaviors attach, which can break other behaviors on the page.

Steps to reproduce

  1. Set anchor_scroll.enable: false in bs_lib.settings.
  2. Add bs_lib/anchor_scroll as a dependency to any custom library and attach it to a
    page.
  3. Load the page: Drupal.behaviors.BSLibAnchorScroll throws a TypeError
    on attach, and any call to BSLib.getFixedElementsOffset() throws as well.

Proposed resolution

Do not rely on the settings being present:

  • getFixedElementsOffset() returns 0 when
    drupalSettings.bs_lib.anchor_scroll is not available. This also keeps the
    location.hash auto-scroll path safe, since it calls through
    calculateScrollOffset().
  • Drupal.behaviors.BSLibAnchorScroll.attach() returns early when the settings are
    not available - without them there is no anchor scroll configuration, so there is nothing to
    attach.

Issue fork bs_lib-3610738

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pivica created an issue. See original summary.

  • pivica committed 44aefbb2 on 2.0.x
    fix: #3610738 anchor-scroll.js throws a TypeError when the anchor_scroll...

pivica’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.