diff --git a/frontend/src/app/features/settings/pages/languages/language-add-form.component.ts b/frontend/src/app/features/settings/pages/languages/language-add-form.component.ts index 980bb8356..f234b9ad5 100644 --- a/frontend/src/app/features/settings/pages/languages/language-add-form.component.ts +++ b/frontend/src/app/features/settings/pages/languages/language-add-form.component.ts @@ -8,7 +8,7 @@ import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { Observable, of } from 'rxjs'; -import { AddLanguageForm, AutocompleteComponent, AutocompleteSource, FormHintComponent, LanguageDto, LanguagesState, TranslatePipe } from '@app/shared'; +import { AddLanguageForm, AutocompleteComponent, AutocompleteSource, FormHintComponent, LanguageDto, LanguagesState, StringHelper, TranslatePipe } from '@app/shared'; class LanguageSource implements AutocompleteSource { constructor( @@ -21,7 +21,7 @@ class LanguageSource implements AutocompleteSource { return of(this.languages); } - const regex = new RegExp(query, 'i'); + const regex = StringHelper.searchRegex(query); const results: LanguageDto[] = []; const result = this.languages.find(x => x.iso2Code === query); diff --git a/frontend/src/app/features/teams/state/team-contributors.state.ts b/frontend/src/app/features/teams/state/team-contributors.state.ts index bc4b4f49e..808ae18a3 100644 --- a/frontend/src/app/features/teams/state/team-contributors.state.ts +++ b/frontend/src/app/features/teams/state/team-contributors.state.ts @@ -8,7 +8,7 @@ import { Injectable } from '@angular/core'; import { EMPTY, Observable, throwError } from 'rxjs'; import { catchError, finalize, tap } from 'rxjs/operators'; -import { AssignContributorDto, ContributorDto, ContributorsDto, debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, TeamsState, Types, VersionTag } from '@app/shared'; +import { AssignContributorDto, ContributorDto, ContributorsDto, debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, StringHelper, TeamsState, Types, VersionTag } from '@app/shared'; import { TeamContributorsService } from '../internal'; interface Snapshot extends ListState { @@ -159,7 +159,7 @@ export class TeamContributorsState extends State { } function getRegex(query?: string): RegExp | undefined { - return query ? new RegExp(query, 'i') : undefined; + return query ? StringHelper.searchRegex(query) : undefined; } function getFilteredContributors(snapshot: Snapshot) { @@ -168,7 +168,7 @@ function getFilteredContributors(snapshot: Snapshot) { let filtered = contributors; if (query) { - const regex = new RegExp(query, 'i'); + const regex = StringHelper.searchRegex(query); filtered = filtered.filter(x => regex.test(x.contributorName)); } diff --git a/frontend/src/app/framework/angular/forms/editors/dropdown.component.ts b/frontend/src/app/framework/angular/forms/editors/dropdown.component.ts index 7a67208a5..44803da48 100644 --- a/frontend/src/app/framework/angular/forms/editors/dropdown.component.ts +++ b/frontend/src/app/framework/angular/forms/editors/dropdown.component.ts @@ -9,7 +9,7 @@ import { AfterContentInit, booleanAttribute, ChangeDetectionStrategy, Component, ContentChildren, EventEmitter, forwardRef, Input, OnInit, Output, QueryList, TemplateRef } from '@angular/core'; import { FormsModule, NG_VALUE_ACCESSOR, ReactiveFormsModule, UntypedFormControl } from '@angular/forms'; import { map } from 'rxjs/operators'; -import { FloatingPlacement, Keys, ModalModel, StatefulControlComponent, Subscriptions, TypedSimpleChanges, Types } from '@app/framework/internal'; +import { FloatingPlacement, Keys, ModalModel, StatefulControlComponent, StringHelper, Subscriptions, TypedSimpleChanges, Types } from '@app/framework/internal'; import { DropdownMenuComponent } from '../../dropdown-menu.component'; import { LoaderComponent } from '../../loader.component'; import { ModalPlacementDirective } from '../../modals/modal-placement.directive'; @@ -136,7 +136,7 @@ export class DropdownComponent extends StatefulControlComponent { if (Types.isString(x)) { diff --git a/frontend/src/app/framework/angular/pipes/highlight.pipe.ts b/frontend/src/app/framework/angular/pipes/highlight.pipe.ts index 5f7a72511..3e2458686 100644 --- a/frontend/src/app/framework/angular/pipes/highlight.pipe.ts +++ b/frontend/src/app/framework/angular/pipes/highlight.pipe.ts @@ -6,7 +6,7 @@ */ import { Pipe, PipeTransform } from '@angular/core'; -import { escapeHTML, Types } from '@app/framework/internal'; +import { escapeHTML, StringHelper, Types } from '@app/framework/internal'; @Pipe({ name: 'sqxHighlight', @@ -21,7 +21,7 @@ export class HighlightPipe implements PipeTransform { } if (Types.isString(highlight)) { - highlight = new RegExp(highlight, 'i'); + highlight = StringHelper.searchRegex(highlight); } const result = text.replace(highlight, s => `${s}`); diff --git a/frontend/src/app/framework/utils/string-helper.spec.ts b/frontend/src/app/framework/utils/string-helper.spec.ts index d9e1fc6c5..0b7fb1cd0 100644 --- a/frontend/src/app/framework/utils/string-helper.spec.ts +++ b/frontend/src/app/framework/utils/string-helper.spec.ts @@ -58,6 +58,17 @@ describe('StringHelper', () => { expect(StringHelper.hashCode('ABC')).not.toBe(StringHelper.hashCode('XYZ')); }); + it('should escape regex special characters', () => { + expect(StringHelper.escapeRegex('a(b)[c]*.+?^$|{}\\/-')).toBe('a\\(b\\)\\[c\\]\\*\\.\\+\\?\\^\\$\\|\\{\\}\\\\\\/\\-'); + }); + + it('should create search regex that matches special characters literally', () => { + const regex = StringHelper.searchRegex('(A.'); + + expect(regex.test('x(a.y')).toBeTruthy(); + expect(regex.test('xaby')).toBeFalsy(); + }); + it('should build query for empty object', () => { const url = StringHelper.buildQuery({}); diff --git a/frontend/src/app/framework/utils/string-helper.ts b/frontend/src/app/framework/utils/string-helper.ts index 8a0354f46..72ac1c6b4 100644 --- a/frontend/src/app/framework/utils/string-helper.ts +++ b/frontend/src/app/framework/utils/string-helper.ts @@ -54,6 +54,14 @@ export module StringHelper { } } + export function escapeRegex(value: string) { + return value.replace(/[.*+?^${}()|[\]\\/-]/g, '\\$&'); + } + + export function searchRegex(query: string) { + return new RegExp(escapeRegex(query), 'i'); + } + export function hashCode(value: string) { let hash = 0; diff --git a/frontend/src/app/shared/state/contributors.state.spec.ts b/frontend/src/app/shared/state/contributors.state.spec.ts index f41d9e263..4c70fe8fc 100644 --- a/frontend/src/app/shared/state/contributors.state.spec.ts +++ b/frontend/src/app/shared/state/contributors.state.spec.ts @@ -112,6 +112,19 @@ describe('ContributorsState', () => { expect(contributorsState.snapshot.pageSize).toEqual(10); }); + it('should treat search query as literal text', () => { + contributorsState.load().subscribe(); + contributorsState.search('(4'); + + let contributors: ReadonlyArray; + + contributorsState.contributorsFiltered.subscribe(result => { + contributors = result; + }); + + expect(contributors!).toEqual([]); + }); + it('should show notification on load if reload is true', () => { contributorsState.load(true).subscribe(); diff --git a/frontend/src/app/shared/state/contributors.state.ts b/frontend/src/app/shared/state/contributors.state.ts index fef5068c4..b8a6d553d 100644 --- a/frontend/src/app/shared/state/contributors.state.ts +++ b/frontend/src/app/shared/state/contributors.state.ts @@ -8,7 +8,7 @@ import { Injectable } from '@angular/core'; import { EMPTY, Observable, throwError } from 'rxjs'; import { catchError, finalize, tap } from 'rxjs/operators'; -import { debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, Types, VersionTag } from '@app/framework'; +import { debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, StringHelper, Types, VersionTag } from '@app/framework'; import { AssignContributorDto, ContributorDto, ContributorsDto } from '../model'; import { ContributorsService } from '../services/contributors.service'; import { AppsState } from './apps.state'; @@ -165,7 +165,7 @@ export class ContributorsState extends State { } function getRegex(query?: string): RegExp | undefined { - return query ? new RegExp(query, 'i') : undefined; + return query ? StringHelper.searchRegex(query) : undefined; } function getFilteredContributors(snapshot: Snapshot) { @@ -174,7 +174,7 @@ function getFilteredContributors(snapshot: Snapshot) { let filtered = contributors; if (query) { - const regex = new RegExp(query, 'i'); + const regex = StringHelper.searchRegex(query); filtered = filtered.filter(x => regex.test(x.contributorName)); } diff --git a/frontend/src/app/shared/state/schemas.state.ts b/frontend/src/app/shared/state/schemas.state.ts index 16a14d4b0..dbee19c35 100644 --- a/frontend/src/app/shared/state/schemas.state.ts +++ b/frontend/src/app/shared/state/schemas.state.ts @@ -8,7 +8,7 @@ import { Injectable } from '@angular/core'; import { EMPTY, forkJoin, Observable, of } from 'rxjs'; import { catchError, finalize, tap } from 'rxjs/operators'; -import { debug, DialogService, LoadingState, shareMapSubscribed, shareSubscribed, State, Version } from '@app/framework'; +import { debug, DialogService, LoadingState, shareMapSubscribed, shareSubscribed, State, StringHelper, Version } from '@app/framework'; import { AddFieldDto, ChangeCategoryDto, ConfigureFieldRulesDto, ConfigureUIFieldsDto, CreateSchemaDto, ExportContentsDto, FieldDto, MigrateContentsDto, NestedFieldDto, SchemaDto, SynchronizeSchemaDto, UpdateFieldDto, UpdateSchemaDto } from '../model'; import { SchemasService } from '../services/schemas.service'; import { AppsState } from './apps.state'; @@ -454,7 +454,7 @@ export function getCategoryTree(allSchemas: ReadonlyArray, categories let match = (_: SchemaDto) => true; if (filter) { - const terms = filter.split(' ').map(filter => new RegExp(filter.trim(), 'i')); + const terms = filter.split(' ').map(filter => StringHelper.searchRegex(filter.trim())); const matches = (value: string | undefined | null) => { return !!value && terms.every(term => value.search(term) >= 0);