Skip to content

Commit 9628b56

Browse files
committed
fix: Improve JavaScript injection logic to replace only the last endblock in template
1 parent 98a5335 commit 9628b56

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/patches/patch_template.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def patch_template():
3737

3838
original_content = content
3939

40-
# Find the endblock and add JavaScript before it
40+
# Find the LAST endblock (content block) and add JavaScript before it
4141
js_code = '''
4242
<script>
4343
document.addEventListener('DOMContentLoaded', function() {
@@ -54,10 +54,14 @@ def patch_template():
5454
5555
{% endblock %}'''
5656

57-
if "{% endblock %}" in content:
58-
# Replace the last endblock with our JS + endblock
59-
content = content.replace("{% endblock %}", js_code, 1)
60-
print(" [OK] Added JavaScript for dynamic form switching")
57+
# Count endblocks - we want to replace the LAST one (content block, not title block)
58+
endblock_count = content.count("{% endblock %}")
59+
if endblock_count >= 2:
60+
# Replace only the last occurrence
61+
# Find the last {% endblock %} and replace it
62+
last_idx = content.rfind("{% endblock %}")
63+
content = content[:last_idx] + js_code
64+
print(f" [OK] Added JavaScript before last endblock (found {endblock_count} endblocks)")
6165
else:
6266
print(" [WARN] Could not find endblock in template")
6367
return False

0 commit comments

Comments
 (0)