mirror of
https://github.com/django-components/django-components.git
synced 2025-08-18 13:10:13 +00:00
fix: format tag param with translation correctly (#849)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
aaeba99f54
commit
a7bc3f7c4c
2 changed files with 129 additions and 52 deletions
|
@ -34,6 +34,12 @@ class TagAttr:
|
||||||
value = f"...{value}"
|
value = f"...{value}"
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def formatted(self) -> str:
|
||||||
|
s = self.formatted_value()
|
||||||
|
if self.key:
|
||||||
|
return f"{self.key}={s}"
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
# Parse the content of a Django template tag like this:
|
# Parse the content of a Django template tag like this:
|
||||||
#
|
#
|
||||||
|
@ -139,6 +145,7 @@ def parse_tag_attrs(text: str) -> Tuple[str, List[TagAttr]]:
|
||||||
take_while(TAG_WHITESPACE)
|
take_while(TAG_WHITESPACE)
|
||||||
|
|
||||||
start_index = len(normalized)
|
start_index = len(normalized)
|
||||||
|
is_translation = False
|
||||||
|
|
||||||
# If token starts with a quote, we assume it's a value without key part.
|
# If token starts with a quote, we assume it's a value without key part.
|
||||||
# e.g. `component 'my_comp'`
|
# e.g. `component 'my_comp'`
|
||||||
|
@ -185,8 +192,6 @@ def parse_tag_attrs(text: str) -> Tuple[str, List[TagAttr]]:
|
||||||
if is_next_token("_("):
|
if is_next_token("_("):
|
||||||
taken_n(2) # _(
|
taken_n(2) # _(
|
||||||
is_translation = True
|
is_translation = True
|
||||||
else:
|
|
||||||
is_translation = False
|
|
||||||
|
|
||||||
# NOTE: We assume no space between the translation syntax and the quote.
|
# NOTE: We assume no space between the translation syntax and the quote.
|
||||||
quote_char = taken_n(1) # " or '
|
quote_char = taken_n(1) # " or '
|
||||||
|
@ -216,7 +221,7 @@ def parse_tag_attrs(text: str) -> Tuple[str, List[TagAttr]]:
|
||||||
start_index=start_index,
|
start_index=start_index,
|
||||||
quoted=quoted,
|
quoted=quoted,
|
||||||
spread=is_spread,
|
spread=is_spread,
|
||||||
translation=False,
|
translation=is_translation,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -9,61 +9,96 @@ setup_test_config({"autodiscover": False})
|
||||||
class TagParserTests(BaseTestCase):
|
class TagParserTests(BaseTestCase):
|
||||||
def test_tag_parser(self):
|
def test_tag_parser(self):
|
||||||
_, attrs = parse_tag_attrs("component 'my_comp' key=val key2='val2 two' ")
|
_, attrs = parse_tag_attrs("component 'my_comp' key=val key2='val2 two' ")
|
||||||
|
|
||||||
|
expected_attrs = [
|
||||||
|
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key=None, value="my_comp", start_index=10, quoted="'", spread=False, translation=False),
|
||||||
|
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key="key2", value="val2 two", start_index=28, quoted="'", spread=False, translation=False),
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(attrs, expected_attrs)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
attrs,
|
[a.formatted() for a in attrs],
|
||||||
[
|
[
|
||||||
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
"component",
|
||||||
TagAttr(key=None, value="my_comp", start_index=10, quoted="'", spread=False, translation=False),
|
"'my_comp'",
|
||||||
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
"key=val",
|
||||||
TagAttr(key="key2", value="val2 two", start_index=28, quoted="'", spread=False, translation=False),
|
"key2='val2 two'",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_tag_parser_nested_quotes(self):
|
def test_tag_parser_nested_quotes(self):
|
||||||
_, attrs = parse_tag_attrs("component 'my_comp' key=val key2='val2 \"two\"' text=\"organisation's\" ")
|
_, attrs = parse_tag_attrs("component 'my_comp' key=val key2='val2 \"two\"' text=\"organisation's\" ")
|
||||||
|
|
||||||
|
expected_attrs = [
|
||||||
|
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key=None, value="my_comp", start_index=10, quoted="'", spread=False, translation=False),
|
||||||
|
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key="key2", value='val2 "two"', start_index=28, quoted="'", spread=False, translation=False),
|
||||||
|
TagAttr(key="text", value="organisation's", start_index=46, quoted='"', spread=False, translation=False),
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(attrs, expected_attrs)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
attrs,
|
[a.formatted() for a in attrs],
|
||||||
[
|
[
|
||||||
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
"component",
|
||||||
TagAttr(key=None, value="my_comp", start_index=10, quoted="'", spread=False, translation=False),
|
"'my_comp'",
|
||||||
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
"key=val",
|
||||||
TagAttr(key="key2", value='val2 "two"', start_index=28, quoted="'", spread=False, translation=False),
|
"key2='val2 \"two\"'",
|
||||||
TagAttr(
|
'text="organisation\'s"',
|
||||||
key="text", value="organisation's", start_index=46, quoted='"', spread=False, translation=False
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_tag_parser_trailing_quote_single(self):
|
def test_tag_parser_trailing_quote_single(self):
|
||||||
_, attrs = parse_tag_attrs("component 'my_comp' key=val key2='val2 \"two\"' text=\"organisation's\" 'abc")
|
_, attrs = parse_tag_attrs("component 'my_comp' key=val key2='val2 \"two\"' text=\"organisation's\" 'abc")
|
||||||
|
|
||||||
|
expected_attrs = [
|
||||||
|
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key=None, value="my_comp", start_index=10, quoted="'", spread=False, translation=False),
|
||||||
|
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key="key2", value='val2 "two"', start_index=28, quoted="'", spread=False, translation=False),
|
||||||
|
TagAttr(key="text", value="organisation's", start_index=46, quoted='"', spread=False, translation=False),
|
||||||
|
TagAttr(key=None, value="'abc", start_index=68, quoted=None, spread=False, translation=False),
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(attrs, expected_attrs)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
attrs,
|
[a.formatted() for a in attrs],
|
||||||
[
|
[
|
||||||
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
"component",
|
||||||
TagAttr(key=None, value="my_comp", start_index=10, quoted="'", spread=False, translation=False),
|
"'my_comp'",
|
||||||
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
"key=val",
|
||||||
TagAttr(key="key2", value='val2 "two"', start_index=28, quoted="'", spread=False, translation=False),
|
"key2='val2 \"two\"'",
|
||||||
TagAttr(
|
'text="organisation\'s"',
|
||||||
key="text", value="organisation's", start_index=46, quoted='"', spread=False, translation=False
|
"'abc",
|
||||||
),
|
|
||||||
TagAttr(key=None, value="'abc", start_index=68, quoted=None, spread=False, translation=False),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_tag_parser_trailing_quote_double(self):
|
def test_tag_parser_trailing_quote_double(self):
|
||||||
_, attrs = parse_tag_attrs('component "my_comp" key=val key2="val2 \'two\'" text=\'organisation"s\' "abc')
|
_, attrs = parse_tag_attrs('component "my_comp" key=val key2="val2 \'two\'" text=\'organisation"s\' "abc')
|
||||||
|
expected_attrs = [
|
||||||
|
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key=None, value="my_comp", start_index=10, quoted='"', spread=False, translation=False),
|
||||||
|
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key="key2", value="val2 'two'", start_index=28, quoted='"', spread=False, translation=False),
|
||||||
|
TagAttr(
|
||||||
|
key="text", value='organisation"s', start_index=46, quoted="'", spread=False, translation=False
|
||||||
|
), # noqa: E501
|
||||||
|
TagAttr(key=None, value='"abc', start_index=68, quoted=None, spread=False, translation=False),
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(attrs, expected_attrs)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
attrs,
|
[a.formatted() for a in attrs],
|
||||||
[
|
[
|
||||||
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
"component",
|
||||||
TagAttr(key=None, value="my_comp", start_index=10, quoted='"', spread=False, translation=False),
|
'"my_comp"',
|
||||||
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
"key=val",
|
||||||
TagAttr(key="key2", value="val2 'two'", start_index=28, quoted='"', spread=False, translation=False),
|
"key2=\"val2 'two'\"",
|
||||||
TagAttr(
|
"text='organisation\"s'",
|
||||||
key="text", value='organisation"s', start_index=46, quoted="'", spread=False, translation=False
|
'"abc',
|
||||||
), # noqa: E501
|
|
||||||
TagAttr(key=None, value='"abc', start_index=68, quoted=None, spread=False, translation=False),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -71,17 +106,25 @@ class TagParserTests(BaseTestCase):
|
||||||
_, attrs = parse_tag_attrs(
|
_, attrs = parse_tag_attrs(
|
||||||
"component 'my_comp' key=val key2='val2 \"two\"' text=\"organisation's\" value='abc"
|
"component 'my_comp' key=val key2='val2 \"two\"' text=\"organisation's\" value='abc"
|
||||||
)
|
)
|
||||||
|
expected_attrs = [
|
||||||
|
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key=None, value="my_comp", start_index=10, quoted="'", spread=False, translation=False),
|
||||||
|
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key="key2", value='val2 "two"', start_index=28, quoted="'", spread=False, translation=False),
|
||||||
|
TagAttr(key="text", value="organisation's", start_index=46, quoted='"', spread=False, translation=False),
|
||||||
|
TagAttr(key="value", value="'abc", start_index=68, quoted=None, spread=False, translation=False),
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(attrs, expected_attrs)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
attrs,
|
[a.formatted() for a in attrs],
|
||||||
[
|
[
|
||||||
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
"component",
|
||||||
TagAttr(key=None, value="my_comp", start_index=10, quoted="'", spread=False, translation=False),
|
"'my_comp'",
|
||||||
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
"key=val",
|
||||||
TagAttr(key="key2", value='val2 "two"', start_index=28, quoted="'", spread=False, translation=False),
|
"key2='val2 \"two\"'",
|
||||||
TagAttr(
|
'text="organisation\'s"',
|
||||||
key="text", value="organisation's", start_index=46, quoted='"', spread=False, translation=False
|
"value='abc",
|
||||||
),
|
|
||||||
TagAttr(key="value", value="'abc", start_index=68, quoted=None, spread=False, translation=False),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -89,16 +132,45 @@ class TagParserTests(BaseTestCase):
|
||||||
_, attrs = parse_tag_attrs(
|
_, attrs = parse_tag_attrs(
|
||||||
'component "my_comp" key=val key2="val2 \'two\'" text=\'organisation"s\' value="abc'
|
'component "my_comp" key=val key2="val2 \'two\'" text=\'organisation"s\' value="abc'
|
||||||
)
|
)
|
||||||
|
expected_attrs = [
|
||||||
|
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key=None, value="my_comp", start_index=10, quoted='"', spread=False, translation=False),
|
||||||
|
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key="key2", value="val2 'two'", start_index=28, quoted='"', spread=False, translation=False),
|
||||||
|
TagAttr(key="text", value='organisation"s', start_index=46, quoted="'", spread=False, translation=False),
|
||||||
|
TagAttr(key="value", value='"abc', start_index=68, quoted=None, spread=False, translation=False),
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(attrs, expected_attrs)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
attrs,
|
[a.formatted() for a in attrs],
|
||||||
[
|
[
|
||||||
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
"component",
|
||||||
TagAttr(key=None, value="my_comp", start_index=10, quoted='"', spread=False, translation=False),
|
'"my_comp"',
|
||||||
TagAttr(key="key", value="val", start_index=20, quoted=None, spread=False, translation=False),
|
"key=val",
|
||||||
TagAttr(key="key2", value="val2 'two'", start_index=28, quoted='"', spread=False, translation=False),
|
"key2=\"val2 'two'\"",
|
||||||
TagAttr(
|
"text='organisation\"s'",
|
||||||
key="text", value='organisation"s', start_index=46, quoted="'", spread=False, translation=False
|
'value="abc',
|
||||||
),
|
],
|
||||||
TagAttr(key="value", value='"abc', start_index=68, quoted=None, spread=False, translation=False),
|
)
|
||||||
|
|
||||||
|
def test_tag_parser_translation(self):
|
||||||
|
_, attrs = parse_tag_attrs('component "my_comp" _("one") key=_("two")')
|
||||||
|
|
||||||
|
expected_attrs = [
|
||||||
|
TagAttr(key=None, value="component", start_index=0, quoted=None, spread=False, translation=False),
|
||||||
|
TagAttr(key=None, value="my_comp", start_index=10, quoted='"', spread=False, translation=False),
|
||||||
|
TagAttr(key=None, value="one", start_index=20, quoted='"', spread=False, translation=True),
|
||||||
|
TagAttr(key="key", value="two", start_index=29, quoted='"', spread=False, translation=True),
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(attrs, expected_attrs)
|
||||||
|
self.assertEqual(
|
||||||
|
[a.formatted() for a in attrs],
|
||||||
|
[
|
||||||
|
"component",
|
||||||
|
'"my_comp"',
|
||||||
|
'_("one")',
|
||||||
|
'key=_("two")',
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue