Skip to content

Commit 373d295

Browse files
committed
Re: #45 - Prevent unrecognized selector expressions from erroring
1 parent 97244ae commit 373d295

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

demos/default.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,7 @@ <h2>Donation</h2>
438438
$(function() {
439439

440440
var toc = $("#toc").tocify({
441-
selectors: "h2,h3,h4,h5",
442-
scrollHistory: true
441+
selectors: "h2,h3,h4,h5"
443442
}).data("toc-tocify");
444443

445444
prettyPrint();

src/javascripts/jquery.tocify.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@
308308

309309
hash = window.location.hash.substring(1),
310310

311-
elem = self.element.find("li[data-unique='" + hash + "']");
311+
elem = self.element.find('li[data-unique="' + hash + '"]');
312312

313313
if(hash.length) {
314314

@@ -632,7 +632,7 @@
632632

633633
currentElem = self.element.find('li.active');
634634

635-
self._scrollTo($("div[data-unique=" + currentElem.attr("data-unique") + "]"));
635+
self._scrollTo($('div[data-unique="' + currentElem.attr("data-unique") + '"]'));
636636

637637
}
638638

@@ -950,7 +950,14 @@
950950

951951
var self = this,
952952
duration = self.options.smoothScroll || 0,
953-
scrollTo = self.options.scrollTo;
953+
scrollTo = self.options.scrollTo,
954+
currentDiv = $('div[data-unique="' + elem.attr("data-unique") + '"]');
955+
956+
if(!currentDiv.length) {
957+
958+
return self;
959+
960+
}
954961

955962
// Once all animations on the page are complete, this callback function will be called
956963
$("html, body").promise().done(function() {
@@ -959,7 +966,7 @@
959966
$("html, body").animate({
960967

961968
// Sets the jQuery `scrollTop` to the top offset of the HTML div tag that matches the current list item's `data-unique` tag
962-
"scrollTop": $('div[data-unique="' + elem.attr("data-unique") + '"]').offset().top - ($.isFunction(scrollTo) ? scrollTo.call() : scrollTo) + "px"
969+
"scrollTop": currentDiv.offset().top - ($.isFunction(scrollTo) ? scrollTo.call() : scrollTo) + "px"
963970

964971
}, {
965972

src/javascripts/jquery.tocify.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)