@@ -191,7 +191,7 @@ var FilterHostname = function(s, hostname) {
191191} ;
192192
193193FilterHostname . prototype . retrieve = function ( hostname , out ) {
194- if ( hostname . slice ( - this . hostname . length ) === this . hostname ) {
194+ if ( hostname . endsWith ( this . hostname ) ) {
195195 out . push ( this . s ) ;
196196 }
197197} ;
@@ -219,7 +219,7 @@ var FilterEntity = function(s, entity) {
219219} ;
220220
221221FilterEntity . prototype . retrieve = function ( entity , out ) {
222- if ( entity . slice ( - this . entity . length ) === this . entity ) {
222+ if ( entity . endsWith ( this . entity ) ) {
223223 out . push ( this . s ) ;
224224 }
225225} ;
@@ -244,7 +244,6 @@ var FilterParser = function() {
244244 this . hostnames = [ ] ;
245245 this . invalid = false ;
246246 this . cosmetic = true ;
247- this . reScriptContains = / ^ s c r i p t : c o n t a i n s \( .+ ?\) $ / ;
248247} ;
249248
250249/******************************************************************************/
@@ -321,7 +320,7 @@ FilterParser.prototype.parse = function(raw) {
321320 // Cosmetic filters with explicit style properties can apply only:
322321 // - to specific cosmetic filters (those which apply to a specific site)
323322 // - to block cosmetic filters (not exception cosmetic filters)
324- if ( this . suffix . slice ( - 1 ) === '}' ) {
323+ if ( this . suffix . endsWith ( '}' ) ) {
325324 // Not supported for now: this code will ensure some backward
326325 // compatibility for when cosmetic filters with explicit style
327326 // properties start to be in use.
@@ -341,7 +340,7 @@ FilterParser.prototype.parse = function(raw) {
341340 // Normalize high-medium selectors: `href` is assumed to imply `a` tag. We
342341 // need to do this here in order to correctly avoid duplicates. The test
343342 // is designed to minimize overhead -- this is a low occurrence filter.
344- if ( this . suffix . charAt ( 1 ) === '[' && this . suffix . slice ( 2 , 9 ) === ' href^="' ) {
343+ if ( this . suffix . startsWith ( '[ href^="', 1 ) ) {
345344 this . suffix = this . suffix . slice ( 1 ) ;
346345 }
347346
@@ -357,9 +356,9 @@ FilterParser.prototype.parse = function(raw) {
357356
358357 // Inline script tag filter?
359358 if (
360- this . suffix . charAt ( 0 ) !== 's' ||
361- this . reScriptContains . test ( this . suffix ) === false )
362- {
359+ this . suffix . startsWith ( 'script:contains(' ) === false ||
360+ this . suffix . endsWith ( ')' ) === false
361+ ) {
363362 return this ;
364363 }
365364
@@ -370,17 +369,17 @@ FilterParser.prototype.parse = function(raw) {
370369 return this ;
371370 }
372371
373- var suffix = this . suffix ;
372+ var suffix = this . suffix . slice ( 16 , - 1 ) ;
374373 this . suffix = 'script//:' ;
375374
376375 // Plain string-based?
377- if ( suffix . charAt ( 16 ) !== '/' || suffix . slice ( - 2 ) !== '/)' ) {
378- this . suffix += suffix . slice ( 16 , - 1 ) . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
376+ if ( suffix . startsWith ( '/' ) === false || suffix . endsWith ( '/' ) === false ) {
377+ this . suffix += suffix . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
379378 return this ;
380379 }
381380
382381 // Regex-based
383- this . suffix += suffix . slice ( 17 , - 2 ) . replace ( / \\ / g , '\\' ) ;
382+ this . suffix += suffix . slice ( 1 , - 1 ) ;
384383
385384 // Valid regex?
386385 if ( isBadRegex ( this . suffix ) ) {
@@ -687,7 +686,7 @@ FilterContainer.prototype.isValidSelector = (function() {
687686 return true ;
688687 } catch ( e ) {
689688 }
690- if ( s . lastIndexOf ( 'script//:' , 0 ) === 0 ) {
689+ if ( s . startsWith ( 'script//:' ) ) {
691690 return true ;
692691 }
693692 console . error ( 'uBlock> invalid cosmetic filter:' , s ) ;
@@ -730,10 +729,10 @@ FilterContainer.prototype.compile = function(s, out) {
730729 var hostname ;
731730 while ( i -- ) {
732731 hostname = hostnames [ i ] ;
733- if ( hostname . charAt ( 0 ) !== '~' ) {
732+ if ( hostname . startsWith ( '~' ) === false ) {
734733 applyGlobally = false ;
735734 }
736- if ( hostname . slice ( - 2 ) === '.*' ) {
735+ if ( hostname . endsWith ( '.*' ) ) {
737736 this . compileEntitySelector ( hostname , parsed , out ) ;
738737 } else {
739738 this . compileHostnameSelector ( hostname , parsed , out ) ;
@@ -839,7 +838,7 @@ FilterContainer.prototype.reHighMedium = /^\[href\^="https?:\/\/([^"]{8})[^"]*"\
839838FilterContainer . prototype . compileHostnameSelector = function ( hostname , parsed , out ) {
840839 // https://github.com/chrisaljoudi/uBlock/issues/145
841840 var unhide = parsed . unhide ;
842- if ( hostname . charAt ( 0 ) === '~' ) {
841+ if ( hostname . startsWith ( '~' ) ) {
843842 hostname = hostname . slice ( 1 ) ;
844843 unhide ^= 1 ;
845844 }
@@ -915,7 +914,7 @@ FilterContainer.prototype.fromCompiledContent = function(text, lineBeg, skip) {
915914 // h ir twitter.com .promoted-tweet
916915 if ( fields [ 0 ] === 'h' ) {
917916 // Special filter: script tags. Not a real CSS selector.
918- if ( fields [ 3 ] . lastIndexOf ( 'script//:' , 0 ) === 0 ) {
917+ if ( fields [ 3 ] . startsWith ( 'script//:' ) ) {
919918 this . createScriptTagFilter ( fields [ 2 ] , fields [ 3 ] . slice ( 9 ) ) ;
920919 continue ;
921920 }
@@ -951,7 +950,7 @@ FilterContainer.prototype.fromCompiledContent = function(text, lineBeg, skip) {
951950 // entity selector
952951 if ( fields [ 0 ] === 'e' ) {
953952 // Special filter: script tags. Not a real CSS selector.
954- if ( fields [ 2 ] . lastIndexOf ( 'script//:' , 0 ) === 0 ) {
953+ if ( fields [ 2 ] . startsWith ( 'script//:' ) ) {
955954 this . createScriptTagFilter ( fields [ 1 ] , fields [ 2 ] . slice ( 9 ) ) ;
956955 continue ;
957956 }
@@ -1224,16 +1223,17 @@ FilterContainer.prototype.addToSelectorCache = function(details) {
12241223/******************************************************************************/
12251224
12261225FilterContainer . prototype . removeFromSelectorCache = function ( targetHostname , type ) {
1226+ var targetHostnameLength = targetHostname . length ;
12271227 for ( var hostname in this . selectorCache ) {
12281228 if ( this . selectorCache . hasOwnProperty ( hostname ) === false ) {
12291229 continue ;
12301230 }
12311231 if ( targetHostname !== '*' ) {
1232- if ( hostname . slice ( 0 - targetHostname . length ) !== targetHostname ) {
1232+ if ( hostname . endsWith ( targetHostname ) === false ) {
12331233 continue ;
12341234 }
1235- if ( hostname . length !== targetHostname . length &&
1236- hostname . charAt ( 0 - targetHostname . length - 1 ) !== '.' ) {
1235+ if ( hostname . length !== targetHostnameLength &&
1236+ hostname . charAt ( hostname . length - targetHostnameLength - 1 ) !== '.' ) {
12371237 continue ;
12381238 }
12391239 }
0 commit comments