I am working on a system where users are able to submit HTML to the system, and then 'preview' the resulting page. The HTML submitted by the user is given to the page via a call to HTML.Raw in the view, where it is displayed as part of a table with other data. In a naive implementation, it is being displayed like so:
...
<td>
<div>@Html.Raw(UserEnteredHTMLIsFetchedHere)</div>
</td>
...
The problem I am having is that the user-entered HTML is not guaranteed to be error-free (far from it, in fact). If a user enters an unclosed div tag in the HTML and then goes to preview it, that unclosed tag returned by Html.Raw will close on the /div outside of the Html.Raw call and create improper nesting on the entire page.
I am looking for a way to sequester the output of Html.Raw so that unclosed tags will not interfere with the rest of the page.
I have attempted to use iframe tags to accomplish this (setting the src to the encoded HTML), but it did not play nice with CSS. I would like the existing CSS of the page to be preserved within the sequestered HTML if at all possible.
I'm not sure if this is a way that Html.Raw is even supposed to be used, but this is an implementation that I am modifying, not creating, and I would like to preserve as much of the original implementation as possible.