From 1b2e7c956f7a29a4b6b83e5fe6df337afd88131a Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Tue, 12 Mar 2019 01:18:07 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20{{body=5Fclass}}=20helpe?= =?UTF-8?q?r=20when=20using=20`data:=20page.{slug}`=20in=20routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs #10082 - throwed a 500 because this.page was not handled - v2 differentiates between page and post --- core/server/helpers/body_class.js | 21 +++++++++++---------- core/test/unit/helpers/body_class_spec.js | 11 ++++++++++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/core/server/helpers/body_class.js b/core/server/helpers/body_class.js index 564aaaa48c37..09f2ac8ece1c 100644 --- a/core/server/helpers/body_class.js +++ b/core/server/helpers/body_class.js @@ -8,19 +8,19 @@ var proxy = require('./proxy'), // We use the name body_class to match the helper for consistency: module.exports = function body_class(options) { // eslint-disable-line camelcase - var classes = [], - context = options.data.root.context, - post = this.post, - tags = this.post && this.post.tags ? this.post.tags : this.tags || [], - page = this.post && this.post.page ? this.post.page : this.page || false; + let classes = []; + const context = options.data.root.context; + const obj = this.post || this.page; + const tags = obj && obj.tags ? obj.tags : []; + const isPage = !!(obj && obj.page); if (_.includes(context, 'home')) { classes.push('home-template'); - } else if (_.includes(context, 'post') && post) { + } else if (_.includes(context, 'post') && obj) { classes.push('post-template'); - } else if (_.includes(context, 'page') && page) { + } else if (_.includes(context, 'page') && obj && isPage) { classes.push('page-template'); - classes.push('page-' + this.post.slug); + classes.push('page-' + obj.slug); } else if (_.includes(context, 'tag') && this.tag) { classes.push('tag-template'); classes.push('tag-' + this.tag.slug); @@ -33,7 +33,7 @@ module.exports = function body_class(options) { // eslint-disable-line camelcase if (tags) { classes = classes.concat(tags.map(function (tag) { - return 'tag-' + tag.slug; + return 'tag-' + tag.slug; })); } @@ -42,8 +42,9 @@ module.exports = function body_class(options) { // eslint-disable-line camelcase } classes = _.reduce(classes, function (memo, item) { - return memo + ' ' + item; + return memo + ' ' + item; }, ''); + return new SafeString(classes.trim()); }; diff --git a/core/test/unit/helpers/body_class_spec.js b/core/test/unit/helpers/body_class_spec.js index 0d0357f019ad..234f05f9912b 100644 --- a/core/test/unit/helpers/body_class_spec.js +++ b/core/test/unit/helpers/body_class_spec.js @@ -133,7 +133,7 @@ describe('{{body_class}} helper', function () { rendered.string.should.equal('post-template tag-foo tag-bar'); }); - it('a static page', function () { + it('v0.1: a static page', function () { var rendered = callBodyClassWithContext( ['page'], {relativeUrl: '/about', post: {page: true, slug: 'about'}} @@ -142,6 +142,15 @@ describe('{{body_class}} helper', function () { rendered.string.should.equal('page-template page-about'); }); + it('v2: a static page', function () { + var rendered = callBodyClassWithContext( + ['page'], + {relativeUrl: '/about', page: {page: true, slug: 'about'}} + ); + + rendered.string.should.equal('page-template page-about'); + }); + it('a static page with custom template (is now the same as one without)', function () { var rendered = callBodyClassWithContext( ['page'],