Skip to content

Commit

Permalink
Search text on sanitised field
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Mar 15, 2024
1 parent 401f2be commit 64192b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Basesnippet', function (this: Suite) {
versionable: true,
name: 'aSearchableName',
description: 'aSearchableDescription',
textcontent: '<p>aSearchable TextContent</p>',
textcontent: '<p>aSearchable TextContent &hearts;</p>',
};

before('setupApplication', async () => {
Expand Down Expand Up @@ -218,23 +218,25 @@ describe('Basesnippet', function (this: Suite) {
});
});

it('Search with token should return 200 and matching body.textcontent', async () => {
await client
.get(`/basesnippets/search=aSearchable TextCont`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
.then(
result => (
expect(result.body.length).to.be.eql(1),
expect(result.body[0].textcontent).to.be.eql(
'<p>aSearchable TextContent</p>',
)
),
)
.catch(err => {
throw err;
});
['aSearchable TextCont', '♥'].forEach((t, i) => {
it(`Search with token should return 200 and matching body.textcontent ${i}`, async () => {
await client
.get(`/basesnippets/search=${encodeURIComponent(t)}`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
.then(
result => (
expect(result.body.length).to.be.eql(1),
expect(result.body[0].textcontent).to.be.eql(
'<p>aSearchable TextContent &hearts;</p>',
)
),
)
.catch(err => {
throw err;
});
});
});

it('Search with token should return 200 and matching body.tags', async () => {
Expand Down
11 changes: 2 additions & 9 deletions sci-log-db/src/mixins/basesnippet.repository-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,18 +391,11 @@ function FindWithSearchRepositoryMixin<
) {
if (!searchText) return;
searchText = searchText.trimStart();
const searchRegex = {regexp: new RegExp(`.*?${searchText}.*?`, 'i')};
const searchRegex = {regexp: new RegExp(`.*${searchText}.*`, 'i')};
const searchCondition = [
{name: searchRegex},
{description: searchRegex},
{
textcontent: {
regexp: new RegExp(
`(?<=<p>)((?!&).)*${searchText}((?!&).)*(?=<\/p>)`,
'i',
),
},
},
{htmlTextcontent: searchRegex},
];
additionalConditions.or = searchCondition;
}
Expand Down

0 comments on commit 64192b8

Please sign in to comment.