Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: clean() got an unexpected keyword argument 'styles' #494

Closed
haradhansharma opened this issue May 20, 2023 · 4 comments
Closed

Comments

@haradhansharma
Copy link

clean of bleach does not have styles but why it is passing here I do not understand! return bleach.clean( value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, styles=STYLES)

Below is the clean of bleach:
def clean( text, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, protocols=ALLOWED_PROTOCOLS, strip=False, strip_comments=True, css_sanitizer=None, ):

@Stefan-ci
Copy link

Stefan-ci commented Dec 3, 2023

If you're using a normal form (not ModelForm), you may avoid using SummernoteTextFormField() or SummernoteTextField(). Just use the widget SummernoteInplaceWidget() or any widget. I faced the same issue and that's how I solved it. Anyway you sould not use it now. Consider having something like : comment = forms.CharField(widget=SummernoteWidget(attrs={}))
Of course you should import the widget first.

@adampei
Copy link

adampei commented Feb 7, 2024

I am using an older version of the bleach library. To resolve the issue, I uninstalled it and then reinstalled version 4.1.0 using the command

pip install bleach==4.1.0

@larrybotha
Copy link

larrybotha commented Feb 12, 2024

An alternative to pinning to an older version of bleach is to re-implement the summernote field using bleach's updated API:

import bleach
from bleach.css_sanitizer import CSSSanitizer
from django.forms import fields
from django_summernote import settings as summernote_settings
from django_summernote.widgets import SummernoteWidget


class RichTextField(fields.CharField):
    def __init__(self, *args, **kwargs):
        kwargs.update({"widget": SummernoteWidget()})
        super().__init__(*args, **kwargs)

    def to_python(self, value):
        value = super().to_python(value)

        return bleach.clean(
            value,
            attributes=summernote_settings.ATTRIBUTES,
            css_sanitizer=CSSSanitizer(allowed_css_properties=summernote_settings.STYLES), 
            tags=summernote_settings.ALLOWED_TAGS,
        )

        # or, using nh3, instead of bleach
        # See https://daniel.feldroy.com/posts/2023-06-converting-from-bleach-to-nh3
        return nh3.clean(
            value,
            attributes={k: set(v) for k, v in summernote_settings.ATTRIBUTES.items()},
            tags=set(summernote_settings.ALLOWED_TAGS),
            url_schemes={"http", "https", "mailto"},
        )

Bleach no longer accepts the style argument, and instead accepts a css_sanitizer argument.

This is most likely what the SummernoteFormTextField would look like once updated

EDIT: Added option for integration with nh3 instead of bleach

@claudep
Copy link
Contributor

claudep commented Feb 16, 2024

In PR #499, bleach was replaced by nh3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants