diff --git a/cypress/integration/trim_values_before_search.spec.js b/cypress/integration/trim_values_before_search.spec.js new file mode 100644 index 000000000..e14cb61e3 --- /dev/null +++ b/cypress/integration/trim_values_before_search.spec.js @@ -0,0 +1,43 @@ +describe('Trim values before search', () => { + beforeEach(() => { + cy.visit('/tests/index.html'); + }); + + it('Matches even if there are leading and trailing spaces', () => { + cy.selectpicker({ + html: ` + + ` + }).then(($select) => { + const button = `[data-id="${$select[0].id}"]`; + cy.get(button).click(); + $select.on('fetched.bs.select', cy.stub().as('fetched')); + [{ + searchValue: ' app ', // leading and trailing spaces + expectedContain: 'apple' + }, { + searchValue: '  pea', // both half and full space mixed + expectedContain: 'pear' + }, { + searchValue: ' ', // text with half-space also match + expectedContain: 'dragon fruit' + }].forEach((test) => { + cy.get('input').type(test.searchValue); + cy.get('.dropdown-menu').find('li').first().should(($el) => { + expect($el).to.have.class('active') + expect($el).to.contain(test.expectedContain); + }); + cy.get('.dropdown-menu').find('li').first().click(); + cy.get(button).contains(test.expectedContain).click(); + }); + }); + }); +}); diff --git a/js/bootstrap-select.js b/js/bootstrap-select.js index d59c189c9..e9e413785 100644 --- a/js/bootstrap-select.js +++ b/js/bootstrap-select.js @@ -3007,6 +3007,11 @@ this.$searchbox.on('input propertychange', function () { var searchValue = that.$searchbox[0].value; + var isWhitespace = /^\s*$/.test(searchValue); + if (!isWhitespace) { + // trim leading and trailing half-width spaces and full-width spaces. + searchValue = searchValue.replace(/^\s+|\s+$/g, ''); + } that.selectpicker.search.elements = []; that.selectpicker.search.data = [];