From bb6921ce888cfc981c4df8d5f079b0b7a4121ff6 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sat, 4 Feb 2012 12:48:21 +0000 Subject: [PATCH] Fixed #17596 -- Stopped the AdminField class from double quoting its label. Thanks, guettli and claudep. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17431 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/helpers.py | 6 +++--- tests/regressiontests/admin_util/tests.py | 26 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index 04a3492e1c..b7c65e175f 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -115,17 +115,17 @@ class AdminField(object): def label_tag(self): classes = [] + contents = conditional_escape(force_unicode(self.field.label)) if self.is_checkbox: classes.append(u'vCheckboxLabel') - contents = force_unicode(escape(self.field.label)) else: - contents = force_unicode(escape(self.field.label)) + u':' + contents += u':' if self.field.field.required: classes.append(u'required') if not self.is_first: classes.append(u'inline') attrs = classes and {'class': u' '.join(classes)} or {} - return self.field.label_tag(contents=contents, attrs=attrs) + return self.field.label_tag(contents=mark_safe(contents), attrs=attrs) def errors(self): return mark_safe(self.field.errors.as_ul()) diff --git a/tests/regressiontests/admin_util/tests.py b/tests/regressiontests/admin_util/tests.py index eb110159c4..8113f2e16d 100644 --- a/tests/regressiontests/admin_util/tests.py +++ b/tests/regressiontests/admin_util/tests.py @@ -4,14 +4,17 @@ from datetime import datetime from django.conf import settings from django.contrib import admin +from django.contrib.admin import helpers from django.contrib.admin.util import (display_for_field, label_for_field, lookup_field, NestedObjects) from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE from django.contrib.sites.models import Site from django.db import models, DEFAULT_DB_ALIAS +from django import forms from django.test import TestCase from django.utils import unittest from django.utils.formats import localize +from django.utils.safestring import mark_safe from .models import Article, Count, Event, Location @@ -258,3 +261,26 @@ class UtilTests(unittest.TestCase): self.assertTrue( unicode(log_entry).startswith('Deleted ') ) + + def test_safestring_in_field_label(self): + # safestring should not be escaped + class MyForm(forms.Form): + text = forms.CharField(label=mark_safe('text')) + cb = forms.BooleanField(label=mark_safe('cb')) + + form = MyForm() + self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(), + '') + self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(), + '') + + # normal strings needs to be escaped + class MyForm(forms.Form): + text = forms.CharField(label='&text') + cb = forms.BooleanField(label='&cb') + + form = MyForm() + self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(), + '') + self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(), + '')