From 9605f367f9021324f5d03023005be1086ace9954 Mon Sep 17 00:00:00 2001 From: Matt Triff Date: Thu, 30 Dec 2021 22:05:03 -0500 Subject: [PATCH] Search using non-sanitised input value --- src/scripts/choices.test.ts | 32 ++++++++++++++++++++++++++++++++ src/scripts/choices.ts | 2 +- src/scripts/components/input.ts | 4 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/scripts/choices.test.ts b/src/scripts/choices.test.ts index 9ff6987..5d54936 100644 --- a/src/scripts/choices.test.ts +++ b/src/scripts/choices.test.ts @@ -1734,6 +1734,38 @@ describe('choices', () => { }); }); + describe('events', () => { + describe('search', () => { + beforeEach(() => { + document.body.innerHTML = ` + + `; + + instance = new Choices('[data-choice]', { + allowHTML: false, + searchEnabled: true, + }); + }); + + it('details are passed', (done) => { + const query = + 'This is a query & a "test" with characters that should not be sanitised.'; + + instance.input.value = query; + instance.input.focus(); + instance.passedElement.element.addEventListener('search', (event) => { + expect(event.detail).to.eql({ + value: query, + resultCount: 0, + }); + done(); + }); + + instance._onKeyUp({ target: null, keyCode: null }); + }); + }); + }); + describe('private methods', () => { describe('_createGroupsFragment', () => { let _createChoicesFragmentStub; diff --git a/src/scripts/choices.ts b/src/scripts/choices.ts index a448656..62e8f83 100644 --- a/src/scripts/choices.ts +++ b/src/scripts/choices.ts @@ -1513,7 +1513,7 @@ class Choices implements Choices { this._isSearching = false; this._store.dispatch(activateChoices(true)); } else if (canSearch) { - this._handleSearch(this.input.value); + this._handleSearch(this.input.rawValue); } } diff --git a/src/scripts/components/input.ts b/src/scripts/components/input.ts index 0c911ea..6eafc07 100644 --- a/src/scripts/components/input.ts +++ b/src/scripts/components/input.ts @@ -52,6 +52,10 @@ export default class Input { this.element.value = value; } + get rawValue(): string { + return this.element.value; + } + addEventListeners(): void { this.element.addEventListener('paste', this._onPaste); this.element.addEventListener('input', this._onInput, {