apostrophecms / sanitize-html

Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis. Built on htmlparser2 for speed and tolerance

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow code snippets

BenRacicot opened this issue · comments

If you're unsure how to proceed with a problem and are not sure if it is a bug, please provide some information so we can help you. You can also use the Apostrophe Discord chat and Github Discussions to ask questions as well.

Question or comment

Please include as much detail as possible so we can help more quickly.

  • What are you trying to do?
    I'm trying to allow code snippets through sanitization.
  • What have you already tried?
    Implemented sanitize-html and read the docs (might be missing something)

Details

sanitize-html is wokring great in my project, however after implementing my rich-text-editor and allowing users to add code snippets obviously they are removed once I sanitize the content.

Is there a way to allow any code tag only within <code> ... </code>?

In HTML, the code element formats things like code, but it does not mean you don't have to escape characters like < and & and > correctly. You can do so using the normal HTML escapes e.g. &lt; &amp; and &gt;. You can see this if you make an HTML page with a code element and put some additional HTML code inside it. The elements are interpreted normally by the browser, even when inside code.

Ah escape first! Brilliant advice, thank you so much.