Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #22560 from ADLR-es/fix-bug-1040881
Browse files Browse the repository at this point in the history
Bug 1040881 - Searching doesn't work on contacts with middle names
  • Loading branch information
jmcanterafonseca committed Aug 8, 2014
2 parents 1826e29 + 3f8a5e7 commit 0366e78
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
21 changes: 21 additions & 0 deletions apps/communications/contacts/test/unit/views/list_test.js
Expand Up @@ -1270,6 +1270,27 @@ suite('Render contacts list', function() {
});
});

test('Search for contacts with middle names', function(done) {
mockContacts = new MockContactsList();
var contactIndex = Math.floor(Math.random() * mockContacts.length);
var contact = mockContacts[contactIndex];
mockContacts[contactIndex].givenName[0] = contact.givenName[0] + ' Juan';

doLoad(subject, mockContacts, function() {
contacts.List.initSearch(function onInit() {
searchBox.value = contact.givenName[0][0] + ' ' +
contact.familyName[0][0];
contacts.Search.search(function search_finished() {
contacts.Search.invalidateCache();
done(function() {
assert.isTrue(noResults.classList.contains('hide'));
assertContactFound(contact);
});
});
});
});
});

test('Search phrase highlightded correctly for first letter',
function(done) {
mockContacts = new MockContactsList();
Expand Down
30 changes: 22 additions & 8 deletions shared/js/contacts/search.js
Expand Up @@ -22,6 +22,7 @@ contacts.Search = (function() {
// On the steady state holds the list result of the current search
searchableNodes = null,
currentTextToSearch = '',
currentSearchTerms = [],
prevTextToSearch = '',
// Pointer to the nodes which are currently on the result list
currentSet = {},
Expand Down Expand Up @@ -153,11 +154,14 @@ contacts.Search = (function() {
// This regexp match against everything except HTML tags
// 'currentTextToSearch' should be relatively safe from
// regex symbols getting passed through since it was previously normalized
var hRegEx = new RegExp('(' + currentTextToSearch + ')(?=[^>]*<)', 'gi');
node.innerHTML = node.innerHTML.replace(
hRegEx,
'<span class="' + highlightClass + '">$1</span>'
);
for (var i=0, l=currentSearchTerms.length; i<l; i++) {
var hRegEx = new RegExp('(' + currentSearchTerms[i] + ')(?=[^>]*<)',
'gi');
node.innerHTML = node.innerHTML.replace(
hRegEx,
'<span class="' + highlightClass + '">$1</span>'
);
}
};

var updateSearchList = function updateSearchList(cb) {
Expand Down Expand Up @@ -216,6 +220,7 @@ contacts.Search = (function() {
function resetState() {
prevTextToSearch = '';
currentTextToSearch = '';
currentSearchTerms = [];
searchableNodes = null;
canReuseSearchables = false;
currentSet = {};
Expand Down Expand Up @@ -365,10 +370,11 @@ contacts.Search = (function() {

// Search the next chunk of contacts
var end = from + CHUNK_SIZE;
currentSearchTerms = pattern.source.split(/\s+/);
for (var c = from; c < end && c < contacts.length; c++) {
var contact = contacts[c].node || contacts[c];
var contactText = contacts[c].text || getSearchText(contacts[c]);
if (!pattern.test(contactText)) {
if (!checkContactMatch(currentSearchTerms, contactText)) {
if (contact.dataset.uuid in currentSet) {
searchList.removeChild(currentSet[contact.dataset.uuid]);
delete currentSet[contact.dataset.uuid];
Expand Down Expand Up @@ -475,11 +481,10 @@ contacts.Search = (function() {

// If we have a current search then we need to determine whether the
// new nodes should show up in that search.
var pattern = new RegExp(currentTextToSearch, 'i');
for (var i = 0, n = nodes.length; i < n; ++i) {
var node = nodes[i];
var nodeText = getSearchText(node);
if (pattern.test(nodeText)) {
if (!checkContactMatch(currentSearchTerms, nodeText)) {
searchableNodes.push({
node: node,
text: nodeText
Expand All @@ -488,6 +493,15 @@ contacts.Search = (function() {
}
};

var checkContactMatch = function checkContactMatch(searchTerms, text) {
for (var i=0, m=searchTerms.length; i < m; i++){
if (!RegExp(searchTerms[i], 'i').test(text)) {
return false;
}
}
return true;
};

var search = function performSearch(searchDoneCb) {
prevTextToSearch = currentTextToSearch;

Expand Down

0 comments on commit 0366e78

Please sign in to comment.