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

Inject attributes to an element while scrubbing #96

Closed
vivekjMSFT opened this issue Nov 28, 2016 · 6 comments
Closed

Inject attributes to an element while scrubbing #96

vivekjMSFT opened this issue Nov 28, 2016 · 6 comments

Comments

@vivekjMSFT
Copy link

is there a way that while scrubbing, the sanitizer can inject any attribute to an element. Such as while scrubbing anchor tags , I want to add target="_blank" to every anchor tag encountered by sanitizer?

@tiesont
Copy link

tiesont commented Nov 29, 2016

There isn't anything built in, but you could probably do that by subscribing to the PostProcessNode event, and adding that logic.

Since HtmlSanitizer uses the AngleSharp library for parsing HTML, you could also just use it directly on the sanitized markup.

@mganss
Copy link
Owner

mganss commented Nov 29, 2016

Here's how you could do this using the PostProcessNode event:

sanitizer.PostProcessNode += (s, e) => (e.Node as IHtmlAnchorElement)?.SetAttribute("target", "_blank");

@mganss mganss closed this as completed Dec 14, 2016
@richardtallent
Copy link

Perfect! This fixes my use case as well -- securing <a> tags with rel="noreferrer noopener nofollow".

@hammond13
Copy link

Nice example. I would think that adding "nofollow" to anchor tags would be a very common use case. I suggest adding that as an example in the wiki.

@mganss
Copy link
Owner

mganss commented Jan 21, 2019

@hammond13 🆗, I've added example 5 to the wiki.

@vanillajonathan
Copy link
Contributor

To anyone who stumbled across this, other rels which may be of interest include sponsored and ucg (user-generated content) according to Google.
https://developers.google.com/search/docs/advanced/guidelines/qualify-outbound-links

As well as external.
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel

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

No branches or pull requests

6 participants