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
|
/**
* 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"
#if ENABLE(WML)
#include "WMLSelectElement.h"
#include "CString.h"
#include "HTMLNames.h"
#include "MappedAttribute.h"
#include "OptionElement.h"
#include "RenderListBox.h"
#include "RenderMenuList.h"
#include "WMLDocument.h"
#include "WMLNames.h"
#include "WMLVariables.h"
#include <wtf/StdLibExtras.h>
namespace WebCore {
using namespace WMLNames;
WMLSelectElement::WMLSelectElement(const QualifiedName& tagName, Document* document)
: WMLFormControlElement(tagName, document)
{
}
WMLSelectElement::~WMLSelectElement()
{
}
String WMLSelectElement::title() const
{
return substituteVariableReferences(getAttribute(HTMLNames::titleAttr), document());
}
const AtomicString& WMLSelectElement::formControlName() const
{
AtomicString name = this->name();
return name.isNull() ? emptyAtom : name;
}
const AtomicString& WMLSelectElement::formControlType() const
{
DEFINE_STATIC_LOCAL(const AtomicString, selectMultiple, ("select-multiple"));
DEFINE_STATIC_LOCAL(const AtomicString, selectOne, ("select-one"));
return m_data.multiple() ? selectMultiple : selectOne;
}
bool WMLSelectElement::isKeyboardFocusable(KeyboardEvent* event) const
{
if (renderer())
return isFocusable();
return WMLFormControlElement::isKeyboardFocusable(event);
}
bool WMLSelectElement::isMouseFocusable() const
{
if (renderer())
return isFocusable();
return WMLFormControlElement::isMouseFocusable();
}
void WMLSelectElement::selectAll()
{
SelectElement::selectAll(m_data, this);
}
void WMLSelectElement::recalcStyle(StyleChange change)
{
SelectElement::recalcStyle(m_data, this);
WMLFormControlElement::recalcStyle(change);
}
void WMLSelectElement::dispatchFocusEvent()
{
SelectElement::dispatchFocusEvent(m_data, this);
WMLFormControlElement::dispatchFocusEvent();
}
void WMLSelectElement::dispatchBlurEvent()
{
SelectElement::dispatchBlurEvent(m_data, this);
WMLFormControlElement::dispatchBlurEvent();
}
int WMLSelectElement::selectedIndex() const
{
return SelectElement::selectedIndex(m_data, this);
}
void WMLSelectElement::setSelectedIndex(int optionIndex, bool deselect)
{
SelectElement::setSelectedIndex(m_data, this, optionIndex, deselect, false, false);
}
void WMLSelectElement::setSelectedIndexByUser(int optionIndex, bool deselect, bool fireOnChangeNow)
{
SelectElement::setSelectedIndex(m_data, this, optionIndex, deselect, fireOnChangeNow, true);
}
bool WMLSelectElement::saveFormControlState(String& value) const
{
return SelectElement::saveFormControlState(m_data, this, value);
}
void WMLSelectElement::restoreFormControlState(const String& state)
{
SelectElement::restoreFormControlState(m_data, this, state);
}
void WMLSelectElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
SelectElement::setRecalcListItems(m_data, this);
WMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
}
void WMLSelectElement::parseMappedAttribute(MappedAttribute* attr)
{
if (attr->name() == HTMLNames::multipleAttr)
SelectElement::parseMultipleAttribute(m_data, this, attr);
else
WMLFormControlElement::parseMappedAttribute(attr);
}
RenderObject* WMLSelectElement::createRenderer(RenderArena* arena, RenderStyle*)
{
if (m_data.usesMenuList())
return new (arena) RenderMenuList(this);
return new (arena) RenderListBox(this);
}
bool WMLSelectElement::appendFormData(FormDataList& list, bool)
{
return SelectElement::appendFormData(m_data, this, list);
}
int WMLSelectElement::optionToListIndex(int optionIndex) const
{
return SelectElement::optionToListIndex(m_data, this, optionIndex);
}
int WMLSelectElement::listToOptionIndex(int listIndex) const
{
return SelectElement::listToOptionIndex(m_data, this, listIndex);
}
void WMLSelectElement::reset()
{
SelectElement::reset(m_data, this);
}
void WMLSelectElement::defaultEventHandler(Event* event)
{
SelectElement::defaultEventHandler(m_data, this, event);
// FIXME: There must be a better place to update the page variable state. Investigate.
updateVariables();
if (event->defaultHandled())
return;
WMLFormControlElement::defaultEventHandler(event);
}
void WMLSelectElement::accessKeyAction(bool sendToAnyElement)
{
focus();
dispatchSimulatedClick(0, sendToAnyElement);
}
void WMLSelectElement::setActiveSelectionAnchorIndex(int index)
{
SelectElement::setActiveSelectionAnchorIndex(m_data, this, index);
}
void WMLSelectElement::setActiveSelectionEndIndex(int index)
{
SelectElement::setActiveSelectionEndIndex(m_data, index);
}
void WMLSelectElement::updateListBoxSelection(bool deselectOtherOptions)
{
SelectElement::updateListBoxSelection(m_data, this, deselectOtherOptions);
}
void WMLSelectElement::listBoxOnChange()
{
SelectElement::listBoxOnChange(m_data, this);
}
void WMLSelectElement::menuListOnChange()
{
SelectElement::menuListOnChange(m_data, this);
}
int WMLSelectElement::activeSelectionStartListIndex() const
{
if (m_data.activeSelectionAnchorIndex() >= 0)
return m_data.activeSelectionAnchorIndex();
return optionToListIndex(selectedIndex());
}
int WMLSelectElement::activeSelectionEndListIndex() const
{
if (m_data.activeSelectionEndIndex() >= 0)
return m_data.activeSelectionEndIndex();
return SelectElement::lastSelectedListIndex(m_data, this);
}
void WMLSelectElement::accessKeySetSelectedIndex(int index)
{
SelectElement::accessKeySetSelectedIndex(m_data, this, index);
}
void WMLSelectElement::setRecalcListItems()
{
SelectElement::setRecalcListItems(m_data, this);
}
void WMLSelectElement::scrollToSelection()
{
SelectElement::scrollToSelection(m_data, this);
}
void WMLSelectElement::selectInitialOptions()
{
// Spec: Step 1 - the default option index is determined using iname and ivalue
calculateDefaultOptionIndices();
if (m_defaultOptionIndices.isEmpty())
return;
// Spec: Step 2 – initialise variables
initializeVariables();
// Spec: Step 3 – pre-select option(s) specified by the default option index
selectDefaultOptions();
}
void WMLSelectElement::insertedIntoTree(bool deep)
{
SelectElement::insertedIntoTree(m_data, this);
WMLFormControlElement::insertedIntoTree(deep);
}
void WMLSelectElement::calculateDefaultOptionIndices()
{
WMLPageState* pageState = wmlPageStateForDocument(document());
if (!pageState)
return;
String variable;
// Spec: If the 'iname' attribute is specified and names a variable that is set,
// then the default option index is the validated value of that variable.
String iname = this->iname();
if (!iname.isEmpty()) {
variable = pageState->getVariable(iname);
if (!variable.isEmpty())
m_defaultOptionIndices = parseIndexValueString(variable);
}
// Spec: If the default option index is empty and the 'ivalue' attribute is specified,
// then the default option index is the validated attribute value.
String ivalue = this->ivalue();
if (m_defaultOptionIndices.isEmpty() && !ivalue.isEmpty())
m_defaultOptionIndices = parseIndexValueString(ivalue);
// Spec: If the default option index is empty, and the 'name' attribute is specified
// and the 'name' ttribute names a variable that is set, then for each value in the 'name'
// variable that is present as a value in the select's option elements, the index of the
// first option element containing that value is added to the default index if that
// index has not been previously added.
String name = this->name();
if (m_defaultOptionIndices.isEmpty() && !name.isEmpty()) {
variable = pageState->getVariable(name);
if (!variable.isEmpty())
m_defaultOptionIndices = valueStringToOptionIndices(variable);
}
String value = parseValueSubstitutingVariableReferences(getAttribute(HTMLNames::valueAttr));
// Spec: If the default option index is empty and the 'value' attribute is specified then
// for each value in the 'value' attribute that is present as a value in the select's
// option elements, the index of the first option element containing that value is added
// to the default index if that index has not been previously added.
if (m_defaultOptionIndices.isEmpty() && !value.isEmpty())
m_defaultOptionIndices = valueStringToOptionIndices(value);
// Spec: If the default option index is empty and the select is a multi-choice, then the
// default option index is set to zero. If the select is single-choice, then the default
// option index is set to one.
if (m_defaultOptionIndices.isEmpty())
m_defaultOptionIndices.append((unsigned) !m_data.multiple());
}
void WMLSelectElement::selectDefaultOptions()
{
ASSERT(!m_defaultOptionIndices.isEmpty());
if (!m_data.multiple()) {
setSelectedIndex(m_defaultOptionIndices.first() - 1, false);
return;
}
Vector<unsigned>::const_iterator end = m_defaultOptionIndices.end();
for (Vector<unsigned>::const_iterator it = m_defaultOptionIndices.begin(); it != end; ++it)
setSelectedIndex((*it) - 1, false);
}
void WMLSelectElement::initializeVariables()
{
ASSERT(!m_defaultOptionIndices.isEmpty());
WMLPageState* pageState = wmlPageStateForDocument(document());
if (!pageState)
return;
const Vector<Element*>& items = m_data.listItems(this);
if (items.isEmpty())
return;
// Spec: If the 'iname' attribute is specified, then the named variable is set with the default option index.
String iname = this->iname();
if (!iname.isEmpty())
pageState->storeVariable(iname, optionIndicesToString());
String name = this->name();
if (name.isEmpty())
return;
if (m_data.multiple()) {
// Spec: If the 'name' attribute is specified and the select is a multiple-choice element,
// then for each index greater than zero, the value of the 'value' attribute on the option
// element at the index is added to the name variable.
pageState->storeVariable(name, optionIndicesToValueString());
return;
}
// Spec: If the 'name' attribute is specified and the select is a single-choice element,
// then the named variable is set with the value of the 'value' attribute on the option
// element at the default option index.
unsigned optionIndex = m_defaultOptionIndices.first();
ASSERT(optionIndex >= 1);
int listIndex = optionToListIndex(optionIndex - 1);
ASSERT(listIndex >= 0);
ASSERT(listIndex < (int) items.size());
if (OptionElement* optionElement = toOptionElement(items[listIndex]))
pageState->storeVariable(name, optionElement->value());
}
void WMLSelectElement::updateVariables()
{
WMLPageState* pageState = wmlPageStateForDocument(document());
if (!pageState)
return;
String name = this->name();
String iname = this->iname();
if (iname.isEmpty() && name.isEmpty())
return;
String nameString;
String inameString;
unsigned optionIndex = 0;
const Vector<Element*>& items = m_data.listItems(this);
for (unsigned i = 0; i < items.size(); ++i) {
OptionElement* optionElement = toOptionElement(items[i]);
if (!optionElement)
continue;
++optionIndex;
if (!optionElement->selected())
continue;
if (!nameString.isEmpty())
nameString += ";";
if (!inameString.isEmpty())
inameString += ";";
nameString += optionElement->value();
inameString += String::number(optionIndex);
}
if (!name.isEmpty())
pageState->storeVariable(name, nameString);
if (!iname.isEmpty())
pageState->storeVariable(iname, inameString);
}
Vector<unsigned> WMLSelectElement::parseIndexValueString(const String& indexValue) const
{
Vector<unsigned> indices;
if (indexValue.isEmpty())
return indices;
Vector<String> indexStrings;
indexValue.split(';', indexStrings);
bool ok = false;
unsigned optionCount = SelectElement::optionCount(m_data, this);
Vector<String>::const_iterator end = indexStrings.end();
for (Vector<String>::const_iterator it = indexStrings.begin(); it != end; ++it) {
unsigned parsedValue = (*it).toUIntStrict(&ok);
// Spec: Remove all non-integer indices from the value. Remove all out-of-range indices
// from the value, where out-of-range is defined as any index with a value greater than
// the number of options in the select or with a value less than one.
if (!ok || parsedValue < 1 || parsedValue > optionCount)
continue;
// Spec: Remove duplicate indices.
if (indices.find(parsedValue) == notFound)
indices.append(parsedValue);
}
return indices;
}
Vector<unsigned> WMLSelectElement::valueStringToOptionIndices(const String& value) const
{
Vector<unsigned> indices;
if (value.isEmpty())
return indices;
const Vector<Element*>& items = m_data.listItems(this);
if (items.isEmpty())
return indices;
Vector<String> indexStrings;
value.split(';', indexStrings);
unsigned optionIndex = 0;
Vector<String>::const_iterator end = indexStrings.end();
for (Vector<String>::const_iterator it = indexStrings.begin(); it != end; ++it) {
String value = *it;
for (unsigned i = 0; i < items.size(); ++i) {
if (!isOptionElement(items[i]))
continue;
++optionIndex;
if (OptionElement* optionElement = toOptionElement(items[i])) {
if (optionElement->value() == value) {
indices.append(optionIndex);
break;
}
}
}
}
return indices;
}
String WMLSelectElement::optionIndicesToValueString() const
{
String valueString;
if (m_defaultOptionIndices.isEmpty())
return valueString;
const Vector<Element*>& items = m_data.listItems(this);
if (items.isEmpty())
return valueString;
Vector<unsigned>::const_iterator end = m_defaultOptionIndices.end();
for (Vector<unsigned>::const_iterator it = m_defaultOptionIndices.begin(); it != end; ++it) {
unsigned optionIndex = (*it);
if (optionIndex < 1 || optionIndex > items.size())
continue;
int listIndex = optionToListIndex((*it) - 1);
ASSERT(listIndex >= 0);
ASSERT(listIndex < (int) items.size());
if (OptionElement* optionElement = toOptionElement(items[listIndex])) {
if (!valueString.isEmpty())
valueString += ";";
valueString += optionElement->value();
}
}
return valueString;
}
String WMLSelectElement::optionIndicesToString() const
{
String valueString;
if (m_defaultOptionIndices.isEmpty())
return valueString;
Vector<unsigned>::const_iterator end = m_defaultOptionIndices.end();
for (Vector<unsigned>::const_iterator it = m_defaultOptionIndices.begin(); it != end; ++it) {
if (!valueString.isEmpty())
valueString += ";";
valueString += String::number(*it);
}
return valueString;
}
String WMLSelectElement::name() const
{
return parseValueForbiddingVariableReferences(getAttribute(HTMLNames::nameAttr));
}
String WMLSelectElement::value() const
{
return parseValueSubstitutingVariableReferences(getAttribute(HTMLNames::valueAttr));
}
String WMLSelectElement::iname() const
{
return parseValueForbiddingVariableReferences(getAttribute(inameAttr));
}
String WMLSelectElement::ivalue() const
{
return parseValueSubstitutingVariableReferences(getAttribute(ivalueAttr));
}
}
#endif
|