Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

fix: tag search causing regex exception #660

Merged
merged 6 commits into from
Mar 18, 2022
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
34 changes: 34 additions & 0 deletions packages/snjs/lib/services/Items/ItemManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,40 @@ describe('itemManager', () => {
expect(tag).toBeTruthy()
expect(itemManager.isTemplateItem(tag)).toEqual(false)
})

it('should search tags correctly', async () => {
itemManager = createService()
setupRandomUuid()

const foo = await itemManager.createTag('foo[')
const foobar = await itemManager.createTag('foo[bar]')
const bar = await itemManager.createTag('bar[')
const barfoo = await itemManager.createTag('bar[foo]')
const fooDelimiter = await itemManager.createTag('bar.foo')
const barFooDelimiter = await itemManager.createTag('baz.bar.foo')
const fooAttached = await itemManager.createTag('Foo')
const note = createNote('note')
await itemManager.insertItems([
foo,
foobar,
bar,
barfoo,
fooDelimiter,
barFooDelimiter,
fooAttached,
note,
])
await itemManager.addTagToNote(note, fooAttached)

const fooResults = itemManager.searchTags('foo')
expect(fooResults).toContainEqual(foo)
expect(fooResults).toContainEqual(foobar)
expect(fooResults).toContainEqual(barfoo)
expect(fooResults).toContainEqual(fooDelimiter)
expect(fooResults).toContainEqual(barFooDelimiter)
expect(fooResults).not.toContainEqual(bar)
expect(fooResults).not.toContainEqual(fooAttached)
})
})

describe('tags notes index', () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/snjs/lib/services/Items/ItemManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { NotesDisplayCriteria } from '@Lib/protocol/collection/notes_display_cri
import { isString, naturalSort, removeFromArray, UuidGenerator } from '@standardnotes/utils'
import { SNComponent } from '@Models/app/component'
import { SNItemsKey } from '@Models/app/items_key'
import { isTag, SNTag, TagFolderDelimitter } from '@Models/app/tag'
import { isTag, SNTag } from '@Models/app/tag'
import { Uuids } from '@Models/functions'
import { CreateItemFromPayload } from '@Models/generator'
import { PayloadsByDuplicating } from '@Payloads/functions'
Expand Down Expand Up @@ -900,9 +900,8 @@ export class ItemManager
public searchTags(searchQuery: string, note?: SNNote): SNTag[] {
return naturalSort(
this.tags.filter((tag) => {
const regex = new RegExp(`^${searchQuery}|${TagFolderDelimitter}${searchQuery}`, 'i')
const expandedTitle = this.getTagLongTitle(tag)
const matchesQuery = regex.test(expandedTitle)
const matchesQuery = expandedTitle.toLowerCase().includes(searchQuery.toLowerCase())
const tagInNote = note
? this.itemsReferencingItem(note.uuid).some((item) => item?.uuid === tag.uuid)
: false
Expand Down