Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HtmlSanitizerOptions #359

Merged
merged 14 commits into from
Jul 12, 2022
Merged

Add HtmlSanitizerOptions #359

merged 14 commits into from
Jul 12, 2022

Conversation

vanillajonathan
Copy link
Contributor

No description provided.

@codecov
Copy link

codecov bot commented Jun 14, 2022

Codecov Report

Merging #359 (b21e34a) into master (874f1be) will increase coverage by 0.11%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #359      +/-   ##
==========================================
+ Coverage   94.32%   94.44%   +0.11%     
==========================================
  Files           4        5       +1     
  Lines         811      828      +17     
  Branches       85       85              
==========================================
+ Hits          765      782      +17     
  Misses         34       34              
  Partials       12       12              
Impacted Files Coverage Δ
src/HtmlSanitizer/HtmlSanitizer.cs 96.17% <100.00%> (+0.05%) ⬆️
src/HtmlSanitizer/HtmlSanitizerOptions.cs 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 874f1be...b21e34a. Read the comment docs.

/// <summary>
/// Gets or sets the allowed tag names such as "a" and "div".
/// </summary>
public ISet<string> AllowedTags { get; set; } = new HashSet<string>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these have private setters? Also, they should be initialized with StringComparer.OrdinalIgnoreCase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I would want public setters because then I can easily assign a set to the properties using object initializer syntax. Without a setter, I would have to loop over a set and append each item in the set using the Add method.

Yes, it should probably be initialized with StringComparer.OrdinalIgnoreCase if we want it to be case-insensitive, which we probably do want.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use object and collection initializer syntax even if the setter is private. Collection initializer syntax works with the Add() method so you can do this:

public class SetTest
{
    public HashSet<string> Set { get; private set; } = new HashSet<string>();
}

var s = new SetTest()
{
    Set = { "a", "b" }
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. but you cannot assign from a predefined set like this:

public static class Presets
{
    public static HashSet<string> Default { get; } = new HashSet<string>();
}

var s = new SetTest()
{
    Set = Presets.Default
};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, then how about making them init-only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I like that!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's this problem now. Not sure how to deal with it or if it's even worth it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems easier to just skip init and just have public setters instead.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah ok, I agree.

@mganss
Copy link
Owner

mganss commented Jun 15, 2022

Thanks for starting this. I'm wondering how the HtmlSanitizerOptions should tie in with the static DefaultAllowedTags etc. properties that are already in HtmlSanitizer. Should there be an additional DefaultOptions property or should it replace the individual properties? cc @tiesont

@tiesont
Copy link

tiesont commented Jun 15, 2022

Thanks for starting this. I'm wondering how the HtmlSanitizerOptions should tie in with the static DefaultAllowedTags etc. properties that are already in HtmlSanitizer. Should there be an additional DefaultOptions property or should it replace the individual properties? cc @tiesont

I think you'd wind up with a simpler process for setting up a sanitizer if this just completely replaces the existing properties.

@vanillajonathan
Copy link
Contributor Author

I agree with @tiesont.

@vanillajonathan
Copy link
Contributor Author

Sorry, I had forgot about this. It builds now.

@mganss
Copy link
Owner

mganss commented Jul 11, 2022

Could you adapt the unit tests to cover the changes?

@vanillajonathan
Copy link
Contributor Author

Now it says there is coverage.

@mganss mganss merged commit d9368e1 into mganss:master Jul 12, 2022
@vanillajonathan vanillajonathan deleted the patch-2 branch July 12, 2022 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants