From 20eb1438490fa94307026efcaa4c58dd7c40de6f Mon Sep 17 00:00:00 2001 From: Ryan Michela Date: Thu, 4 Feb 2016 12:47:26 -0800 Subject: [PATCH 1/2] Added Tag attributes to event args --- HtmlSanitizer/EventArgs.cs | 16 ++++++++++++++++ HtmlSanitizer/HtmlSanitizer.cs | 9 +++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/HtmlSanitizer/EventArgs.cs b/HtmlSanitizer/EventArgs.cs index 07fe1f3..d0aa0db 100644 --- a/HtmlSanitizer/EventArgs.cs +++ b/HtmlSanitizer/EventArgs.cs @@ -75,6 +75,14 @@ public class RemovingTagEventArgs : CancelEventArgs /// public class RemovingAttributeEventArgs : CancelEventArgs { + /// + /// The tag containing the attribute to be removed. + /// + /// + /// The tag. + /// + public IElement Tag { get; set; } + /// /// Gets or sets the attribute to be removed. /// @@ -97,6 +105,14 @@ public class RemovingAttributeEventArgs : CancelEventArgs /// public class RemovingStyleEventArgs : CancelEventArgs { + /// + /// The tag containing the style to be removed. + /// + /// + /// The tag. + /// + public IElement Tag { get; set; } + /// /// Gets or sets the style to be removed. /// diff --git a/HtmlSanitizer/HtmlSanitizer.cs b/HtmlSanitizer/HtmlSanitizer.cs index fe4c995..f675f1d 100644 --- a/HtmlSanitizer/HtmlSanitizer.cs +++ b/HtmlSanitizer/HtmlSanitizer.cs @@ -490,7 +490,7 @@ protected void SanitizeStyle(IHtmlElement element, string baseUrl) foreach (var style in removeStyles) { - RemoveStyle(styles, style.Item1, style.Item2); + RemoveStyle(element, styles, style.Item1, style.Item2); } foreach (var style in setStyles) @@ -595,7 +595,7 @@ private void RemoveTag(IElement tag, RemoveReason reason) /// reason why to be removed private void RemoveAttribute(IElement tag, IAttr attribute, RemoveReason reason) { - var e = new RemovingAttributeEventArgs { Attribute = attribute, Reason = reason }; + var e = new RemovingAttributeEventArgs { Tag = tag, Attribute = attribute, Reason = reason }; OnRemovingAttribute(e); if (!e.Cancel) tag.RemoveAttribute(attribute.Name); } @@ -603,12 +603,13 @@ private void RemoveAttribute(IElement tag, IAttr attribute, RemoveReason reason) /// /// Remove a style from the document. /// + /// tag where the style belongs /// collection where the style to belongs /// to be removed /// reason why to be removed - private void RemoveStyle(ICssStyleDeclaration styles, ICssProperty style, RemoveReason reason) + private void RemoveStyle(IElement tag, ICssStyleDeclaration styles, ICssProperty style, RemoveReason reason) { - var e = new RemovingStyleEventArgs { Style = style, Reason = reason }; + var e = new RemovingStyleEventArgs { Tag = tag, Style = style, Reason = reason }; OnRemovingStyle(e); if (!e.Cancel) styles.RemoveProperty(style.Name); } From 9be558996e3091a1cb648ed180023aff696084f4 Mon Sep 17 00:00:00 2001 From: Ryan Michela Date: Thu, 4 Feb 2016 13:10:22 -0800 Subject: [PATCH 2/2] EventArg Tag attribute unit tests --- HtmlSanitizer.Tests/Tests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/HtmlSanitizer.Tests/Tests.cs b/HtmlSanitizer.Tests/Tests.cs index 8e467b8..4797e8d 100644 --- a/HtmlSanitizer.Tests/Tests.cs +++ b/HtmlSanitizer.Tests/Tests.cs @@ -2065,6 +2065,15 @@ public void RemovingAttributeEventTest() Assert.That(sanitizer.Sanitize(html), Is.EqualTo(@"
").IgnoreCase); } + [Test] + public void RemovingAttributeEventTagTest() + { + var sanitizer = new HtmlSanitizer(); + sanitizer.RemovingAttribute += (s, e) => Assert.That(e.Tag, Is.InstanceOf()); + var html = @"
"; + sanitizer.Sanitize(html); + } + [Test] public void RemovingStyleEventTest() { @@ -2074,6 +2083,15 @@ public void RemovingStyleEventTest() Assert.That(sanitizer.Sanitize(html), Is.EqualTo(@"
").IgnoreCase); } + [Test] + public void RemovingStyleEventTagTest() + { + var sanitizer = new HtmlSanitizer(); + sanitizer.RemovingStyle += (s, e) => Assert.That(e.Tag, Is.InstanceOf()); + var html = @"
"; + sanitizer.Sanitize(html); + } + [Test] public void ProtocolRelativeTest() {