Skip to content

Using the Library: Custom Tags and Attributes

Kevin Cheng edited this page Jan 28, 2018 · 3 revisions

If your project supports custom tags and attributes, for example the below

<myContainer myId="101">
    <div>
        <a href=\"www.google.com\">Click Me</a>
    </div>
</myContainer>

the tag and attribute myContainer and myId needs to be added to the appropriate custom whitelists. Using the default whitelists shall result in an empty string, because while div and a are whitelisted, the parent myContainer tag is not, and thus everything within it are also removed.

For the above example to be accepted, the following custom whitelists must be used.

var myTags = new List<String>() { "myContainer", "div", "a" };
var myAttributes = new List<String>() { "myId", "href" };

Extending the Default Whitelists with Special Tags and Attributes

If the defaults provided by MarkupSanity are acceptable, but you need to support additional non-standard tags and attributes, they can be configured in the Supplemental lists. The Supplemental lists extends the default whitelists.

In the application startup, add these values as shown

 MarkupSanity.Configure.SupplementalTags = new List<String>() { "myContainer" };
 MarkupSanity.Configure.SupplementalAttributes = new List<String>() { "myId" };

Now anytime the SanitizeHtml() function is called without any parameters, these specials tags and attributes are allowed and shall no longer be stripped out.

NOTE: The Supplemental lists do not affect the custom whitelists as those lists should already contain the special values.