Skip to content

Commit 154215c

Browse files
committed
cveRecordSearchModule: add normalizeSearchString(); cveRecord: add isErrorMessage
1 parent 66ac2af commit 154215c

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/components/cveRecordSearchModule.vue

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ import { usecveRecordStore, useErrorMessageStore } from '@/stores/cveRecord';
6565
import { useGenericGlobalsStore } from '@/stores/genericGlobals';
6666
6767
const cveIdRegex = /^CVE\p{Pd}(?<year>\d{4})\p{Pd}(?<id>\d{4,})$/iu;
68-
const wordRegex = /^[a-z0-9 ]+$/i;
6968
7069
// This is the current maximum supported length of the "suffix" portion of
7170
// the CVE ID. The schema defines a suffix length up to 19, but code in
@@ -85,7 +84,6 @@ const searchOptionLabel = 'Search CVE List';
8584
8685
let prevSearchValue = ref('');
8786
let queryString = ref('');
88-
let errorMessage = ref('');
8987
9088
let cveGenericGlobalsStore = useGenericGlobalsStore();
9189
let cveRecordStore = usecveRecordStore();
@@ -119,7 +117,7 @@ watch(
119117
&& !cveGenericGlobalsStore.isProductionWebsite)
120118
searchType.value = legacyOptionLabel;
121119
else if (route.query?.query) {
122-
queryString.value = route.query.query.trim();
120+
queryString.value = route.query.query;
123121
validate();
124122
}
125123
}
@@ -200,23 +198,17 @@ function validateQueryString() {
200198
}
201199
202200
if (isSearch) {
201+
if (!errorMessageStore.isErrorMessage) {
203202
204-
// Search: if the query string isn't a CVE ID and it doesn't just consist
205-
// of "typical" words (alphanumeric phrases), then it's an error.
206-
207-
if (!cveIdMatch && !wordRegex.test(searchValue)) {
208-
errorMessageStore.setErrorMessage(contentMessage);
209-
} else if (!errorMessage.value) {
210-
211-
// The provided search string is good.
203+
// The search can proceed with the provided string.
212204
cveListSearchStore.isSearchButtonDisabled = false;
213205
}
214206
} else if (cveIdValid) {
215207
216208
// Legacy Find by CVE ID and it's in the correct format.
217209
cveListSearchStore.isSearchButtonDisabled = false;
218210
219-
} else if (!errorMessage.value) {
211+
} else if (!errorMessageStore.isErrorMessage) {
220212
221213
// Legacy Find by CVE ID but the query string is not a CVE ID.
222214
errorMessageStore.cveIdFormatMessage();
@@ -225,6 +217,25 @@ function validateQueryString() {
225217
return !cveListSearchStore.isSearchButtonDisabled;
226218
}
227219
220+
function normalizeSearchString() {
221+
222+
if (!searchTypeBoolean.value)
223+
return
224+
225+
// Each search string is "normalized" by removing excess spaces (both at
226+
// the beginning and end of the string, as well as compressing multiple
227+
// spaces separating "words" to a single space). Dashes (em-, en-) that
228+
// may have been pasted in from elsewhere (e.g., Word) are converted to
229+
// the minus (hyphen) character.
230+
231+
let normalizedString = queryString.value.trim().replace(/\s+/g, ' ');
232+
233+
normalizedString = normalizedString.replace(/\p{Pd}/g, '-');
234+
235+
if (normalizedString !== queryString.value)
236+
queryString.value = normalizedString;
237+
}
238+
228239
function onInputChange() {
229240
// This function is called when the search string changes. The only purpose
230241
// is to clear the way for the search if there's a value (it's not empty)
@@ -261,6 +272,8 @@ function validate() {
261272
//
262273
// Provided the search string is valid, the search is started.
263274
275+
normalizeSearchString();
276+
264277
if (validateQueryString()) {
265278
try {
266279
cveListSearchStore.isSearchButtonDisabled = true;

src/stores/cveRecord.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export const useErrorMessageStore = defineStore('errorMessage', {
8282
showErrorMessage: false
8383
}
8484
},
85+
getters: {
86+
isErrorMessage: (state) => state.errorMessage.length > 0,
87+
},
8588
actions: {
8689
cveIdFormatMessage() {
8790
const formatMessage = 'Required CVE ID format: CVE-YYYY-NNNN';

0 commit comments

Comments
 (0)