Projet

Général

Profil

0001-misc-don-t-use-duplicated-slugs-as-HTML-id-attribute.patch

Frédéric Péters, 10 juillet 2019 17:58

Télécharger (3,27 ko)

Voir les différences:

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

 combo/public/templates/combo/placeholder.html |  2 +-
 combo/public/views.py                         | 11 +++++++++++
 tests/test_public.py                          | 14 ++++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)
combo/public/templates/combo/placeholder.html
2 2
{% if render %}
3 3
{% for cell in cells %}
4 4
<div class="cell {{ cell.css_class_names }} {% if cell.slug %}{{cell.slug}}{% endif %} {% if cell|shown_because_admin:request %}shown-because-admin{% endif %}"
5
     {% if cell.slug %}id="{{ cell.slug }}"{% endif %}
5
     {% if cell.slug and cell.use_slug_as_id %}id="{{ cell.slug }}"{% endif %}
6 6
     data-ajax-cell-url="{{ site_base }}{% url 'combo-public-ajax-page-cell' page_pk=cell.page.id cell_reference=cell.get_reference %}"
7 7
     data-ajax-cell-loading-message="{{ cell.loading_message }}"
8 8
     {% if cell.ajax_refresh %}data-ajax-cell-refresh="{{ cell.ajax_refresh }}"{% endif %}
combo/public/views.py
464 464
    extend_with_parent_cells(cells, hierarchy=pages)
465 465
    cells = [x for x in cells if x.is_visible(user=request.user)]
466 466

  
467
    # mark duplicated slugs to avoid using them in HTML id attributes.
468
    cell_by_slugs = {}
469
    for cell in cells:
470
        if cell.slug not in cell_by_slugs:
471
            cell_by_slugs[cell.slug] = []
472
        cell_by_slugs[cell.slug].append(cell)
473
    for slug, slug_cells in cell_by_slugs.items():
474
        for cell in slug_cells:
475
            cell.use_slug_as_id = bool(len(slug_cells) == 1)
476

  
467 477
    ctx = {
468 478
        'check_badges': should_check_badges(),
469 479
        'page': page,
480
        'xxx': 'TOTO',
470 481
        'page_cells': cells,
471 482
        'pages': pages,
472 483
        'request': request,
tests/test_public.py
869 869
            # unknown name id => no selected_user
870 870
            resp = app.get('/users/foo/', status=200)
871 871
            assert 'XXYY' in resp.text
872

  
873
def test_cell_slugs(app):
874
    Page.objects.all().delete()
875
    page = Page(title='Home', slug='index', template_name='standard')
876
    page.save()
877

  
878
    TextCell(page=page, placeholder='content', text='Foobar', order=0).save()
879
    TextCell(page=page, placeholder='content', text='Foobar', slug='unique', order=1).save()
880
    TextCell(page=page, placeholder='content', text='Foobar', slug='dup', order=2).save()
881
    TextCell(page=page, placeholder='content', text='Foobar', slug='dup', order=3).save()
882

  
883
    resp = app.get('/', status=200)
884
    assert 'id="unique"' in resp.text
885
    assert 'id="dup"' not in resp.text
872
-