From dfceca42b37e4c7e916ebdb8bfaa4423b72d14e2 Mon Sep 17 00:00:00 2001 From: Tan Zhen Yong Date: Sun, 6 Jan 2019 22:43:48 +0800 Subject: [PATCH] Fix missing anchor icons when anchor in URL does not exist (#552) --- asset/js/setup.js | 7 +++++-- test/test_site/expected/markbind/js/setup.js | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/asset/js/setup.js b/asset/js/setup.js index 8587cf0228..2d42b56bb9 100644 --- a/asset/js/setup.js +++ b/asset/js/setup.js @@ -5,8 +5,11 @@ Vue.use(VueStrap); function scrollToUrlAnchorHeading() { if (window.location.hash) { // remove leading hash to get element ID - document.getElementById(window.location.hash.slice(1)).scrollIntoView(); - window.scrollBy(0, -document.body.style.paddingTop.replace('px', '')); + const headingElement = document.getElementById(window.location.hash.slice(1)); + if (headingElement) { + headingElement.scrollIntoView(); + window.scrollBy(0, -document.body.style.paddingTop.replace('px', '')); + } } } diff --git a/test/test_site/expected/markbind/js/setup.js b/test/test_site/expected/markbind/js/setup.js index 8587cf0228..2d42b56bb9 100644 --- a/test/test_site/expected/markbind/js/setup.js +++ b/test/test_site/expected/markbind/js/setup.js @@ -5,8 +5,11 @@ Vue.use(VueStrap); function scrollToUrlAnchorHeading() { if (window.location.hash) { // remove leading hash to get element ID - document.getElementById(window.location.hash.slice(1)).scrollIntoView(); - window.scrollBy(0, -document.body.style.paddingTop.replace('px', '')); + const headingElement = document.getElementById(window.location.hash.slice(1)); + if (headingElement) { + headingElement.scrollIntoView(); + window.scrollBy(0, -document.body.style.paddingTop.replace('px', '')); + } } }