Skip to content

Commit da0c2cb

Browse files
authored
Allow whitespace characters within component regex (#96)
1 parent 1c250cb commit da0c2cb

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Using the following categories, list your changes in this order:
2828
- `use_query` hook for fetching database values.
2929
- `use_mutation` hook for modifying database values.
3030

31+
### Fixed
32+
33+
- IDOM preloader is no longer sensitive to whitespace within template tags.
34+
3135
## [1.1.0] - 2022-07-01
3236

3337
### Added

src/django_idom/utils.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@
1313
from django_idom.config import IDOM_REGISTERED_COMPONENTS
1414

1515

16-
COMPONENT_REGEX = re.compile(r"{% *component +((\"[^\"']*\")|('[^\"']*'))(.*?)%}")
1716
_logger = logging.getLogger(__name__)
17+
_component_tag = r"(?P<tag>component)"
18+
_component_path = r"(?P<path>(\"[^\"'\s]+\")|('[^\"'\s]+'))"
19+
_component_kwargs = r"(?P<kwargs>(.*?|\s*?)*)"
20+
COMPONENT_REGEX = re.compile(
21+
r"{%\s*"
22+
+ _component_tag
23+
+ r"\s*"
24+
+ _component_path
25+
+ r"\s*"
26+
+ _component_kwargs
27+
+ r"\s*%}"
28+
)
1829

1930

2031
def _register_component(full_component_name: str) -> None:
@@ -102,12 +113,12 @@ def _get_components(self, templates: set[str]) -> set[str]:
102113
for template in templates:
103114
with contextlib.suppress(Exception):
104115
with open(template, "r", encoding="utf-8") as template_file:
105-
match = COMPONENT_REGEX.findall(template_file.read())
106-
if not match:
107-
continue
108-
components.update(
109-
[group[0].replace('"', "").replace("'", "") for group in match]
110-
)
116+
regex_iterable = COMPONENT_REGEX.finditer(template_file.read())
117+
component_paths = [
118+
match.group("path").replace('"', "").replace("'", "")
119+
for match in regex_iterable
120+
]
121+
components.update(component_paths)
111122
if not components:
112123
_logger.warning(
113124
"\033[93m"

tests/test_app/tests/test_regex.py

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def test_component_regex(self):
1313
r"{% component 'my.component' %}",
1414
r'{% component "my.component" class="my_thing" %}',
1515
r'{% component "my.component" class="my_thing" attr="attribute" %}',
16+
r"""{%
17+
18+
component
19+
"my.component"
20+
class="my_thing"
21+
attr="attribute"
22+
23+
%}""", # noqa: W291
1624
}:
1725
self.assertRegex(component, COMPONENT_REGEX)
1826

@@ -26,5 +34,10 @@ def test_component_regex(self):
2634
r"{%%}",
2735
r" ",
2836
r"",
37+
r'{% component " my.component " %}',
38+
r"""{% component "my.component
39+
" %}""",
40+
r'{{ component """ }}',
41+
r'{{ component "" }}',
2942
}:
3043
self.assertNotRegex(fake_component, COMPONENT_REGEX)

0 commit comments

Comments
 (0)