Skip to content

Commit 2433936

Browse files
committed
fix to prevent errors from being thrown if elements are not present and (#7)
tests
1 parent 5894aa6 commit 2433936

File tree

6 files changed

+34
-2
lines changed

6 files changed

+34
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ tocbot.refresh()
178178
## Changelog
179179

180180

181+
### v2.1.2
182+
183+
#### Fixed
184+
- [patch] prevent errors from being thrown when elements are not present and add tests.
185+
186+
181187
### v2.1.1
182188

183189
#### Changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tocbot",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "Generate a table of contents based on the heading structure of a html document.",
55
"main": "src/js/index.js",
66
"scripts": {

src/js/build-html.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module.exports = function(options) {
4545

4646
var parent = document.querySelector(selector);
4747

48+
// Return if no parent is found.
49+
if (parent === null) {
50+
return;
51+
}
52+
4853
// Remove existing child if it exists.
4954
if (parent.firstChild) {
5055
parent.removeChild(parent.firstChild);

src/js/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@
138138
// Destroy it if it exists first.
139139
tocbot.destroy();
140140

141-
// Get headings array
141+
// Get headings array.
142142
headingsArray = parseContent.selectHeadings(options.contentSelector, options.headingSelector);
143+
// Return if no headings are found.
144+
if (headingsArray === null) {
145+
return;
146+
}
143147

144148
// Build nested headings array.
145149
var nestedHeadingsObj = parseContent.nestHeadingsArray(headingsArray);

src/js/parse-content.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ module.exports = function parseContent(options) {
9292
.querySelectorAll(selectors);
9393
} catch (e) {
9494
console.warn('Element not found: ' + contentSelector); // eslint-disable-line
95+
return null;
9596
}
9697
}
9798

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ describe('Tocbot', function () {
7777
// The 4th event is added by smooth-scroll.
7878
expect(count).to.equal(4);
7979
});
80+
81+
it('should not throw an error if a content element isn\'t found', function () {
82+
GLOBAL.window.tocbot.destroy();
83+
expect(GLOBAL.window.tocbot.init).to.not.throw(Error);
84+
GLOBAL.window.tocbot.init({
85+
tocSelector: '.missing'
86+
});
87+
});
88+
89+
it('should not throw an error if a toc element isn\'t found', function () {
90+
GLOBAL.window.tocbot.destroy();
91+
expect(GLOBAL.window.tocbot.init).to.not.throw(Error);
92+
GLOBAL.window.tocbot.init({
93+
contentSelector: '.not-here'
94+
});
95+
});
8096
});
8197

8298
describe('#destroy', function () {

0 commit comments

Comments
 (0)