Skip to content

Commit 3789509

Browse files
committed
UserHighlighterSimple: refactor to remove mw.Uri (deprecated)
1 parent 7fe5744 commit 3789509

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

UserHighlighterSimple/modules/UserHighlighterSimple.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,7 @@ export class UserHighlighterSimple {
133133

134134
url = this.addDomainIfMissing( url );
135135

136-
// mw.Uri(url) throws an error if it doesn't like the URL. An example of a URL it doesn't like is https://meta.wikimedia.org/wiki/Community_Wishlist_Survey_2022/Larger_suggestions#1%, which has a section link to a section titled 1% (one percent).
137-
let urlHelper;
138-
try {
139-
urlHelper = new this.mw.Uri( url );
140-
} catch {
141-
return false;
142-
}
136+
const urlHelper = new URL( url );
143137

144138
// Skip links that aren't to user pages
145139
const isUserPageLink = url.includes( '/w/index.php?title=User' ) || url.includes( '/wiki/User' );
@@ -148,11 +142,11 @@ export class UserHighlighterSimple {
148142
}
149143

150144
// Even if it is a link to a userpage, skip URLs that have any parameters except title=User, action=edit, and redlink=. We don't want links to diff pages, section editing pages, etc. to be highlighted.
151-
const urlParameters = urlHelper.query;
145+
const urlParameters = this.getObjectWithUriParamsFromQueryString( urlHelper );
152146
delete urlParameters.title;
153147
delete urlParameters.action;
154148
delete urlParameters.redlink;
155-
const hasNonUserpageParametersInUrl = !this.$.isEmptyObject( urlParameters );
149+
const hasNonUserpageParametersInUrl = Object.keys( urlParameters ).length > 0;
156150
if ( hasNonUserpageParametersInUrl ) {
157151
return false;
158152
}
@@ -174,6 +168,16 @@ export class UserHighlighterSimple {
174168
return true;
175169
}
176170

171+
getObjectWithUriParamsFromQueryString( url ) {
172+
const params = {};
173+
const queryString = url.search;
174+
const urlParams = new URLSearchParams( queryString );
175+
for ( const [ key, value ] of urlParams.entries() ) {
176+
params[ key ] = value;
177+
}
178+
return params;
179+
}
180+
177181
hasHref( url ) {
178182
return Boolean( url );
179183
}
@@ -189,7 +193,7 @@ export class UserHighlighterSimple {
189193
}
190194

191195
/**
192-
* mw.Uri(url) expects a complete URL. If we get something like /wiki/User:Test, convert it to https://en.wikipedia.org/wiki/User:Test. Without this, UserHighlighterSimple doesn't work on metawiki.
196+
* URL( url ) expects a complete URL. If we get something like /wiki/User:Test, convert it to https://en.wikipedia.org/wiki/User:Test. Without this, UserHighlighterSimple doesn't work on metawiki.
193197
*
194198
* @param {string} url
195199
* @return {string} url
@@ -205,21 +209,19 @@ export class UserHighlighterSimple {
205209
* Figure out the wikipedia article title of the link
206210
*
207211
* @param {string} url
208-
* @param {mw.Uri} urlHelper
209212
* @return {string}
210213
*/
211214
getTitle( url ) {
212-
const urlHelper = new this.mw.Uri( url );
213-
214215
// for links in the format /w/index.php?title=Blah
215216
const titleParameterOfUrl = this.mw.util.getParamValue( 'title', url );
216217
if ( titleParameterOfUrl ) {
217218
return titleParameterOfUrl;
218219
}
219220

220221
// for links in the format /wiki/PageName. Slice off the /wiki/ (first 6 characters)
221-
if ( urlHelper.path.startsWith( '/wiki/' ) ) {
222-
return decodeURIComponent( urlHelper.path.slice( 6 ) );
222+
const urlHelper = new URL( url );
223+
if ( urlHelper.pathname.startsWith( '/wiki/' ) ) {
224+
return decodeURIComponent( urlHelper.pathname.slice( 6 ) );
223225
}
224226

225227
return '';

0 commit comments

Comments
 (0)