diff --git a/DistillNET/DistillNET.csproj b/DistillNET/DistillNET.csproj index f0adcee..b9f57a0 100644 --- a/DistillNET/DistillNET.csproj +++ b/DistillNET/DistillNET.csproj @@ -9,13 +9,13 @@ https://github.com/TechnikEmpire/DistillNET DistillNET is a library for matching and filtering HTTP requests URLs using the Adblock Plus Filter format. Copyright © 2017 - 2018 Jesse Nicholson - 1.6.2 + 1.6.3 Jesse Nicholson Technik Empire DistillNET Adblock AdblockPlus Adblock-Plus URL-Filter URL-Filtering Content-Filter Filter - Fixes an issue where cache is not built on an instance where no rules are ever loaded. - 1.6.2.0 - 1.6.2.0 + Upgrades dependencies to latest version. + 1.6.3.0 + 1.6.3.0 @@ -27,8 +27,8 @@ - - + + diff --git a/DistillNET/DistillNET/AbpFormatRuleParser.cs b/DistillNET/DistillNET/AbpFormatRuleParser.cs index 84993cf..68f65ae 100644 --- a/DistillNET/DistillNET/AbpFormatRuleParser.cs +++ b/DistillNET/DistillNET/AbpFormatRuleParser.cs @@ -10,6 +10,7 @@ using DistillNET.Extensions; using System; using System.Collections.Generic; +using System.Linq; namespace DistillNET { @@ -94,6 +95,7 @@ int IEqualityComparer.GetHashCode(string obj) { "~elemhide", UrlFilter.UrlFilterOptions.ExceptElemHide }, { "other", UrlFilter.UrlFilterOptions.Other }, { "~other", UrlFilter.UrlFilterOptions.ExceptOther }, + #pragma warning disable CS0618 // Type or member is obsolete { "media", UrlFilter.UrlFilterOptions.Media }, { "~media", UrlFilter.UrlFilterOptions.ExceptMedia }, { "font", UrlFilter.UrlFilterOptions.Font }, @@ -105,6 +107,7 @@ int IEqualityComparer.GetHashCode(string obj) { "generichide", UrlFilter.UrlFilterOptions.GenericHide }, { "genericblock", UrlFilter.UrlFilterOptions.GenericBlock }, { "ping", UrlFilter.UrlFilterOptions.Ping } + #pragma warning restore CS0618 // Type or member is obsolete }; /// @@ -114,8 +117,13 @@ int IEqualityComparer.GetHashCode(string obj) private static readonly char[] s_anchoredEndIndicators = new[] { '/', ':', '?', '=', '&', '*', '^' }; /// + /// Constructs a new instance. /// /// + /// The rule string. + /// + /// + /// The category ID to assign to this rule. /// /// /// @@ -169,7 +177,7 @@ public Filter ParseAbpFormattedRule(string rule, short categoryId) { return ParseCssSelector(rule, cssSelectorStart, isException, categoryId); } - catch(ArgumentOutOfRangeException e) + catch (ArgumentOutOfRangeException) { throw new ArgumentException("Out of range exception while parsing CSS selector rule. Rule must be malformed.", nameof(rule)); } @@ -179,7 +187,7 @@ public Filter ParseAbpFormattedRule(string rule, short categoryId) { return ParseUrlFilter(rule, optionsStart, hasOptions, isException, categoryId); } - catch(ArgumentOutOfRangeException e) + catch (ArgumentOutOfRangeException) { throw new ArgumentException("Out of range exception while parsing filter rule. Rule must be malformed.", nameof(rule)); } @@ -198,6 +206,9 @@ public Filter ParseAbpFormattedRule(string rule, short categoryId) /// Whether or not this rule is an exception rule. It is obviously assumed that this has been /// predetermined externally and this will not be determined internally. /// + /// + /// The category ID to assign to this rule. + /// /// /// A css selector filtering class instance built from the parsed rule. /// @@ -205,7 +216,7 @@ private Filter ParseCssSelector(string rule, int selectorStartOffset, bool isExc { var originalRuleCopy = rule; - string[] applicableDomains = null; + List applicableDomains = null; if(selectorStartOffset > 0) { @@ -213,7 +224,7 @@ private Filter ParseCssSelector(string rule, int selectorStartOffset, bool isExc // indicates that it's a domain-specific CSS selector rule. As such, one or more // domains, separated by a comma character, will preceed the actual CSS selector // rule. - applicableDomains = rule.Substring(0, selectorStartOffset).Split(s_optionsDelim, StringSplitOptions.None); + applicableDomains = rule.Substring(0, selectorStartOffset).Split(s_optionsDelim, StringSplitOptions.None).ToList(); } // If it's an exception, we need to cut off three characters from the start of the CSS diff --git a/DistillNET/DistillNET/FilterDbCollection.cs b/DistillNET/DistillNET/FilterDbCollection.cs index 101936a..740427e 100644 --- a/DistillNET/DistillNET/FilterDbCollection.cs +++ b/DistillNET/DistillNET/FilterDbCollection.cs @@ -550,14 +550,15 @@ private List GetAllPossibleSubdomains(string inputDomain) return retVal; } - public IEnumerable GetFiltersForRequest(Uri requestString, string referer = "") - { - return null; - } - #region IDisposable Support private bool disposedValue = false; // To detect redundant calls + /// + /// Disposes the object. + /// + /// + /// Disposing val. + /// protected virtual void Dispose(bool disposing) { if(!disposedValue) @@ -585,6 +586,9 @@ protected virtual void Dispose(bool disposing) // } // This code added to correctly implement the disposable pattern. + /// + /// Disposes the object. + /// public void Dispose() { // Do not change this code. Put cleanup code in Dispose(bool disposing) above. diff --git a/DistillNET/DistillNET/HtmlFilter.cs b/DistillNET/DistillNET/HtmlFilter.cs index bf9ad90..71ca3a6 100644 --- a/DistillNET/DistillNET/HtmlFilter.cs +++ b/DistillNET/DistillNET/HtmlFilter.cs @@ -6,21 +6,56 @@ */ using System; -using System.Diagnostics; +using System.Collections.Generic; namespace DistillNET { + /// + /// The HtmlFilter is a class that represents a CSS selector that can be removed or whitelisted + /// in source HTML. + /// public class HtmlFilter : Filter { /// - /// Gets an array of all domains that this CSS selector rule applies to. In the event that - /// this array is empty, the rule applies globally, to all domains. + /// Gets a list of all referers that this HTML filter rule applies to. In the event that this + /// array is empty, the referer field on requests will not be checked. /// - public string[] ApplicableDomains + public List ApplicableReferers { get; private set; - } = new string[0]; + } + + /// + /// Gets a list of all referers that this HTML filter rule applies to. In the event that this + /// array is empty, the referer field on requests will not be checked. + /// + public List ExceptReferers + { + get; + private set; + } + + /// + /// Gets a list of all domains that this HTML filter rule applies to. In the event that this + /// array is empty, the rule applies globally, to all domains. + /// + public List ApplicableDomains + { + get; + private set; + } + + /// + /// Gets a list of all domains that this HTML filter should not be applied to. In the event + /// that this array is empty, the rule applies either globally, or exclusively to the list of + /// applicable domains, if that property is not empty. + /// + public List ExceptionDomains + { + get; + private set; + } /// /// Gets the raw CSS selector rule string. @@ -49,14 +84,17 @@ public string CssSelector /// Whether or not this CSS selector rule is an exception. Exception rules protect matching /// content from being filtered out. /// + /// + /// The category ID to assign to this rule. + /// /// /// If compiled with TE_FILTERING_VERIFY_RULE_DATA, the cssSelector parameter will undergo /// checks to ensure that it is not null, empty or whitespace. If these checks are performed /// and any of those conditions is true, the constructor will throw this exception. /// - internal HtmlFilter(string originalRule, string[] applicableDomains, string cssSelector, bool isException, short categoryId) : base(originalRule, isException, categoryId) + internal HtmlFilter(string originalRule, List applicableDomains, string cssSelector, bool isException, short categoryId) : base(originalRule, isException, categoryId) { - if(applicableDomains != null && applicableDomains.Length > 0) + if (applicableDomains != null && applicableDomains.Count > 0) { ApplicableDomains = applicableDomains; } @@ -72,13 +110,13 @@ internal HtmlFilter(string originalRule, string[] applicableDomains, string cssS CssSelector = cssSelector; } + /// + /// Removes verbose data from the constructed instance. + /// public override void TrimExcessData() { - if(this.ApplicableDomains != null) - { - Array.Clear(this.ApplicableDomains, 0, this.ApplicableDomains.Length); - this.ApplicableDomains = new string[0]; - } + ApplicableDomains.Clear(); + ExceptionDomains.Clear(); OriginalRule = string.Empty; } diff --git a/DistillNET/DistillNET/UrlFilter.cs b/DistillNET/DistillNET/UrlFilter.cs index 69ff994..0bef24a 100644 --- a/DistillNET/DistillNET/UrlFilter.cs +++ b/DistillNET/DistillNET/UrlFilter.cs @@ -260,6 +260,17 @@ public enum UrlFilterOptions : long /// public class UrlFilteringRuleFragment { + /// + /// Gets whether or not the supplied URI matches this rule segment. + /// + /// + /// The URI. + /// + /// + /// The current position in scan. + /// + /// + /// public virtual int IsMatch(Uri source, int lastPosition) { return -1; @@ -272,6 +283,17 @@ public virtual int IsMatch(Uri source, int lastPosition) /// public class WildcardFragment : UrlFilteringRuleFragment { + /// + /// Gets whether or not the supplied URI matches this rule segment. + /// + /// + /// The URI. + /// + /// + /// The current position in scan. + /// + /// + /// public override int IsMatch(Uri source, int lastPosition) { var stepOne = lastPosition + 1; @@ -290,8 +312,22 @@ public override int IsMatch(Uri source, int lastPosition) /// public class SeparatorFragment : UrlFilteringRuleFragment { + /// + /// An array of characters that qualify as separator characters. + /// public static readonly char[] SeparatorChars = new[] { '/', ':', '?', '=', '&' }; + /// + /// Gets whether or not the supplied URI matches this rule segment. + /// + /// + /// The URI. + /// + /// + /// The current position in scan. + /// + /// + /// public override int IsMatch(Uri source, int lastPosition) { if(lastPosition > source.AbsoluteUri.Length) @@ -309,17 +345,29 @@ public override int IsMatch(Uri source, int lastPosition) /// public class StringFragment : UrlFilteringRuleFragment { + /// + /// Gets whether or not this rule ignores case during comparison. + /// public bool ICase { get; private set; } = true; + /// + /// Constructs a new instance. + /// + /// + /// Whether or not this rule ignores case during comparison. + /// public StringFragment(bool iCase) { ICase = iCase; } + /// + /// Constructs a new instance. + /// public StringFragment() { } @@ -331,21 +379,47 @@ public StringFragment() /// public class AnchoredAddressFragment : StringFragment { + /// + /// Gets the request segment to be matched. + /// public string Request { get; private set; } = string.Empty; + /// + /// Constructs a new instance. + /// + /// + /// The request segment to match. + /// + /// + /// Whether or not matching will ignore case. + /// public AnchoredAddressFragment(string request, bool iCase) : base(iCase) { Request = request; } + /// + /// Constructs a new instance. + /// public AnchoredAddressFragment() { } + /// + /// Gets whether or not the supplied URI matches this rule segment. + /// + /// + /// The URI. + /// + /// + /// The current position in scan. + /// + /// + /// public override int IsMatch(Uri source, int lastPosition) { // Anchored stuff like this always starts at position zero. @@ -385,21 +459,44 @@ public override int IsMatch(Uri source, int lastPosition) /// public class AnchoredDomainFragment : UrlFilteringRuleFragment { + /// + /// Gets the domain that should be matched. + /// public string Domain { get; private set; } = string.Empty; + /// + /// Constructs a new instance. + /// + /// + /// The domain that should be matched. + /// public AnchoredDomainFragment(string domain) { Domain = domain; } + /// + /// Constructs a new instance. + /// public AnchoredDomainFragment() { } + /// + /// Gets whether or not the supplied URI matches this rule segment. + /// + /// + /// The URI. + /// + /// + /// The current position in scan. + /// + /// + /// public override int IsMatch(Uri source, int lastPosition) { if(Domain.Length > source.Host.Length) @@ -423,21 +520,47 @@ public override int IsMatch(Uri source, int lastPosition) /// public class StringLiteralFragment : StringFragment { + /// + /// Gets the value to be matched. + /// public string Value { get; private set; } = string.Empty; + /// + /// Constructs a new instance. + /// + /// + /// The value to be matched. + /// + /// + /// Whether or not case should be ignored during matching. + /// public StringLiteralFragment(string value, bool iCase) : base(iCase) { Value = value; } + /// + /// Constructs a new instance. + /// public StringLiteralFragment() { } + /// + /// Gets whether or not the supplied URI matches this rule segment. + /// + /// + /// The URI. + /// + /// + /// The current position in scan. + /// + /// + /// public override int IsMatch(Uri source, int lastPosition) { if(lastPosition > source.AbsoluteUri.Length) @@ -462,7 +585,7 @@ public override int IsMatch(Uri source, int lastPosition) } /// - /// Gets a hashset of all referers that this URL filter rule applies to. In the event that + /// Gets a list of all referers that this URL filter rule applies to. In the event that /// this array is empty, the referer field on requests will not be checked. /// public List ApplicableReferers @@ -472,7 +595,7 @@ public List ApplicableReferers } /// - /// Gets a hashset of all referers that this URL filter rule applies to. In the event that + /// Gets a list of all referers that this URL filter rule applies to. In the event that /// this array is empty, the referer field on requests will not be checked. /// public List ExceptReferers @@ -482,7 +605,7 @@ public List ExceptReferers } /// - /// Gets a hashset of all domains that this URL filter rule applies to. In the event that + /// Gets a list of all domains that this URL filter rule applies to. In the event that /// this array is empty, the rule applies globally, to all domains. /// public List ApplicableDomains @@ -492,7 +615,7 @@ public List ApplicableDomains } /// - /// Gets a hashset of all domains that this URL filter should not be applied to. In the event + /// Gets a list of all domains that this URL filter should not be applied to. In the event /// that this array is empty, the rule applies either globally, or exclusively to the list of /// applicable domains, if that property is not empty. /// @@ -540,25 +663,31 @@ public List Parts /// are meant to be constructed outside of the AbpFormatRuleParser class. /// /// - /// The original rule string used to build this filter. + /// The original rule string used to build this filter. /// /// - /// The UrlFilterFragment parts that make up this filter. + /// The UrlFilterFragment parts that make up this filter. /// /// - /// The filter options. + /// The filter options. /// /// - /// Domains that the filter should be applied to. + /// Domains that the filter should be applied to. /// /// - /// Domains that the filter should not be applied to. + /// Domains that the filter should not be applied to. + /// + /// + /// Referers that the filter rule should be applied to. + /// + /// + /// Referers that the filter rule should not be applied to. /// /// - /// Whether or not the filter is an exception, that is to say, a whitelisting filter. + /// Whether or not the filter is an exception, that is to say, a whitelisting filter. /// /// - /// The category ID of the category this filter belongs to. + /// The category ID of the category this filter belongs to. /// internal UrlFilter(string originalRule, List parts, UrlFilterOptions options, List applicableDomains, List exceptionDomains, List applicableReferers, List exceptionReferers, bool isException, short categoryId) : base(originalRule, isException, categoryId) { diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index f4af694..aac01a9 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -66,7 +66,7 @@ - 2.1.0-rc1-final + 2.2.0