|
1 /* |
|
2 * Copyright (C) 2009 Google Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions are |
|
6 * met: |
|
7 * |
|
8 * * Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * * Redistributions in binary form must reproduce the above |
|
11 * copyright notice, this list of conditions and the following disclaimer |
|
12 * in the documentation and/or other materials provided with the |
|
13 * distribution. |
|
14 * * Neither the name of Google Inc. nor the names of its |
|
15 * contributors may be used to endorse or promote products derived from |
|
16 * this software without specific prior written permission. |
|
17 * |
|
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 #include "config.h" |
|
32 #include "WebSearchableFormData.h" |
|
33 |
|
34 #include "Document.h" |
|
35 #include "FormDataBuilder.h" |
|
36 #include "FormDataList.h" |
|
37 #include "Frame.h" |
|
38 #include "HTMLFormControlElement.h" |
|
39 #include "HTMLFormElement.h" |
|
40 #include "HTMLInputElement.h" |
|
41 #include "HTMLNames.h" |
|
42 #include "HTMLOptionElement.h" |
|
43 #include "HTMLOptionsCollection.h" |
|
44 #include "HTMLSelectElement.h" |
|
45 #include "TextEncoding.h" |
|
46 #include "WebFormElement.h" |
|
47 |
|
48 using namespace WebCore; |
|
49 |
|
50 namespace { |
|
51 |
|
52 // Gets the encoding for the form. |
|
53 void GetFormEncoding(const HTMLFormElement* form, TextEncoding* encoding) |
|
54 { |
|
55 String str(form->getAttribute(HTMLNames::accept_charsetAttr)); |
|
56 str.replace(',', ' '); |
|
57 Vector<String> charsets; |
|
58 str.split(' ', charsets); |
|
59 for (Vector<String>::const_iterator i(charsets.begin()); i != charsets.end(); ++i) { |
|
60 *encoding = TextEncoding(*i); |
|
61 if (encoding->isValid()) |
|
62 return; |
|
63 } |
|
64 const Frame* frame = form->document()->frame(); |
|
65 *encoding = frame ? TextEncoding(frame->loader()->writer()->encoding()) : Latin1Encoding(); |
|
66 } |
|
67 |
|
68 // Returns true if the submit request results in an HTTP URL. |
|
69 bool IsHTTPFormSubmit(const HTMLFormElement* form) |
|
70 { |
|
71 String action(form->action()); |
|
72 return form->document()->frame()->loader()->completeURL(action.isNull() ? "" : action).protocol() == "http"; |
|
73 } |
|
74 |
|
75 // If the form does not have an activated submit button, the first submit |
|
76 // button is returned. |
|
77 HTMLFormControlElement* GetButtonToActivate(HTMLFormElement* form) |
|
78 { |
|
79 HTMLFormControlElement* firstSubmitButton = 0; |
|
80 // FIXME: Consider refactoring this code so that we don't call form->associatedElements() twice. |
|
81 for (Vector<HTMLFormControlElement*>::const_iterator i(form->associatedElements().begin()); i != form->associatedElements().end(); ++i) { |
|
82 HTMLFormControlElement* formElement = *i; |
|
83 if (formElement->isActivatedSubmit()) |
|
84 // There's a button that is already activated for submit, return 0. |
|
85 return 0; |
|
86 if (!firstSubmitButton && formElement->isSuccessfulSubmitButton()) |
|
87 firstSubmitButton = formElement; |
|
88 } |
|
89 return firstSubmitButton; |
|
90 } |
|
91 |
|
92 // Returns true if the selected state of all the options matches the default |
|
93 // selected state. |
|
94 bool IsSelectInDefaultState(const HTMLSelectElement* select) |
|
95 { |
|
96 const Vector<Element*>& listItems = select->listItems(); |
|
97 if (select->multiple() || select->size() > 1) { |
|
98 for (Vector<Element*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) { |
|
99 if (!(*i)->hasLocalName(HTMLNames::optionTag)) |
|
100 continue; |
|
101 const HTMLOptionElement* optionElement = static_cast<const HTMLOptionElement*>(*i); |
|
102 if (optionElement->selected() != optionElement->defaultSelected()) |
|
103 return false; |
|
104 } |
|
105 return true; |
|
106 } |
|
107 |
|
108 // The select is rendered as a combobox (called menulist in WebKit). At |
|
109 // least one item is selected, determine which one. |
|
110 const HTMLOptionElement* initialSelected = 0; |
|
111 for (Vector<Element*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) { |
|
112 if (!(*i)->hasLocalName(HTMLNames::optionTag)) |
|
113 continue; |
|
114 const HTMLOptionElement* optionElement = static_cast<const HTMLOptionElement*>(*i); |
|
115 if (optionElement->defaultSelected()) { |
|
116 // The page specified the option to select. |
|
117 initialSelected = optionElement; |
|
118 break; |
|
119 } |
|
120 if (!initialSelected) |
|
121 initialSelected = optionElement; |
|
122 } |
|
123 return initialSelected ? initialSelected->selected() : true; |
|
124 } |
|
125 |
|
126 // Returns true if the form element is in its default state, false otherwise. |
|
127 // The default state is the state of the form element on initial load of the |
|
128 // page, and varies depending upon the form element. For example, a checkbox is |
|
129 // in its default state if the checked state matches the defaultChecked state. |
|
130 bool IsInDefaultState(const HTMLFormControlElement* formElement) |
|
131 { |
|
132 if (formElement->hasTagName(HTMLNames::inputTag)) { |
|
133 const HTMLInputElement* inputElement = static_cast<const HTMLInputElement*>(formElement); |
|
134 if (inputElement->inputType() == HTMLInputElement::CHECKBOX || inputElement->inputType() == HTMLInputElement::RADIO) |
|
135 return inputElement->checked() == inputElement->defaultChecked(); |
|
136 } else if (formElement->hasTagName(HTMLNames::selectTag)) |
|
137 return IsSelectInDefaultState(static_cast<const HTMLSelectElement*>(formElement)); |
|
138 return true; |
|
139 } |
|
140 |
|
141 // If form has only one text input element, return true. If a valid input |
|
142 // element is not found, return false. Additionally, the form data for all |
|
143 // elements is added to enc_string and the encoding used is set in |
|
144 // encoding_name. |
|
145 bool HasSuitableTextElement(const HTMLFormElement* form, Vector<char>* encodedString, String* encodingName) |
|
146 { |
|
147 TextEncoding encoding; |
|
148 GetFormEncoding(form, &encoding); |
|
149 if (!encoding.isValid()) { |
|
150 // Need a valid encoding to encode the form elements. |
|
151 // If the encoding isn't found webkit ends up replacing the params with |
|
152 // empty strings. So, we don't try to do anything here. |
|
153 return 0; |
|
154 } |
|
155 *encodingName = encoding.name(); |
|
156 |
|
157 HTMLInputElement* textElement = 0; |
|
158 // FIXME: Consider refactoring this code so that we don't call form->associatedElements() twice. |
|
159 for (Vector<HTMLFormControlElement*>::const_iterator i(form->associatedElements().begin()); i != form->associatedElements().end(); ++i) { |
|
160 HTMLFormControlElement* formElement = *i; |
|
161 if (formElement->disabled() || formElement->name().isNull()) |
|
162 continue; |
|
163 |
|
164 if (!IsInDefaultState(formElement) || formElement->hasTagName(HTMLNames::textareaTag)) |
|
165 return 0; |
|
166 |
|
167 bool isTextElement = false; |
|
168 if (formElement->hasTagName(HTMLNames::inputTag)) { |
|
169 switch (static_cast<const HTMLInputElement*>(formElement)->inputType()) { |
|
170 case HTMLInputElement::TEXT: |
|
171 case HTMLInputElement::ISINDEX: |
|
172 isTextElement = true; |
|
173 break; |
|
174 case HTMLInputElement::PASSWORD: |
|
175 // Don't store passwords! This is most likely an https anyway. |
|
176 // Fall through. |
|
177 case HTMLInputElement::FILE: |
|
178 // Too big, don't try to index this. |
|
179 return 0; |
|
180 default: |
|
181 // All other input types are indexable. |
|
182 break; |
|
183 } |
|
184 } |
|
185 |
|
186 FormDataList dataList(encoding); |
|
187 if (!formElement->appendFormData(dataList, false)) |
|
188 continue; |
|
189 |
|
190 const BlobItemList& items = dataList.items(); |
|
191 if (isTextElement && !items.isEmpty()) { |
|
192 if (textElement) { |
|
193 // The auto-complete bar only knows how to fill in one value. |
|
194 // This form has multiple fields; don't treat it as searchable. |
|
195 return false; |
|
196 } |
|
197 textElement = static_cast<HTMLInputElement*>(formElement); |
|
198 } |
|
199 for (BlobItemList::const_iterator j(items.begin()); j != items.end(); ++j) { |
|
200 const StringBlobItem* item = (*j)->toStringBlobItem(); |
|
201 ASSERT(item); |
|
202 // Handle ISINDEX / <input name=isindex> specially, but only if it's |
|
203 // the first entry. |
|
204 if (!encodedString->isEmpty() || item->cstr() != "isindex") { |
|
205 if (!encodedString->isEmpty()) |
|
206 encodedString->append('&'); |
|
207 FormDataBuilder::encodeStringAsFormData(*encodedString, item->cstr()); |
|
208 encodedString->append('='); |
|
209 } |
|
210 ++j; |
|
211 if (formElement == textElement) |
|
212 encodedString->append("{searchTerms}", 13); |
|
213 else |
|
214 FormDataBuilder::encodeStringAsFormData(*encodedString, item->cstr()); |
|
215 } |
|
216 } |
|
217 |
|
218 return textElement; |
|
219 } |
|
220 |
|
221 } // namespace |
|
222 |
|
223 namespace WebKit { |
|
224 |
|
225 WebSearchableFormData::WebSearchableFormData(const WebFormElement& form) |
|
226 { |
|
227 RefPtr<HTMLFormElement> formElement = form.operator PassRefPtr<HTMLFormElement>(); |
|
228 const Frame* frame = formElement->document()->frame(); |
|
229 if (!frame) |
|
230 return; |
|
231 |
|
232 // Only consider forms that GET data and the action targets an http page. |
|
233 if (equalIgnoringCase(formElement->getAttribute(HTMLNames::methodAttr), "post") || !IsHTTPFormSubmit(formElement.get())) |
|
234 return; |
|
235 |
|
236 HTMLFormControlElement* firstSubmitButton = GetButtonToActivate(formElement.get()); |
|
237 if (firstSubmitButton) { |
|
238 // The form does not have an active submit button, make the first button |
|
239 // active. We need to do this, otherwise the URL will not contain the |
|
240 // name of the submit button. |
|
241 firstSubmitButton->setActivatedSubmit(true); |
|
242 } |
|
243 Vector<char> encodedString; |
|
244 String encoding; |
|
245 bool hasElement = HasSuitableTextElement(formElement.get(), &encodedString, &encoding); |
|
246 if (firstSubmitButton) |
|
247 firstSubmitButton->setActivatedSubmit(false); |
|
248 if (!hasElement) { |
|
249 // Not a searchable form. |
|
250 return; |
|
251 } |
|
252 |
|
253 String action(formElement->action()); |
|
254 KURL url(frame->loader()->completeURL(action.isNull() ? "" : action)); |
|
255 RefPtr<FormData> formData = FormData::create(encodedString); |
|
256 url.setQuery(formData->flattenToString()); |
|
257 m_url = url; |
|
258 m_encoding = encoding; |
|
259 } |
|
260 |
|
261 } // namespace WebKit |