mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-03 08:25:06 +03:00
Fix missing nav-current class bug when trailing slashes don't match
closes #6422 - trim trailing slashes before comparing URLs in navigation helper - add test case to make sure nav-current is appended regardless of trailing slash presence
This commit is contained in:
parent
6fd4e43cc5
commit
dc957d7d2e
@ -40,6 +40,13 @@ navigation = function (options) {
|
||||
return label.toLowerCase().replace(/[^\w ]+/g, '').replace(/ +/g, '-');
|
||||
}
|
||||
|
||||
// strips trailing slashes and compares urls
|
||||
function _isCurrentUrl(href, currentUrl) {
|
||||
var strippedHref = href.replace(/\/+$/, ''),
|
||||
strippedCurrentUrl = currentUrl.replace(/\/+$/, '');
|
||||
return strippedHref === strippedCurrentUrl;
|
||||
}
|
||||
|
||||
// {{navigation}} should no-op if no data passed in
|
||||
if (navigationData.length === 0) {
|
||||
return new hbs.SafeString('');
|
||||
@ -47,7 +54,7 @@ navigation = function (options) {
|
||||
|
||||
output = navigationData.map(function (e) {
|
||||
var out = {};
|
||||
out.current = e.url === currentUrl;
|
||||
out.current = _isCurrentUrl(e.url, currentUrl);
|
||||
out.label = e.label;
|
||||
out.slug = _slugify(e.label);
|
||||
out.url = hbs.handlebars.Utils.escapeExpression(e.url);
|
||||
|
@ -117,6 +117,22 @@ describe('{{navigation}} helper', function () {
|
||||
rendered.string.should.containEql('nav-foo nav-current');
|
||||
rendered.string.should.containEql('nav-bar"');
|
||||
});
|
||||
|
||||
it('can annotate current url with trailing slash', function () {
|
||||
var firstItem = {label: 'Foo', url: '/foo'},
|
||||
secondItem = {label: 'Bar', url: '/qux'},
|
||||
rendered;
|
||||
|
||||
optionsData.data.blog.navigation = [firstItem, secondItem];
|
||||
optionsData.data.root.relativeUrl = '/foo/';
|
||||
rendered = helpers.navigation(optionsData);
|
||||
|
||||
should.exist(rendered);
|
||||
rendered.string.should.containEql('nav-foo');
|
||||
rendered.string.should.containEql('nav-current');
|
||||
rendered.string.should.containEql('nav-foo nav-current');
|
||||
rendered.string.should.containEql('nav-bar"');
|
||||
});
|
||||
});
|
||||
|
||||
describe('{{navigation}} helper with custom template', function () {
|
||||
|
Loading…
Reference in New Issue
Block a user