Skip to content

Commit 945ba50

Browse files
committed
fix #241 New text lands in completely wrong place: PAGENAME
and refactor
1 parent 5740d77 commit 945ba50

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
},
1616
"rules": {
1717
// default rules we don't like
18-
"camelcase": "off",
1918
"es-x/no-class-fields": "off",
2019
"es-x/no-optional-catch-binding": "off",
2120
"es-x/no-regexp-lookbehind-assertions": "off",
@@ -43,6 +42,7 @@
4342
"no-nested-ternary": "error",
4443

4544
// rules we should probably fix someday
45+
"camelcase": "warn",
4646
"eqeqeq": "warn",
4747
"no-console": "warn"
4848
}

UnblockReview/modules/UnblockReview.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,27 @@ export class UnblockReview {
6060
throw new Error( 'Searching for target text failed!' );
6161
}
6262

63-
// Loop through all the potential matches, and peek at the characters in front of the match. Eliminate false positives ({{tlx|unblock}}, the same text not anywhere near an {{unblock}} template, etc.). If a true positive, return the beginning of the template.
63+
// Loop through all the potential matches, trying to find an {{Unblock template. If found, return the beginning of the template.
6464
for ( const match of matches ) {
65-
// The position of the match within the wikicode.
66-
const MatchPos = match.index;
67-
// The position of the unblock template of that match within the wikicode. Set them equal initially. Will be adjusted below.
68-
let UnblockTemplateStartPos = MatchPos;
69-
70-
// check for {{tlx|unblock. if found, this isn't what we want, skip.
71-
const startOfSplice = UnblockTemplateStartPos - 50 < 0 ? 0 : UnblockTemplateStartPos - 50;
72-
const chunkFiftyCharactersWide = wikitext.slice( startOfSplice, UnblockTemplateStartPos );
73-
if ( /\{\{\s*tlx\s*\|\s*unblock/i.test( chunkFiftyCharactersWide ) ) {
74-
continue;
75-
}
65+
const matchPos = match.index;
66+
let unblockTemplateStartPos;
7667

7768
// Scan backwards from the match until we find {{
78-
let i = 0;
79-
while ( wikitext[ UnblockTemplateStartPos ] !== '{' && i < 50 ) {
80-
UnblockTemplateStartPos--;
81-
i++;
69+
// Stop at the beginning of the string OR after 50 characters
70+
const stopPos = Math.max( 0, matchPos - 50 );
71+
for ( let i = matchPos; i > stopPos; i-- ) {
72+
if ( wikitext[ i ] === '{' && wikitext[ i - 1 ] === '{' ) {
73+
unblockTemplateStartPos = i - 1;
74+
break;
75+
}
8276
}
8377

84-
// If the above scan couldn't find the beginning of the template within 50 characters of the match, then that wasn't it. Even a long template like `{{Unblock-auto|reason=` isn't 50 characters long. Move on to the next match.
85-
if ( i === 50 ) {
78+
// Don't match stuff that isn't an unblock template
79+
const initialText = wikitext.slice( unblockTemplateStartPos, matchPos );
80+
if ( !initialText.match( /^\{\{unblock/i ) ) {
8681
continue;
8782
}
8883

89-
// The above scan stopped at {Unblock. Subtract one so it's {{Unblock
90-
UnblockTemplateStartPos--;
91-
92-
const initialText = wikitext.slice( UnblockTemplateStartPos, MatchPos );
9384
return initialText;
9485
}
9586

UnblockReview/tests/UnblockReview.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ describe( 'getLeftHalfOfUnblockTemplate( wikitext, appealReason )', () => {
103103
expect( () => {unblockReview.getLeftHalfOfUnblockTemplate( wikitext, appealReason );} ).toThrow( Error );
104104
expect( () => {unblockReview.getLeftHalfOfUnblockTemplate( wikitext, appealReason );} ).toThrow( "Searching for target text failed!" );
105105
} );
106+
107+
test( '{{AAA}}AAAblock', () => {
108+
const wikitext =
109+
`{{PAGENAME}}]]<!-- Template:Uw-soablock -->
110+
111+
:IP CDOs: An Encyclopedic Overview
112+
113+
:{{unblock|reason=IP CDOs: An Encyclopedic Overview}}
114+
`;
115+
const appealReason = `IP CDOs: An Encyclopedic Overview`;
116+
const expected = `{{unblock|reason=`;
117+
expect( unblockReview.getLeftHalfOfUnblockTemplate( wikitext, appealReason ) ).toBe( expected );
118+
} );
106119
} );
107120

108121
describe( 'processAcceptOrDecline( wikitext, appealReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline )', () => {

0 commit comments

Comments
 (0)