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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
* (C) 2006 Alexey Proskuryakov (ap@nypop.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"
#include "HTMLFormElement.h"
#include "CSSHelper.h"
#include "ChromeClient.h"
#include "Document.h"
#include "Event.h"
#include "EventNames.h"
#include "FileSystem.h"
#include "FormData.h"
#include "FormDataList.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "HTMLDocument.h"
#include "HTMLFormCollection.h"
#include "HTMLImageElement.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "MIMETypeRegistry.h"
#include "Page.h"
#include "RenderTextControl.h"
#if PLATFORM(QT)
#include <QtCore/QFileInfo>
#endif
#if PLATFORM(WX)
#include <wx/defs.h>
#include <wx/filename.h>
#endif
#if PLATFORM(WIN_OS)
#include <shlwapi.h>
#endif
namespace WebCore {
using namespace EventNames;
using namespace HTMLNames;
static const char hexDigits[17] = "0123456789ABCDEF";
HTMLFormElement::HTMLFormElement(Document* doc)
: HTMLElement(formTag, doc)
, m_elementAliases(0)
, collectionInfo(0)
, m_enctype("application/x-www-form-urlencoded")
, m_post(false)
, m_multipart(false)
, m_autocomplete(true)
, m_insubmit(false)
, m_doingsubmit(false)
, m_inreset(false)
, m_malformed(false)
{
}
HTMLFormElement::~HTMLFormElement()
{
delete m_elementAliases;
delete collectionInfo;
for (unsigned i = 0; i < formElements.size(); ++i)
formElements[i]->formDestroyed();
for (unsigned i = 0; i < imgElements.size(); ++i)
imgElements[i]->m_form = 0;
}
bool HTMLFormElement::formWouldHaveSecureSubmission(const String& url)
{
return document()->completeURL(url).protocolIs("https");
}
void HTMLFormElement::attach()
{
HTMLElement::attach();
}
void HTMLFormElement::insertedIntoDocument()
{
if (document()->isHTMLDocument())
static_cast<HTMLDocument*>(document())->addNamedItem(m_name);
HTMLElement::insertedIntoDocument();
}
void HTMLFormElement::removedFromDocument()
{
if (document()->isHTMLDocument())
static_cast<HTMLDocument*>(document())->removeNamedItem(m_name);
HTMLElement::removedFromDocument();
}
void HTMLFormElement::handleLocalEvents(Event* event, bool useCapture)
{
EventTargetNode* targetNode = event->target()->toNode();
if (!useCapture && targetNode && targetNode != this && (event->type() == submitEvent || event->type() == resetEvent)) {
event->stopPropagation();
return;
}
HTMLElement::handleLocalEvents(event, useCapture);
}
unsigned HTMLFormElement::length() const
{
int len = 0;
for (unsigned i = 0; i < formElements.size(); ++i)
if (formElements[i]->isEnumeratable())
++len;
return len;
}
Node* HTMLFormElement::item(unsigned index)
{
return elements()->item(index);
}
void HTMLFormElement::submitClick(Event* event)
{
bool submitFound = false;
for (unsigned i = 0; i < formElements.size(); ++i) {
if (formElements[i]->hasLocalName(inputTag)) {
HTMLInputElement* element = static_cast<HTMLInputElement*>(formElements[i]);
if (element->isSuccessfulSubmitButton() && element->renderer()) {
submitFound = true;
element->dispatchSimulatedClick(event);
break;
}
}
}
if (!submitFound) // submit the form without a submit or image input
prepareSubmit(event);
}
static void appendString(Vector<char>& buffer, const char* string)
{
buffer.append(string, strlen(string));
}
static void appendString(Vector<char>& buffer, const CString& string)
{
buffer.append(string.data(), string.length());
}
static void appendEncodedString(Vector<char>& buffer, const CString& string)
{
// http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
int length = string.length();
for (int i = 0; i < length; i++) {
unsigned char c = string.data()[i];
// Same safe characters as Netscape for compatibility.
static const char safe[] = "-._*";
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || strchr(safe, c))
buffer.append(c);
else if (c == ' ')
buffer.append('+');
else if (c == '\n' || (c == '\r' && (i + 1 >= length || string.data()[i + 1] != '\n')))
appendString(buffer, "%0D%0A");
else if (c != '\r') {
buffer.append('%');
buffer.append(hexDigits[c >> 4]);
buffer.append(hexDigits[c & 0xF]);
}
}
}
// FIXME: Move to platform directory?
static int randomNumber()
{
static bool randomSeeded = false;
#if PLATFORM(DARWIN)
if (!randomSeeded) {
srandomdev();
randomSeeded = true;
}
return random();
#else
if (!randomSeeded) {
srand(static_cast<unsigned>(time(0)));
randomSeeded = true;
}
return rand();
#endif
}
TextEncoding HTMLFormElement::dataEncoding() const
{
if (isMailtoForm())
return UTF8Encoding();
TextEncoding encoding;
String str = m_acceptcharset;
str.replace(',', ' ');
Vector<String> charsets;
str.split(' ', charsets);
Vector<String>::const_iterator end = charsets.end();
for (Vector<String>::const_iterator it = charsets.begin(); it != end; ++it)
if ((encoding = TextEncoding(*it)).isValid())
return encoding;
if (Frame* frame = document()->frame())
return frame->loader()->encoding();
return Latin1Encoding();
}
PassRefPtr<FormData> HTMLFormElement::formData(const char* boundary) const
{
Vector<char> encodedData;
TextEncoding encoding = dataEncoding();
RefPtr<FormData> result = FormData::create();
for (unsigned i = 0; i < formElements.size(); ++i) {
HTMLFormControlElement* control = formElements[i];
FormDataList list(encoding);
if (!control->disabled() && control->appendFormData(list, m_multipart)) {
size_t ln = list.list().size();
for (size_t j = 0; j < ln; ++j) {
const FormDataListItem& item = list.list()[j];
if (!m_multipart) {
// Omit the name "isindex" if it's the first form data element.
// FIXME: Why is this a good rule? Is this obsolete now?
if (encodedData.isEmpty() && item.m_data == "isindex")
appendEncodedString(encodedData, list.list()[++j].m_data);
else {
if (!encodedData.isEmpty())
encodedData.append('&');
appendEncodedString(encodedData, item.m_data);
encodedData.append('=');
appendEncodedString(encodedData, list.list()[++j].m_data);
}
} else {
Vector<char> header;
appendString(header, "--");
appendString(header, boundary);
appendString(header, "\r\n");
appendString(header, "Content-Disposition: form-data; name=\"");
header.append(item.m_data.data(), item.m_data.length());
header.append('"');
bool shouldGenerateFile = false;
// if the current type is FILE, then we also need to
// include the filename
if (control->hasLocalName(inputTag)
&& static_cast<HTMLInputElement*>(control)->inputType() == HTMLInputElement::FILE) {
String path = static_cast<HTMLInputElement*>(control)->value();
String filename = pathGetFileName(path);
// Let the application specify a filename if its going to generate a replacement file for the upload
if (Page* page = document()->page()) {
String generatedFilename;
shouldGenerateFile = page->chrome()->client()->shouldReplaceWithGeneratedFileForUpload(path, generatedFilename);
if (shouldGenerateFile)
filename = generatedFilename;
}
// FIXME: This won't work if the filename includes a " mark,
// or control characters like CR or LF. This also does strange
// things if the filename includes characters you can't encode
// in the website's character set.
appendString(header, "; filename=\"");
appendString(header, encoding.encode(filename.characters(), filename.length(), QuestionMarksForUnencodables));
header.append('"');
if (!filename.isEmpty()) {
String mimeType = MIMETypeRegistry::getMIMETypeForPath(filename);
if (!mimeType.isEmpty()) {
appendString(header, "\r\nContent-Type: ");
appendString(header, mimeType.latin1());
}
}
}
appendString(header, "\r\n\r\n");
// append body
result->appendData(header.data(), header.size());
const FormDataListItem& item = list.list()[j + 1];
if (size_t dataSize = item.m_data.length())
result->appendData(item.m_data.data(), dataSize);
else if (!item.m_path.isEmpty())
result->appendFile(item.m_path, shouldGenerateFile);
result->appendData("\r\n", 2);
++j;
}
}
}
}
if (m_multipart) {
appendString(encodedData, "--");
appendString(encodedData, boundary);
appendString(encodedData, "--\r\n");
}
result->appendData(encodedData.data(), encodedData.size());
return result;
}
void HTMLFormElement::parseEnctype(const String& type)
{
if(type.contains("multipart", false) || type.contains("form-data", false)) {
m_enctype = "multipart/form-data";
m_multipart = true;
} else if (type.contains("text", false) || type.contains("plain", false)) {
m_enctype = "text/plain";
m_multipart = false;
} else {
m_enctype = "application/x-www-form-urlencoded";
m_multipart = false;
}
}
bool HTMLFormElement::isMailtoForm() const
{
return protocolIs(m_url, "mailto");
}
bool HTMLFormElement::prepareSubmit(Event* event)
{
Frame* frame = document()->frame();
if (m_insubmit || !frame)
return m_insubmit;
m_insubmit = true;
m_doingsubmit = false;
if (dispatchHTMLEvent(submitEvent, true, true) && !m_doingsubmit)
m_doingsubmit = true;
m_insubmit = false;
if (m_doingsubmit)
submit(event, true);
return m_doingsubmit;
}
void HTMLFormElement::submit()
{
submit(0, false);
}
// Returns a 0-terminated C string in the vector.
static void getUniqueBoundaryString(Vector<char>& boundary)
{
// The RFC 2046 spec says the AlphaNumeric characters plus the following characters
// are legal for boundaries: '()+_,-./:=?
// However the following characters, though legal, cause some sites to fail:
// (),./:=
// http://bugs.webkit.org/show_bug.cgi?id=13352
static const char AlphaNumericEncMap[64] =
{
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x41
// FIXME <rdar://problem/5252577> gmail does not accept legal characters in the form boundary
// As stated above, some legal characters cause, sites to fail. Specifically
// the / character which was the last character in the above array. I have
// replaced the last character with another character already in the array
// (notice the first and last values are both 0x41, A). Instead of picking
// another unique legal character for boundary strings that, because it has
// never been tested, may or may not break other sites, I simply
// replaced / with A. This means A is twice as likely to occur in our boundary
// strings than any other character but I think this is fine for the time being.
// The FIXME here is about restoring the / character once the aforementioned
// radar has been resolved.
};
// Start with an informative prefix.
const char boundaryPrefix[] = "----WebKitFormBoundary";
boundary.append(boundaryPrefix, strlen(boundaryPrefix));
// Append 16 random 7bit ascii AlphaNumeric characters.
Vector<char> randomBytes;
for (int i = 0; i < 4; ++i) {
int randomness = randomNumber();
randomBytes.append(AlphaNumericEncMap[(randomness >> 24) & 0x3F]);
randomBytes.append(AlphaNumericEncMap[(randomness >> 16) & 0x3F]);
randomBytes.append(AlphaNumericEncMap[(randomness >> 8) & 0x3F]);
randomBytes.append(AlphaNumericEncMap[randomness & 0x3F]);
}
boundary.append(randomBytes);
boundary.append(0); // Add a 0 at the end so we can use this as a C-style string.
}
void HTMLFormElement::submit(Event* event, bool activateSubmitButton)
{
FrameView* view = document()->view();
Frame* frame = document()->frame();
if (!view || !frame)
return;
if (m_insubmit) {
m_doingsubmit = true;
return;
}
m_insubmit = true;
HTMLFormControlElement* firstSuccessfulSubmitButton = 0;
bool needButtonActivation = activateSubmitButton; // do we need to activate a submit button?
frame->loader()->clearRecordedFormValues();
for (unsigned i = 0; i < formElements.size(); ++i) {
HTMLFormControlElement* control = formElements[i];
if (control->hasLocalName(inputTag)) {
HTMLInputElement* input = static_cast<HTMLInputElement*>(control);
if (input->isTextField()) {
frame->loader()->recordFormValue(input->name(), input->value(), this);
if (input->isSearchField())
input->addSearchResult();
}
}
if (needButtonActivation) {
if (control->isActivatedSubmit())
needButtonActivation = false;
else if (firstSuccessfulSubmitButton == 0 && control->isSuccessfulSubmitButton())
firstSuccessfulSubmitButton = control;
}
}
if (needButtonActivation && firstSuccessfulSubmitButton)
firstSuccessfulSubmitButton->setActivatedSubmit(true);
if (!m_url)
m_url = document()->url().string();
if (m_post) {
if (m_multipart && isMailtoForm()) {
setEnctype("application/x-www-form-urlencoded");
m_multipart = false;
}
if (!m_multipart) {
RefPtr<FormData> data = formData(0);
if (isMailtoForm()) {
String body = data->flattenToString();
if (equalIgnoringCase(enctype(), "text/plain")) {
// Convention seems to be to decode, and s/&/\r\n/. Also, spaces are encoded as %20.
body = decodeURLEscapeSequences(body.replace('&', "\r\n").replace('+', ' ') + "\r\n");
}
Vector<char> bodyData;
appendString(bodyData, "body=");
appendEncodedString(bodyData, body.utf8());
data = FormData::create(String(bodyData.data(), bodyData.size()).replace('+', "%20").latin1());
}
frame->loader()->submitForm("POST", m_url, data, m_target, enctype(), String(), event);
} else {
Vector<char> boundary;
getUniqueBoundaryString(boundary);
frame->loader()->submitForm("POST", m_url, formData(boundary.data()), m_target, enctype(), boundary.data(), event);
}
} else {
m_multipart = false;
frame->loader()->submitForm("GET", m_url, formData(0), m_target, String(), String(), event);
}
if (needButtonActivation && firstSuccessfulSubmitButton)
firstSuccessfulSubmitButton->setActivatedSubmit(false);
m_doingsubmit = m_insubmit = false;
}
void HTMLFormElement::reset()
{
Frame* frame = document()->frame();
if (m_inreset || !frame)
return;
m_inreset = true;
// ### DOM2 labels this event as not cancelable, however
// common browsers( sick! ) allow it be cancelled.
if ( !dispatchHTMLEvent(resetEvent,true, true) ) {
m_inreset = false;
return;
}
for (unsigned i = 0; i < formElements.size(); ++i)
formElements[i]->reset();
m_inreset = false;
}
void HTMLFormElement::parseMappedAttribute(MappedAttribute* attr)
{
if (attr->name() == actionAttr)
m_url = parseURL(attr->value());
else if (attr->name() == targetAttr)
m_target = attr->value();
else if (attr->name() == methodAttr) {
if (equalIgnoringCase(attr->value(), "post"))
m_post = true;
else if (equalIgnoringCase(attr->value(), "get"))
m_post = false;
} else if (attr->name() == enctypeAttr)
parseEnctype(attr->value());
else if (attr->name() == accept_charsetAttr)
// space separated list of charsets the server
// accepts - see rfc2045
m_acceptcharset = attr->value();
else if (attr->name() == acceptAttr) {
// ignore this one for the moment...
} else if (attr->name() == autocompleteAttr)
m_autocomplete = !equalIgnoringCase(attr->value(), "off");
else if (attr->name() == onsubmitAttr)
setHTMLEventListener(submitEvent, attr);
else if (attr->name() == onresetAttr)
setHTMLEventListener(resetEvent, attr);
else if (attr->name() == nameAttr) {
const AtomicString& newName = attr->value();
if (inDocument() && document()->isHTMLDocument()) {
HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
document->removeNamedItem(m_name);
document->addNamedItem(newName);
}
m_name = newName;
} else
HTMLElement::parseMappedAttribute(attr);
}
template<class T, size_t n> static void removeFromVector(Vector<T*, n> & vec, T* item)
{
size_t size = vec.size();
for (size_t i = 0; i != size; ++i)
if (vec[i] == item) {
vec.remove(i);
break;
}
}
unsigned HTMLFormElement::formElementIndex(HTMLFormControlElement* e)
{
// Check for the special case where this element is the very last thing in
// the form's tree of children; we don't want to walk the entire tree in that
// common case that occurs during parsing; instead we'll just return a value
// that says "add this form element to the end of the array".
if (e->traverseNextNode(this)) {
unsigned i = 0;
for (Node* node = this; node; node = node->traverseNextNode(this)) {
if (node == e)
return i;
if (node->isHTMLElement()
&& static_cast<HTMLElement*>(node)->isGenericFormElement()
&& static_cast<HTMLFormControlElement*>(node)->form() == this)
++i;
}
}
return formElements.size();
}
void HTMLFormElement::registerFormElement(HTMLFormControlElement* e)
{
document()->checkedRadioButtons().removeButton(e);
m_checkedRadioButtons.addButton(e);
formElements.insert(formElementIndex(e), e);
}
void HTMLFormElement::removeFormElement(HTMLFormControlElement* e)
{
m_checkedRadioButtons.removeButton(e);
removeFromVector(formElements, e);
}
bool HTMLFormElement::isURLAttribute(Attribute* attr) const
{
return attr->name() == actionAttr;
}
void HTMLFormElement::registerImgElement(HTMLImageElement* e)
{
imgElements.append(e);
}
void HTMLFormElement::removeImgElement(HTMLImageElement* e)
{
removeFromVector(imgElements, e);
}
PassRefPtr<HTMLCollection> HTMLFormElement::elements()
{
return HTMLFormCollection::create(this);
}
String HTMLFormElement::name() const
{
return getAttribute(nameAttr);
}
void HTMLFormElement::setName(const String &value)
{
setAttribute(nameAttr, value);
}
String HTMLFormElement::acceptCharset() const
{
return getAttribute(accept_charsetAttr);
}
void HTMLFormElement::setAcceptCharset(const String &value)
{
setAttribute(accept_charsetAttr, value);
}
String HTMLFormElement::action() const
{
return getAttribute(actionAttr);
}
void HTMLFormElement::setAction(const String &value)
{
setAttribute(actionAttr, value);
}
void HTMLFormElement::setEnctype(const String &value)
{
setAttribute(enctypeAttr, value);
}
String HTMLFormElement::method() const
{
return getAttribute(methodAttr);
}
void HTMLFormElement::setMethod(const String &value)
{
setAttribute(methodAttr, value);
}
String HTMLFormElement::target() const
{
return getAttribute(targetAttr);
}
void HTMLFormElement::setTarget(const String &value)
{
setAttribute(targetAttr, value);
}
PassRefPtr<HTMLFormControlElement> HTMLFormElement::elementForAlias(const AtomicString& alias)
{
if (alias.isEmpty() || !m_elementAliases)
return 0;
return m_elementAliases->get(alias.impl());
}
void HTMLFormElement::addElementAlias(HTMLFormControlElement* element, const AtomicString& alias)
{
if (alias.isEmpty())
return;
if (!m_elementAliases)
m_elementAliases = new AliasMap;
m_elementAliases->set(alias.impl(), element);
}
void HTMLFormElement::getNamedElements(const AtomicString& name, Vector<RefPtr<Node> >& namedItems)
{
elements()->namedItems(name, namedItems);
// see if we have seen something with this name before
RefPtr<HTMLFormControlElement> aliasElem;
if (aliasElem = elementForAlias(name)) {
bool found = false;
for (unsigned n = 0; n < namedItems.size(); n++) {
if (namedItems[n] == aliasElem.get()) {
found = true;
break;
}
}
if (!found)
// we have seen it before but it is gone now. still, we need to return it.
namedItems.append(aliasElem.get());
}
// name has been accessed, remember it
if (namedItems.size() && aliasElem != namedItems.first())
addElementAlias(static_cast<HTMLFormControlElement*>(namedItems.first().get()), name);
}
void HTMLFormElement::CheckedRadioButtons::addButton(HTMLFormControlElement* element)
{
// We only want to add radio buttons.
if (!element->isRadioButton())
return;
// Without a name, there is no group.
if (element->name().isEmpty())
return;
HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(element);
// We only track checked buttons.
if (!inputElement->checked())
return;
if (!m_nameToCheckedRadioButtonMap)
m_nameToCheckedRadioButtonMap.set(new NameToInputMap);
pair<NameToInputMap::iterator, bool> result = m_nameToCheckedRadioButtonMap->add(element->name().impl(), inputElement);
if (result.second)
return;
HTMLInputElement* oldCheckedButton = result.first->second;
if (oldCheckedButton == inputElement)
return;
result.first->second = inputElement;
oldCheckedButton->setChecked(false);
}
HTMLInputElement* HTMLFormElement::CheckedRadioButtons::checkedButtonForGroup(const AtomicString& name) const
{
if (!m_nameToCheckedRadioButtonMap)
return 0;
return m_nameToCheckedRadioButtonMap->get(name.impl());
}
void HTMLFormElement::CheckedRadioButtons::removeButton(HTMLFormControlElement* element)
{
if (element->name().isEmpty() || !m_nameToCheckedRadioButtonMap)
return;
NameToInputMap::iterator it = m_nameToCheckedRadioButtonMap->find(element->name().impl());
if (it == m_nameToCheckedRadioButtonMap->end() || it->second != element)
return;
ASSERT(element->isRadioButton());
ASSERT(element->isChecked());
m_nameToCheckedRadioButtonMap->remove(it);
if (m_nameToCheckedRadioButtonMap->isEmpty())
m_nameToCheckedRadioButtonMap.clear();
}
} // namespace
|