mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
🐛 Downgraded errors to warnings for img_url
refs #8703 - Instead of throwing errors, throw warnings for incorrect usage of the img_url helper - Differentiate between no attribute passed, and attribute evaluating to undefined
This commit is contained in:
parent
c49dba12a0
commit
688d8c9051
3 changed files with 46 additions and 33 deletions
|
@ -1,39 +1,39 @@
|
||||||
|
|
||||||
// Usage:
|
// Usage:
|
||||||
// `{{img_url}}` - does not work, argument is required
|
|
||||||
// `{{img_url feature_image}}`
|
// `{{img_url feature_image}}`
|
||||||
// `{{img_url profile_image absolute="true"}}`
|
// `{{img_url profile_image absolute="true"}}`
|
||||||
|
// Note:
|
||||||
|
// `{{img_url}}` - does not work, argument is required
|
||||||
//
|
//
|
||||||
// Returns the URL for the current object scope i.e. If inside a post scope will return image permalink
|
// Returns the URL for the current object scope i.e. If inside a post scope will return image permalink
|
||||||
// `absolute` flag outputs absolute URL, else URL is relative.
|
// `absolute` flag outputs absolute URL, else URL is relative.
|
||||||
|
|
||||||
var proxy = require('./proxy'),
|
var proxy = require('./proxy'),
|
||||||
errors = require('../errors'),
|
logging = require('../logging'),
|
||||||
i18n = require('../i18n'),
|
i18n = require('../i18n'),
|
||||||
url = proxy.url;
|
url = proxy.url;
|
||||||
|
|
||||||
module.exports = function imgUrl(attr, options) {
|
module.exports = function imgUrl(attr, options) {
|
||||||
var absolute;
|
// CASE: if no attribute is passed, e.g. `{{img_url}}` we show a warning
|
||||||
|
if (arguments.length < 2) {
|
||||||
// CASE: if you pass e.g. cover_image, but it is not set, then attr is null!
|
logging.warn(i18n.t('warnings.helpers.img_url.attrIsRequired'));
|
||||||
// in this case we don't throw an error
|
|
||||||
if (!options) {
|
|
||||||
attr = undefined;
|
|
||||||
options = attr;
|
|
||||||
}
|
|
||||||
|
|
||||||
absolute = options && options.hash && options.hash.absolute;
|
|
||||||
|
|
||||||
if (attr === undefined) {
|
|
||||||
throw new errors.IncorrectUsageError({
|
|
||||||
message: i18n.t('warnings.helpers.img_url.attrIsRequired')
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// CASE: property is not set in the model e.g. cover_image
|
|
||||||
if (attr === null) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return url.urlFor('image', {image: attr}, absolute);
|
var absolute = options && options.hash && options.hash.absolute;
|
||||||
|
|
||||||
|
// CASE: if attribute is passed, but it is undefined, then the attribute was
|
||||||
|
// an unknown value, e.g. {{img_url feature_img}} and we also show a warning
|
||||||
|
if (attr === undefined) {
|
||||||
|
logging.warn(i18n.t('warnings.helpers.img_url.attrIsRequired'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attr) {
|
||||||
|
return url.urlFor('image', {image: attr}, absolute);
|
||||||
|
} else {
|
||||||
|
// CASE: if you pass e.g. cover_image, but it is not set, then attr is null!
|
||||||
|
// in this case we don't show a warning
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -521,7 +521,8 @@
|
||||||
"valuesMustBeDefined": "All values must be defined for empty, singular and plural"
|
"valuesMustBeDefined": "All values must be defined for empty, singular and plural"
|
||||||
},
|
},
|
||||||
"img_url": {
|
"img_url": {
|
||||||
"attrIsRequired": "Attribute is required e.g. \\{\\{img_url feature_image\\}\\}"
|
"attrIsRequired": "Attribute is required e.g. \\{\\{img_url feature_image\\}\\}",
|
||||||
|
"attrIsUnknown": "Attribute passed to \\{\\{img_url\\}\\} is unknown"
|
||||||
},
|
},
|
||||||
"template": {
|
"template": {
|
||||||
"templateNotFound": "Template {name} not found."
|
"templateNotFound": "Template {name} not found."
|
||||||
|
|
|
@ -4,15 +4,21 @@ var should = require('should'), // jshint ignore:line
|
||||||
|
|
||||||
// Stuff we are testing
|
// Stuff we are testing
|
||||||
helpers = require('../../../server/helpers'),
|
helpers = require('../../../server/helpers'),
|
||||||
errors = require('../../../server/errors'),
|
logging = require('../../../server/logging'),
|
||||||
|
|
||||||
sandbox = sinon.sandbox.create();
|
sandbox = sinon.sandbox.create();
|
||||||
|
|
||||||
describe('{{image}} helper', function () {
|
describe('{{image}} helper', function () {
|
||||||
|
var logWarnStub;
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
configUtils.set({url: 'http://localhost:82832/'});
|
configUtils.set({url: 'http://localhost:82832/'});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
logWarnStub = sandbox.stub(logging, 'warn');
|
||||||
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
sandbox.restore();
|
sandbox.restore();
|
||||||
});
|
});
|
||||||
|
@ -25,33 +31,39 @@ describe('{{image}} helper', function () {
|
||||||
var rendered = helpers.img_url('/content/images/image-relative-url.png', {});
|
var rendered = helpers.img_url('/content/images/image-relative-url.png', {});
|
||||||
should.exist(rendered);
|
should.exist(rendered);
|
||||||
rendered.should.equal('/content/images/image-relative-url.png');
|
rendered.should.equal('/content/images/image-relative-url.png');
|
||||||
|
logWarnStub.called.should.be.false();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should output absolute url of image if the option is present ', function () {
|
it('should output absolute url of image if the option is present ', function () {
|
||||||
var rendered = helpers.img_url('/content/images/image-relative-url.png', {hash: {absolute: 'true'}});
|
var rendered = helpers.img_url('/content/images/image-relative-url.png', {hash: {absolute: 'true'}});
|
||||||
should.exist(rendered);
|
should.exist(rendered);
|
||||||
rendered.should.equal('http://localhost:82832/content/images/image-relative-url.png');
|
rendered.should.equal('http://localhost:82832/content/images/image-relative-url.png');
|
||||||
|
logWarnStub.called.should.be.false();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should output author url', function () {
|
it('should output author url', function () {
|
||||||
var rendered = helpers.img_url('/content/images/author-image-relative-url.png', {});
|
var rendered = helpers.img_url('/content/images/author-image-relative-url.png', {});
|
||||||
should.exist(rendered);
|
should.exist(rendered);
|
||||||
rendered.should.equal('/content/images/author-image-relative-url.png');
|
rendered.should.equal('/content/images/author-image-relative-url.png');
|
||||||
|
logWarnStub.called.should.be.false();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have no output if there is no image ', function (done) {
|
it('should have no output if the image attributeis not provided (with warning)', function () {
|
||||||
try {
|
var rendered = helpers.img_url({hash: {absolute: 'true'}});
|
||||||
helpers.img_url(undefined, {hash: {absolute: 'true'}});
|
should.not.exist(rendered);
|
||||||
done(new Error('we expect an error from img_url'));
|
logWarnStub.calledOnce.should.be.true();
|
||||||
} catch (err) {
|
|
||||||
(err instanceof errors.IncorrectUsageError).should.eql(true);
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have no output if there is no image ', function () {
|
it('should have no output if the image attribute evaluates to undefined (with warning)', function () {
|
||||||
|
var rendered = helpers.img_url(undefined, {hash: {absolute: 'true'}});
|
||||||
|
should.not.exist(rendered);
|
||||||
|
logWarnStub.calledOnce.should.be.true();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have no output if the image attribute evaluates to null (no waring)', function () {
|
||||||
var rendered = helpers.img_url(null, {hash: {absolute: 'true'}});
|
var rendered = helpers.img_url(null, {hash: {absolute: 'true'}});
|
||||||
should.not.exist(rendered);
|
should.not.exist(rendered);
|
||||||
|
logWarnStub.calledOnce.should.be.false();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with sub-directory', function () {
|
describe('with sub-directory', function () {
|
||||||
|
|
Loading…
Add table
Reference in a new issue