Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: webkit/api/src/WebSearchableFormData.cpp

Issue 385057: Deleted webkit/api which now lives in webkit.org (Closed)
Patch Set: Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webkit/api/src/WebScriptController.cpp ('k') | webkit/api/src/WebSecurityOrigin.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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()->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 for (Vector<HTMLFormControlElement*>::const_iterator i(form->formElements.begin()); i != form->formElements.end(); ++i) {
81 HTMLFormControlElement* formElement = *i;
82 if (formElement->isActivatedSubmit())
83 // There's a button that is already activated for submit, return 0.
84 return 0;
85 if (!firstSubmitButton && formElement->isSuccessfulSubmitButton())
86 firstSubmitButton = formElement;
87 }
88 return firstSubmitButton;
89 }
90
91 // Returns true if the selected state of all the options matches the default
92 // selected state.
93 bool IsSelectInDefaultState(const HTMLSelectElement* select)
94 {
95 const Vector<Element*>& listItems = select->listItems();
96 if (select->multiple() || select->size() > 1) {
97 for (Vector<Element*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
98 if (!(*i)->hasLocalName(HTMLNames::optionTag))
99 continue;
100 const HTMLOptionElement* optionElement = static_cast<const HTMLOptionElement*>(*i);
101 if (optionElement->selected() != optionElement->defaultSelected())
102 return false;
103 }
104 return true;
105 }
106
107 // The select is rendered as a combobox (called menulist in WebKit). At
108 // least one item is selected, determine which one.
109 const HTMLOptionElement* initialSelected = 0;
110 for (Vector<Element*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
111 if (!(*i)->hasLocalName(HTMLNames::optionTag))
112 continue;
113 const HTMLOptionElement* optionElement = static_cast<const HTMLOptionElement*>(*i);
114 if (optionElement->defaultSelected()) {
115 // The page specified the option to select.
116 initialSelected = optionElement;
117 break;
118 }
119 if (!initialSelected)
120 initialSelected = optionElement;
121 }
122 return initialSelected ? initialSelected->selected() : true;
123 }
124
125 // Returns true if the form element is in its default state, false otherwise.
126 // The default state is the state of the form element on initial load of the
127 // page, and varies depending upon the form element. For example, a checkbox is
128 // in its default state if the checked state matches the defaultChecked state.
129 bool IsInDefaultState(const HTMLFormControlElement* formElement)
130 {
131 if (formElement->hasTagName(HTMLNames::inputTag)) {
132 const HTMLInputElement* inputElement = static_cast<const HTMLInputElement*>(formElement);
133 if (inputElement->inputType() == HTMLInputElement::CHECKBOX || inputElement->inputType() == HTMLInputElement::RADIO)
134 return inputElement->checked() == inputElement->defaultChecked();
135 } else if (formElement->hasTagName(HTMLNames::selectTag))
136 return IsSelectInDefaultState(static_cast<const HTMLSelectElement*>(formElement));
137 return true;
138 }
139
140 // If form has only one text input element, return true. If a valid input
141 // element is not found, return false. Additionally, the form data for all
142 // elements is added to enc_string and the encoding used is set in
143 // encoding_name.
144 bool HasSuitableTextElement(const HTMLFormElement* form, Vector<char>* encodedString, String* encodingName)
145 {
146 TextEncoding encoding;
147 GetFormEncoding(form, &encoding);
148 if (!encoding.isValid()) {
149 // Need a valid encoding to encode the form elements.
150 // If the encoding isn't found webkit ends up replacing the params with
151 // empty strings. So, we don't try to do anything here.
152 return 0;
153 }
154 *encodingName = encoding.name();
155
156 HTMLInputElement* textElement = 0;
157 for (Vector<HTMLFormControlElement*>::const_iterator i(form->formElements.begin()); i != form->formElements.end(); ++i) {
158 HTMLFormControlElement* formElement = *i;
159 if (formElement->disabled() || formElement->name().isNull())
160 continue;
161
162 if (!IsInDefaultState(formElement) || formElement->hasTagName(HTMLNames::textareaTag))
163 return 0;
164
165 bool isTextElement = false;
166 if (formElement->hasTagName(HTMLNames::inputTag)) {
167 switch (static_cast<const HTMLInputElement*>(formElement)->inputType()) {
168 case HTMLInputElement::TEXT:
169 case HTMLInputElement::ISINDEX:
170 isTextElement = true;
171 break;
172 case HTMLInputElement::PASSWORD:
173 // Don't store passwords! This is most likely an https anyway.
174 // Fall through.
175 case HTMLInputElement::FILE:
176 // Too big, don't try to index this.
177 return 0;
178 default:
179 // All other input types are indexable.
180 break;
181 }
182 }
183
184 FormDataList dataList(encoding);
185 if (!formElement->appendFormData(dataList, false))
186 continue;
187
188 const Vector<FormDataList::Item>& itemList = dataList.list();
189 if (isTextElement && !itemList.isEmpty()) {
190 if (textElement) {
191 // The auto-complete bar only knows how to fill in one value.
192 // This form has multiple fields; don't treat it as searchable.
193 return false;
194 }
195 textElement = static_cast<HTMLInputElement*>(formElement);
196 }
197 for (Vector<FormDataList::Item>::const_iterator j(itemList.begin()); j != itemList.end(); ++j) {
198 // Handle ISINDEX / <input name=isindex> specially, but only if it's
199 // the first entry.
200 if (!encodedString->isEmpty() || j->data() != "isindex") {
201 if (!encodedString->isEmpty())
202 encodedString->append('&');
203 FormDataBuilder::encodeStringAsFormData(*encodedString, j->data());
204 encodedString->append('=');
205 }
206 ++j;
207 if (formElement == textElement)
208 encodedString->append("{searchTerms}", 13);
209 else
210 FormDataBuilder::encodeStringAsFormData(*encodedString, j->data());
211 }
212 }
213
214 return textElement;
215 }
216
217 } // namespace
218
219 namespace WebKit {
220
221 WebSearchableFormData::WebSearchableFormData(const WebFormElement& form)
222 {
223 RefPtr<HTMLFormElement> formElement = form.operator PassRefPtr<HTMLFormElement>();
224 const Frame* frame = formElement->document()->frame();
225 if (!frame)
226 return;
227
228 // Only consider forms that GET data and the action targets an http page.
229 if (equalIgnoringCase(formElement->getAttribute(HTMLNames::methodAttr), "post") || !IsHTTPFormSubmit(formElement.get()))
230 return;
231
232 HTMLFormControlElement* firstSubmitButton = GetButtonToActivate(formElement.get());
233 if (firstSubmitButton) {
234 // The form does not have an active submit button, make the first button
235 // active. We need to do this, otherwise the URL will not contain the
236 // name of the submit button.
237 firstSubmitButton->setActivatedSubmit(true);
238 }
239 Vector<char> encodedString;
240 String encoding;
241 bool hasElement = HasSuitableTextElement(formElement.get(), &encodedString, &encoding);
242 if (firstSubmitButton)
243 firstSubmitButton->setActivatedSubmit(false);
244 if (!hasElement) {
245 // Not a searchable form.
246 return;
247 }
248
249 String action(formElement->action());
250 KURL url(frame->loader()->completeURL(action.isNull() ? "" : action));
251 RefPtr<FormData> formData = FormData::create(encodedString);
252 url.setQuery(formData->flattenToString());
253 m_url = url;
254 m_encoding = encoding;
255 }
256
257 } // namespace WebKit
OLDNEW
« no previous file with comments | « webkit/api/src/WebScriptController.cpp ('k') | webkit/api/src/WebSecurityOrigin.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698