Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/filterClasses.js

Issue 29760704: Issue 6592 - Implement $rewrite filter option (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Added test. The rewrite is now part of BlockingFilter Created April 25, 2018, 3:33 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/filterClasses.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 753 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 text = text.substr(2); 764 text = text.substr(2);
765 } 765 }
766 766
767 let contentType = null; 767 let contentType = null;
768 let matchCase = null; 768 let matchCase = null;
769 let domains = null; 769 let domains = null;
770 let sitekeys = null; 770 let sitekeys = null;
771 let thirdParty = null; 771 let thirdParty = null;
772 let collapse = null; 772 let collapse = null;
773 let csp = null; 773 let csp = null;
774 let rewrite = null;
774 let options; 775 let options;
775 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); 776 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null);
776 if (match) 777 if (match)
777 { 778 {
778 options = match[1].split(","); 779 options = match[1].split(",");
779 text = match.input.substr(0, match.index); 780 text = match.input.substr(0, match.index);
780 for (let option of options) 781 for (let option of options)
781 { 782 {
782 let value = null; 783 let value = null;
783 let separatorIndex = option.indexOf("="); 784 let separatorIndex = option.indexOf("=");
(...skipping 27 matching lines...) Expand all
811 else if (option == "THIRD_PARTY") 812 else if (option == "THIRD_PARTY")
812 thirdParty = true; 813 thirdParty = true;
813 else if (option == "~THIRD_PARTY") 814 else if (option == "~THIRD_PARTY")
814 thirdParty = false; 815 thirdParty = false;
815 else if (option == "COLLAPSE") 816 else if (option == "COLLAPSE")
816 collapse = true; 817 collapse = true;
817 else if (option == "~COLLAPSE") 818 else if (option == "~COLLAPSE")
818 collapse = false; 819 collapse = false;
819 else if (option == "SITEKEY" && value) 820 else if (option == "SITEKEY" && value)
820 sitekeys = value.toUpperCase(); 821 sitekeys = value.toUpperCase();
822 else if (option == "REWRITE" && value)
823 rewrite = value;
821 else 824 else
822 return new InvalidFilter(origText, "filter_unknown_option"); 825 return new InvalidFilter(origText, "filter_unknown_option");
823 } 826 }
824 } 827 }
825 828
826 try 829 try
827 { 830 {
828 if (blocking) 831 if (blocking)
829 { 832 {
830 if (csp && Filter.invalidCSPRegExp.test(csp)) 833 if (csp && Filter.invalidCSPRegExp.test(csp))
831 return new InvalidFilter(origText, "filter_invalid_csp"); 834 return new InvalidFilter(origText, "filter_invalid_csp");
832 835
833 return new BlockingFilter(origText, text, contentType, matchCase, domains, 836 return new BlockingFilter(origText, text, contentType, matchCase, domains,
834 thirdParty, sitekeys, collapse, csp); 837 thirdParty, sitekeys, collapse, csp, rewrite);
835 } 838 }
836 return new WhitelistFilter(origText, text, contentType, matchCase, domains, 839 return new WhitelistFilter(origText, text, contentType, matchCase, domains,
837 thirdParty, sitekeys); 840 thirdParty, sitekeys);
838 } 841 }
839 catch (e) 842 catch (e)
840 { 843 {
841 return new InvalidFilter(origText, "filter_invalid_regexp"); 844 return new InvalidFilter(origText, "filter_invalid_regexp");
842 } 845 }
843 }; 846 };
844 847
845 /** 848 /**
846 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks 849 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks
847 */ 850 */
848 RegExpFilter.typeMap = { 851 RegExpFilter.typeMap = {
Manish Jethani 2018/04/30 20:10:55 We should add REWRITE here. Then we should exclude
hub 2018/05/01 18:32:15 I don't think it make sense to do so. This is just
Manish Jethani 2018/05/02 15:31:09 Maybe for the sake of consistency? CSP is also not
hub 2018/05/02 22:14:32 I still don't think this is necessary. We don't ne
849 OTHER: 1, 852 OTHER: 1,
850 SCRIPT: 2, 853 SCRIPT: 2,
851 IMAGE: 4, 854 IMAGE: 4,
852 STYLESHEET: 8, 855 STYLESHEET: 8,
853 OBJECT: 16, 856 OBJECT: 16,
854 SUBDOCUMENT: 32, 857 SUBDOCUMENT: 32,
855 DOCUMENT: 64, 858 DOCUMENT: 64,
856 WEBSOCKET: 128, 859 WEBSOCKET: 128,
857 WEBRTC: 256, 860 WEBRTC: 256,
858 CSP: 512, 861 CSP: 512,
(...skipping 28 matching lines...) Expand all
887 * @param {string} regexpSource see RegExpFilter() 890 * @param {string} regexpSource see RegExpFilter()
888 * @param {number} contentType see RegExpFilter() 891 * @param {number} contentType see RegExpFilter()
889 * @param {boolean} matchCase see RegExpFilter() 892 * @param {boolean} matchCase see RegExpFilter()
890 * @param {string} domains see RegExpFilter() 893 * @param {string} domains see RegExpFilter()
891 * @param {boolean} thirdParty see RegExpFilter() 894 * @param {boolean} thirdParty see RegExpFilter()
892 * @param {string} sitekeys see RegExpFilter() 895 * @param {string} sitekeys see RegExpFilter()
893 * @param {boolean} collapse 896 * @param {boolean} collapse
894 * defines whether the filter should collapse blocked content, can be null 897 * defines whether the filter should collapse blocked content, can be null
895 * @param {string} [csp] 898 * @param {string} [csp]
896 * Content Security Policy to inject when the filter matches 899 * Content Security Policy to inject when the filter matches
900 * @param {string} [rewrite]
901 * The rewrite expression
897 * @constructor 902 * @constructor
898 * @augments RegExpFilter 903 * @augments RegExpFilter
899 */ 904 */
900 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, 905 function BlockingFilter(text, regexpSource, contentType, matchCase, domains,
901 thirdParty, sitekeys, collapse, csp) 906 thirdParty, sitekeys, collapse, csp, rewrite)
902 { 907 {
903 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 908 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
904 thirdParty, sitekeys); 909 thirdParty, sitekeys);
905 910
906 this.collapse = collapse; 911 this.collapse = collapse;
907 this.csp = csp; 912 this.csp = csp;
913 this.rewrite = rewrite;
908 } 914 }
909 exports.BlockingFilter = BlockingFilter; 915 exports.BlockingFilter = BlockingFilter;
910 916
911 BlockingFilter.prototype = extend(RegExpFilter, { 917 BlockingFilter.prototype = extend(RegExpFilter, {
912 type: "blocking", 918 type: "blocking",
913 919
914 /** 920 /**
915 * Defines whether the filter should collapse blocked content. 921 * Defines whether the filter should collapse blocked content.
916 * Can be null (use the global preference). 922 * Can be null (use the global preference).
917 * @type {boolean} 923 * @type {boolean}
918 */ 924 */
919 collapse: null, 925 collapse: null,
920 926
921 /** 927 /**
922 * Content Security Policy to inject for matching requests. 928 * Content Security Policy to inject for matching requests.
923 * @type {string} 929 * @type {string}
924 */ 930 */
925 csp: null 931 csp: null,
932
933 /**
934 * The rewrite expression
935 * @type {string}
936 */
937 rewrite: null,
938
939 /**
940 * Perform the URL rewrite
941 * @param {string} urlString the string URL to rewrite
942 * @returns {string?} the rewritten URL, or null if it doesn't match.
943 */
944 doRewrite(urlString)
Manish Jethani 2018/05/02 17:52:23 For what it's worth I would rather call this rewri
hub 2018/05/02 22:14:32 Done.
945 {
946 let matches = this.regexp.exec(urlString);
947 if (matches)
948 return this.rewrite.replace("$1", matches[0]);
hub 2018/04/27 00:48:22 This is simplistic though. It does work, but the i
Manish Jethani 2018/04/30 20:07:11 What's wrong with the following? doRewrite(urlS
hub 2018/05/01 18:32:15 I thought about this initially but then couldn't m
Manish Jethani 2018/05/02 15:31:09 Agreed.
hub 2018/05/02 22:14:32 Done.
949
950 return null;
951 }
926 }); 952 });
927 953
928 /** 954 /**
929 * Class for whitelist filters 955 * Class for whitelist filters
930 * @param {string} text see Filter() 956 * @param {string} text see Filter()
931 * @param {string} regexpSource see RegExpFilter() 957 * @param {string} regexpSource see RegExpFilter()
932 * @param {number} contentType see RegExpFilter() 958 * @param {number} contentType see RegExpFilter()
933 * @param {boolean} matchCase see RegExpFilter() 959 * @param {boolean} matchCase see RegExpFilter()
934 * @param {string} domains see RegExpFilter() 960 * @param {string} domains see RegExpFilter()
935 * @param {boolean} thirdParty see RegExpFilter() 961 * @param {boolean} thirdParty see RegExpFilter()
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 */ 1106 */
1081 function ElemHideEmulationFilter(text, domains, selector) 1107 function ElemHideEmulationFilter(text, domains, selector)
1082 { 1108 {
1083 ElemHideBase.call(this, text, domains, selector); 1109 ElemHideBase.call(this, text, domains, selector);
1084 } 1110 }
1085 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1111 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1086 1112
1087 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1113 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1088 type: "elemhideemulation" 1114 type: "elemhideemulation"
1089 }); 1115 });
OLDNEW
« no previous file with comments | « no previous file | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld