Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix escaping in regex and add tests #1131

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
});
});
});
Loading