Allow multiple use of a HTMLElement/jQuery argument in jQueryMsg

This resolves the issue that one cannot use a replacement placeholder
(e.g. `$4`) multiple times in a message if one intends to provide an
jQuery or HTMLElement as argument.

Change-Id: I6246264621c05fdb894d64e86d3ca248c11156a5
This commit is contained in:
Michael Große 2020-01-21 17:38:38 +01:00
parent eb583a4417
commit bb48ecbbc7
2 changed files with 36 additions and 0 deletions

View file

@ -1059,6 +1059,22 @@ mw.jqueryMsg.HtmlEmitter.prototype = {
var index = parseInt( nodes[ 0 ], 10 );
if ( index < replacements.length ) {
if ( typeof replacements[ index ] !== 'string' ) {
if ( !replacements[ index ].hasAlreadyBeenUsedAsAReplacement ) {
// only actually clone on second use
replacements[ index ].hasAlreadyBeenUsedAsAReplacement = true;
return replacements[ index ];
}
if ( typeof replacements[ index ].clone === 'function' ) {
// if it is a jQuery object, use jQuery's clone method
return replacements[ index ].clone( true );
}
if ( typeof replacements[ index ].cloneNode === 'function' ) {
// if it is a Node, then use the native cloning functionality
return replacements[ index ].cloneNode( true );
}
return replacements[ index ];
}
return replacements[ index ];
} else {
// index not found, fallback to displaying variable

View file

@ -189,6 +189,26 @@
'HTMLElement[] arrays are preserved as raw html'
);
mw.messages.set( 'simple-double-replace', 'Foo 1: $1 2: $1' );
assert.strictEqual(
formatParse( 'simple-double-replace', 'bar' ),
'Foo 1: bar 2: bar',
'string params can be used multiple times'
);
mw.messages.set( 'object-double-replace', 'Foo 1: $1 2: $1' );
assert.strictEqual(
formatParse( 'object-double-replace', $( '<div class="bar">&gt;</div>' ) ),
'Foo 1: <div class="bar">&gt;</div> 2: <div class="bar">&gt;</div>',
'jQuery objects can be used multiple times'
);
assert.strictEqual(
formatParse( 'object-double-replace', $( '<div class="bar">&gt;</div>' ).get( 0 ) ),
'Foo 1: <div class="bar">&gt;</div> 2: <div class="bar">&gt;</div>',
'HTMLElement can be used multiple times'
);
assert.strictEqual(
formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
'Foo <a href="http://example.org/?x=y&amp;z">bar</a>',