Add support for - and / in component names in middleware

- Fix the COMPONENT_COMMENT_REGEX to find component names with special symbols.
- Fix the test to catch the error.

Follow-up of e0c29e5 and ref: #262
This commit is contained in:
Roman Imankulov 2023-09-28 15:23:03 +01:00 committed by Emil Stenström
parent c0fc6c0a12
commit a919b648b3
2 changed files with 20 additions and 17 deletions

View file

@ -11,9 +11,9 @@ CSS_DEPENDENCY_PLACEHOLDER = '<link name="CSS_PLACEHOLDER">'
JS_DEPENDENCY_PLACEHOLDER = '<script name="JS_PLACEHOLDER"></script>'
SCRIPT_TAG_REGEX = re.compile("<script")
COMPONENT_COMMENT_REGEX = re.compile(rb"<!-- _RENDERED (?P<name>\w+?) -->")
COMPONENT_COMMENT_REGEX = re.compile(rb"<!-- _RENDERED (?P<name>[\w\-/]+?) -->")
PLACEHOLDER_REGEX = re.compile(
rb"<!-- _RENDERED (?P<name>[\w\-\/]+?) -->"
rb"<!-- _RENDERED (?P<name>[\w\-/]+?) -->"
rb'|<link name="CSS_PLACEHOLDER">'
rb'|<script name="JS_PLACEHOLDER"></script>'
)

View file

@ -399,18 +399,21 @@ class ComponentMediaRenderingTests(SimpleTestCase):
request = Mock()
self.assertEqual(response, middleware(request=request))
def test_middleware_response_with_components_with_slash_and_dash(self):
component.registry.register(
name="test-component", component=SimpleComponent
)
component.registry.register(
name="test/component", component=SimpleComponent
)
template = Template(
"{% load component_tags %}{% component_js_dependencies %}"
"{% component 'test-component' variable='variable' %}"
"{% component 'test/component' variable='variable' %}"
)
rendered = create_and_process_template_response(template)
self.assertNotIn("_RENDERED", rendered)
def test_middleware_response_with_components_with_slash_dash_and_underscore(self):
component_names = ["test-component", "test/component", "test_component"]
for component_name in component_names:
component.registry.register(
name=component_name, component=SimpleComponent
)
template = Template(
"{% load component_tags %}"
"{% component_js_dependencies %}"
"{% component_css_dependencies %}"
f"{{% component '{component_name}' variable='value' %}}"
)
rendered = create_and_process_template_response(template)
self.assertEqual((
'<script src="script.js"></script>'
'<link href="style.css" media="all" rel="stylesheet">'
"Variable: <strong>value</strong>\n"
), rendered)