Skip to content

Commit 07e17c7

Browse files
authored
Merge pull request cferdinandi#435 from cferdinandi/development
Check for closest() method in event.target before calling
2 parents 429cf1f + 279e0f9 commit 07e17c7

10 files changed

+168
-148
lines changed

dist/smooth-scroll.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v14.1.0: Animate scrolling to anchor links
2+
* smooth-scroll v14.1.1: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -280,6 +280,32 @@
280280
return !header ? 0 : (getHeight(header) + header.offsetTop);
281281
};
282282

283+
/**
284+
* Update the URL
285+
* @param {Node} anchor The anchor that was scrolled to
286+
* @param {Boolean} isNum If true, anchor is a number
287+
* @param {Object} options Settings for Smooth Scroll
288+
*/
289+
var updateURL = function (anchor, isNum, options) {
290+
291+
// Bail if the anchor is a number
292+
if (isNum) return;
293+
294+
// Verify that pushState is supported and the updateURL option is enabled
295+
if (!history.pushState || !options.updateURL) return;
296+
297+
// Update URL
298+
history.pushState(
299+
{
300+
smoothScroll: JSON.stringify(options),
301+
anchor: anchor.id
302+
},
303+
document.title,
304+
anchor === document.documentElement ? '#top' : '#' + anchor.id
305+
);
306+
307+
};
308+
283309
/**
284310
* Bring the anchored element into focus
285311
* @param {Node} anchor The anchor element
@@ -307,32 +333,6 @@
307333

308334
};
309335

310-
/**
311-
* Update the URL
312-
* @param {Node} anchor The anchor that was scrolled to
313-
* @param {Boolean} isNum If true, anchor is a number
314-
* @param {Object} options Settings for Smooth Scroll
315-
*/
316-
var updateURL = function (anchor, isNum, options) {
317-
318-
// Bail if the anchor is a number
319-
if (isNum) return;
320-
321-
// Verify that pushState is supported and the updateURL option is enabled
322-
if (!history.pushState || !options.updateURL) return;
323-
324-
// Update URL
325-
history.pushState(
326-
{
327-
smoothScroll: JSON.stringify(options),
328-
anchor: anchor.id
329-
},
330-
document.title,
331-
anchor === document.documentElement ? '#top' : '#' + anchor.id
332-
);
333-
334-
};
335-
336336
/**
337337
* Emit a custom event
338338
* @param {String} type The event type
@@ -490,6 +490,10 @@
490490
// Don't run if right-click or command/control + click
491491
if (event.button !== 0 || event.metaKey || event.ctrlKey) return;
492492

493+
// Check if event.target has closest() method
494+
// By @totegi - https://github.com/cferdinandi/smooth-scroll/pull/401/
495+
if(!('closest' in event.target))return;
496+
493497
// Check if a smooth scroll link was clicked
494498
toggle = event.target.closest(selector);
495499
if (!toggle || toggle.tagName.toLowerCase() !== 'a' || event.target.closest(settings.ignore)) return;
@@ -577,7 +581,7 @@
577581
smoothScroll.init = function (options) {
578582

579583
// feature test
580-
if (!supports()) return;
584+
if (!supports()) throw 'Smooth Scroll: This browser does not support the required JavaScript methods and browser APIs.';
581585

582586
// Destroy any existing initializations
583587
smoothScroll.destroy();

dist/smooth-scroll.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/smooth-scroll.polyfills.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v14.1.0: Animate scrolling to anchor links
2+
* smooth-scroll v14.1.1: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -350,6 +350,32 @@ if (window.Element && !Element.prototype.closest) {
350350
return !header ? 0 : (getHeight(header) + header.offsetTop);
351351
};
352352

353+
/**
354+
* Update the URL
355+
* @param {Node} anchor The anchor that was scrolled to
356+
* @param {Boolean} isNum If true, anchor is a number
357+
* @param {Object} options Settings for Smooth Scroll
358+
*/
359+
var updateURL = function (anchor, isNum, options) {
360+
361+
// Bail if the anchor is a number
362+
if (isNum) return;
363+
364+
// Verify that pushState is supported and the updateURL option is enabled
365+
if (!history.pushState || !options.updateURL) return;
366+
367+
// Update URL
368+
history.pushState(
369+
{
370+
smoothScroll: JSON.stringify(options),
371+
anchor: anchor.id
372+
},
373+
document.title,
374+
anchor === document.documentElement ? '#top' : '#' + anchor.id
375+
);
376+
377+
};
378+
353379
/**
354380
* Bring the anchored element into focus
355381
* @param {Node} anchor The anchor element
@@ -377,32 +403,6 @@ if (window.Element && !Element.prototype.closest) {
377403

378404
};
379405

380-
/**
381-
* Update the URL
382-
* @param {Node} anchor The anchor that was scrolled to
383-
* @param {Boolean} isNum If true, anchor is a number
384-
* @param {Object} options Settings for Smooth Scroll
385-
*/
386-
var updateURL = function (anchor, isNum, options) {
387-
388-
// Bail if the anchor is a number
389-
if (isNum) return;
390-
391-
// Verify that pushState is supported and the updateURL option is enabled
392-
if (!history.pushState || !options.updateURL) return;
393-
394-
// Update URL
395-
history.pushState(
396-
{
397-
smoothScroll: JSON.stringify(options),
398-
anchor: anchor.id
399-
},
400-
document.title,
401-
anchor === document.documentElement ? '#top' : '#' + anchor.id
402-
);
403-
404-
};
405-
406406
/**
407407
* Emit a custom event
408408
* @param {String} type The event type
@@ -560,6 +560,10 @@ if (window.Element && !Element.prototype.closest) {
560560
// Don't run if right-click or command/control + click
561561
if (event.button !== 0 || event.metaKey || event.ctrlKey) return;
562562

563+
// Check if event.target has closest() method
564+
// By @totegi - https://github.com/cferdinandi/smooth-scroll/pull/401/
565+
if(!('closest' in event.target))return;
566+
563567
// Check if a smooth scroll link was clicked
564568
toggle = event.target.closest(selector);
565569
if (!toggle || toggle.tagName.toLowerCase() !== 'a' || event.target.closest(settings.ignore)) return;
@@ -647,7 +651,7 @@ if (window.Element && !Element.prototype.closest) {
647651
smoothScroll.init = function (options) {
648652

649653
// feature test
650-
if (!supports()) return;
654+
if (!supports()) throw 'Smooth Scroll: This browser does not support the required JavaScript methods and browser APIs.';
651655

652656
// Destroy any existing initializations
653657
smoothScroll.destroy();

0 commit comments

Comments
 (0)