| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 if (formElement->hasTagName(HTMLNames::inputTag)) { | 142 if (formElement->hasTagName(HTMLNames::inputTag)) { |
| 143 const HTMLInputElement* inputElement = toHTMLInputElement(formElement); | 143 const HTMLInputElement* inputElement = toHTMLInputElement(formElement); |
| 144 if (inputElement->isCheckbox() || inputElement->isRadioButton()) | 144 if (inputElement->isCheckbox() || inputElement->isRadioButton()) |
| 145 return inputElement->checked() == inputElement->hasAttribute(checked
Attr); | 145 return inputElement->checked() == inputElement->hasAttribute(checked
Attr); |
| 146 } else if (formElement->hasTagName(HTMLNames::selectTag)) { | 146 } else if (formElement->hasTagName(HTMLNames::selectTag)) { |
| 147 return IsSelectInDefaultState(toHTMLSelectElement(formElement)); | 147 return IsSelectInDefaultState(toHTMLSelectElement(formElement)); |
| 148 } | 148 } |
| 149 return true; | 149 return true; |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Look for a suitable search text field in a given HTMLFormElement | 152 // Look for a suitable search text field in a given HTMLFormElement |
| 153 // Return nothing if one of those items are found: | 153 // Return nothing if one of those items are found: |
| 154 // - A text area field | 154 // - A text area field |
| 155 // - A file upload field | 155 // - A file upload field |
| 156 // - A Password field | 156 // - A Password field |
| 157 // - More than one text field | 157 // - More than one text field |
| 158 HTMLInputElement* findSuitableSearchInputElement(const HTMLFormElement* form) | 158 HTMLInputElement* findSuitableSearchInputElement(const HTMLFormElement* form) |
| 159 { | 159 { |
| 160 HTMLInputElement* textElement = 0; | 160 HTMLInputElement* textElement = 0; |
| 161 // FIXME: Consider refactoring this code so that we don't call form->associa
tedElements() twice. | 161 // FIXME: Consider refactoring this code so that we don't call form->associa
tedElements() twice. |
| 162 for (Vector<FormAssociatedElement*>::const_iterator i(form->associatedElemen
ts().begin()); i != form->associatedElements().end(); ++i) { | 162 for (Vector<FormAssociatedElement*>::const_iterator i(form->associatedElemen
ts().begin()); i != form->associatedElements().end(); ++i) { |
| 163 if (!(*i)->isFormControlElement()) | 163 if (!(*i)->isFormControlElement()) |
| 164 continue; | 164 continue; |
| 165 | 165 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 185 return 0; | 185 return 0; |
| 186 } | 186 } |
| 187 textElement = toHTMLInputElement(control); | 187 textElement = toHTMLInputElement(control); |
| 188 } | 188 } |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 return textElement; | 191 return textElement; |
| 192 } | 192 } |
| 193 | 193 |
| 194 // Build a search string based on a given HTMLFormElement and HTMLInputElement | 194 // Build a search string based on a given HTMLFormElement and HTMLInputElement |
| 195 // | 195 // |
| 196 // Search string output example from www.google.com: | 196 // Search string output example from www.google.com: |
| 197 // "hl=en&source=hp&biw=1085&bih=854&q={searchTerms}&btnG=Google+Search&aq=f&aqi
=&aql=&oq=" | 197 // "hl=en&source=hp&biw=1085&bih=854&q={searchTerms}&btnG=Google+Search&aq=f&aqi
=&aql=&oq=" |
| 198 // | 198 // |
| 199 // Return false if the provided HTMLInputElement is not found in the form | 199 // Return false if the provided HTMLInputElement is not found in the form |
| 200 bool buildSearchString(const HTMLFormElement* form, Vector<char>* encodedString,
WTF::TextEncoding* encoding, const HTMLInputElement* textElement) | 200 bool buildSearchString(const HTMLFormElement* form, Vector<char>* encodedString,
WTF::TextEncoding* encoding, const HTMLInputElement* textElement) |
| 201 { | 201 { |
| 202 bool isElementFound = false; | 202 bool isElementFound = false; |
| 203 | 203 |
| 204 // FIXME: Consider refactoring this code so that we don't call form->associa
tedElements() twice. | 204 // FIXME: Consider refactoring this code so that we don't call form->associa
tedElements() twice. |
| 205 for (Vector<FormAssociatedElement*>::const_iterator i(form->associatedElemen
ts().begin()); i != form->associatedElements().end(); ++i) { | 205 for (Vector<FormAssociatedElement*>::const_iterator i(form->associatedElemen
ts().begin()); i != form->associatedElements().end(); ++i) { |
| 206 if (!(*i)->isFormControlElement()) | 206 if (!(*i)->isFormControlElement()) |
| 207 continue; | 207 continue; |
| 208 | 208 |
| 209 HTMLFormControlElement* control = toHTMLFormControlElement(*i); | 209 HTMLFormControlElement* control = toHTMLFormControlElement(*i); |
| 210 | 210 |
| 211 if (control->isDisabledFormControl() || control->name().isNull()) | 211 if (control->isDisabledFormControl() || control->name().isNull()) |
| 212 continue; | 212 continue; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 239 } // namespace | 239 } // namespace |
| 240 | 240 |
| 241 namespace WebKit { | 241 namespace WebKit { |
| 242 | 242 |
| 243 WebSearchableFormData::WebSearchableFormData(const WebFormElement& form, const W
ebInputElement& selectedInputElement) | 243 WebSearchableFormData::WebSearchableFormData(const WebFormElement& form, const W
ebInputElement& selectedInputElement) |
| 244 { | 244 { |
| 245 RefPtr<HTMLFormElement> formElement = form.operator PassRefPtr<HTMLFormEleme
nt>(); | 245 RefPtr<HTMLFormElement> formElement = form.operator PassRefPtr<HTMLFormEleme
nt>(); |
| 246 HTMLInputElement* inputElement = selectedInputElement.operator PassRefPtr<HT
MLInputElement>().get(); | 246 HTMLInputElement* inputElement = selectedInputElement.operator PassRefPtr<HT
MLInputElement>().get(); |
| 247 | 247 |
| 248 // Only consider forms that GET data. | 248 // Only consider forms that GET data. |
| 249 // Allow HTTPS only when an input element is provided. | 249 // Allow HTTPS only when an input element is provided. |
| 250 if (equalIgnoringCase(formElement->getAttribute(methodAttr), "post") | 250 if (equalIgnoringCase(formElement->getAttribute(methodAttr), "post") |
| 251 || (!IsHTTPFormSubmit(formElement.get()) && !inputElement)) | 251 || (!IsHTTPFormSubmit(formElement.get()) && !inputElement)) |
| 252 return; | 252 return; |
| 253 | 253 |
| 254 Vector<char> encodedString; | 254 Vector<char> encodedString; |
| 255 WTF::TextEncoding encoding; | 255 WTF::TextEncoding encoding; |
| 256 | 256 |
| 257 GetFormEncoding(formElement.get(), &encoding); | 257 GetFormEncoding(formElement.get(), &encoding); |
| 258 if (!encoding.isValid()) { | 258 if (!encoding.isValid()) { |
| 259 // Need a valid encoding to encode the form elements. | 259 // Need a valid encoding to encode the form elements. |
| 260 // If the encoding isn't found webkit ends up replacing the params with | 260 // If the encoding isn't found webkit ends up replacing the params with |
| 261 // empty strings. So, we don't try to do anything here. | 261 // empty strings. So, we don't try to do anything here. |
| 262 return; | 262 return; |
| 263 } | 263 } |
| 264 | 264 |
| 265 // Look for a suitable search text field in the form when a | 265 // Look for a suitable search text field in the form when a |
| 266 // selectedInputElement is not provided. | 266 // selectedInputElement is not provided. |
| 267 if (!inputElement) { | 267 if (!inputElement) { |
| 268 inputElement = findSuitableSearchInputElement(formElement.get()); | 268 inputElement = findSuitableSearchInputElement(formElement.get()); |
| 269 | 269 |
| 270 // Return if no suitable text element has been found. | 270 // Return if no suitable text element has been found. |
| 271 if (!inputElement) | 271 if (!inputElement) |
| 272 return; | 272 return; |
| 273 } | 273 } |
| 274 | 274 |
| 275 HTMLFormControlElement* firstSubmitButton = GetButtonToActivate(formElement.
get()); | 275 HTMLFormControlElement* firstSubmitButton = GetButtonToActivate(formElement.
get()); |
| 276 if (firstSubmitButton) { | 276 if (firstSubmitButton) { |
| 277 // The form does not have an active submit button, make the first button | 277 // The form does not have an active submit button, make the first button |
| 278 // active. We need to do this, otherwise the URL will not contain the | 278 // active. We need to do this, otherwise the URL will not contain the |
| 279 // name of the submit button. | 279 // name of the submit button. |
| 280 firstSubmitButton->setActivatedSubmit(true); | 280 firstSubmitButton->setActivatedSubmit(true); |
| 281 } | 281 } |
| 282 | 282 |
| 283 bool isValidSearchString = buildSearchString(formElement.get(), &encodedStri
ng, &encoding, inputElement); | 283 bool isValidSearchString = buildSearchString(formElement.get(), &encodedStri
ng, &encoding, inputElement); |
| 284 | 284 |
| 285 if (firstSubmitButton) | 285 if (firstSubmitButton) |
| 286 firstSubmitButton->setActivatedSubmit(false); | 286 firstSubmitButton->setActivatedSubmit(false); |
| 287 | 287 |
| 288 // Return if the search string is not valid. | 288 // Return if the search string is not valid. |
| 289 if (!isValidSearchString) | 289 if (!isValidSearchString) |
| 290 return; | 290 return; |
| 291 | 291 |
| 292 String action(formElement->action()); | 292 String action(formElement->action()); |
| 293 KURL url(formElement->document()->completeURL(action.isNull() ? "" : action)
); | 293 KURL url(formElement->document()->completeURL(action.isNull() ? "" : action)
); |
| 294 RefPtr<FormData> formData = FormData::create(encodedString); | 294 RefPtr<FormData> formData = FormData::create(encodedString); |
| 295 url.setQuery(formData->flattenToString()); | 295 url.setQuery(formData->flattenToString()); |
| 296 m_url = url; | 296 m_url = url; |
| 297 m_encoding = String(encoding.name()); | 297 m_encoding = String(encoding.name()); |
| 298 } | 298 } |
| 299 | 299 |
| 300 } // namespace WebKit | 300 } // namespace WebKit |
| OLD | NEW |