Skip to content

Commit

Permalink
Fix escaping in regex and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinRiese committed Dec 17, 2024
1 parent 0386886 commit 74f1801
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ define([
updateInstanceQuery: function (query, instanceId, oldId) {
var regexp = INSTANCE_REGEXP;
if (oldId) {
regexp = new RegExp("(^|\W)instance\\((['\"])" +
RegExp.escape(oldId) + "\2\\)", "ig");
regexp = new RegExp("(^|\\W)instance\\((['\"])" +
RegExp.escape(oldId) + "\\2\\)", "ig");
}
return query.replace(regexp, "$1instance('" + instanceId + "')");
},
Expand Down
40 changes: 40 additions & 0 deletions tests/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,5 +713,45 @@ define([
});
});
});

describe("updateInstanceQuery", function () {


it('should not replace ids if name closes with a 2', function() {
// This is just to test the previous behavior does not happen anymore
const form = util.loadXML("");
const query = "instance('oldId\2)/session/data/case_id";
const instanceId = "newId";
const oldInstanceId = "oldId";
const updatedQuery = form.updateInstanceQuery(query, instanceId, oldInstanceId);
assert.equal(updatedQuery, query);
});

it('should replace ids if old instance Id is provided', function() {
const form = util.loadXML("");
const query = "instance('oldId')/session/data/case_id";
const instanceId = "newId";
const oldInstanceId = "oldId";
const updatedQuery = form.updateInstanceQuery(query, instanceId, oldInstanceId);
assert.equal(updatedQuery, "instance('newId')/session/data/case_id");
});

it('should replace ids if there is leading white space', function() {
const form = util.loadXML("");
const query = "word instance('oldId')/session/data/case_id";
const instanceId = "newId";
const oldInstanceId = "oldId";
const updatedQuery = form.updateInstanceQuery(query, instanceId, oldInstanceId);
assert.equal(updatedQuery, "word instance('newId')/session/data/case_id");
});

it('should replace ids if there is no old', function() {
const form = util.loadXML("");
const query = "instance('oldId')/session/data/case_id";
const instanceId = "newId";
const updatedQuery = form.updateInstanceQuery(query, instanceId);
assert.equal(updatedQuery, "instance('newId')/session/data/case_id");
});
});
});
});

0 comments on commit 74f1801

Please sign in to comment.