Skip to content

Simplified regex expressions #157

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

Merged
merged 4 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/reactpy_django/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

_logger = logging.getLogger(__name__)
_component_tag = r"(?P<tag>component)"
_component_path = r"(?P<path>(\"[^\"'\s]+\")|('[^\"'\s]+'))"
_component_kwargs = r"(?P<kwargs>(.*?|\s*?)*)"
COMMENT_REGEX = re.compile(r"(<!--)(.|\s)*?(-->)")
_component_path = r"(?P<path>\"[^\"'\s]+\"|'[^\"'\s]+')"
_component_kwargs = r"(?P<kwargs>[\s\S]*?)"
COMMENT_REGEX = re.compile(r"<!--[\s\S]*?-->")
COMPONENT_REGEX = re.compile(
r"{%\s*"
+ _component_tag
Expand Down
173 changes: 112 additions & 61 deletions tests/test_app/tests/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,132 @@

class RegexTests(TestCase):
def test_component_regex(self):
for component in {
r'{%component "my.component"%}',
r'{%component "my.component"%}',
r"{%component 'my.component'%}",
r'{% component "my.component" %}',
r"{% component 'my.component' %}",
r'{% component "my.component" class="my_thing" %}',
# Real component matches
self.assertRegex(r'{%component "my.component"%}', COMPONENT_REGEX)
self.assertRegex(r'{%component "my.component"%}', COMPONENT_REGEX)
self.assertRegex(r"{%component 'my.component'%}", COMPONENT_REGEX)
self.assertRegex(r'{% component "my.component" %}', COMPONENT_REGEX)
self.assertRegex(r"{% component 'my.component' %}", COMPONENT_REGEX)
self.assertRegex(
r'{% component "my.component" class="my_thing" %}', COMPONENT_REGEX
)
self.assertRegex(
r'{% component "my.component" class="my_thing" attr="attribute" %}',
COMPONENT_REGEX,
)
self.assertRegex(
r"""{%
component
"my.component"
class="my_thing"
attr="attribute"

component
"my.component"
class="my_thing"
attr="attribute"
%}""", # noqa: W291
COMPONENT_REGEX,
)

%}""", # noqa: W291
}:
self.assertRegex(component, COMPONENT_REGEX)
# Fake component matches
self.assertNotRegex(r'{% not_a_real_thing "my.component" %}', COMPONENT_REGEX)
self.assertNotRegex(r"{% component my.component %}", COMPONENT_REGEX)
self.assertNotRegex(r"""{% component 'my.component" %}""", COMPONENT_REGEX)
self.assertNotRegex(r'{ component "my.component" }', COMPONENT_REGEX)
self.assertNotRegex(r'{{ component "my.component" }}', COMPONENT_REGEX)
self.assertNotRegex(r"component", COMPONENT_REGEX)
self.assertNotRegex(r"{%%}", COMPONENT_REGEX)
self.assertNotRegex(r" ", COMPONENT_REGEX)
self.assertNotRegex(r"", COMPONENT_REGEX)
self.assertNotRegex(r'{% component " my.component " %}', COMPONENT_REGEX)
self.assertNotRegex(
r"""{% component "my.component COMPONENT_REGEX)
self.assertNotRegex( " %}""",
COMPONENT_REGEX,
)
self.assertNotRegex(r'{{ component """ }}', COMPONENT_REGEX)
self.assertNotRegex(r'{{ component "" }}', COMPONENT_REGEX)

for fake_component in {
r'{% not_a_real_thing "my.component" %}',
r"{% component my.component %}",
r"""{% component 'my.component" %}""",
r'{ component "my.component" }',
r'{{ component "my.component" }}',
r"component",
r"{%%}",
r" ",
r"",
r'{% component " my.component " %}',
r"""{% component "my.component
" %}""",
r'{{ component """ }}',
r'{{ component "" }}',
}:
self.assertNotRegex(fake_component, COMPONENT_REGEX)
# Make sure back-to-back components are not merged into one match
double_component_match = COMPONENT_REGEX.search(
r'{% component "my.component" %} {% component "my.component" %}'
)
self.assertTrue(double_component_match[0] == r'{% component "my.component" %}') # type: ignore

def test_comment_regex(self):
for comment in {
r"<!-- comment -->",
# Real comment matches
self.assertRegex(r"<!-- comment -->", COMMENT_REGEX)
self.assertRegex(
r"""<!-- comment
-->""",
-->""",
COMMENT_REGEX,
)
self.assertRegex(
r"""<!--
comment -->""",
comment -->""",
COMMENT_REGEX,
)
self.assertRegex(
r"""<!--
comment
-->""",
comment
-->""",
COMMENT_REGEX,
)
self.assertRegex(
r"""<!--
a comment
another comments
drink some cement
-->""", # noqa: W291
}:
self.assertRegex(comment, COMMENT_REGEX)
a comment
another comments
drink some cement
-->""", # noqa: W291
COMMENT_REGEX,
)

for fake_comment in {
r"<!-- a comment ",
r"another comment -->",
r"<! - - comment - - >",
r'{% component "my.component" %}',
}:
self.assertNotRegex(fake_comment, COMMENT_REGEX)
# Fake comment matches
self.assertNotRegex(r"<!-- a comment ", COMMENT_REGEX)
self.assertNotRegex(r"another comment -->", COMMENT_REGEX)
self.assertNotRegex(r"<! - - comment - - >", COMMENT_REGEX)
self.assertNotRegex(r'{% component "my.component" %}', COMMENT_REGEX)

for embedded_comment in {
r'{% component "my.component" %} <!-- comment -->',
r'<!-- comment --> {% component "my.component" %}',
r'<!-- comment --> {% component "my.component" %} <!-- comment -->',
r"""<!-- comment
# Components surrounded by comments
self.assertEquals(
COMMENT_REGEX.sub(
"", r'{% component "my.component" %} <!-- comment -->'
).strip(),
'{% component "my.component" %}',
)
self.assertEquals(
COMMENT_REGEX.sub(
"", r'<!-- comment --> {% component "my.component" %}'
).strip(),
'{% component "my.component" %}',
)
self.assertEquals(
COMMENT_REGEX.sub(
"", r'<!-- comment --> {% component "my.component" %} <!-- comment -->'
).strip(),
'{% component "my.component" %}',
)
self.assertEquals(
COMMENT_REGEX.sub(
"",
r"""<!-- comment
--> {% component "my.component" %}
<!-- comment -->
<!--
comment -->""",
}: # noqa: W291
text = COMMENT_REGEX.sub("", embedded_comment)
if text.strip() != '{% component "my.component" %}':
raise self.failureException(
f"Regex pattern {COMMENT_REGEX.pattern} failed to remove comment from {embedded_comment}"
)
).strip(),
'{% component "my.component" %}',
)

# Components surrounded by comments
self.assertEquals(
COMMENT_REGEX.sub("", r'<!-- {% component "my.component" %} -->'),
"",
)
self.assertEquals(
COMMENT_REGEX.sub(
"",
r"""<!--
{% component "my.component" %}
{% component "my.component" %}
-->""", # noqa: W291
),
"",
)