@@ -4236,57 +4236,41 @@ FilterContainer.prototype.redirectRequest = function(fctxt) {
4236
4236
const directives = this . matchAndFetchModifiers ( fctxt , 'redirect-rule' ) ;
4237
4237
// No directive is the most common occurrence.
4238
4238
if ( directives === undefined ) { return ; }
4239
- // A single directive should be the next most common occurrence.
4240
- if ( directives . length === 1 ) {
4241
- const directive = directives [ 0 ] ;
4242
- if ( ( directive . bits & AllowAction ) !== 0 ) { return directive ; }
4243
- const modifier = directive . modifier ;
4244
- const { token } = this . parseRedirectRequestValue ( modifier ) ;
4239
+ // More than a single directive means more work.
4240
+ if ( directives . length !== 1 ) {
4241
+ directives . sort ( FilterContainer . compareRedirectRequests ) ;
4242
+ }
4243
+ // Redirect to highest-ranked directive
4244
+ const directive = directives [ directives . length - 1 ] ;
4245
+ if ( ( directive . bits & AllowAction ) === 0 ) {
4246
+ const { token } =
4247
+ FilterContainer . parseRedirectRequestValue ( directive . modifier ) ;
4245
4248
fctxt . redirectURL = µb . redirectEngine . tokenToURL ( fctxt , token ) ;
4246
4249
if ( fctxt . redirectURL === undefined ) { return ; }
4247
- return directive ;
4248
- }
4249
- // Multiple directives mean more work to do.
4250
- let winningDirective ;
4251
- let winningPriority = 0 ;
4252
- for ( const directive of directives ) {
4253
- const modifier = directive . modifier ;
4254
- const isException = ( directive . bits & AllowAction ) !== 0 ;
4255
- if ( isException && modifier . value === '' ) {
4256
- winningDirective = directive ;
4257
- break ;
4258
- }
4259
- const { token, priority } = this . parseRedirectRequestValue ( modifier ) ;
4260
- if ( µb . redirectEngine . hasToken ( token ) === false ) { continue ; }
4261
- if ( winningDirective === undefined || priority > winningPriority ) {
4262
- winningDirective = directive ;
4263
- winningPriority = priority ;
4264
- }
4265
- }
4266
- if ( winningDirective === undefined ) { return ; }
4267
- if ( ( winningDirective . bits & AllowAction ) === 0 ) {
4268
- fctxt . redirectURL = µb . redirectEngine . tokenToURL (
4269
- fctxt ,
4270
- winningDirective . modifier . cache . token
4271
- ) ;
4272
4250
}
4273
- return winningDirective ;
4251
+ return directives ;
4274
4252
} ;
4275
4253
4276
- FilterContainer . prototype . parseRedirectRequestValue = function ( modifier ) {
4254
+ FilterContainer . parseRedirectRequestValue = function ( modifier ) {
4277
4255
if ( modifier . cache === undefined ) {
4278
- let token = modifier . value ;
4279
- let priority = 1 ;
4280
- const match = / : ( \d + ) $ / . exec ( token ) ;
4281
- if ( match !== null ) {
4282
- token = token . slice ( 0 , match . index ) ;
4283
- priority = parseInt ( match [ 1 ] , 10 ) ;
4284
- }
4285
- modifier . cache = { token, priority } ;
4256
+ modifier . cache =
4257
+ vAPI . StaticFilteringParser . parseRedirectValue ( modifier . value ) ;
4286
4258
}
4287
4259
return modifier . cache ;
4288
4260
} ;
4289
4261
4262
+ FilterContainer . compareRedirectRequests = function ( a , b ) {
4263
+ if ( ( a . bits & AllowAction ) !== 0 ) { return - 1 ; }
4264
+ if ( ( b . bits & AllowAction ) !== 0 ) { return 1 ; }
4265
+ const { token : atok , priority : aint } =
4266
+ FilterContainer . parseRedirectRequestValue ( a . modifier ) ;
4267
+ if ( µb . redirectEngine . hasToken ( atok ) === false ) { return - 1 ; }
4268
+ const { token : btok , priority : bint } =
4269
+ FilterContainer . parseRedirectRequestValue ( b . modifier ) ;
4270
+ if ( µb . redirectEngine . hasToken ( btok ) === false ) { return 1 ; }
4271
+ return aint - bint ;
4272
+ } ;
4273
+
4290
4274
/******************************************************************************/
4291
4275
4292
4276
FilterContainer . prototype . filterQuery = function ( fctxt ) {
0 commit comments