1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
|
/*
* Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "SelectElement.h"
#include "CharacterNames.h"
#include "ChromeClient.h"
#include "Element.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "FormDataList.h"
#include "Frame.h"
#include "HTMLFormElement.h"
#include "HTMLNames.h"
#include "HTMLKeygenElement.h"
#include "HTMLSelectElement.h"
#include "KeyboardEvent.h"
#include "MappedAttribute.h"
#include "MouseEvent.h"
#include "OptionElement.h"
#include "OptionGroupElement.h"
#include "Page.h"
#include "RenderListBox.h"
#include "RenderMenuList.h"
#include <wtf/Assertions.h>
#if ENABLE(WML)
#include "WMLNames.h"
#include "WMLSelectElement.h"
#endif
// Configure platform-specific behavior when focused pop-up receives arrow/space/return keystroke.
// (PLATFORM(MAC) and PLATFORM(GTK) are always false in Chromium, hence the extra tests.)
#if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && PLATFORM(DARWIN))
#define ARROW_KEYS_POP_MENU 1
#define SPACE_OR_RETURN_POP_MENU 0
#elif PLATFORM(GTK) || (PLATFORM(CHROMIUM) && PLATFORM(LINUX))
#define ARROW_KEYS_POP_MENU 0
#define SPACE_OR_RETURN_POP_MENU 1
#else
#define ARROW_KEYS_POP_MENU 0
#define SPACE_OR_RETURN_POP_MENU 0
#endif
using std::min;
using std::max;
using namespace WTF;
using namespace Unicode;
namespace WebCore {
static const DOMTimeStamp typeAheadTimeout = 1000;
void SelectElement::selectAll(SelectElementData& data, Element* element)
{
ASSERT(!data.usesMenuList());
if (!element->renderer() || !data.multiple())
return;
// Save the selection so it can be compared to the new selectAll selection when dispatching change events
saveLastSelection(data, element);
data.setActiveSelectionState(true);
setActiveSelectionAnchorIndex(data, element, nextSelectableListIndex(data, element, -1));
setActiveSelectionEndIndex(data, previousSelectableListIndex(data, element, -1));
updateListBoxSelection(data, element, false);
listBoxOnChange(data, element);
}
void SelectElement::saveLastSelection(SelectElementData& data, Element* element)
{
if (data.usesMenuList()) {
data.setLastOnChangeIndex(selectedIndex(data, element));
return;
}
Vector<bool>& lastOnChangeSelection = data.lastOnChangeSelection();
lastOnChangeSelection.clear();
const Vector<Element*>& items = data.listItems(element);
for (unsigned i = 0; i < items.size(); ++i) {
OptionElement* optionElement = toOptionElement(items[i]);
lastOnChangeSelection.append(optionElement && optionElement->selected());
}
}
int SelectElement::nextSelectableListIndex(SelectElementData& data, Element* element, int startIndex)
{
const Vector<Element*>& items = data.listItems(element);
int index = startIndex + 1;
while (index >= 0 && (unsigned) index < items.size() && (!isOptionElement(items[index]) || items[index]->disabled()))
++index;
if ((unsigned) index == items.size())
return startIndex;
return index;
}
int SelectElement::previousSelectableListIndex(SelectElementData& data, Element* element, int startIndex)
{
const Vector<Element*>& items = data.listItems(element);
if (startIndex == -1)
startIndex = items.size();
int index = startIndex - 1;
while (index >= 0 && (unsigned) index < items.size() && (!isOptionElement(items[index]) || items[index]->disabled()))
--index;
if (index == -1)
return startIndex;
return index;
}
void SelectElement::setActiveSelectionAnchorIndex(SelectElementData& data, Element* element, int index)
{
data.setActiveSelectionAnchorIndex(index);
// Cache the selection state so we can restore the old selection as the new selection pivots around this anchor index
Vector<bool>& cachedStateForActiveSelection = data.cachedStateForActiveSelection();
cachedStateForActiveSelection.clear();
const Vector<Element*>& items = data.listItems(element);
for (unsigned i = 0; i < items.size(); ++i) {
OptionElement* optionElement = toOptionElement(items[i]);
cachedStateForActiveSelection.append(optionElement && optionElement->selected());
}
}
void SelectElement::setActiveSelectionEndIndex(SelectElementData& data, int index)
{
data.setActiveSelectionEndIndex(index);
}
void SelectElement::updateListBoxSelection(SelectElementData& data, Element* element, bool deselectOtherOptions)
{
ASSERT(element->renderer() && element->renderer()->isListBox());
ASSERT(data.activeSelectionAnchorIndex() >= 0);
unsigned start = min(data.activeSelectionAnchorIndex(), data.activeSelectionEndIndex());
unsigned end = max(data.activeSelectionAnchorIndex(), data.activeSelectionEndIndex());
Vector<bool>& cachedStateForActiveSelection = data.cachedStateForActiveSelection();
const Vector<Element*>& items = data.listItems(element);
for (unsigned i = 0; i < items.size(); ++i) {
OptionElement* optionElement = toOptionElement(items[i]);
if (!optionElement || items[i]->disabled())
continue;
if (i >= start && i <= end)
optionElement->setSelectedState(data.activeSelectionState());
else if (deselectOtherOptions || i >= cachedStateForActiveSelection.size())
optionElement->setSelectedState(false);
else
optionElement->setSelectedState(cachedStateForActiveSelection[i]);
}
scrollToSelection(data, element);
}
void SelectElement::listBoxOnChange(SelectElementData& data, Element* element)
{
ASSERT(!data.usesMenuList());
Vector<bool>& lastOnChangeSelection = data.lastOnChangeSelection();
const Vector<Element*>& items = data.listItems(element);
// If the cached selection list is empty, or the size has changed, then fire dispatchFormControlChangeEvent, and return early.
if (lastOnChangeSelection.isEmpty() || lastOnChangeSelection.size() != items.size()) {
element->dispatchFormControlChangeEvent();
return;
}
// Update lastOnChangeSelection and fire dispatchFormControlChangeEvent
bool fireOnChange = false;
for (unsigned i = 0; i < items.size(); ++i) {
OptionElement* optionElement = toOptionElement(items[i]);
bool selected = optionElement && optionElement->selected();
if (selected != lastOnChangeSelection[i])
fireOnChange = true;
lastOnChangeSelection[i] = selected;
}
if (fireOnChange)
element->dispatchFormControlChangeEvent();
}
void SelectElement::menuListOnChange(SelectElementData& data, Element* element)
{
ASSERT(data.usesMenuList());
int selected = selectedIndex(data, element);
if (data.lastOnChangeIndex() != selected && data.userDrivenChange()) {
data.setLastOnChangeIndex(selected);
data.setUserDrivenChange(false);
element->dispatchFormControlChangeEvent();
}
}
void SelectElement::scrollToSelection(SelectElementData& data, Element* element)
{
if (data.usesMenuList())
return;
if (RenderObject* renderer = element->renderer())
toRenderListBox(renderer)->selectionChanged();
}
void SelectElement::recalcStyle(SelectElementData& data, Element* element)
{
RenderObject* renderer = element->renderer();
if (element->childNeedsStyleRecalc() && renderer) {
if (data.usesMenuList())
toRenderMenuList(renderer)->setOptionsChanged(true);
else
toRenderListBox(renderer)->setOptionsChanged(true);
} else if (data.shouldRecalcListItems())
recalcListItems(data, element);
}
void SelectElement::setRecalcListItems(SelectElementData& data, Element* element)
{
data.setShouldRecalcListItems(true);
data.setActiveSelectionAnchorIndex(-1); // Manual selection anchor is reset when manipulating the select programmatically.
if (RenderObject* renderer = element->renderer()) {
if (data.usesMenuList())
toRenderMenuList(renderer)->setOptionsChanged(true);
else
toRenderListBox(renderer)->setOptionsChanged(true);
}
element->setNeedsStyleRecalc();
}
void SelectElement::recalcListItems(SelectElementData& data, const Element* element, bool updateSelectedStates)
{
Vector<Element*>& listItems = data.rawListItems();
listItems.clear();
OptionElement* foundSelected = 0;
for (Node* currentNode = element->firstChild(); currentNode;) {
if (!currentNode->isElementNode()) {
currentNode = currentNode->traverseNextSibling(element);
continue;
}
Element* current = static_cast<Element*>(currentNode);
// optgroup tags may not nest. However, both FireFox and IE will
// flatten the tree automatically, so we follow suit.
// (http://www.w3.org/TR/html401/interact/forms.html#h-17.6)
if (isOptionGroupElement(current)) {
listItems.append(current);
if (current->firstChild()) {
currentNode = current->firstChild();
continue;
}
}
if (OptionElement* optionElement = toOptionElement(current)) {
listItems.append(current);
if (updateSelectedStates) {
if (!foundSelected && (data.usesMenuList() || (!data.multiple() && optionElement->selected()))) {
foundSelected = optionElement;
foundSelected->setSelectedState(true);
} else if (foundSelected && !data.multiple() && optionElement->selected()) {
foundSelected->setSelectedState(false);
foundSelected = optionElement;
}
}
}
if (current->hasTagName(HTMLNames::hrTag))
listItems.append(current);
// In conforming HTML code, only <optgroup> and <option> will be found
// within a <select>. We call traverseNextSibling so that we only step
// into those tags that we choose to. For web-compat, we should cope
// with the case where odd tags like a <div> have been added but we
// handle this because such tags have already been removed from the
// <select>'s subtree at this point.
currentNode = currentNode->traverseNextSibling(element);
}
data.setShouldRecalcListItems(false);
}
int SelectElement::selectedIndex(const SelectElementData& data, const Element* element)
{
unsigned index = 0;
// return the number of the first option selected
const Vector<Element*>& items = data.listItems(element);
for (size_t i = 0; i < items.size(); ++i) {
if (OptionElement* optionElement = toOptionElement(items[i])) {
if (optionElement->selected())
return index;
++index;
}
}
return -1;
}
void SelectElement::setSelectedIndex(SelectElementData& data, Element* element, int optionIndex, bool deselect, bool fireOnChangeNow, bool userDrivenChange)
{
const Vector<Element*>& items = data.listItems(element);
int listIndex = optionToListIndex(data, element, optionIndex);
if (!data.multiple())
deselect = true;
Element* excludeElement = 0;
if (OptionElement* optionElement = (listIndex >= 0 ? toOptionElement(items[listIndex]) : 0)) {
excludeElement = items[listIndex];
if (data.activeSelectionAnchorIndex() < 0 || deselect)
setActiveSelectionAnchorIndex(data, element, listIndex);
if (data.activeSelectionEndIndex() < 0 || deselect)
setActiveSelectionEndIndex(data, listIndex);
optionElement->setSelectedState(true);
}
if (deselect)
deselectItems(data, element, excludeElement);
// For the menu list case, this is what makes the selected element appear.
if (RenderObject* renderer = element->renderer())
renderer->updateFromElement();
scrollToSelection(data, element);
// This only gets called with fireOnChangeNow for menu lists.
if (data.usesMenuList()) {
data.setUserDrivenChange(userDrivenChange);
if (fireOnChangeNow)
menuListOnChange(data, element);
}
if (Frame* frame = element->document()->frame())
frame->page()->chrome()->client()->formStateDidChange(element);
}
int SelectElement::optionToListIndex(const SelectElementData& data, const Element* element, int optionIndex)
{
const Vector<Element*>& items = data.listItems(element);
int listSize = (int) items.size();
if (optionIndex < 0 || optionIndex >= listSize)
return -1;
int optionIndex2 = -1;
for (int listIndex = 0; listIndex < listSize; ++listIndex) {
if (isOptionElement(items[listIndex])) {
++optionIndex2;
if (optionIndex2 == optionIndex)
return listIndex;
}
}
return -1;
}
int SelectElement::listToOptionIndex(const SelectElementData& data, const Element* element, int listIndex)
{
const Vector<Element*>& items = data.listItems(element);
if (listIndex < 0 || listIndex >= int(items.size()) ||
!isOptionElement(items[listIndex]))
return -1;
int optionIndex = 0; // actual index of option not counting OPTGROUP entries that may be in list
for (int i = 0; i < listIndex; ++i)
if (isOptionElement(items[i]))
++optionIndex;
return optionIndex;
}
void SelectElement::dispatchFocusEvent(SelectElementData& data, Element* element)
{
// Save the selection so it can be compared to the new selection when dispatching change events during blur event dispatchal
if (data.usesMenuList())
saveLastSelection(data, element);
}
void SelectElement::dispatchBlurEvent(SelectElementData& data, Element* element)
{
// We only need to fire change events here for menu lists, because we fire change events for list boxes whenever the selection change is actually made.
// This matches other browsers' behavior.
if (data.usesMenuList())
menuListOnChange(data, element);
}
void SelectElement::deselectItems(SelectElementData& data, Element* element, Element* excludeElement)
{
const Vector<Element*>& items = data.listItems(element);
for (unsigned i = 0; i < items.size(); ++i) {
if (items[i] == excludeElement)
continue;
if (OptionElement* optionElement = toOptionElement(items[i]))
optionElement->setSelectedState(false);
}
}
bool SelectElement::saveFormControlState(const SelectElementData& data, const Element* element, String& value)
{
const Vector<Element*>& items = data.listItems(element);
int length = items.size();
// FIXME: Change this code to use the new StringImpl::createUninitialized code path.
Vector<char, 1024> characters(length);
for (int i = 0; i < length; ++i) {
OptionElement* optionElement = toOptionElement(items[i]);
bool selected = optionElement && optionElement->selected();
characters[i] = selected ? 'X' : '.';
}
value = String(characters.data(), length);
return true;
}
void SelectElement::restoreFormControlState(SelectElementData& data, Element* element, const String& state)
{
recalcListItems(data, element);
const Vector<Element*>& items = data.listItems(element);
int length = items.size();
for (int i = 0; i < length; ++i) {
if (OptionElement* optionElement = toOptionElement(items[i]))
optionElement->setSelectedState(state[i] == 'X');
}
element->setNeedsStyleRecalc();
}
void SelectElement::parseMultipleAttribute(SelectElementData& data, Element* element, MappedAttribute* attribute)
{
bool oldUsesMenuList = data.usesMenuList();
data.setMultiple(!attribute->isNull());
if (oldUsesMenuList != data.usesMenuList() && element->attached()) {
element->detach();
element->attach();
}
}
bool SelectElement::appendFormData(SelectElementData& data, Element* element, FormDataList& list)
{
const AtomicString& name = element->formControlName();
if (name.isEmpty())
return false;
bool successful = false;
const Vector<Element*>& items = data.listItems(element);
for (unsigned i = 0; i < items.size(); ++i) {
OptionElement* optionElement = toOptionElement(items[i]);
if (optionElement && optionElement->selected()) {
list.appendData(name, optionElement->value());
successful = true;
}
}
// FIXME: This case should not happen. Make sure that we select the first option
// in any case, otherwise we have no consistency with the DOM interface.
// We return the first one if it was a combobox select
if (!successful && !data.multiple() && data.size() <= 1 && items.size()) {
OptionElement* optionElement = toOptionElement(items[0]);
if (optionElement) {
const AtomicString& value = optionElement->value();
if (value.isNull())
list.appendData(name, optionElement->text().stripWhiteSpace());
else
list.appendData(name, value);
successful = true;
}
}
return successful;
}
void SelectElement::reset(SelectElementData& data, Element* element)
{
OptionElement* firstOption = 0;
OptionElement* selectedOption = 0;
const Vector<Element*>& items = data.listItems(element);
for (unsigned i = 0; i < items.size(); ++i) {
OptionElement* optionElement = toOptionElement(items[i]);
if (!optionElement)
continue;
if (!items[i]->getAttribute(HTMLNames::selectedAttr).isNull()) {
if (selectedOption && !data.multiple())
selectedOption->setSelectedState(false);
optionElement->setSelectedState(true);
selectedOption = optionElement;
} else
optionElement->setSelectedState(false);
if (!firstOption)
firstOption = optionElement;
}
if (!selectedOption && firstOption && data.usesMenuList())
firstOption->setSelectedState(true);
element->setNeedsStyleRecalc();
}
#if !ARROW_KEYS_POP_MENU
enum SkipDirection {
SkipBackwards = -1,
SkipForwards = 1
};
// Returns the index of the next valid list item |skip| items past |listIndex| in direction |direction|.
static int nextValidIndex(const Vector<Element*>& listItems, int listIndex, SkipDirection direction, int skip)
{
int lastGoodIndex = listIndex;
int size = listItems.size();
for (listIndex += direction; listIndex >= 0 && listIndex < size; listIndex += direction) {
--skip;
if (!listItems[listIndex]->disabled() && isOptionElement(listItems[listIndex])) {
lastGoodIndex = listIndex;
if (skip <= 0)
break;
}
}
return lastGoodIndex;
}
#endif
void SelectElement::menuListDefaultEventHandler(SelectElementData& data, Element* element, Event* event, HTMLFormElement* htmlForm)
{
#if !ARROW_KEYS_POP_MENU
UNUSED_PARAM(htmlForm);
#endif
if (event->type() == eventNames().keydownEvent) {
if (!element->renderer() || !event->isKeyboardEvent())
return;
String keyIdentifier = static_cast<KeyboardEvent*>(event)->keyIdentifier();
bool handled = false;
#if ARROW_KEYS_POP_MENU
if (keyIdentifier == "Down" || keyIdentifier == "Up") {
element->focus();
// Save the selection so it can be compared to the new selection when dispatching change events during setSelectedIndex,
// which gets called from RenderMenuList::valueChanged, which gets called after the user makes a selection from the menu.
saveLastSelection(data, element);
if (RenderMenuList* menuList = toRenderMenuList(element->renderer()))
menuList->showPopup();
handled = true;
}
#else
const Vector<Element*>& listItems = data.listItems(element);
int listIndex = optionToListIndex(data, element, selectedIndex(data, element));
if (keyIdentifier == "Down" || keyIdentifier == "Right") {
listIndex = nextValidIndex(listItems, listIndex, SkipForwards, 1);
handled = true;
} else if (keyIdentifier == "Up" || keyIdentifier == "Left") {
listIndex = nextValidIndex(listItems, listIndex, SkipBackwards, 1);
handled = true;
} else if (keyIdentifier == "PageDown") {
listIndex = nextValidIndex(listItems, listIndex, SkipForwards, 3);
handled = true;
} else if (keyIdentifier == "PageUp") {
listIndex = nextValidIndex(listItems, listIndex, SkipBackwards, 3);
handled = true;
} else if (keyIdentifier == "Home") {
listIndex = nextValidIndex(listItems, -1, SkipForwards, 1);
handled = true;
} else if (keyIdentifier == "End") {
listIndex = nextValidIndex(listItems, listItems.size(), SkipBackwards, 1);
handled = true;
}
if (handled && listIndex >= 0 && (unsigned)listIndex < listItems.size())
setSelectedIndex(data, element, listToOptionIndex(data, element, listIndex));
#endif
if (handled)
event->setDefaultHandled();
}
// Use key press event here since sending simulated mouse events
// on key down blocks the proper sending of the key press event.
if (event->type() == eventNames().keypressEvent) {
if (!element->renderer() || !event->isKeyboardEvent())
return;
int keyCode = static_cast<KeyboardEvent*>(event)->keyCode();
bool handled = false;
#if SPACE_OR_RETURN_POP_MENU
if (keyCode == ' ' || keyCode == '\r') {
element->focus();
// Save the selection so it can be compared to the new selection when dispatching change events during setSelectedIndex,
// which gets called from RenderMenuList::valueChanged, which gets called after the user makes a selection from the menu.
saveLastSelection(data, element);
if (RenderMenuList* menuList = toRenderMenuList(element->renderer()))
menuList->showPopup();
handled = true;
}
#elif ARROW_KEYS_POP_MENU
if (keyCode == ' ') {
element->focus();
// Save the selection so it can be compared to the new selection when dispatching change events during setSelectedIndex,
// which gets called from RenderMenuList::valueChanged, which gets called after the user makes a selection from the menu.
saveLastSelection(data, element);
if (RenderMenuList* menuList = toRenderMenuList(element->renderer()))
menuList->showPopup();
handled = true;
} else if (keyCode == '\r') {
menuListOnChange(data, element);
if (htmlForm)
htmlForm->submitClick(event);
handled = true;
}
#else
int listIndex = optionToListIndex(data, element, selectedIndex(data, element));
if (keyCode == '\r') {
// listIndex should already be selected, but this will fire the onchange handler.
setSelectedIndex(data, element, listToOptionIndex(data, element, listIndex), true, true);
handled = true;
}
#endif
if (handled)
event->setDefaultHandled();
}
if (event->type() == eventNames().mousedownEvent && event->isMouseEvent() && static_cast<MouseEvent*>(event)->button() == LeftButton) {
element->focus();
if (element->renderer() && element->renderer()->isMenuList()) {
if (RenderMenuList* menuList = toRenderMenuList(element->renderer())) {
if (menuList->popupIsVisible())
menuList->hidePopup();
else {
// Save the selection so it can be compared to the new selection when we call onChange during setSelectedIndex,
// which gets called from RenderMenuList::valueChanged, which gets called after the user makes a selection from the menu.
saveLastSelection(data, element);
menuList->showPopup();
}
}
}
event->setDefaultHandled();
}
}
void SelectElement::listBoxDefaultEventHandler(SelectElementData& data, Element* element, Event* event, HTMLFormElement* htmlForm)
{
const Vector<Element*>& listItems = data.listItems(element);
if (event->type() == eventNames().mousedownEvent && event->isMouseEvent() && static_cast<MouseEvent*>(event)->button() == LeftButton) {
element->focus();
// Convert to coords relative to the list box if needed.
MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
IntPoint localOffset = roundedIntPoint(element->renderer()->absoluteToLocal(mouseEvent->absoluteLocation(), false, true));
int listIndex = toRenderListBox(element->renderer())->listIndexAtOffset(localOffset.x(), localOffset.y());
if (listIndex >= 0) {
// Save the selection so it can be compared to the new selection when dispatching change events during mouseup, or after autoscroll finishes.
saveLastSelection(data, element);
data.setActiveSelectionState(true);
bool multiSelectKeyPressed = false;
#if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && PLATFORM(DARWIN))
multiSelectKeyPressed = mouseEvent->metaKey();
#else
multiSelectKeyPressed = mouseEvent->ctrlKey();
#endif
bool shiftSelect = data.multiple() && mouseEvent->shiftKey();
bool multiSelect = data.multiple() && multiSelectKeyPressed && !mouseEvent->shiftKey();
Element* clickedElement = listItems[listIndex];
OptionElement* option = toOptionElement(clickedElement);
if (option) {
// Keep track of whether an active selection (like during drag selection), should select or deselect
if (option->selected() && multiSelectKeyPressed)
data.setActiveSelectionState(false);
if (!data.activeSelectionState())
option->setSelectedState(false);
}
// If we're not in any special multiple selection mode, then deselect all other items, excluding the clicked option.
// If no option was clicked, then this will deselect all items in the list.
if (!shiftSelect && !multiSelect)
deselectItems(data, element, clickedElement);
// If the anchor hasn't been set, and we're doing a single selection or a shift selection, then initialize the anchor to the first selected index.
if (data.activeSelectionAnchorIndex() < 0 && !multiSelect)
setActiveSelectionAnchorIndex(data, element, selectedIndex(data, element));
// Set the selection state of the clicked option
if (option && !clickedElement->disabled())
option->setSelectedState(true);
// If there was no selectedIndex() for the previous initialization, or
// If we're doing a single selection, or a multiple selection (using cmd or ctrl), then initialize the anchor index to the listIndex that just got clicked.
if (listIndex >= 0 && (data.activeSelectionAnchorIndex() < 0 || !shiftSelect))
setActiveSelectionAnchorIndex(data, element, listIndex);
setActiveSelectionEndIndex(data, listIndex);
updateListBoxSelection(data, element, !multiSelect);
if (Frame* frame = element->document()->frame())
frame->eventHandler()->setMouseDownMayStartAutoscroll();
event->setDefaultHandled();
}
} else if (event->type() == eventNames().mouseupEvent && event->isMouseEvent() && static_cast<MouseEvent*>(event)->button() == LeftButton && element->document()->frame()->eventHandler()->autoscrollRenderer() != element->renderer())
// This makes sure we fire dispatchFormControlChangeEvent for a single click. For drag selection, onChange will fire when the autoscroll timer stops.
listBoxOnChange(data, element);
else if (event->type() == eventNames().keydownEvent) {
if (!event->isKeyboardEvent())
return;
String keyIdentifier = static_cast<KeyboardEvent*>(event)->keyIdentifier();
int endIndex = 0;
if (data.activeSelectionEndIndex() < 0) {
// Initialize the end index
if (keyIdentifier == "Down")
endIndex = nextSelectableListIndex(data, element, lastSelectedListIndex(data, element));
else if (keyIdentifier == "Up")
endIndex = previousSelectableListIndex(data, element, optionToListIndex(data, element, selectedIndex(data, element)));
} else {
// Set the end index based on the current end index
if (keyIdentifier == "Down")
endIndex = nextSelectableListIndex(data, element, data.activeSelectionEndIndex());
else if (keyIdentifier == "Up")
endIndex = previousSelectableListIndex(data, element, data.activeSelectionEndIndex());
}
if (keyIdentifier == "Down" || keyIdentifier == "Up") {
// Save the selection so it can be compared to the new selection when dispatching change events immediately after making the new selection.
saveLastSelection(data, element);
ASSERT(endIndex >= 0 && (unsigned) endIndex < listItems.size());
setActiveSelectionEndIndex(data, endIndex);
// If the anchor is unitialized, or if we're going to deselect all other options, then set the anchor index equal to the end index.
bool deselectOthers = !data.multiple() || !static_cast<KeyboardEvent*>(event)->shiftKey();
if (data.activeSelectionAnchorIndex() < 0 || deselectOthers) {
data.setActiveSelectionState(true);
if (deselectOthers)
deselectItems(data, element);
setActiveSelectionAnchorIndex(data, element, data.activeSelectionEndIndex());
}
toRenderListBox(element->renderer())->scrollToRevealElementAtListIndex(endIndex);
updateListBoxSelection(data, element, deselectOthers);
listBoxOnChange(data, element);
event->setDefaultHandled();
}
} else if (event->type() == eventNames().keypressEvent) {
if (!event->isKeyboardEvent())
return;
int keyCode = static_cast<KeyboardEvent*>(event)->keyCode();
if (keyCode == '\r') {
if (htmlForm)
htmlForm->submitClick(event);
event->setDefaultHandled();
return;
}
}
}
void SelectElement::defaultEventHandler(SelectElementData& data, Element* element, Event* event, HTMLFormElement* htmlForm)
{
if (!element->renderer())
return;
if (data.usesMenuList())
menuListDefaultEventHandler(data, element, event, htmlForm);
else
listBoxDefaultEventHandler(data, element, event, htmlForm);
if (event->defaultHandled())
return;
if (event->type() == eventNames().keypressEvent && event->isKeyboardEvent()) {
KeyboardEvent* keyboardEvent = static_cast<KeyboardEvent*>(event);
if (!keyboardEvent->ctrlKey() && !keyboardEvent->altKey() && !keyboardEvent->metaKey() && isPrintableChar(keyboardEvent->charCode())) {
typeAheadFind(data, element, keyboardEvent);
event->setDefaultHandled();
return;
}
}
}
int SelectElement::lastSelectedListIndex(const SelectElementData& data, const Element* element)
{
// return the number of the last option selected
unsigned index = 0;
bool found = false;
const Vector<Element*>& items = data.listItems(element);
for (size_t i = 0; i < items.size(); ++i) {
if (OptionElement* optionElement = toOptionElement(items[i])) {
if (optionElement->selected()) {
index = i;
found = true;
}
}
}
return found ? (int) index : -1;
}
static String stripLeadingWhiteSpace(const String& string)
{
int length = string.length();
int i;
for (i = 0; i < length; ++i) {
if (string[i] != noBreakSpace && (string[i] <= 0x7F ? !isASCIISpace(string[i]) : (direction(string[i]) != WhiteSpaceNeutral)))
break;
}
return string.substring(i, length - i);
}
void SelectElement::typeAheadFind(SelectElementData& data, Element* element, KeyboardEvent* event)
{
if (event->timeStamp() < data.lastCharTime())
return;
DOMTimeStamp delta = event->timeStamp() - data.lastCharTime();
data.setLastCharTime(event->timeStamp());
UChar c = event->charCode();
String prefix;
int searchStartOffset = 1;
if (delta > typeAheadTimeout) {
prefix = String(&c, 1);
data.setTypedString(prefix);
data.setRepeatingChar(c);
} else {
data.typedString().append(c);
if (c == data.repeatingChar())
// The user is likely trying to cycle through all the items starting with this character, so just search on the character
prefix = String(&c, 1);
else {
data.setRepeatingChar(0);
prefix = data.typedString();
searchStartOffset = 0;
}
}
const Vector<Element*>& items = data.listItems(element);
int itemCount = items.size();
if (itemCount < 1)
return;
int selected = selectedIndex(data, element);
int index = (optionToListIndex(data, element, selected >= 0 ? selected : 0) + searchStartOffset) % itemCount;
ASSERT(index >= 0);
// Compute a case-folded copy of the prefix string before beginning the search for
// a matching element. This code uses foldCase to work around the fact that
// String::startWith does not fold non-ASCII characters. This code can be changed
// to use startWith once that is fixed.
String prefixWithCaseFolded(prefix.foldCase());
for (int i = 0; i < itemCount; ++i, index = (index + 1) % itemCount) {
OptionElement* optionElement = toOptionElement(items[index]);
if (!optionElement || items[index]->disabled())
continue;
// Fold the option string and check if its prefix is equal to the folded prefix.
String text = optionElement->textIndentedToRespectGroupLabel();
if (stripLeadingWhiteSpace(text).foldCase().startsWith(prefixWithCaseFolded)) {
setSelectedIndex(data, element, listToOptionIndex(data, element, index));
if (!data.usesMenuList())
listBoxOnChange(data, element);
element->setNeedsStyleRecalc();
return;
}
}
}
void SelectElement::insertedIntoTree(SelectElementData& data, Element* element)
{
// When the element is created during document parsing, it won't have any items yet - but for innerHTML
// and related methods, this method is called after the whole subtree is constructed.
recalcListItems(data, element, true);
}
void SelectElement::accessKeySetSelectedIndex(SelectElementData& data, Element* element, int index)
{
// first bring into focus the list box
if (!element->focused())
element->accessKeyAction(false);
// if this index is already selected, unselect. otherwise update the selected index
const Vector<Element*>& items = data.listItems(element);
int listIndex = optionToListIndex(data, element, index);
if (OptionElement* optionElement = (listIndex >= 0 ? toOptionElement(items[listIndex]) : 0)) {
if (optionElement->selected())
optionElement->setSelectedState(false);
else
setSelectedIndex(data, element, index, false, true);
}
listBoxOnChange(data, element);
scrollToSelection(data, element);
}
unsigned SelectElement::optionCount(const SelectElementData& data, const Element* element)
{
unsigned options = 0;
const Vector<Element*>& items = data.listItems(element);
for (unsigned i = 0; i < items.size(); ++i) {
if (isOptionElement(items[i]))
++options;
}
return options;
}
// SelectElementData
SelectElementData::SelectElementData()
: m_multiple(false)
, m_size(0)
, m_lastOnChangeIndex(-1)
, m_activeSelectionState(false)
, m_activeSelectionAnchorIndex(-1)
, m_activeSelectionEndIndex(-1)
, m_recalcListItems(false)
, m_repeatingChar(0)
, m_lastCharTime(0)
{
}
void SelectElementData::checkListItems(const Element* element) const
{
#ifndef NDEBUG
const Vector<Element*>& items = m_listItems;
SelectElement::recalcListItems(*const_cast<SelectElementData*>(this), element, false);
ASSERT(items == m_listItems);
#else
UNUSED_PARAM(element);
#endif
}
Vector<Element*>& SelectElementData::listItems(const Element* element)
{
if (m_recalcListItems)
SelectElement::recalcListItems(*this, element);
else
checkListItems(element);
return m_listItems;
}
const Vector<Element*>& SelectElementData::listItems(const Element* element) const
{
if (m_recalcListItems)
SelectElement::recalcListItems(*const_cast<SelectElementData*>(this), element);
else
checkListItems(element);
return m_listItems;
}
SelectElement* toSelectElement(Element* element)
{
if (element->isHTMLElement()) {
if (element->hasTagName(HTMLNames::selectTag))
return static_cast<HTMLSelectElement*>(element);
if (element->hasTagName(HTMLNames::keygenTag))
return static_cast<HTMLKeygenElement*>(element);
}
#if ENABLE(WML)
if (element->isWMLElement() && element->hasTagName(WMLNames::selectTag))
return static_cast<WMLSelectElement*>(element);
#endif
return 0;
}
}
|