 Issue 30011555:
  Issue 7303 - Deprecate the use of String.prototype.substr()  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/
    
  
    Issue 30011555:
  Issue 7303 - Deprecate the use of String.prototype.substr()  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/| Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 1 /* | 1 /* | 
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 
| 3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH | 
| 4 * | 4 * | 
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify | 
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as | 
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. | 
| 8 * | 8 * | 
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, | 
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 312 // text, being careful to preserve its spaces. | 312 // text, being careful to preserve its spaces. | 
| 313 let beforeOptions = strippedText.substring(0, optionsMatch.index); | 313 let beforeOptions = strippedText.substring(0, optionsMatch.index); | 
| 314 let strippedDollarIndex = -1; | 314 let strippedDollarIndex = -1; | 
| 315 let dollarIndex = -1; | 315 let dollarIndex = -1; | 
| 316 do | 316 do | 
| 317 { | 317 { | 
| 318 strippedDollarIndex = beforeOptions.indexOf("$", strippedDollarIndex + 1); | 318 strippedDollarIndex = beforeOptions.indexOf("$", strippedDollarIndex + 1); | 
| 319 dollarIndex = text.indexOf("$", dollarIndex + 1); | 319 dollarIndex = text.indexOf("$", dollarIndex + 1); | 
| 320 } | 320 } | 
| 321 while (strippedDollarIndex != -1); | 321 while (strippedDollarIndex != -1); | 
| 322 let optionsText = text.substr(dollarIndex + 1); | 322 let optionsText = text.substring(dollarIndex + 1); | 
| 323 | 323 | 
| 324 // Then we can normalize spaces in the options part safely | 324 // Then we can normalize spaces in the options part safely | 
| 325 let options = optionsText.split(","); | 325 let options = optionsText.split(","); | 
| 326 for (let i = 0; i < options.length; i++) | 326 for (let i = 0; i < options.length; i++) | 
| 327 { | 327 { | 
| 328 let option = options[i]; | 328 let option = options[i]; | 
| 329 let cspMatch = /^ *c *s *p *=/i.exec(option); | 329 let cspMatch = /^ *c *s *p *=/i.exec(option); | 
| 330 if (cspMatch) | 330 if (cspMatch) | 
| 331 { | 331 { | 
| 332 options[i] = cspMatch[0].replace(/ +/g, "") + | 332 options[i] = cspMatch[0].replace(/ +/g, "") + | 
| 333 option.substr(cspMatch[0].length).trim().replace(/ +/g, " "); | 333 option.substring(cspMatch[0].length).trim().replace(/ +/g, " "); | 
| 334 } | 334 } | 
| 335 else | 335 else | 
| 336 options[i] = option.replace(/ +/g, ""); | 336 options[i] = option.replace(/ +/g, ""); | 
| 337 } | 337 } | 
| 338 | 338 | 
| 339 return beforeOptions + "$" + options.join(); | 339 return beforeOptions + "$" + options.join(); | 
| 340 }; | 340 }; | 
| 341 | 341 | 
| 342 /** | 342 /** | 
| 343 * Class for invalid filters | 343 * Class for invalid filters | 
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 for (let i = 0; i < list.length; i++) | 524 for (let i = 0; i < list.length; i++) | 
| 525 { | 525 { | 
| 526 let domain = list[i]; | 526 let domain = list[i]; | 
| 527 if (domain == "") | 527 if (domain == "") | 
| 528 continue; | 528 continue; | 
| 529 | 529 | 
| 530 let include; | 530 let include; | 
| 531 if (domain[0] == "~") | 531 if (domain[0] == "~") | 
| 532 { | 532 { | 
| 533 include = false; | 533 include = false; | 
| 534 domain = domain.substr(1); | 534 domain = domain.substring(1); | 
| 535 } | 535 } | 
| 536 else | 536 else | 
| 537 { | 537 { | 
| 538 include = true; | 538 include = true; | 
| 539 hasIncludes = true; | 539 hasIncludes = true; | 
| 540 } | 540 } | 
| 541 | 541 | 
| 542 if (!domains) | 542 if (!domains) | 
| 543 domains = new Map(); | 543 domains = new Map(); | 
| 544 | 544 | 
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 723 this.rewrite = rewrite; | 723 this.rewrite = rewrite; | 
| 724 if (resourceName) | 724 if (resourceName) | 
| 725 this.resourceName = resourceName; | 725 this.resourceName = resourceName; | 
| 726 | 726 | 
| 727 if (regexpSource.length >= 2 && | 727 if (regexpSource.length >= 2 && | 
| 728 regexpSource[0] == "/" && | 728 regexpSource[0] == "/" && | 
| 729 regexpSource[regexpSource.length - 1] == "/") | 729 regexpSource[regexpSource.length - 1] == "/") | 
| 730 { | 730 { | 
| 731 // The filter is a regular expression - convert it immediately to | 731 // The filter is a regular expression - convert it immediately to | 
| 732 // catch syntax errors | 732 // catch syntax errors | 
| 733 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), | 733 let regexp = new RegExp(regexpSource.substring(1, regexpSource.length - 1), | 
| 734 this.matchCase ? "" : "i"); | 734 this.matchCase ? "" : "i"); | 
| 735 Object.defineProperty(this, "regexp", {value: regexp}); | 735 Object.defineProperty(this, "regexp", {value: regexp}); | 
| 736 } | 736 } | 
| 737 else | 737 else | 
| 738 { | 738 { | 
| 739 // Patterns like /foo/bar/* exist so that they are not treated as regular | 739 // Patterns like /foo/bar/* exist so that they are not treated as regular | 
| 740 // expressions. We drop any superfluous wildcards here so our optimizations | 740 // expressions. We drop any superfluous wildcards here so our optimizations | 
| 741 // can kick in. | 741 // can kick in. | 
| 742 if (this.rewrite == null || this.resourceName) | 742 if (this.rewrite == null || this.resourceName) | 
| 743 regexpSource = regexpSource.replace(/^\*+/, "").replace(/\*+$/, ""); | 743 regexpSource = regexpSource.replace(/^\*+/, "").replace(/\*+$/, ""); | 
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 905 | 905 | 
| 906 if (!this.matchCase) | 906 if (!this.matchCase) | 
| 907 location = lowerCaseLocation || location.toLowerCase(); | 907 location = lowerCaseLocation || location.toLowerCase(); | 
| 908 | 908 | 
| 909 let {pattern} = this; | 909 let {pattern} = this; | 
| 910 | 910 | 
| 911 let startsWithDoubleAnchor = pattern[0] == "|" && pattern[1] == "|"; | 911 let startsWithDoubleAnchor = pattern[0] == "|" && pattern[1] == "|"; | 
| 912 let endsWithSeparator = pattern[pattern.length - 1] == "^"; | 912 let endsWithSeparator = pattern[pattern.length - 1] == "^"; | 
| 913 | 913 | 
| 914 if (startsWithDoubleAnchor) | 914 if (startsWithDoubleAnchor) | 
| 915 pattern = pattern.substr(2); | 915 pattern = pattern.substring(2); | 
| 
Manish Jethani
2019/02/21 15:04:39
This is a hot spot, let's leave it as it is.
 
hub
2019/02/21 18:09:42
Done.
 | |
| 916 | 916 | 
| 917 if (endsWithSeparator) | 917 if (endsWithSeparator) | 
| 918 pattern = pattern.slice(0, -1); | 918 pattern = pattern.slice(0, -1); | 
| 919 | 919 | 
| 920 let index = location.indexOf(pattern); | 920 let index = location.indexOf(pattern); | 
| 921 | 921 | 
| 922 // The "||" prefix requires that the text that follows does not start | 922 // The "||" prefix requires that the text that follows does not start | 
| 923 // with a forward slash. | 923 // with a forward slash. | 
| 924 return index != -1 && | 924 return index != -1 && | 
| 925 (!startsWithDoubleAnchor || location[index] != "/" && | 925 (!startsWithDoubleAnchor || location[index] != "/" && | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 968 * @param {string} text same as in Filter() | 968 * @param {string} text same as in Filter() | 
| 969 * @return {Filter} | 969 * @return {Filter} | 
| 970 */ | 970 */ | 
| 971 RegExpFilter.fromText = function(text) | 971 RegExpFilter.fromText = function(text) | 
| 972 { | 972 { | 
| 973 let blocking = true; | 973 let blocking = true; | 
| 974 let origText = text; | 974 let origText = text; | 
| 975 if (text[0] == "@" && text[1] == "@") | 975 if (text[0] == "@" && text[1] == "@") | 
| 976 { | 976 { | 
| 977 blocking = false; | 977 blocking = false; | 
| 978 text = text.substr(2); | 978 text = text.substring(2); | 
| 979 } | 979 } | 
| 980 | 980 | 
| 981 let contentType = null; | 981 let contentType = null; | 
| 982 let matchCase = null; | 982 let matchCase = null; | 
| 983 let domains = null; | 983 let domains = null; | 
| 984 let sitekeys = null; | 984 let sitekeys = null; | 
| 985 let thirdParty = null; | 985 let thirdParty = null; | 
| 986 let collapse = null; | 986 let collapse = null; | 
| 987 let csp = null; | 987 let csp = null; | 
| 988 let rewrite = null; | 988 let rewrite = null; | 
| 989 let resourceName = null; | 989 let resourceName = null; | 
| 990 let options; | 990 let options; | 
| 991 let match = text.includes("$") ? Filter.optionsRegExp.exec(text) : null; | 991 let match = text.includes("$") ? Filter.optionsRegExp.exec(text) : null; | 
| 992 if (match) | 992 if (match) | 
| 993 { | 993 { | 
| 994 options = match[1].split(","); | 994 options = match[1].split(","); | 
| 995 text = match.input.substr(0, match.index); | 995 text = match.input.substring(0, match.index); | 
| 996 for (let option of options) | 996 for (let option of options) | 
| 997 { | 997 { | 
| 998 let value = null; | 998 let value = null; | 
| 999 let separatorIndex = option.indexOf("="); | 999 let separatorIndex = option.indexOf("="); | 
| 1000 if (separatorIndex >= 0) | 1000 if (separatorIndex >= 0) | 
| 1001 { | 1001 { | 
| 1002 value = option.substr(separatorIndex + 1); | 1002 value = option.substring(separatorIndex + 1); | 
| 1003 option = option.substr(0, separatorIndex); | 1003 option = option.substring(0, separatorIndex); | 
| 1004 } | 1004 } | 
| 1005 | 1005 | 
| 1006 let inverse = option[0] == "~"; | 1006 let inverse = option[0] == "~"; | 
| 1007 if (inverse) | 1007 if (inverse) | 
| 1008 option = option.substr(1); | 1008 option = option.substring(1); | 
| 1009 | 1009 | 
| 1010 let type = RegExpFilter.typeMap[option.replace(/-/, "_").toUpperCase()]; | 1010 let type = RegExpFilter.typeMap[option.replace(/-/, "_").toUpperCase()]; | 
| 1011 if (type) | 1011 if (type) | 
| 1012 { | 1012 { | 
| 1013 if (inverse) | 1013 if (inverse) | 
| 1014 { | 1014 { | 
| 1015 if (contentType == null) | 1015 if (contentType == null) | 
| 1016 ({contentType} = RegExpFilter.prototype); | 1016 ({contentType} = RegExpFilter.prototype); | 
| 1017 contentType &= ~type; | 1017 contentType &= ~type; | 
| 1018 } | 1018 } | 
| (...skipping 30 matching lines...) Expand all Loading... | |
| 1049 case "sitekey": | 1049 case "sitekey": | 
| 1050 if (!value) | 1050 if (!value) | 
| 1051 return new InvalidFilter(origText, "filter_unknown_option"); | 1051 return new InvalidFilter(origText, "filter_unknown_option"); | 
| 1052 sitekeys = value.toUpperCase(); | 1052 sitekeys = value.toUpperCase(); | 
| 1053 break; | 1053 break; | 
| 1054 case "rewrite": | 1054 case "rewrite": | 
| 1055 if (value == null) | 1055 if (value == null) | 
| 1056 return new InvalidFilter(origText, "filter_unknown_option"); | 1056 return new InvalidFilter(origText, "filter_unknown_option"); | 
| 1057 rewrite = value; | 1057 rewrite = value; | 
| 1058 if (value.startsWith("abp-resource:")) | 1058 if (value.startsWith("abp-resource:")) | 
| 1059 resourceName = value.substr("abp-resource:".length); | 1059 resourceName = value.substring("abp-resource:".length); | 
| 1060 break; | 1060 break; | 
| 1061 default: | 1061 default: | 
| 1062 return new InvalidFilter(origText, "filter_unknown_option"); | 1062 return new InvalidFilter(origText, "filter_unknown_option"); | 
| 1063 } | 1063 } | 
| 1064 } | 1064 } | 
| 1065 } | 1065 } | 
| 1066 } | 1066 } | 
| 1067 | 1067 | 
| 1068 // For security reasons, never match $rewrite filters | 1068 // For security reasons, never match $rewrite filters | 
| 1069 // against requests that might load any code to be executed. | 1069 // against requests that might load any code to be executed. | 
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1437 | 1437 | 
| 1438 /** | 1438 /** | 
| 1439 * Script that should be executed | 1439 * Script that should be executed | 
| 1440 * @type {string} | 1440 * @type {string} | 
| 1441 */ | 1441 */ | 
| 1442 get script() | 1442 get script() | 
| 1443 { | 1443 { | 
| 1444 return this.body; | 1444 return this.body; | 
| 1445 } | 1445 } | 
| 1446 }); | 1446 }); | 
| OLD | NEW |