Code

Ticket #4860: 7629.labels-with-fors.diff

File 7629.labels-with-fors.diff, 9.6 KB (added by batiste@…, 6 years ago)

Patch for labels in new forms. Add a "for" attribute when possible

Line 
1Index: django/newforms/widgets.py
2===================================================================
3--- django/newforms/widgets.py  (révision 7661)
4+++ django/newforms/widgets.py  (copie de travail)
5@@ -281,8 +281,8 @@
6         self.index = index
7 
8     def __unicode__(self):
9-        return mark_safe(u'<label>%s %s</label>' % (self.tag(),
10-                conditional_escape(force_unicode(self.choice_label))))
11+        label_for =  ' for="%s_%s"' % (self.attrs['id'], self.index) if 'id' in self.attrs else ''
12+        return mark_safe(u'<label%s>%s %s</label>' % (label_for, self.tag(), conditional_escape(force_unicode(self.choice_label))))
13 
14     def is_checked(self):
15         return self.value == self.choice_value
16@@ -367,7 +367,8 @@
17             cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
18             option_value = force_unicode(option_value)
19             rendered_cb = cb.render(name, option_value)
20-            output.append(u'<li><label>%s %s</label></li>' % (rendered_cb,
21+            label_for = u' for="%s"' % final_attrs['id'] if 'id' in final_attrs else ''
22+            output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb,
23                     conditional_escape(force_unicode(option_label))))
24         output.append(u'</ul>')
25         return mark_safe(u'\n'.join(output))
26Index: tests/regressiontests/forms/regressions.py
27===================================================================
28--- tests/regressiontests/forms/regressions.py  (révision 7629)
29+++ tests/regressiontests/forms/regressions.py  (copie de travail)
30@@ -40,7 +40,7 @@
31 ...     somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect(), label=u'\xc5\xf8\xdf')
32 >>> f = SomeForm()
33 >>> f.as_p()
34-u'<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
35+u'<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
36 
37 Testing choice validation with UTF-8 bytestrings as input (these are the
38 Russian abbreviations "мес." and "шт.".
39@@ -56,7 +56,7 @@
40 >>> activate('ru')
41 >>> f = SomeForm({})
42 >>> f.as_p()
43-u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
44+u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
45 >>> deactivate()
46 
47 Deep copying translated text shouldn't raise an error
48Index: tests/regressiontests/forms/forms.py
49===================================================================
50--- tests/regressiontests/forms/forms.py        (révision 7629)
51+++ tests/regressiontests/forms/forms.py        (copie de travail)
52@@ -443,8 +443,8 @@
53 >>> f = FrameworkForm(auto_id='id_%s')
54 >>> print f['language']
55 <ul>
56-<li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
57-<li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
58+<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
59+<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
60 </ul>
61 
62 When RadioSelect is used with auto_id, and the whole form is printed using
63@@ -453,20 +453,20 @@
64 >>> print f
65 <tr><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>
66 <tr><th><label for="id_language_0">Language:</label></th><td><ul>
67-<li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
68-<li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
69+<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
70+<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
71 </ul></td></tr>
72 >>> print f.as_ul()
73 <li><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>
74 <li><label for="id_language_0">Language:</label> <ul>
75-<li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
76-<li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
77+<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
78+<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
79 </ul></li>
80 >>> print f.as_p()
81 <p><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
82 <p><label for="id_language_0">Language:</label> <ul>
83-<li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
84-<li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
85+<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
86+<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
87 </ul></p>
88 
89 MultipleChoiceField is a special case, as its data is required to be a list:
90@@ -535,8 +535,8 @@
91 >>> f = SongForm(auto_id='%s_id')
92 >>> print f['composers']
93 <ul>
94-<li><label><input type="checkbox" name="composers" value="J" id="composers_id_0" /> John Lennon</label></li>
95-<li><label><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
96+<li><label for="composers_id_0"><input type="checkbox" name="composers" value="J" id="composers_id_0" /> John Lennon</label></li>
97+<li><label for="composers_id_1"><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
98 </ul>
99 
100 Data for a MultipleChoiceField should be a list. QueryDict and MultiValueDict
101Index: tests/regressiontests/forms/widgets.py
102===================================================================
103--- tests/regressiontests/forms/widgets.py      (révision 7629)
104+++ tests/regressiontests/forms/widgets.py      (copie de travail)
105@@ -741,20 +741,20 @@
106 >>> w = RadioSelect(attrs={'id':'foo'})
107 >>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
108 <ul>
109-<li><label><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
110-<li><label><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
111-<li><label><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
112-<li><label><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li>
113+<li><label for="foo_0"><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
114+<li><label for="foo_1"><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
115+<li><label for="foo_2"><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
116+<li><label for="foo_3"><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li>
117 </ul>
118 
119 # Attributes provided at render-time are passed to the constituent inputs
120 >>> w = RadioSelect()
121 >>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'})
122 <ul>
123-<li><label><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
124-<li><label><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
125-<li><label><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>
126-<li><label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
127+<li><label for="bar_0"><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
128+<li><label for="bar_1"><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
129+<li><label for="bar_2"><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>
130+<li><label for="bar_3"><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
131 </ul>
132 
133 # CheckboxSelectMultiple Widget ###############################################