6

I have a .NET MVC page with a list of items that each have <%: %> encoded descriptions in the rel. I want to be able to search for all items with a rel that contains my search query.

One of the fields has a value with htmlentities rel='D&eacute;coration'

I type "Décoration" in the search box, let jQuery search for all elements that have a 'rel' attribute that contains (indexOf != -1) that value:

no results!

Why? because Décoration != D&eacute;coration.

What would be the best solution to compare these two? (Has to work for all special accented characters, not just &eacute;)

P.S. (I tried escape/unescape on both sides, also tried the trick to append it to a div and then read it as text, this replaces dangerous stuff, but doesn't replace é (it doesn't have to because it's valid in utf-8 anyway))

Wayne Koorts
  • 10,861
  • 13
  • 46
  • 72
Michiel Cornille
  • 2,067
  • 1
  • 19
  • 42
  • 8
    Escape your input using Javascript before searching. See [this question](http://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript). – Humberto Feb 04 '11 at 13:42
  • 1
    Don't mangle your source code to avoid unwanted formatting. Instead, use the `{}` toolbar button to tag it as code. I've done it for you this time. – Álvaro González Feb 04 '11 at 13:47
  • Why do you match on a translated string ? Can't you use an identifier that won't change ? – Nekresh Feb 04 '11 at 13:53
  • Humberto, I was hoping for a built in solution but apparently that doesn't really exist. Alvaro, thanks for the hints, i'm pretty new to stackoverflow. While we're at it alvaro, how can I flag humberto's mini-comment as the answer? Nekresh, no I can't do that – Michiel Cornille Feb 04 '11 at 15:18
  • you'll have to wait for @Humberto to add his comment as an answer - then you can accept it. He might pick up on this if we poke him a bit! – Fenton Mar 14 '11 at 15:07

3 Answers3

2

Since the &eacute; and like are html entities, you can set the html content of a temporary div with the garbled string, and retrive the decoded string using the text content of the element. The browser will do the decoding work for you.

Using jQuery :

function searchInRel(needle) {
    return $('[rel]').filter(function(i,e) {
        var decodedText = $('<div/>').html(e.attr('rel')).text();
        return (decodedText.indexOf(needle) != -1);
    };
}

Using just the DOM :

function decodeEntities(text) {
    var tempDiv = document.getElementById('tempDiv');
    tempDiv.innerHTML = text;
    return tempDiv.textContent;
}
Suzanne Soy
  • 3,027
  • 6
  • 38
  • 56
1

If you serve your pages with UTF-8 encoding, you won't need to use entities for all the accented characters. Problem solved.

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

You can decode the html entities. Just copy the two javascript methods from HERE

var decoded = 'Décoration';
var entity = html_entity_decode('D&eacute;coration');
console.log(decoded == entity);
ChrisThompson
  • 1,998
  • 12
  • 17