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
|
/*
* Copyright (c) 2010 The Chromium Authors. All rights reserved.
* Copyright 2010, The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "FormManagerAndroid.h"
#include "HTMLFormControlElement.h"
#include "HTMLFormElement.h"
#include "HTMLTextAreaElement.h"
#include "HTMLInputElement.h"
#include "HTMLLabelElement.h"
#include "HTMLNames.h"
#include "HTMLSelectElement.h"
#include "NodeList.h"
#include "HTMLCollection.h"
#include "FormFieldAndroid.h"
#include "QualifiedName.h"
// TODO: This file is taken from chromium/chrome/renderer/form_manager.cc and
// customised to use WebCore types rather than WebKit API types. It would be
// nice and would ease future merge pain if the two could be combined.
using webkit_glue::FormData;
using webkit_glue::FormField;
using WebCore::HTMLElement;
using WebCore::HTMLFormControlElement;
using WebCore::HTMLFormElement;
using WebCore::HTMLLabelElement;
using WebCore::HTMLSelectElement;
using WebCore::Node;
using WebCore::NodeList;
using namespace WebCore::HTMLNames;
namespace {
// The number of fields required by AutoFill. Ideally we could send the forms
// to AutoFill no matter how many fields are in the forms; however, finding the
// label for each field is a costly operation and we can't spare the cycles if
// it's not necessary.
// Note the on ANDROID we reduce this from Chromium's 3 as it allows us to
// autofill simple name/email forms for example. This improves the mobile
// device experience where form filling can be time consuming and frustrating.
const size_t kRequiredAutoFillFields = 2;
string16 WTFStringToString16(const WTF::String& wtfString)
{
WTF::String str = wtfString;
if (str.charactersWithNullTermination())
return string16(str.charactersWithNullTermination());
else
return string16();
}
string16 S(const WTF::AtomicString& str)
{
return WTFStringToString16(str.string());
}
string16 S(const WTF::String& string)
{
return WTFStringToString16(string);
}
// This is a helper function for the FindChildText() function.
// Returns the node value of the descendant or sibling of |node| that is a
// non-empty text node. This is a faster alternative to |innerText()| for
// performance critical operations. It does a full depth-first search so
// can be used when the structure is not directly known. It does not aggregate
// the text of multiple nodes, it just returns the value of the first found.
// "Non-empty" in this case means non-empty after the whitespace has been
// stripped.
string16 FindChildTextInner(const Node* node) {
string16 element_text;
if (!node)
return element_text;
element_text = S(node->nodeValue());
TrimWhitespace(element_text, TRIM_ALL, &element_text);
if (!element_text.empty())
return element_text;
element_text = FindChildTextInner(node->firstChild());
if (!element_text.empty())
return element_text;
element_text = FindChildTextInner(node->nextSibling());
if (!element_text.empty())
return element_text;
return element_text;
}
// Returns the node value of the first decendant of |element| that is a
// non-empty text node. "Non-empty" in this case means non-empty after the
// whitespace has been stripped.
string16 FindChildText(const HTMLElement* element) {
Node* child = element->firstChild();
return FindChildTextInner(child);
}
} // namespace
namespace android {
FormManager::FormManager() {
}
FormManager::~FormManager() {
Reset();
}
// static
void FormManager::HTMLFormControlElementToFormField(
const HTMLFormControlElement& element, bool get_value, FormField* field) {
ASSERT(field);
// The label is not officially part of a HTMLFormControlElement; however, the
// labels for all form control elements are scraped from the DOM and set in
// WebFormElementToFormData.
field->set_name(S(element.name()));
field->set_form_control_type(S(element.type()));
if (!get_value)
return;
string16 value;
if (element.type() == WTF::AtomicString("text")) {
const WebCore::HTMLTextAreaElement* input_element = static_cast<const WebCore::HTMLTextAreaElement*>(&element);
if (input_element->renderer())
value = S(input_element->value());
} else if (element.type() == WTF::AtomicString("select-one")) {
// TODO: This is ugly. SelectElement::value() is a non-const
// method. Look into fixing this on the WebKit side.
HTMLFormControlElement& e = const_cast<HTMLFormControlElement&>(element);
HTMLSelectElement& select_element = static_cast<HTMLSelectElement&>(e);
value = S(select_element.value());
}
field->set_value(value);
}
// static
string16 FormManager::LabelForElement(const HTMLFormControlElement& element) {
RefPtr<NodeList> labels = element.document()->getElementsByTagName("label");
for (unsigned i = 0; i < labels->length(); ++i) {
Node* e = labels->item(i);
if (e->hasTagName(labelTag)) {
HTMLLabelElement* label = static_cast<HTMLLabelElement*>(e);
if (label->control() == &element)
return FindChildText(label);
}
}
// Infer the label from context if not found in label element.
return FormManager::InferLabelForElement(element);
}
// static
bool FormManager::HTMLFormElementToFormData(HTMLFormElement& element,
RequirementsMask requirements,
bool get_values,
FormData* form) {
ASSERT(form);
const WebCore::Document* document = element.document();
if (!document)
return false;
if (requirements & REQUIRE_AUTOCOMPLETE && !element.autoComplete())
return false;
form->name = S(element.name());
form->method = S(element.method());
form->origin = GURL(S(document->documentURI()));
form->action = GURL(S(document->completeURL(element.action())));
// If the completed URL is not valid, just use the action we get from
// WebKit.
if (!form->action.is_valid())
form->action = GURL(S(element.action()));
// A map from a FormField's name to the FormField itself.
std::map<string16, FormField*> name_map;
// The extracted FormFields. We use pointers so we can store them in
// |name_map|.
ScopedVector<FormField> form_fields;
WTF::Vector<WebCore::HTMLFormControlElement*> control_elements = element.associatedElements();
// A vector of bools that indicate whether each field in the form meets the
// requirements and thus will be in the resulting |form|.
std::vector<bool> fields_extracted(control_elements.size(), false);
for (size_t i = 0; i < control_elements.size(); ++i) {
const HTMLFormControlElement* control_element = control_elements[i];
if (requirements & REQUIRE_AUTOCOMPLETE &&
control_element->type() == WTF::String("text")) {
const WebCore::HTMLInputElement* input_element = static_cast<const WebCore::HTMLInputElement*>(control_element);
if (!input_element->autoComplete())
continue;
}
if (requirements & REQUIRE_ELEMENTS_ENABLED && !control_element->isEnabledFormControl())
continue;
// Create a new FormField, fill it out and map it to the field's name.
FormField* field = new FormField;
HTMLFormControlElementToFormField(*control_element, get_values, field);
form_fields.push_back(field);
// TODO: A label element is mapped to a form control element's id.
// field->name() will contain the id only if the name does not exist. Add
// an id() method to HTMLFormControlElement and use that here.
name_map[field->name()] = field;
fields_extracted[i] = true;
}
// Don't extract field labels if we have no fields.
if (form_fields.empty())
return false;
// Loop through the label elements inside the form element. For each label
// element, get the corresponding form control element, use the form control
// element's name as a key into the <name, FormField> map to find the
// previously created FormField and set the FormField's label to the
// label.firstChild().nodeValue() of the label element.
RefPtr<WebCore::NodeList> labels = element.getElementsByTagName("label");
for (unsigned i = 0; i < labels->length(); ++i) {
WebCore::HTMLLabelElement* label = static_cast<WebCore::HTMLLabelElement*>(labels->item(i));
HTMLFormControlElement* field_element = label->control();
if (!field_element || field_element->type() == "hidden")
continue;
std::map<string16, FormField*>::iterator iter =
name_map.find(S(field_element->name()));
if (iter != name_map.end())
iter->second->set_label(FindChildText(label));
}
// Loop through the form control elements, extracting the label text from the
// DOM. We use the |fields_extracted| vector to make sure we assign the
// extracted label to the correct field, as it's possible |form_fields| will
// not contain all of the elements in |control_elements|.
for (size_t i = 0, field_idx = 0;
i < control_elements.size() && field_idx < form_fields.size(); ++i) {
// This field didn't meet the requirements, so don't try to find a label for
// it.
if (!fields_extracted[i])
continue;
const HTMLFormControlElement* control_element = control_elements[i];
if (form_fields[field_idx]->label().empty())
form_fields[field_idx]->set_label(
FormManager::InferLabelForElement(*control_element));
++field_idx;
}
// Copy the created FormFields into the resulting FormData object.
for (ScopedVector<FormField>::const_iterator iter = form_fields.begin();
iter != form_fields.end(); ++iter) {
form->fields.push_back(**iter);
}
return true;
}
void FormManager::ExtractForms(WebCore::Document* document) {
WTF::PassRefPtr<WebCore::HTMLCollection> collection = document->forms();
WebCore::HTMLFormElement* form;
WebCore::HTMLInputElement* input;
for (Node* node = collection->firstItem();
node && !node->namespaceURI().isNull() && !node->namespaceURI().isEmpty();
node = collection->nextItem()) {
FormElement* form_elements = new FormElement;
form = static_cast<WebCore::HTMLFormElement*>(node);
if (form->autoComplete()) {
WTF::Vector<WebCore::HTMLFormControlElement*> elements = form->associatedElements();
size_t size = elements.size();
for (size_t i = 0; i < size; i++) {
WebCore::HTMLFormControlElement* e = elements[i];
form_elements->control_elements.push_back(e);
}
form_elements->form_element = form;
}
form_elements_map_[document].push_back(form_elements);
}
}
void FormManager::GetForms(RequirementsMask requirements,
std::vector<FormData>* forms) {
ASSERT(forms);
for (DocumentFormElementMap::iterator iter = form_elements_map_.begin();
iter != form_elements_map_.end(); ++iter) {
for (std::vector<FormElement*>::iterator form_iter = iter->second.begin();
form_iter != iter->second.end(); ++form_iter) {
FormData form;
if (HTMLFormElementToFormData(*(*form_iter)->form_element,
requirements,
true,
&form))
forms->push_back(form);
}
}
}
void FormManager::GetFormsInDocument(const WebCore::Document* document,
RequirementsMask requirements,
std::vector<FormData>* forms) {
ASSERT(document);
ASSERT(forms);
DocumentFormElementMap::iterator iter = form_elements_map_.find(document);
if (iter == form_elements_map_.end())
return;
// TODO: Factor this out and use it here and in GetForms.
const std::vector<FormElement*>& form_elements = iter->second;
for (std::vector<FormElement*>::const_iterator form_iter =
form_elements.begin();
form_iter != form_elements.end(); ++form_iter) {
FormElement* form_element = *form_iter;
// We need at least |kRequiredAutoFillFields| fields before appending this
// form to |forms|.
if (form_element->control_elements.size() < kRequiredAutoFillFields)
continue;
if (requirements & REQUIRE_AUTOCOMPLETE &&
!form_element->form_element->autoComplete())
continue;
FormData form;
FormElementToFormData(document, form_element, requirements, &form);
if (form.fields.size() >= kRequiredAutoFillFields)
forms->push_back(form);
}
}
bool FormManager::FindForm(const HTMLFormElement& element,
RequirementsMask requirements,
FormData* form) {
ASSERT(form);
const WebCore::Document* document = element.document();
DocumentFormElementMap::const_iterator document_iter =
form_elements_map_.find(document);
if (document_iter == form_elements_map_.end())
return false;
for (std::vector<FormElement*>::const_iterator iter =
document_iter->second.begin();
iter != document_iter->second.end(); ++iter) {
if ((*iter)->form_element->name() != element.name())
continue;
return FormElementToFormData(document, *iter, requirements, form);
}
return false;
}
bool FormManager::FindFormWithFormControlElement(
const HTMLFormControlElement& element,
RequirementsMask requirements,
FormData* form) {
ASSERT(form);
const WebCore::Document* document = element.document();
if (form_elements_map_.find(document) == form_elements_map_.end())
return false;
const std::vector<FormElement*> forms = form_elements_map_[document];
for (std::vector<FormElement*>::const_iterator iter = forms.begin();
iter != forms.end(); ++iter) {
const FormElement* form_element = *iter;
for (std::vector<HTMLFormControlElement*>::const_iterator iter =
form_element->control_elements.begin();
iter != form_element->control_elements.end(); ++iter) {
if ((*iter)->name() == element.name()) {
HTMLFormElementToFormData(
*form_element->form_element, requirements, true, form);
return true;
}
}
}
return false;
}
bool FormManager::FillForm(const FormData& form) {
FormElement* form_element = NULL;
for (DocumentFormElementMap::iterator iter = form_elements_map_.begin();
iter != form_elements_map_.end(); ++iter) {
const WebCore::Document* document = iter->first;
for (std::vector<FormElement*>::iterator form_iter = iter->second.begin();
form_iter != iter->second.end(); ++form_iter) {
// TODO: matching on form name here which is not guaranteed to
// be unique for the page, nor is it guaranteed to be non-empty. Need to
// find a way to uniquely identify the form cross-process. For now we'll
// check form name and form action for identity.
// http://crbug.com/37990 test file sample8.html.
// Also note that WebString() == WebString(string16()) does not seem to
// evaluate to |true| for some reason TBD, so forcing to string16.
string16 element_name(S((*form_iter)->form_element->name()));
GURL action(
S(document->completeURL((*form_iter)->form_element->action()).string()));
if (element_name == form.name && action == form.action) {
form_element = *form_iter;
break;
}
}
}
if (!form_element)
return false;
// It's possible that the site has injected fields into the form after the
// page has loaded, so we can't assert that the size of the cached control
// elements is equal to the size of the fields in |form|. Fortunately, the
// one case in the wild where this happens, paypal.com signup form, the fields
// are appended to the end of the form and are not visible.
for (size_t i = 0, j = 0;
i < form_element->control_elements.size() && j < form.fields.size();
++i, ++j) {
// Once again, empty WebString != empty string16, so we have to explicitly
// check for this case.
if (form_element->control_elements[i]->name().length() == 0 &&
form.fields[j].name().empty())
continue;
// We assume that the intersection of the fields in
// |form_element->control_elements| and |form.fields| is ordered, but it's
// possible that one or the other sets may have more fields than the other,
// so loop past non-matching fields in the set with more elements.
while (S(form_element->control_elements[i]->name()) !=
form.fields[j].name()) {
if (form_element->control_elements.size() > form.fields.size()) {
// We're at the end of the elements already.
if (i + 1 == form_element->control_elements.size())
break;
++i;
} else if (form.fields.size() > form_element->control_elements.size()) {
// We're at the end of the elements already.
if (j + 1 == form.fields.size())
break;
++j;
} else {
// Shouldn't get here.
ASSERT(false);
}
continue;
}
HTMLFormControlElement* element = form_element->control_elements[i];
if (!form.fields[j].value().empty() &&
element->type() != WTF::String("submit")) {
if (element->type() == WTF::String("text")) {
WebCore::HTMLInputElement* input_element = static_cast<WebCore::HTMLInputElement*>(element);
// If the maxlength attribute contains a negative value, maxLength()
// returns the default maxlength value.
input_element->setValue(
WTF::String(form.fields[j].value().substr(0, input_element->maxLength()).c_str()));
input_element->setAutofilled(true);
} else if (element->type() ==
WTF::String("select-one")) {
WebCore::HTMLSelectElement* select_element = static_cast<WebCore::HTMLSelectElement*>(element);
select_element->setValue(WTF::String(form.fields[j].value().c_str()));
}
}
}
return true;
}
void FormManager::FillForms(const std::vector<webkit_glue::FormData>& forms) {
for (std::vector<webkit_glue::FormData>::const_iterator iter = forms.begin();
iter != forms.end(); ++iter) {
FillForm(*iter);
}
}
void FormManager::Reset() {
for (DocumentFormElementMap::iterator iter = form_elements_map_.begin();
iter != form_elements_map_.end(); ++iter) {
STLDeleteElements(&iter->second);
}
form_elements_map_.clear();
}
void FormManager::ResetFrame(const WebCore::Document* document) {
DocumentFormElementMap::iterator iter = form_elements_map_.find(document);
if (iter != form_elements_map_.end()) {
STLDeleteElements(&iter->second);
form_elements_map_.erase(iter);
}
}
// static
bool FormManager::FormElementToFormData(const WebCore::Document* document,
const FormElement* form_element,
RequirementsMask requirements,
FormData* form) {
if (requirements & REQUIRE_AUTOCOMPLETE &&
!form_element->form_element->autoComplete())
return false;
form->name = S(form_element->form_element->name());
form->method = S(form_element->form_element->method());
form->origin = GURL(S(document->documentURI()));
form->action = GURL(S(document->completeURL(form_element->form_element->action())));
// If the completed URL is not valid, just use the action we get from
// WebKit.
if (!form->action.is_valid())
form->action = GURL(S(form_element->form_element->action()));
// Form elements loop.
for (std::vector<HTMLFormControlElement*>::const_iterator element_iter =
form_element->control_elements.begin();
element_iter != form_element->control_elements.end(); ++element_iter) {
const HTMLFormControlElement* control_element = *element_iter;
if (requirements & REQUIRE_AUTOCOMPLETE &&
control_element->type() == WTF::String("text")) {
const WebCore::HTMLInputElement* input_element =
static_cast<const WebCore::HTMLInputElement*>(control_element);
if (!input_element->autoComplete())
continue;
}
if (requirements & REQUIRE_ELEMENTS_ENABLED && !control_element->isEnabledFormControl())
continue;
FormField field;
HTMLFormControlElementToFormField(*control_element, false, &field);
form->fields.push_back(field);
}
return true;
}
// static
string16 FormManager::InferLabelForElement(
const HTMLFormControlElement& element) {
string16 inferred_label;
Node* previous = element.previousSibling();
if (previous) {
if (previous->isTextNode()) {
inferred_label = S(previous->nodeValue());
TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label);
}
// If we didn't find text, check for previous paragraph.
// Eg. <p>Some Text</p><input ...>
// Note the lack of whitespace between <p> and <input> elements.
if (inferred_label.empty()) {
if (previous->isElementNode()) {
if (previous->hasTagName(pTag)) {
inferred_label = FindChildText((HTMLElement*)previous);
}
}
}
// If we didn't find paragraph, check for previous paragraph to this.
// Eg. <p>Some Text</p> <input ...>
// Note the whitespace between <p> and <input> elements.
if (inferred_label.empty()) {
previous = previous->previousSibling();
if (previous) {
if (previous->hasTagName(pTag)) {
inferred_label = FindChildText((HTMLElement*)previous);
}
}
}
}
// If we didn't find paragraph, check for table cell case.
// Eg. <tr><td>Some Text</td><td><input ...></td></tr>
// Eg. <tr><td><b>Some Text</b></td><td><b><input ...></b></td></tr>
if (inferred_label.empty()) {
Node* parent = element.parentNode();
while (parent &&
!parent->hasTagName(tdTag))
parent = parent->parentNode();
if (parent) {
if (parent->hasTagName(tdTag)) {
previous = parent->previousSibling();
// Skip by any intervening text nodes.
while (previous && previous->isTextNode())
previous = previous->previousSibling();
if (previous) {
if (previous->hasTagName(tdTag)) {
inferred_label = FindChildText((HTMLElement*)previous);
}
}
}
}
}
return inferred_label;
}
}
|