Making WordPress.org

Changeset 3167


Ignore:
Timestamp:
05/18/2016 11:11:33 AM (10 years ago)
Author:
ocean90
Message:

Trac: Improve handling of mentions in HTML attributes.

  • Revert [3151].
  • Make the regex less greedy and add support for multiline attributes.
  • Replace matches for mentions in attributes with a placeholder so other mentions in none-attributes can be still replaced.

See #1541.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/style/trac/wp-trac.js

    r3151 r3167  
    5252        ];
    5353
    54         // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters
    55         function escapeRegExp( string ) {
    56                 return string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' );
    57         }
    58 
    5954        wpTrac = {
    6055
    … …  
    10398                linkMentions: function() {
    10499                        // See https://github.com/regexps/mentions-regex/blob/master/index.js#L21
    105                         var mentionsRegEx = /(^|[^a-zA-Z0-9_@!@#$%&*])(?:(?:@|@)(?!\/))([a-zA-Z0-9/_.]{1,20})(?:\b(?!@|@)|$)/g;
     100                        var mentionsRegEx = /(^|[^a-zA-Z0-9_@!@#$%&*])(?:(?:@|@)(?!\/))([a-zA-Z0-9/_.]{1,20})(?:\b(?!@|@)|$)/g,
     101                                mentionsInAttrRegEx = new RegExp( '="[^"]*?' + mentionsRegEx.source + '[\\s\\S]*?"' );
    106102
    107103                        $( 'div.change .comment, #ticket .description' ).each( function() {
    108104                                $comment = $( this ).html();
    109105                                if ( mentionsRegEx.test( $comment ) ) {
     106                                        var placeholders = [];
     107
     108                                        if ( mentionsInAttrRegEx.test( $comment ) ) {
     109                                                // Preserve mentions in HTML attributes.
     110                                                $comment = $comment.replace( mentionsInAttrRegEx, function( match ) {
     111                                                        placeholders.push( match );
     112                                                        return '__PLACEHOLDER__';
     113                                                } );
     114                                        }
     115
    110116                                        $comment = $comment.replace( mentionsRegEx, function( match, pre, username ) {
    111117                                                if ( -1 !== $.inArray( username, reservedTerms ) ) {
    … …  
    113119                                                }
    114120
    115                                                 var matchInAttr = new RegExp( '=".*' + escapeRegExp( match ) + '.*"' );
    116                                                 if ( matchInAttr.test( $comment ) ) {
    117                                                         return match;
    118                                                 }
    119 
    120121                                                var meClass = ( username === wpTrac.currentUser ) ? ' me' : '';
    121122                                                return pre + '<a class="mention' + meClass + '" href="https://profiles.wordpress.org/' + username + '">@' + username + '</a>';
    122123                                        } );
     124
     125                                        // Restore mentions in HTML attributes.
     126                                        if ( placeholders.length ) {
     127                                                $comment = $comment.replace( '__PLACEHOLDER__', function() {
     128                                                        return placeholders.shift();
     129                                                } );
     130                                        }
     131
    123132                                        $( this ).html( $comment );
    124133                                }
Note: See TracChangeset for help on using the changeset viewer.