diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a4ca3b8..c8e1bb14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,10 @@ Using the following categories, list your changes in this order: - `use_query` hook for fetching database values. - `use_mutation` hook for modifying database values. +### Fixed + +- IDOM preloader is no longer sensitive to whitespace within template tags. + ## [1.1.0] - 2022-07-01 ### Added diff --git a/src/django_idom/utils.py b/src/django_idom/utils.py index 9dd635f8..27d106a4 100644 --- a/src/django_idom/utils.py +++ b/src/django_idom/utils.py @@ -13,8 +13,19 @@ from django_idom.config import IDOM_REGISTERED_COMPONENTS -COMPONENT_REGEX = re.compile(r"{% *component +((\"[^\"']*\")|('[^\"']*'))(.*?)%}") _logger = logging.getLogger(__name__) +_component_tag = r"(?Pcomponent)" +_component_path = r"(?P(\"[^\"'\s]+\")|('[^\"'\s]+'))" +_component_kwargs = r"(?P(.*?|\s*?)*)" +COMPONENT_REGEX = re.compile( + r"{%\s*" + + _component_tag + + r"\s*" + + _component_path + + r"\s*" + + _component_kwargs + + r"\s*%}" +) def _register_component(full_component_name: str) -> None: @@ -102,12 +113,12 @@ def _get_components(self, templates: set[str]) -> set[str]: for template in templates: with contextlib.suppress(Exception): with open(template, "r", encoding="utf-8") as template_file: - match = COMPONENT_REGEX.findall(template_file.read()) - if not match: - continue - components.update( - [group[0].replace('"', "").replace("'", "") for group in match] - ) + regex_iterable = COMPONENT_REGEX.finditer(template_file.read()) + component_paths = [ + match.group("path").replace('"', "").replace("'", "") + for match in regex_iterable + ] + components.update(component_paths) if not components: _logger.warning( "\033[93m" diff --git a/tests/test_app/tests/test_regex.py b/tests/test_app/tests/test_regex.py index c0942dba..4fff532d 100644 --- a/tests/test_app/tests/test_regex.py +++ b/tests/test_app/tests/test_regex.py @@ -13,6 +13,14 @@ def test_component_regex(self): r"{% component 'my.component' %}", r'{% component "my.component" class="my_thing" %}', r'{% component "my.component" class="my_thing" attr="attribute" %}', + r"""{% + + component + "my.component" + class="my_thing" + attr="attribute" + + %}""", # noqa: W291 }: self.assertRegex(component, COMPONENT_REGEX) @@ -26,5 +34,10 @@ def test_component_regex(self): r"{%%}", r" ", r"", + r'{% component " my.component " %}', + r"""{% component "my.component + " %}""", + r'{{ component """ }}', + r'{{ component "" }}', }: self.assertNotRegex(fake_component, COMPONENT_REGEX)