EmilStenstrom / django-components

Create simple reusable template components in Django.
MIT License
1.18k stars 76 forks source link

Bug: Incorrect parsing of apostrophes #752

Open MartinBoge opened 1 week ago

MartinBoge commented 1 week ago

Hi Emil

First off - thank you very much for this sweet sweet library. If this issue is redundant or you find it irrelevant just go ahead and close it.

I currently have a card header component that i want to use like so:

{% component "card_header" ...other code... tooltip_text=_("Responsible for the organisation's data protection compliance") %} {% endcomponent %}

But i get the following error:

The workaround for me right is to let the tag be self-closing via the / %} syntax and then it works.

{% component "card_header" ...other code... tooltip_text=_("Responsible for the organisation's data protection compliance") / %}

I have narrowed down the cause to being that i have an apostrophe ( ' ) in my text and causes and issue when the template is being parsed. That means i do NOT get the error if i:

  1. Remove the apostrophe from my str variable.
  2. Let the tag be self-closing.

And also note that with or without translation the bug remains.

Is this a known issue?

EmilStenstrom commented 6 days ago

Hi Martin, it sounds like a bug that should be fixed.

@JuroOravec Mind helping me understand what the right syntax should be here? tooltip_text="{% trans "Responsible..." %}"?

JuroOravec commented 5 days ago

@MartinBoge Do you have access to the full stack trace? Could you post it here?

Btw, @MartinBoge, so if you omit the single apostrophe, then you are able to pass the text like so?

{% component "card_header" tooltip_text=_("Responsible for the organisations data protection compliance") / %}

I haven't worked with Django's translation / internationalisation features, so personally I never tried passing the key=_("some text") syntax through components. I wonder if that has something to do with it?

@MartinBoge could you check which of the following syntaxes work correctly? The one you mentioned above is case 7. below. I expect that cases 1-4 SHOULD work, but no idea how 5-8 behave.

  1. Without _(), without single apostrophe, self-closing:

    {% component "card_header" tooltip_text="organisations" / %}
  2. Without _(), without single apostrophe, end-tag:

    {% component "card_header" tooltip_text="organisations" %} {% endcomponent %}
  3. Without _(), with single apostrophe, self-closing:

    {% component "card_header" tooltip_text="organisation's" / %}
  4. Without _(), with single apostrophe, end-tag:

    {% component "card_header" tooltip_text="organisation's" %} {% endcomponent %}
  5. With _(), without single apostrophe, self-closing:

    {% component "card_header" tooltip_text=_("organisations") / %}
  6. With _(), without single apostrophe, end-tag:

    {% component "card_header" tooltip_text=_("organisations") %} {% endcomponent %}
  7. With _(), with single apostrophe, self-closing:

    {% component "card_header" tooltip_text=_("organisation's") / %}
  8. With _(), with single apostrophe, end-tag:

    {% component "card_header" tooltip_text=_("organisation's") %} {% endcomponent %}

I'll be able to have a look at this towards the end of this week :)