From 838516efb0117d782789d9e75f43cf0730a0426b Mon Sep 17 00:00:00 2001 From: monkey sees Date: Sun, 26 Feb 2023 00:16:45 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20Handlebars=E2=80=99=20as?= =?UTF-8?q?set=20helper=20for=20SafeString=20input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes https://github.com/TryGhost/Ghost/issues/16332 Passing `SafeString` input to `asset` helper was resulting in the exception being thrown. This meant that we couldn’t combine `asset` helper with other helpers which produce `SafeString` e.g. `concat` helper for string concatenation. --- ghost/core/core/frontend/meta/asset-url.js | 9 +++++++++ ghost/core/test/unit/frontend/meta/asset-url.test.js | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/ghost/core/core/frontend/meta/asset-url.js b/ghost/core/core/frontend/meta/asset-url.js index ab56bca80c..0547429a17 100644 --- a/ghost/core/core/frontend/meta/asset-url.js +++ b/ghost/core/core/frontend/meta/asset-url.js @@ -2,6 +2,7 @@ const crypto = require('crypto'); const config = require('../../shared/config'); const {blogIcon} = require('../../server/lib/image'); const urlUtils = require('../../shared/url-utils'); +const {SafeString} = require('../services/handlebars'); /** * Serve either uploaded favicon or default @@ -11,7 +12,15 @@ function getFaviconUrl() { return blogIcon.getIconUrl(); } +/** + * Prepare URL for an asset + * @param {string|SafeString} path — the asset’s path + * @param {boolean} hasMinFile — flag for the existence of a minified version for the asset + * @returns {string} + */ function getAssetUrl(path, hasMinFile) { + path = path instanceof SafeString ? path.string : path; + // CASE: favicon - this is special path with its own functionality if (path.match(/\/?favicon\.(ico|png)$/)) { // @TODO, resolve this - we should only be resolving subdirectory and extension. diff --git a/ghost/core/test/unit/frontend/meta/asset-url.test.js b/ghost/core/test/unit/frontend/meta/asset-url.test.js index a3e3fcde4b..26b1ea2548 100644 --- a/ghost/core/test/unit/frontend/meta/asset-url.test.js +++ b/ghost/core/test/unit/frontend/meta/asset-url.test.js @@ -1,5 +1,6 @@ const should = require('should'); const sinon = require('sinon'); +const {SafeString} = require('../../../../core/frontend/services/handlebars'); const imageLib = require('../../../../core/server/lib/image'); const settingsCache = require('../../../../core/shared/settings-cache'); const configUtils = require('../../../utils/configUtils'); @@ -38,6 +39,11 @@ describe('getAssetUrl', function () { testUrl.should.equal(`/assets/myfile.svg?v=${config.get('assetHash')}#arrow-up`); }); + it('should handle Handlebars’ SafeString', function () { + const testUrl = getAssetUrl(new SafeString('myfile.js')); + testUrl.should.equal('/assets/myfile.js?v=' + config.get('assetHash')); + }); + describe('favicon', function () { it('should not add asset to url if favicon.ico', function () { const testUrl = getAssetUrl('favicon.ico');