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
|
/*
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
* (C) 2006 Alexey Proskuryakov (ap@nypop.com)
* Copyright (C) 2010 Google Inc. All rights reserved.
* 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 "core/html/forms/TypeAhead.h"
#include "core/events/KeyboardEvent.h"
#include "wtf/unicode/CharacterNames.h"
using namespace WTF::Unicode;
namespace WebCore {
TypeAhead::TypeAhead(TypeAheadDataSource* dataSource)
: m_dataSource(dataSource)
, m_lastTypeTime(0)
, m_repeatingChar(0)
{
}
static const DOMTimeStamp typeAheadTimeout = 1000;
static String stripLeadingWhiteSpace(const String& string)
{
unsigned length = string.length();
unsigned i;
for (i = 0; i < length; ++i) {
if (string[i] != noBreakSpace && !isSpaceOrNewline(string[i]))
break;
}
return string.substring(i, length - i);
}
int TypeAhead::handleEvent(KeyboardEvent* event, MatchModeFlags matchMode)
{
if (event->timeStamp() < m_lastTypeTime)
return -1;
int optionCount = m_dataSource->optionCount();
DOMTimeStamp delta = event->timeStamp() - m_lastTypeTime;
m_lastTypeTime = event->timeStamp();
UChar c = event->charCode();
if (delta > typeAheadTimeout)
m_buffer.clear();
m_buffer.append(c);
if (optionCount < 1)
return -1;
int searchStartOffset = 1;
String prefix;
if (matchMode & CycleFirstChar && c == m_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);
m_repeatingChar = c;
} else if (matchMode & MatchPrefix) {
prefix = m_buffer.toString();
if (m_buffer.length() > 1) {
m_repeatingChar = 0;
searchStartOffset = 0;
} else {
m_repeatingChar = c;
}
}
if (!prefix.isEmpty()) {
int selected = m_dataSource->indexOfSelectedOption();
int index = (selected < 0 ? 0 : selected) + searchStartOffset;
index %= optionCount;
// 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 < optionCount; ++i, index = (index + 1) % optionCount) {
// Fold the option string and check if its prefix is equal to the folded prefix.
String text = m_dataSource->optionAtIndex(index);
if (stripLeadingWhiteSpace(text).foldCase().startsWith(prefixWithCaseFolded))
return index;
}
}
if (matchMode & MatchIndex) {
bool ok = false;
int index = m_buffer.toString().toInt(&ok);
if (index > 0 && index <= optionCount)
return index - 1;
}
return -1;
}
} // namespace WebCore
|