Skip to content

Commit 32fbca9

Browse files
committed
UnblockReview: fix wrong test and associated code
1 parent 4f303e4 commit 32fbca9

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

‎UnblockReview/modules/UnblockReview.js‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ export class UnblockReview {
8282
const templateName = initialText.match( /^\{\{([A-Za-z-]+)/i )[ 1 ];
8383
let matchedReason = hasUsernameParameter ? match[ 3 ] : match[ 1 ];
8484
let remainingText = wikitext.slice( match.index + match[ 0 ].length );
85+
// Preserve the existing plain-text replacement for a leading {{u|...}}.
86+
if ( !hasUsernameParameter && /^\{\{\s*u\s*\|/i.test( matchedReason ) ) {
87+
const reasonAndRemainingText = matchedReason + remainingText;
88+
const userTemplate = reasonAndRemainingText.match( /^(\{\{\s*u\s*\|\s*)([^{}|\n]+?)(\}\})/i );
89+
if ( !userTemplate || !matchedReason.startsWith( userTemplate[ 1 ] + userTemplate[ 2 ] ) ) {
90+
throw new Error( 'Unable to safely match the nested template in the unblock reason' );
91+
}
92+
const closeOffset = userTemplate[ 1 ].length + userTemplate[ 2 ].length;
93+
const remainingStart = Math.max( matchedReason.length, closeOffset + 2 );
94+
matchedReason = userTemplate[ 2 ] + reasonAndRemainingText.slice( closeOffset + 2, remainingStart );
95+
remainingText = reasonAndRemainingText.slice( remainingStart );
96+
}
8597
// If the match starts inside {{u|...}}, unwrap it without assuming how other templates render.
8698
const nestedStart = initialText.lastIndexOf( '{{' );
8799
if ( nestedStart > 0 && nestedStart > initialText.lastIndexOf( '}}' ) ) {
@@ -338,11 +350,8 @@ export class UnblockReview {
338350
* @return {string} Regex pattern for matching the original wikitext.
339351
*/
340352
getAppealReasonPattern( appealReason, hasUsernameParameter = false ) {
341-
// Matches tags, wikilink markup, templates, and emphasis apostrophes between rendered characters.
342-
// Template pattern allows both opening {{ and closing }} to appear between characters,
343-
// since rendered text from {{u|username}} renders as just "username" with the template markup invisible.
344-
// The brace depth check in getLeftHalfOfUnblockTemplate() ensures we don't match inside nested templates.
345-
const markup = '(?:<\\/?[a-zA-Z][a-zA-Z0-9:-]*(?:\\s+(?:"[^"]*"|\'[^\']*\'|[^\'"<>])*)?\\s*\\/?>|\\[\\[[^\\[\\]|\\n]*\\||\\[\\[|\\]\\]|\'{2,}(?!\')|\\{[^{}]*\\}|\\{\\{[^}]*\\}\\}|\\}\\})*';
353+
// Only {{u|...}} is known to render its argument as visible text.
354+
const markup = '(?:<\\/?[a-zA-Z][a-zA-Z0-9:-]*(?:\\s+(?:"[^"]*"|\'[^\']*\'|[^\'"<>])*)?\\s*\\/?>|\\[\\[[^\\[\\]|\\n]*\\||\\[\\[|\\]\\]|\'{2,}(?!\')|\\{[^{}]*\\}|\\{\\{\\s*[uU]\\s*\\|\\s*|\\{\\{[^}]*\\}\\}|\\}\\})*';
346355
// The HTML omits parameter names and whitespace around the username separator.
347356
let usernameSeparator = hasUsernameParameter;
348357
return Array.from( appealReason, ( character ) => {

‎UnblockReview/tests/UnblockReview.test.js‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,15 @@ describe( 'getLeftHalfOfUnblockTemplate( wikitext, appealReason )', () => {
184184
const wikitext = `{{unblock|reason={{u|331dot}} I am requesting a reconsideration of this block.}}`;
185185
// The appeal reason as extracted from HTML - should be the full visible text
186186
const appealReason = `331dot I am requesting a reconsideration of this block.`;
187-
// When the pattern matches in wikitext, it will match starting at the {{u| since that's
188-
// where "331dot" appears in the wikitext. So the left half includes that.
189-
const expected = `{{unblock|reason={{u|`;
187+
const expected = `{{unblock|reason=`;
190188
expect( unblockReview.getLeftHalfOfUnblockTemplate( wikitext, appealReason ) ).toBe( expected );
191189
} );
192190

193191
test( 'Multiple template syntax variations with wiki markup', () => {
194192
// Test with different parameter styles
195193
const wikitext = `{{unblock|{{u|TestUser}} Please reconsider this block.}}`;
196194
const appealReason = `TestUser Please reconsider this block.`;
197-
// The pattern matches starting at "TestUser" (position 14), which is inside the {{u|...}} template
198-
// So the left half includes everything from {{unblock| up to the start of the match
199-
const expected = `{{unblock|{{u|`;
195+
const expected = `{{unblock|`;
200196
expect( unblockReview.getLeftHalfOfUnblockTemplate( wikitext, appealReason ) ).toBe( expected );
201197
} );
202198

@@ -210,8 +206,7 @@ describe( 'getLeftHalfOfUnblockTemplate( wikitext, appealReason )', () => {
210206
test( 'Reason with multiple wikilinks and templates', () => {
211207
const wikitext = `{{unblock|reason={{u|Admin}} and [[User:Mod|moderator]] agree my block was a mistake.}}`;
212208
const appealReason = `Admin and moderator agree my block was a mistake.`;
213-
// Pattern matches starting at {{u|Admin}}, so the expected left half includes that
214-
const expected = `{{unblock|reason={{u|`;
209+
const expected = `{{unblock|reason=`;
215210
expect( unblockReview.getLeftHalfOfUnblockTemplate( wikitext, appealReason ) ).toBe( expected );
216211
} );
217212
} );
@@ -590,6 +585,15 @@ I was blocked a few days ago for... [[User:Test]]}}`;
590585
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
591586
} );
592587

588+
test( 'Preserve a user template in the middle of an appeal reason', () => {
589+
const wikitext = `{{unblock|reason=Please {{u|331dot}} reconsider.}}`;
590+
const paramsAndReason = `Please 331dot reconsider.`;
591+
const acceptDeclineReason = `Insufficient.`;
592+
const acceptOrDecline = `decline`;
593+
const expected = `{{unblock reviewed|decline=Insufficient. ~~~~|1=Please {{u|331dot}} reconsider.}}`;
594+
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
595+
} );
596+
593597
test( 'Does not assume another template renders its argument as a user link', () => {
594598
const wikitext = `{{unblock|reason={{other|331dot}} I am requesting reconsideration.}}`;
595599
const paramsAndReason = `331dot I am requesting reconsideration.`;

‎UnblockReview/tests/main.test.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import { readFileSync } from 'fs';
88
// Specifically tests the case where HTML rendering includes tags that change
99
// the perceived text content vs. the actual wikitext.
1010

11-
const unblockReview = new UnblockReview();
12-
const DEFAULT_DECLINE_REASON = `{{subst:Decline reason here}}`;
13-
1411
async function clickReviewButton( wikitext, html, action = 'decline', hasUsernameParameter = false ) {
1512
document.body.innerHTML = html;
1613
if ( hasUsernameParameter ) {

0 commit comments

Comments
 (0)