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_scrollunguarded and throwsTypeError: Cannot read properties of undefined (reading 'anchor_scroll')for every caller.
Drupal.behaviors.BSLibAnchorScroll.attach()reads
drupalSettings.bs_lib.anchor_scroll.exclude_linksunguarded and throws on every
behaviors attach, which can break other behaviors on the page.
Steps to reproduce
- Set
anchor_scroll.enable: falseinbs_lib.settings. - Add
bs_lib/anchor_scrollas a dependency to any custom library and attach it to a
page. - Load the page:
Drupal.behaviors.BSLibAnchorScrollthrows aTypeError
on attach, and any call toBSLib.getFixedElementsOffset()throws as well.
Proposed resolution
Do not rely on the settings being present:
getFixedElementsOffset()returns0when
drupalSettings.bs_lib.anchor_scrollis not available. This also keeps the
location.hashauto-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
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
Comment #4
pivica commented