2021-09-28 17:06:33 +03:00
|
|
|
const {SafeString} = require('../services/rendering');
|
2021-09-26 23:01:13 +03:00
|
|
|
|
|
|
|
const logging = require('@tryghost/logging');
|
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
|
2021-06-07 22:22:06 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2021-09-23 22:46:22 +03:00
|
|
|
const messages = {
|
|
|
|
invalidAttribute: 'Invalid or no attribute given to match helper'
|
|
|
|
};
|
|
|
|
|
2021-06-07 22:22:06 +03:00
|
|
|
/**
|
2021-09-17 11:47:10 +03:00
|
|
|
* This is identical to the built-in if helper, except inverse/fn calls are replaced with false/true
|
|
|
|
* https://github.com/handlebars-lang/handlebars.js/blob/19bdace85a8d0bc5ed3a4dec4071cb08c8d003f2/lib/handlebars/helpers/if.js#L9-L20
|
2021-06-07 22:22:06 +03:00
|
|
|
*/
|
2021-09-17 11:47:10 +03:00
|
|
|
function isEmptyValue(value) {
|
|
|
|
if (!value && value !== 0) {
|
|
|
|
return true;
|
|
|
|
} else if (Array.isArray(value) && value.length === 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 22:22:06 +03:00
|
|
|
const handleConditional = (conditional, options) => {
|
|
|
|
if (_.isFunction(conditional)) {
|
|
|
|
conditional = conditional.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default behavior is to render the positive path if the value is truthy and not empty.
|
|
|
|
// The `includeZero` option may be set to treat the condtional as purely not empty based on the
|
|
|
|
// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.
|
2021-09-17 11:47:10 +03:00
|
|
|
if ((!options.hash.includeZero && !conditional) || isEmptyValue(conditional)) {
|
2021-06-07 22:22:06 +03:00
|
|
|
return false;
|
2021-09-17 11:47:10 +03:00
|
|
|
} else {
|
|
|
|
return true;
|
2021-06-07 22:22:06 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleMatch = (data, operator, value) => {
|
|
|
|
let result;
|
|
|
|
|
|
|
|
switch (operator) {
|
|
|
|
case '!=':
|
|
|
|
result = data !== value;
|
|
|
|
break;
|
2022-03-03 17:43:47 +03:00
|
|
|
case '<':
|
|
|
|
result = data < value;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
result = data > value;
|
|
|
|
break;
|
|
|
|
case '>=':
|
|
|
|
result = data >= value;
|
|
|
|
break;
|
|
|
|
case '<=':
|
|
|
|
result = data <= value;
|
|
|
|
break;
|
2021-06-07 22:22:06 +03:00
|
|
|
default:
|
|
|
|
result = data === value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2021-10-14 21:36:56 +03:00
|
|
|
module.exports = function match(...attrs) {
|
2021-06-07 22:22:06 +03:00
|
|
|
// options = options || {};
|
|
|
|
// options.hash = options.hash || {};
|
|
|
|
// options.data = options.data || {};
|
|
|
|
|
|
|
|
const options = attrs.pop();
|
|
|
|
const isBlock = _.has(options, 'fn');
|
|
|
|
let result;
|
|
|
|
|
|
|
|
if (_.isEmpty(attrs)) {
|
2021-09-23 22:46:22 +03:00
|
|
|
logging.warn(tpl(messages.invalidAttribute));
|
2021-06-07 22:22:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Added full SafeString handling to match helper
refs: https://github.com/TryGhost/Team/issues/759
- No matter what, a handlebars helper outputs a string. So if you return true, you'll always get 'true'.
- SafeStrings are handlebars's way of passing around a string whilst also maintaining a record of the original value e.g. new SafeString(true) results in {string: true}
- We need this for the match helper, so that we know when doing a comparison that we're meant to be comparing against a boolean true, not a string true
- Therefore, we need to putput SafeStrings, but also process them when passed in
The logic
- Figuring out the correct logic here has been a little tricky but essentially:
- {{match safestring}} with a single arg, will return true for any truthy value
- {{match safestring "=" true}} does a direct comparison with the original value of the safe string, so if it was a boolean true, the match will be true else false
- {{match (match something) "=" true}} will therefore work for any level of nesting
- this can result in slightly inconsistent results, but feels correct and documentable
This is documented extensively through the test cases
2021-10-14 13:04:44 +03:00
|
|
|
// If any of the attributes are safe strings, change them back to their original value
|
|
|
|
attrs = attrs.map((attr) => {
|
|
|
|
if (attr instanceof SafeString) {
|
|
|
|
return attr.string;
|
|
|
|
}
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
});
|
|
|
|
|
2021-06-07 22:22:06 +03:00
|
|
|
if (attrs.length === 1) {
|
2021-10-14 14:02:50 +03:00
|
|
|
// CASE: single attribute, treat it as simple true/false (like the if helper)
|
2021-06-07 22:22:06 +03:00
|
|
|
result = handleConditional(attrs[0], options);
|
2021-10-14 14:02:50 +03:00
|
|
|
} else if (attrs.length === 2) {
|
|
|
|
// CASE: two attributes, assume the operator is "="
|
|
|
|
result = handleMatch(attrs[0], '=', attrs[1]);
|
2021-06-07 22:22:06 +03:00
|
|
|
} else if (attrs.length === 3) {
|
2021-10-14 14:02:50 +03:00
|
|
|
// CASE: three attributes, handle the match exactly
|
Added full SafeString handling to match helper
refs: https://github.com/TryGhost/Team/issues/759
- No matter what, a handlebars helper outputs a string. So if you return true, you'll always get 'true'.
- SafeStrings are handlebars's way of passing around a string whilst also maintaining a record of the original value e.g. new SafeString(true) results in {string: true}
- We need this for the match helper, so that we know when doing a comparison that we're meant to be comparing against a boolean true, not a string true
- Therefore, we need to putput SafeStrings, but also process them when passed in
The logic
- Figuring out the correct logic here has been a little tricky but essentially:
- {{match safestring}} with a single arg, will return true for any truthy value
- {{match safestring "=" true}} does a direct comparison with the original value of the safe string, so if it was a boolean true, the match will be true else false
- {{match (match something) "=" true}} will therefore work for any level of nesting
- this can result in slightly inconsistent results, but feels correct and documentable
This is documented extensively through the test cases
2021-10-14 13:04:44 +03:00
|
|
|
result = handleMatch(attrs[0], attrs[1], attrs[2]);
|
2021-06-07 22:22:06 +03:00
|
|
|
} else {
|
2021-09-23 22:46:22 +03:00
|
|
|
logging.warn(tpl(messages.invalidAttribute));
|
2021-06-07 22:22:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're in block mode, return the outcome from the fn/inverse functions
|
|
|
|
if (isBlock) {
|
|
|
|
if (result) {
|
|
|
|
return options.fn(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options.inverse(this);
|
|
|
|
}
|
|
|
|
|
Added full SafeString handling to match helper
refs: https://github.com/TryGhost/Team/issues/759
- No matter what, a handlebars helper outputs a string. So if you return true, you'll always get 'true'.
- SafeStrings are handlebars's way of passing around a string whilst also maintaining a record of the original value e.g. new SafeString(true) results in {string: true}
- We need this for the match helper, so that we know when doing a comparison that we're meant to be comparing against a boolean true, not a string true
- Therefore, we need to putput SafeStrings, but also process them when passed in
The logic
- Figuring out the correct logic here has been a little tricky but essentially:
- {{match safestring}} with a single arg, will return true for any truthy value
- {{match safestring "=" true}} does a direct comparison with the original value of the safe string, so if it was a boolean true, the match will be true else false
- {{match (match something) "=" true}} will therefore work for any level of nesting
- this can result in slightly inconsistent results, but feels correct and documentable
This is documented extensively through the test cases
2021-10-14 13:04:44 +03:00
|
|
|
// Else return the result as a SafeString Eg.{string: false} || {string: true}
|
2021-06-07 22:22:06 +03:00
|
|
|
return new SafeString(result);
|
|
|
|
};
|