c9e09744ad3e9050ff9ab8395de4a2fc1196b016 hiram Sun Sep 8 10:51:52 2024 -0700 correctly trim white space from before and after the query string and correctly test for words beginning with + or - refs #32596 diff --git src/hg/js/assemblySearch.js src/hg/js/assemblySearch.js index 39c4bcb..bd01a27 100644 --- src/hg/js/assemblySearch.js +++ src/hg/js/assemblySearch.js @@ -123,31 +123,32 @@ comment = document.getElementById("comment"); requestSubmitButton = document.getElementById("submitButton"); document.getElementById("modalFeedback").addEventListener("submit", checkForm, false); modalInit(); var tableBody = document.getElementById('tableBody'); tableBody.innerHTML = '(empty table)'; clearButton.addEventListener('click', function() { searchInput.value = ''; // Clear the search input field }); searchForm.addEventListener('submit', function(event) { event.preventDefault(); // Prevent form submission - var searchTerm = document.getElementById('searchBox').value; + // the trim() removes stray white space before or after the string + var searchTerm = document.getElementById('searchBox').value.trim(); var resultCountLimit = document.getElementById('maxItemsOutput'); var mustExist = document.getElementById('mustExist').checked; var notExist = document.getElementById('notExist').checked; browserExist = "mustExist"; if (mustExist && notExist) { browserExist = "mayExist"; } else if (notExist) { browserExist = "notExist"; } makeRequest(searchTerm, browserExist, resultCountLimit.value); }); advancedSearchButton.addEventListener('click', function() { document.getElementById('urlCopyLink').style.display = "none"; let searchOptions = document.getElementById("advancedSearchOptions"); @@ -229,31 +230,31 @@ }); var tableHeader = document.getElementById('tableHeader'); headerRefresh(tableHeader); if (urlParams.has('maxItemsOutput')) { maxItemsOutput = parseInt(urlParams.get('maxItemsOutput'), 10); if (maxItemsOutput < 1) { maxItemsOutput = 1; } else if (maxItemsOutput > 1000) { maxItemsOutput = 1000; } document.getElementById('maxItemsOutput').value = maxItemsOutput; } if (urlParams.has('q')) { - query = urlParams.get('q'); + query = urlParams.get('q').trim(); if (query.length > 0) { searchInput.value = query; document.getElementById('submitSearch').click(); } } document.getElementById("measureTiming").style.display = "none"; }); // refresh the thead columns in the specified table function headerRefresh(tableHead) { // clear existing content tableHead.innerHTML = ''; // re-populate header row - the sortable system added a class to // the last sorted column, need to rebuild the headerRow to get the // header back to pristine condition for the next sort @@ -314,31 +315,31 @@ prefix.push(noPrefix.replace(/\*$/, '')); } } else { wholeWord.push(noPrefix); } } if (wholeWord.length > 0) { for (let word of wholeWord) { for (let key in rowData) { if (rowData.hasOwnProperty(key)) { if (typeof rowData[key] === 'string') { let value = rowData[key]; let subWords = value.match(/(\w+)|(\W+)/g); let newString = ""; let wholeSubs = word.match(/(\w+)/g); - if (wholeSubs.length > 0) { + if (wholeSubs && wholeSubs.length > 0) { for (let whole of wholeSubs) { for (let subWord of subWords) { if ( whole.toLowerCase() === subWord.toLowerCase() ) { newString += "" + subWord + ""; } else { newString += subWord; } } newString = newString.trim(); if (newString !== rowData[key]) rowData[key] = newString; } // for (let whole of wholeSubs) } // if (wholeSubs.length > 0) } // if (typeof rowData[key] === 'string') } // if (rowData.hasOwnProperty(key)) @@ -387,31 +388,31 @@ var genomicEntries = {}; var extraInfo = {}; for (var key in jsonData) { if (jsonData[key].scientificName) { genomicEntries[key] = jsonData[key]; } else { extraInfo[key] = jsonData[key]; } } headerRefresh(tableHeader); var count = 0; for (var id in genomicEntries) { - highlightMatch(extraInfo.q, genomicEntries[id]); + highlightMatch(extraInfo.q.trim(), genomicEntries[id]); var dataRow = ''; var browserUrl = id; var asmInfoUrl = id; if (genomicEntries[id].browserExists) { if (id.startsWith("GC")) { browserUrl = "View"; asmInfoUrl = "" + id + ""; } else { browserUrl = "View"; asmInfoUrl = "" + id + ""; } dataRow += "" + browserUrl + ""; } else { dataRow += ""; } @@ -436,31 +437,31 @@ hardSpace = ""; } status += ""; // status += hardSpace + ""; dataRow += status; dataRow += ''; tableBody.innerHTML += dataRow; } var dataTable = document.getElementById('dataTable'); sorttable.makeSortable(dataTable); var itemCount = parseInt(extraInfo.itemCount, 10); var totalMatchCount = parseInt(extraInfo.totalMatchCount, 10); var availableAssemblies = parseInt(extraInfo.availableAssemblies, 10); - var resultCounts = "results for search string: '" + extraInfo.q + "', "; + var resultCounts = "results for search string: '" + extraInfo.q.trim() + "', "; if ( itemCount === totalMatchCount ) { resultCounts += "showing " + itemCount.toLocaleString() + " match results, "; } else { resultCounts += "showing " + itemCount.toLocaleString() + " match results "; resultCounts += "from " + totalMatchCount.toLocaleString() + " total matches, "; } resultCounts += "out of " + availableAssemblies.toLocaleString() + " total number of assemblies"; document.getElementById('resultCounts').innerHTML = resultCounts; if (measureTiming) { var etMs = extraInfo.elapsedTimeMs; var elapsedTime = "" + etMs.toLocaleString() + " milliseconds"; if ( etMs > 1000 ) { var etSec = etMs/1000; elapsedTime = "" + etSec.toFixed(2) + " seconds"; } @@ -740,40 +741,40 @@ var asmStatus = document.querySelector('input[name="asmStatus"]:checked').value; var refSeqCategory = document.querySelector('input[name="refSeqCategory"]:checked').value; var asmLevel = document.querySelector('input[name="asmLevel"]:checked').value; // start with what the user requested: var queryString = query; // for allWords, place + sign in front of each word if not already there if (wordMatch === "allWords") { var words = queryString.split(/\s+/); if (words.length > 1) { // not needed on only one word var queryPlus = ""; // compose new query string let inQuote = false; words.forEach(function(word) { if (word.match(/^[-+]?"/)) { - if (word.match(/^[-+]/)) + if (/^[-+]/.test(word)) queryPlus += " " + word; // do not add + to - or + already there else queryPlus += " +" + word; inQuote = true; } else if (inQuote) { queryPlus += " " + word; // space separates each word if (word.endsWith('"')) inQuote = false; - } else if (word.match(/[^-+]/)) { + } else if (/^[-+]/.test(word)) { queryPlus += " " + word; // do not add + to - or + already there } else { queryPlus += " +" + word; } }); queryString = queryPlus.trim(); } } // remove stray white space from beginning or end queryString = queryString.trim(); // Show the wait spinner document.querySelector(".submitContainer").classList.add("loading"); document.getElementById("loadingSpinner").style.display = "block";