Ticket #6873: SelectMultiple-default-size-03.diff
File SelectMultiple-default-size-03.diff, 15.0 KB (added by , 16 years ago) |
---|
-
django/forms/widgets.py
345 345 if value is None: value = '' 346 346 final_attrs = self.build_attrs(attrs, name=name) 347 347 output = [u'<select%s>' % flatatt(final_attrs)] 348 options = self.render_options(choices, [value])348 options, dont_care = self.render_options(choices, [value]) 349 349 if options: 350 350 output.append(options) 351 351 output.append('</select>') … … 361 361 # Normalize to strings. 362 362 selected_choices = set([force_unicode(v) for v in selected_choices]) 363 363 output = [] 364 output_length = 0 364 365 for option_value, option_label in chain(self.choices, choices): 366 output_length += 1 365 367 if isinstance(option_label, (list, tuple)): 366 368 output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value))) 367 369 for option in option_label: 370 output_length += 1 368 371 output.append(render_option(*option)) 369 372 output.append(u'</optgroup>') 370 373 else: 371 374 output.append(render_option(option_value, option_label)) 372 return u'\n'.join(output) 375 return u'\n'.join(output), output_length 373 376 374 377 class NullBooleanSelect(Select): 375 378 """ … … 398 401 class SelectMultiple(Select): 399 402 def render(self, name, value, attrs=None, choices=()): 400 403 if value is None: value = [] 404 options, options_length = self.render_options(choices, value) 401 405 final_attrs = self.build_attrs(attrs, name=name) 406 # default size attribute for cross-browser consistent rendering 407 if 'size' not in final_attrs: 408 # emulate the behaviour of firefox: adjust the size to the number of choices, with an upper boundary 409 final_attrs['size'] = min(10, max(1, options_length)) 402 410 output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] 403 options = self.render_options(choices, value)404 411 if options: 405 412 output.append(options) 406 413 output.append('</select>') -
tests/modeltests/model_forms/models.py
342 342 <option value="2">Pending</option> 343 343 <option value="3">Live</option> 344 344 </select></td></tr> 345 <tr><th>Categories:</th><td><select multiple="multiple" name="categories" >345 <tr><th>Categories:</th><td><select multiple="multiple" name="categories" size="3"> 346 346 <option value="1">Entertainment</option> 347 347 <option value="2">It's a test</option> 348 348 <option value="3">Third test</option> … … 398 398 <option value="2">Pending</option> 399 399 <option value="3">Live</option> 400 400 </select></li> 401 <li>Categories: <select multiple="multiple" name="categories" >401 <li>Categories: <select multiple="multiple" name="categories" size="3"> 402 402 <option value="1">Entertainment</option> 403 403 <option value="2">It's a test</option> 404 404 <option value="3">Third test</option> … … 459 459 <option value="2">Pending</option> 460 460 <option value="3">Live</option> 461 461 </select></li> 462 <li>Categories: <select multiple="multiple" name="categories" >462 <li>Categories: <select multiple="multiple" name="categories" size="3"> 463 463 <option value="1" selected="selected">Entertainment</option> 464 464 <option value="2">It's a test</option> 465 465 <option value="3">Third test</option> … … 575 575 <option value="2">Pending</option> 576 576 <option value="3">Live</option> 577 577 </select></li> 578 <li>Categories: <select multiple="multiple" name="categories" >578 <li>Categories: <select multiple="multiple" name="categories" size="3"> 579 579 <option value="1">Entertainment</option> 580 580 <option value="2">It's a test</option> 581 581 <option value="3">Third</option> … … 601 601 <option value="2">Pending</option> 602 602 <option value="3">Live</option> 603 603 </select></li> 604 <li>Categories: <select multiple="multiple" name="categories" >604 <li>Categories: <select multiple="multiple" name="categories" size="4"> 605 605 <option value="1">Entertainment</option> 606 606 <option value="2">It's a test</option> 607 607 <option value="3">Third</option> -
tests/regressiontests/admin_widgets/models.py
41 41 42 42 >>> w = FilteredSelectMultiple('test', False) 43 43 >>> print conditional_escape(w.render('test', 'test')) 44 <select multiple="multiple" name="test" >44 <select multiple="multiple" name="test" size="1"> 45 45 </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script> 46 46 <BLANKLINE> 47 47 -
tests/regressiontests/forms/extra.py
259 259 >>> w = ComplexMultiWidget() 260 260 >>> print w.render('name', 'some text,JP,2007-04-25 06:24:00') 261 261 <input type="text" name="name_0" value="some text" /> 262 <select multiple="multiple" name="name_1" >262 <select multiple="multiple" name="name_1" size="4"> 263 263 <option value="J" selected="selected">John</option> 264 264 <option value="P" selected="selected">Paul</option> 265 265 <option value="G">George</option> … … 300 300 >>> f = ComplexFieldForm() 301 301 >>> print f 302 302 <tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" id="id_field1_0" /> 303 <select multiple="multiple" name="field1_1" id="id_field1_1">303 <select multiple="multiple" size="4" name="field1_1" id="id_field1_1"> 304 304 <option value="J">John</option> 305 305 <option value="P">Paul</option> 306 306 <option value="G">George</option> … … 311 311 >>> f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'}) 312 312 >>> print f 313 313 <tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" value="some text" id="id_field1_0" /> 314 <select multiple="multiple" name="field1_1" id="id_field1_1">314 <select multiple="multiple" size="4" name="field1_1" id="id_field1_1"> 315 315 <option value="J" selected="selected">John</option> 316 316 <option value="P" selected="selected">Paul</option> 317 317 <option value="G">George</option> -
tests/regressiontests/forms/forms.py
476 476 ... composers = MultipleChoiceField() 477 477 >>> f = SongForm(auto_id=False) 478 478 >>> print f['composers'] 479 <select multiple="multiple" name="composers" >479 <select multiple="multiple" name="composers" size="1"> 480 480 </select> 481 481 >>> class SongForm(Form): 482 482 ... name = CharField() 483 483 ... composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')]) 484 484 >>> f = SongForm(auto_id=False) 485 485 >>> print f['composers'] 486 <select multiple="multiple" name="composers" >486 <select multiple="multiple" name="composers" size="2"> 487 487 <option value="J">John Lennon</option> 488 488 <option value="P">Paul McCartney</option> 489 489 </select> … … 491 491 >>> print f['name'] 492 492 <input type="text" name="name" value="Yesterday" /> 493 493 >>> print f['composers'] 494 <select multiple="multiple" name="composers" >494 <select multiple="multiple" name="composers" size="2"> 495 495 <option value="J">John Lennon</option> 496 496 <option value="P" selected="selected">Paul McCartney</option> 497 497 </select> -
tests/regressiontests/forms/widgets.py
527 527 528 528 >>> w = SelectMultiple() 529 529 >>> print w.render('beatles', ['J'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 530 <select multiple="multiple" name="beatles" >530 <select multiple="multiple" name="beatles" size="4"> 531 531 <option value="J" selected="selected">John</option> 532 532 <option value="P">Paul</option> 533 533 <option value="G">George</option> 534 534 <option value="R">Ringo</option> 535 535 </select> 536 536 >>> print w.render('beatles', ['J', 'P'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 537 <select multiple="multiple" name="beatles" >537 <select multiple="multiple" name="beatles" size="4"> 538 538 <option value="J" selected="selected">John</option> 539 539 <option value="P" selected="selected">Paul</option> 540 540 <option value="G">George</option> 541 541 <option value="R">Ringo</option> 542 542 </select> 543 543 >>> print w.render('beatles', ['J', 'P', 'R'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 544 <select multiple="multiple" name="beatles" >544 <select multiple="multiple" name="beatles" size="4"> 545 545 <option value="J" selected="selected">John</option> 546 546 <option value="P" selected="selected">Paul</option> 547 547 <option value="G">George</option> … … 550 550 551 551 If the value is None, none of the options are selected: 552 552 >>> print w.render('beatles', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 553 <select multiple="multiple" name="beatles" >553 <select multiple="multiple" name="beatles" size="4"> 554 554 <option value="J">John</option> 555 555 <option value="P">Paul</option> 556 556 <option value="G">George</option> … … 559 559 560 560 If the value corresponds to a label (but not to an option value), none of the options are selected: 561 561 >>> print w.render('beatles', ['John'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 562 <select multiple="multiple" name="beatles" >562 <select multiple="multiple" name="beatles" size="4"> 563 563 <option value="J">John</option> 564 564 <option value="P">Paul</option> 565 565 <option value="G">George</option> … … 568 568 569 569 If multiple values are given, but some of them are not valid, the valid ones are selected: 570 570 >>> print w.render('beatles', ['J', 'G', 'foo'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 571 <select multiple="multiple" name="beatles" >571 <select multiple="multiple" name="beatles" size="4"> 572 572 <option value="J" selected="selected">John</option> 573 573 <option value="P">Paul</option> 574 574 <option value="G" selected="selected">George</option> … … 577 577 578 578 The value is compared to its str(): 579 579 >>> print w.render('nums', [2], choices=[('1', '1'), ('2', '2'), ('3', '3')]) 580 <select multiple="multiple" name="nums" >580 <select multiple="multiple" name="nums" size="3"> 581 581 <option value="1">1</option> 582 582 <option value="2" selected="selected">2</option> 583 583 <option value="3">3</option> 584 584 </select> 585 585 >>> print w.render('nums', ['2'], choices=[(1, 1), (2, 2), (3, 3)]) 586 <select multiple="multiple" name="nums" >586 <select multiple="multiple" name="nums" size="3"> 587 587 <option value="1">1</option> 588 588 <option value="2" selected="selected">2</option> 589 589 <option value="3">3</option> 590 590 </select> 591 591 >>> print w.render('nums', [2], choices=[(1, 1), (2, 2), (3, 3)]) 592 <select multiple="multiple" name="nums" >592 <select multiple="multiple" name="nums" size="3"> 593 593 <option value="1">1</option> 594 594 <option value="2" selected="selected">2</option> 595 595 <option value="3">3</option> … … 600 600 ... for i in range(5): 601 601 ... yield (i, i) 602 602 >>> print w.render('nums', [2], choices=get_choices()) 603 <select multiple="multiple" name="nums" >603 <select multiple="multiple" name="nums" size="5"> 604 604 <option value="0">0</option> 605 605 <option value="1">1</option> 606 606 <option value="2" selected="selected">2</option> … … 611 611 You can also pass 'choices' to the constructor: 612 612 >>> w = SelectMultiple(choices=[(1, 1), (2, 2), (3, 3)]) 613 613 >>> print w.render('nums', [2]) 614 <select multiple="multiple" name="nums" >614 <select multiple="multiple" name="nums" size="3"> 615 615 <option value="1">1</option> 616 616 <option value="2" selected="selected">2</option> 617 617 <option value="3">3</option> … … 619 619 620 620 If 'choices' is passed to both the constructor and render(), then they'll both be in the output: 621 621 >>> print w.render('nums', [2], choices=[(4, 4), (5, 5)]) 622 <select multiple="multiple" name="nums" >622 <select multiple="multiple" name="nums" size="5"> 623 623 <option value="1">1</option> 624 624 <option value="2" selected="selected">2</option> 625 625 <option value="3">3</option> … … 629 629 630 630 # Choices are escaped correctly 631 631 >>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) 632 <select multiple="multiple" name="escape" >632 <select multiple="multiple" name="escape" size="5"> 633 633 <option value="1">1</option> 634 634 <option value="2">2</option> 635 635 <option value="3">3</option> … … 639 639 640 640 # Unicode choices are correctly rendered as HTML 641 641 >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) 642 u'<select multiple="multiple" name="nums" >\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>'642 u'<select multiple="multiple" name="nums" size="5">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>' 643 643 644 644 # Test the usage of _has_changed 645 645 >>> w._has_changed(None, None) … … 658 658 # Choices can be nested one level in order to create HTML optgroups: 659 659 >>> w.choices = (('outer1', 'Outer 1'), ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2')))) 660 660 >>> print w.render('nestchoice', None) 661 <select multiple="multiple" name="nestchoice" >661 <select multiple="multiple" name="nestchoice" size="4"> 662 662 <option value="outer1">Outer 1</option> 663 663 <optgroup label="Group "1""> 664 664 <option value="inner1">Inner 1</option> … … 667 667 </select> 668 668 669 669 >>> print w.render('nestchoice', ['outer1']) 670 <select multiple="multiple" name="nestchoice" >670 <select multiple="multiple" name="nestchoice" size="4"> 671 671 <option value="outer1" selected="selected">Outer 1</option> 672 672 <optgroup label="Group "1""> 673 673 <option value="inner1">Inner 1</option> … … 676 676 </select> 677 677 678 678 >>> print w.render('nestchoice', ['inner1']) 679 <select multiple="multiple" name="nestchoice" >679 <select multiple="multiple" name="nestchoice" size="4"> 680 680 <option value="outer1">Outer 1</option> 681 681 <optgroup label="Group "1""> 682 682 <option value="inner1" selected="selected">Inner 1</option> … … 685 685 </select> 686 686 687 687 >>> print w.render('nestchoice', ['outer1', 'inner2']) 688 <select multiple="multiple" name="nestchoice" >688 <select multiple="multiple" name="nestchoice" size="4"> 689 689 <option value="outer1" selected="selected">Outer 1</option> 690 690 <optgroup label="Group "1""> 691 691 <option value="inner1">Inner 1</option>