Bugs in `Drupal.behaviors.bootstrapAnchors` method `bootstrapAnchor`
bug 1
When you have a link of "#" the first check for $target.length will be falsey.
Then the second attempt will search for `[name=""]`
The problem here is that if you have any element with an empty name attribute it
will be used as the $target and the link will scroll to an unexpectd point rather than doing nothing.
Yes a solution is to not output a name attribute if its empty, however several Drupal modules
will still output the name attribute on a button or input even if it is empty.
This can be fixed by altering the following:
// Check for anchors that use the name attribute instead.
if (!$target.length) {
attr = 'name';
$target = $('[name="' + element.hash.replace('#', '') + '"]');
}
To:
// Check for anchors that use the name attribute instead.
if (!$target.length) {
attr = 'name';
var name = element.hash.replace('#', '');
if(name !== '') {
$target = $('[name="' + name + '"]');
}
}
bug 2
A link with a hash that doesn't exist on the page will cause the script to throw an error.
ie. <a href="#this_probably_doesnt_exist_on_your_page">test</a>
This can be fixed by altering the following:
(because the anchor is valid even theo theres no target it doesn't exit)
// Immediately stop if no anchors are found.
if (!this.validAnchor && !$target.length) {
return;
}
To:
// Immediately stop if no anchors are found.
if (!this.validAnchor || !$target.length) {
return;
}
Comments
Comment #1
almunningsIn hindsight the fix for the first bug would be easier if the selector specifies a link tag as I assume that was the intention.
ie.
$target = $('a[name="' + element.hash.replace('#', '') + '"]');
that way it wont even find buttons or inputs
Comment #2
markhalliwell