Skip to content
Permalink
Browse files

fix(jexl): fix handling of whitespace characters

  • Loading branch information...
anehx authored and czosel committed Jul 18, 2019
1 parent ba7d236 commit f7839093aca0fb30c53594cf44a1117f3eb7ac2b
@@ -5,6 +5,7 @@ import { reads } from "@ember/object/computed";
import { task } from "ember-concurrency";
import jexl from "jexl";
import { computed } from "@ember/object";
import { cleanExpression } from "ember-caluma/utils/jexl";

export default Component.extend({
layout,
@@ -25,7 +26,7 @@ export default Component.extend({
return tsk;
}),
_requiredTask: task(function*() {
return yield jexl.eval(this.get("question.isRequired"));
return yield jexl.eval(cleanExpression(this.get("question.isRequired")));
}),

didInsertElement() {
@@ -221,8 +221,8 @@ export default Base.extend(Evented, {
* @property {String[]} hiddenDependencies
* @accessor
*/
hiddenDependencies: computed("question.isHidden", function() {
return getDependenciesFromJexl(this.question.isHidden);
hiddenDependencies: computed("question.hiddenExpression", function() {
return getDependenciesFromJexl(this.question.hiddenExpression);
}),

/**
@@ -234,8 +234,8 @@ export default Base.extend(Evented, {
* @property {String[]} optionalDependencies
* @accessor
*/
optionalDependencies: computed("question.isRequired", function() {
return getDependenciesFromJexl(this.question.isRequired);
optionalDependencies: computed("question.requiredExpression", function() {
return getDependenciesFromJexl(this.question.requiredExpression);
}),

/**
@@ -261,7 +261,7 @@ export default Base.extend(Evented, {
fieldsetHidden ||
dependingHidden ||
(yield this.document.jexl.eval(
this.question.isHidden,
this.question.hiddenExpression,
this.document.jexlContext
));

@@ -295,7 +295,7 @@ export default Base.extend(Evented, {
fieldsetHidden ||
dependingHidden ||
!(yield this.document.jexl.eval(
this.question.isRequired,
this.question.requiredExpression,
this.document.jexlContext
))
);
@@ -1,6 +1,7 @@
import Base from "ember-caluma/lib/base";
import { defineProperty } from "@ember/object";
import { assert } from "@ember/debug";
import { expression } from "ember-caluma/utils/jexl";

/**
* Object which represents a question in context of a field
@@ -22,5 +23,8 @@ export default Base.extend({
this._super(...arguments);

this.setProperties(this.raw);
}
},

hiddenExpression: expression("isHidden"),
requiredExpression: expression("isRequired")
});
@@ -1,3 +1,4 @@
import { computed } from "@ember/object";
import Parser from "jexl/lib/parser/Parser";
import jexl from "jexl";

@@ -62,4 +63,32 @@ export const getTransforms = tree => {
return transforms;
};

export default { getTransforms, getAST };
/**
* Function to clean all whitespace characters of a jexl expression
*
* @function cleanExpression
* @param {String} expression The raw expression
* @return {String} The cleaned expression
*
* @todo Remove this when https://github.com/TomFrost/Jexl/pull/54/ is merged
*/
export const cleanExpression = expression => {
return expression.replace(/\s+/g, " ").trim();
};

/**
* Computed property macro for automatically cleaning expression's whitespace
* characters.
*
* @function expression
* @param {String} key Key of the property holding the raw expression
* @return {Function} The computed property
*
* @todo Remove this when https://github.com/TomFrost/Jexl/pull/54/ is merged
*/
export const expression = key =>
computed(key, function() {
return cleanExpression(this.get(key));
});

export default { getTransforms, getAST, cleanExpression, expression };
@@ -202,7 +202,7 @@ module("Unit | Library | field", function(hooks) {

const field = this.document.findField("test-question-2");

const whitespaced = "(\n 1 == 1\n &&\n 2 == 2\n)";
const whitespaced = "(\n 1 == 1\r &&\r 2 == 2\n)";

field.question.set("isHidden", whitespaced);
field.question.set("isRequired", whitespaced);
@@ -1,6 +1,12 @@
import { getAST, getTransforms } from "ember-caluma/utils/jexl";
import {
getAST,
getTransforms,
cleanExpression,
expression
} from "ember-caluma/utils/jexl";
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import EmberObject from "@ember/object";

module("Unit | Utility | jexl", function(hooks) {
setupTest(hooks);
@@ -80,4 +86,18 @@ module("Unit | Utility | jexl", function(hooks) {
type: "BinaryExpression"
});
});

test("clean expression and expression computed macro works", async function(assert) {
assert.expect(2);

const whitespaced = "(\n 1 == 1\r &&\r 2 == 2\n)";
const cleaned = "( 1 == 1 && 2 == 2 )";

const obj = EmberObject.extend({
cleaned: expression("whitespaced")
}).create({ whitespaced });

assert.equal(cleanExpression(whitespaced), cleaned);
assert.equal(obj.cleaned, cleaned);
});
});

0 comments on commit f783909

Please sign in to comment.
You can’t perform that action at this time.