misc: don't use duplicated slugs as HTML id attributes (#34746)

This commit is contained in:
Frédéric Péters 2019-07-10 17:57:28 +02:00
parent 0f1bf1be29
commit 34f9b6f512
3 changed files with 26 additions and 1 deletions

View File

@ -2,7 +2,7 @@
{% if render %}
{% for cell in cells %}
<div class="cell {{ cell.css_class_names }} {% if cell.slug %}{{cell.slug}}{% endif %} {% if cell|shown_because_admin:request %}shown-because-admin{% endif %}"
{% if cell.slug %}id="{{ cell.slug }}"{% endif %}
{% if cell.slug and cell.use_slug_as_id %}id="{{ cell.slug }}"{% endif %}
data-ajax-cell-url="{{ site_base }}{% url 'combo-public-ajax-page-cell' page_pk=cell.page.id cell_reference=cell.get_reference %}"
data-ajax-cell-loading-message="{{ cell.loading_message }}"
{% if cell.ajax_refresh %}data-ajax-cell-refresh="{{ cell.ajax_refresh }}"{% endif %}

View File

@ -461,9 +461,20 @@ def publish_page(request, page, status=200, template_name=None):
extend_with_parent_cells(cells, hierarchy=pages)
cells = [x for x in cells if x.is_visible(user=request.user)]
# mark duplicated slugs to avoid using them in HTML id attributes.
cell_by_slugs = {}
for cell in cells:
if cell.slug not in cell_by_slugs:
cell_by_slugs[cell.slug] = []
cell_by_slugs[cell.slug].append(cell)
for slug, slug_cells in cell_by_slugs.items():
for cell in slug_cells:
cell.use_slug_as_id = bool(len(slug_cells) == 1)
ctx = {
'check_badges': should_check_badges(),
'page': page,
'xxx': 'TOTO',
'page_cells': cells,
'pages': pages,
'request': request,

View File

@ -868,3 +868,17 @@ def test_sub_slug(app, john_doe, jane_doe):
# unknown name id => no selected_user
resp = app.get('/users/foo/', status=200)
assert 'XXYY' in resp.text
def test_cell_slugs(app):
Page.objects.all().delete()
page = Page(title='Home', slug='index', template_name='standard')
page.save()
TextCell(page=page, placeholder='content', text='Foobar', order=0).save()
TextCell(page=page, placeholder='content', text='Foobar', slug='unique', order=1).save()
TextCell(page=page, placeholder='content', text='Foobar', slug='dup', order=2).save()
TextCell(page=page, placeholder='content', text='Foobar', slug='dup', order=3).save()
resp = app.get('/', status=200)
assert 'id="unique"' in resp.text
assert 'id="dup"' not in resp.text