podio / jquery-mentions-input

MIT License
985 stars 333 forks source link

There is a problem when mentioned username contains $& in it. #197

Open karthifairhawn opened 1 year ago

karthifairhawn commented 1 year ago

MentionsInput.updateValues()

function updateValues() {
    var syntaxMessage = getInputBoxValue(); //Get the actual value of the text area

    _.each(mentionsCollection, function (mention) {
        var textSyntax = settings.templates.mentionItemSyntax(mention);
        syntaxMessage = syntaxMessage.replace(new RegExp(utils.regexpEncode(mention.value), 'g'), textSyntax);
    });

    var mentionText = utils.htmlEncode(syntaxMessage); //Encode the syntaxMessage

    _.each(mentionsCollection, function (mention) {
        var formattedMention = _.extend({}, mention, {value: utils.htmlEncode(mention.value)});
        var textSyntax = settings.templates.mentionItemSyntax(formattedMention);
        var textHighlight = settings.templates.mentionItemHighlight(formattedMention);

        mentionText = mentionText.replace(new RegExp(utils.regexpEncode(textSyntax), 'g'), textHighlight);
    });

    mentionText = mentionText.replace(/\n/g, '<br />'); //Replace the escape character for <br />
    mentionText = mentionText.replace(/ {2}/g, '&nbsp; '); //Replace the 2 preceding token to &nbsp;

    elmInputBox.data('messageText', syntaxMessage); //Save the messageText to elmInputBox
    elmInputBox.trigger('updated');
    elmMentionsOverlay.find('div').html(mentionText); //Insert into a div of the elmMentionsOverlay the mention text
}

As per text.replace() -- MDN

When the mentioning username contains the value $& in it, It will be replaced by the username itself again which leads to wrong username value.

Input Username He$&llo

Username Constructed after Regex Manipulation HeHe$&llollo

syntaxMessage = syntaxMessage.replace(new RegExp(utils.regexpEncode(mention.value), 'g'), textSyntax);
mentionText = mentionText.replace(new RegExp(utils.regexpEncode(textSyntax), 'g'), textHighlight);