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 SanitiseHtml helper method #43

Merged
merged 2 commits into from
Nov 16, 2023
Merged

Add SanitiseHtml helper method #43

merged 2 commits into from
Nov 16, 2023

Conversation

donaldgray
Copy link
Member

@donaldgray donaldgray commented Nov 15, 2023

Adds SanitiseHtml() extension method for sanitising markup inline with https://iiif.io/api/presentation/3.0/#45-html-markup-in-property-values

This introduces a dependency on https://github.com/mganss/HtmlSanitizer but this appears to be the goto library for dotnet and saves a lot of work.

The above lib doesn't support specifying valid tags per attribute but provides RemovingAttribute callback that allows same functionality by specifying no attributes are allowed and cancel those that should be safe in the callback.

It's not specified in the spec but the method will wrap content in a provided tag (defaults to span) if the string doesn't start with < and end with >. This makes an assumption that this method is only ever called when consumer wants the output to be markup.

I wasn't sure on this or whether it is better to throw an exception if the provided string doesn't start with < and end with >. Wrapping it is more forgiving but can result in some unexpected results as HtmlSanitizer lib auto-closes some tags.

@donaldgray donaldgray merged commit 34b7d93 into main Nov 16, 2023
1 check passed
@donaldgray donaldgray deleted the feature/sanitize_html branch November 16, 2023 10:07
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.

4 participants