Python bleach.ALLOWED_TAGS Examples

The following are 7 code examples of bleach.ALLOWED_TAGS(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bleach , or try the search function .
Example #1
Source File: html_processing.py    From feedsubs with MIT License 6 votes vote down vote up
def clean_article(content: str, base_url: str=None,
                  replace_images: bool=True) -> str:
    """Clean and format an untrusted chunk of HTML.

    This filter cleans the HTML from dangerous tags and formats it so that
    it fits with the style of the surrounding document by shifting titles.
    """
    soup = bs4.BeautifulSoup(content, 'html.parser')
    remove_unwanted_tags(soup)
    unify_style(soup)
    rewrite_relative_links(soup, base_url)
    if replace_images and READER_CACHE_IMAGES:
        rewrite_image_links(soup)
    content = soup.prettify()

    content = bleach.clean(
        content, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, strip=True
    )

    return content 
Example #2
Source File: staff_notifications.py    From cornerwise with MIT License 5 votes vote down vote up
def clean_message(self):
        message = self.cleaned_data["message"]
        return bleach.clean(
            message,
            tags=bleach.ALLOWED_TAGS + ["p", "pre", "span", "h1", "h2",
                                        "h3", "h4", "h5", "h6"],
            attributes=["title", "href", "style"],
            styles=["text-decoration", "text-align"]) 
Example #3
Source File: models.py    From palanaeum with GNU Affero General Public License v3.0 5 votes vote down vote up
def save(self, *args, **kwargs):
        self.text = bleach.clean(self.text, strip=True, strip_comments=True,
                                 tags=bleach.ALLOWED_TAGS + ['p'])
        self.speaker = bleach.clean(self.speaker, strip=True, strip_comments=True)
        super(EntryLine, self).save(*args, **kwargs) 
Example #4
Source File: utils.py    From PonyConf with Apache License 2.0 5 votes vote down vote up
def markdown_to_html(md):
    html = markdown(md)
    allowed_tags = bleach.ALLOWED_TAGS + ['p', 'pre', 'span' ] + ['h%d' % i for i in range(1, 7) ]
    html = bleach.clean(html, tags=allowed_tags)
    return mark_safe(html) 
Example #5
Source File: applications.py    From puffin with GNU Affero General Public License v3.0 5 votes vote down vote up
def init():
    flaskext.markdown.Markdown(app)
    app.config['BLEACH_ALLOWED_TAGS'] = bleach.ALLOWED_TAGS + ["p", "h1", "h2", "h3", "h4", "h5", "h6", "img"]
    app.config['BLEACH_ALLOWED_ATTRIBUTES'] = dict(bleach.ALLOWED_ATTRIBUTES, img=["src"])
    flask_bleach.Bleach(app) 
Example #6
Source File: utilities.py    From open-humans with MIT License 5 votes vote down vote up
def markdown(value):
    """
    Translate markdown to a safe subset of HTML.
    """
    cleaned = bleach.clean(
        markdown_library.markdown(value),
        tags=bleach.ALLOWED_TAGS + ["p", "h1", "h2", "h3", "h4", "h5", "h6"],
    )

    linkified = bleach.linkify(cleaned)

    return mark_safe(linkified) 
Example #7
Source File: test_html.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_raw_html_write_clean():
    """
    Test that columns can contain raw HTML which is not escaped.
    """
    import bleach

    t = Table([['<script>x</script>'], ['<p>y</p>'], ['<em>y</em>']], names=['a', 'b', 'c'])

    # Confirm that <script> and <p> get escaped but not <em>
    out = StringIO()
    t.write(out, format='ascii.html', htmldict={'raw_html_cols': t.colnames})
    expected = """\
   <tr>
    <td>&lt;script&gt;x&lt;/script&gt;</td>
    <td>&lt;p&gt;y&lt;/p&gt;</td>
    <td><em>y</em></td>
   </tr>"""
    assert expected in out.getvalue()

    # Confirm that we can whitelist <p>
    out = StringIO()
    t.write(out, format='ascii.html',
            htmldict={'raw_html_cols': t.colnames,
                      'raw_html_clean_kwargs': {'tags': bleach.ALLOWED_TAGS + ['p']}})
    expected = """\
   <tr>
    <td>&lt;script&gt;x&lt;/script&gt;</td>
    <td><p>y</p></td>
    <td><em>y</em></td>
   </tr>"""
    assert expected in out.getvalue()