chaitu236 / TakWeb

Javascript based Tak client
https://www.playtak.com
GNU General Public License v2.0
40 stars 18 forks source link

Name highlighting is broken #64

Closed TreffnonX closed 8 years ago

TreffnonX commented 8 years ago

On the master, the name highlighting does not work due to a bad reference.

// someone said our name and link in string doesn't contain name if (occ === occ2 && txt.indexOf(this.myname) > -1) { var tmp = txt.split(this.myname); txt = tmp[0] + '<span class="chatmyname">' + this.myname + '</span>' + tmp[1]; }

the variable myname is in the server object. Also only the first instance of the name will be replaced. Code could be optimized like this: (tested on my fork)

txt = txt.replace(new RegExp('(?:^|\\s)' + server.myname + '(?:^|\\s)', 'gi'), '<span class="chatmyname">$&</span>');

alternatively myname could be moved to the chat handler, but I am not sure, if the variable is used inside server for other purposes.

chaitu236 commented 8 years ago

Thanks Treff. Fixed in fc4f6929143d61ad1dd7f04552a5de0213000e99. Your method however doesn't handle links containing names.

TreffnonX commented 8 years ago

Yeah, I just realized it's a bit more complicated. Still I think regex is the way to go here.

chaitu236 commented 8 years ago

Ah, stupid of me.. I didn't realize I could do it with regex. Changed in 824dbf6bb73167b553005fb3265249bc0cdf6cd3

TreffnonX commented 8 years ago

I finally got it. This time it combines both linkify and name highlighting without any issues (I have found so far). The clue was to demand an even number of '<' and '>' before each match. This way highlighting is not done inside tags. It is however done in the link text. So kakaburra.com will highlight kakaburra, but the link will work and not be scrambled. Cheers!

  var options = {/* ... */};`
  message = message.linkify(options);

  // highlight own name.
  message = ' ' + message + ' ';
  message = message.replace(new RegExp('((?:[^\\w\\d\\<\\>]|(?:\\<[^\\>]*\\>))*)('
      + server.myname + ')(?=[^\\w\\d])', 'gi'),
      '$1<span class="chatmyname">$2</span>');
chaitu236 commented 8 years ago

Ah great thanks!