emposha / FCBKcomplete

Jquery facebook like(fancy) multi-select
http://www.emposha.com
383 stars 115 forks source link

Diacritics issues… and fix proposal #125

Open abstractmatter opened 12 years ago

abstractmatter commented 12 years ago

Hi,

As french user, we face the diacritics issue filtering the content (searching for "franc" gives "franç"). I added this code within the plug-in:

-- tool funcs ---

var nodiacritics = function(s) { s = unescapeHTML(s); var diacritics = [ [/[\300-\306]/g, 'A'], [/[\340-\346]/g, 'a'], [/[\310-\313]/g, 'E'], [/[\350-\353]/g, 'e'], [/[\314-\317]/g, 'I'], [/[\354-\357]/g, 'i'], [/[\322-\330]/g, 'O'], [/[\362-\370]/g, 'o'], [/[\331-\334]/g, 'U'], [/[\371-\374]/g, 'u'], [/[\321]/g, 'N'], [/[\361]/g, 'n'], [/[\307]/g, 'C'], [/[\347]/g, 'c'], [/[\377]/g, 'y'], ]; for (var i = 0; i < diacritics.length; i++) { s = s.replace(diacritics[i][0], diacritics[i][1]); } return s; };

var unescapeHTML = function (s) { var temp = document.createElement("div"); temp.innerHTML = s; var result = temp.childNodes[0].nodeValue; temp.removeChild(temp.firstChild); return result; };

--- modified search function of the cache object ---

'search': function (text, callback) { var temp = new Array(); var regex = new RegExp((options.filter_begin ? '^' : '') + nodiacritics(text), (options.filter_case ? "g": "gi")); $.each(element.data("cache"), function (i, _elem) { if (typeof _elem.search === 'function') { if (nodiacritics(_elem).search(regex) != -1) { temp.push({'key': i, 'value': _elem}); } } }); return temp; }

--- modified illuminated func (sorry, no more filter_begin with this func) ---

function itemIllumination(text, etext) { var string_regex = (options.filter_case ? nodiacritics(etext) : nodiacritics(etext).toLowerCase()); try { var regex = new RegExp(string_regex, ((options.filter_case) ? "g":"gi")); var original = unescapeHTML(text); var text = nodiacritics(text); var pos = text.search(regex); if (pos != -1) { var text = original.substr(0,pos) + '' + original.substr(pos,etext.length) + '' + original.substr(pos+etext.length); } } catch(ex) {}; return text; }