Any way to display some heavily-styled HTML in isolation from the rest of site's styles?

前端 未结 5 746
暗喜
暗喜 2020-12-18 07:15

I am trying to figure out a way to display an archive of email newsletters on my client\'s site. The issue is that the newsletters are full of a zillion inline styles, whic

5条回答
  •  情书的邮戳
    2020-12-18 08:02

    Cutting and pasting raw HTML presents too many security problems, in my opinion. Never trust user's input. Even when the content is entirely benign, next week the designer of newsletter might decide to change their formatting or incorporate some javascript and you'll be responsible for anything that might go wrong.

    Therefore I would implement a parser that would drop anything but the content part and leave only b, a, h*, blockquote and similar simple elements, like the ones allowed in forum posts, as well as their styles. After that, you can display it as a normal post in a CMS. I don't see any reason why that should look differently.

    As for how to isolate that from your other CSS, you don't really need to if you are careful that all of CSS rules of your CMS apply to elements with specific classes. Alternatively, do a CSS reset for your posts:

    .post p {
         margin: 0;
         ... 
    .post /* all the standard CSS reset rules preceded with .post */
    

    and then

    content parsed from your CMS

提交回复
热议问题