Skip to content

Commit

Permalink
Merge pull request #55 from rmichela/EventArgs
Browse files Browse the repository at this point in the history
Added Tag attributes to event args
  • Loading branch information
mganss committed Feb 6, 2016
2 parents 35e0781 + 9be5589 commit 89316d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
18 changes: 18 additions & 0 deletions HtmlSanitizer.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,15 @@ public void RemovingAttributeEventTest()
Assert.That(sanitizer.Sanitize(html), Is.EqualTo(@"<div alt=""alt"" onclick=""test""></div>").IgnoreCase);
}

[Test]
public void RemovingAttributeEventTagTest()
{
var sanitizer = new HtmlSanitizer();
sanitizer.RemovingAttribute += (s, e) => Assert.That(e.Tag, Is.InstanceOf<IHtmlDivElement>());
var html = @"<div alt=""alt"" onclick=""test"" onload=""test""></div>";
sanitizer.Sanitize(html);
}

[Test]
public void RemovingStyleEventTest()
{
Expand All @@ -2074,6 +2083,15 @@ public void RemovingStyleEventTest()
Assert.That(sanitizer.Sanitize(html), Is.EqualTo(@"<div style=""background: 0; test: xyz""></div>").IgnoreCase);
}

[Test]
public void RemovingStyleEventTagTest()
{
var sanitizer = new HtmlSanitizer();
sanitizer.RemovingStyle += (s, e) => Assert.That(e.Tag, Is.InstanceOf<IHtmlDivElement>());
var html = @"<div style=""background: 0; test: xyz; bad: bad;""></div>";
sanitizer.Sanitize(html);
}

[Test]
public void ProtocolRelativeTest()
{
Expand Down
16 changes: 16 additions & 0 deletions HtmlSanitizer/EventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public class RemovingTagEventArgs : CancelEventArgs
/// </summary>
public class RemovingAttributeEventArgs : CancelEventArgs
{
/// <summary>
/// The tag containing the attribute to be removed.
/// </summary>
/// <value>
/// The tag.
/// </value>
public IElement Tag { get; set; }

/// <summary>
/// Gets or sets the attribute to be removed.
/// </summary>
Expand All @@ -97,6 +105,14 @@ public class RemovingAttributeEventArgs : CancelEventArgs
/// </summary>
public class RemovingStyleEventArgs : CancelEventArgs
{
/// <summary>
/// The tag containing the style to be removed.
/// </summary>
/// <value>
/// The tag.
/// </value>
public IElement Tag { get; set; }

/// <summary>
/// Gets or sets the style to be removed.
/// </summary>
Expand Down
9 changes: 5 additions & 4 deletions HtmlSanitizer/HtmlSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -595,20 +595,21 @@ private void RemoveTag(IElement tag, RemoveReason reason)
/// <param name="reason">reason why to be removed</param>
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);
}

/// <summary>
/// Remove a style from the document.
/// </summary>
/// <param name="tag">tag where the style belongs</param>
/// <param name="styles">collection where the style to belongs</param>
/// <param name="style">to be removed</param>
/// <param name="reason">reason why to be removed</param>
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);
}
Expand Down

0 comments on commit 89316d0

Please sign in to comment.