| 1 | /* |
| 2 | * Copyright (C) 2010 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 "DOMFormData.h" |
| 33 | |
| 34 | #include "HTMLFormControlElement.h" |
| 35 | #include "HTMLFormElement.h" |
| 36 | #include <wtf/Optional.h> |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | DOMFormData::DOMFormData(const TextEncoding& encoding) |
| 41 | : m_encoding(encoding) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | DOMFormData::DOMFormData(HTMLFormElement* form) |
| 46 | : m_encoding(UTF8Encoding()) |
| 47 | { |
| 48 | if (!form) |
| 49 | return; |
| 50 | |
| 51 | for (auto& element : form->copyAssociatedElementsVector()) { |
| 52 | if (!element->asHTMLElement().isDisabledFormControl()) |
| 53 | element->appendFormData(*this, true); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // https://xhr.spec.whatwg.org/#create-an-entry |
| 58 | auto DOMFormData::createFileEntry(const String& name, Blob& blob, const String& filename) -> Item |
| 59 | { |
| 60 | if (!blob.isFile()) |
| 61 | return { name, File::create(blob, filename.isNull() ? "blob"_s : filename) }; |
| 62 | |
| 63 | if (!filename.isNull()) |
| 64 | return { name, File::create(downcast<File>(blob), filename) }; |
| 65 | |
| 66 | return { name, RefPtr<File> { &downcast<File>(blob) } }; |
| 67 | } |
| 68 | |
| 69 | void DOMFormData::append(const String& name, const String& value) |
| 70 | { |
| 71 | m_items.append({ name, value }); |
| 72 | } |
| 73 | |
| 74 | void DOMFormData::append(const String& name, Blob& blob, const String& filename) |
| 75 | { |
| 76 | m_items.append(createFileEntry(name, blob, filename)); |
| 77 | } |
| 78 | |
| 79 | void DOMFormData::remove(const String& name) |
| 80 | { |
| 81 | m_items.removeAllMatching([&name] (const auto& item) { |
| 82 | return item.name == name; |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | auto DOMFormData::get(const String& name) -> Optional<FormDataEntryValue> |
| 87 | { |
| 88 | for (auto& item : m_items) { |
| 89 | if (item.name == name) |
| 90 | return item.data; |
| 91 | } |
| 92 | |
| 93 | return WTF::nullopt; |
| 94 | } |
| 95 | |
| 96 | auto DOMFormData::getAll(const String& name) -> Vector<FormDataEntryValue> |
| 97 | { |
| 98 | Vector<FormDataEntryValue> result; |
| 99 | |
| 100 | for (auto& item : m_items) { |
| 101 | if (item.name == name) |
| 102 | result.append(item.data); |
| 103 | } |
| 104 | |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | bool DOMFormData::has(const String& name) |
| 109 | { |
| 110 | for (auto& item : m_items) { |
| 111 | if (item.name == name) |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | void DOMFormData::set(const String& name, const String& value) |
| 119 | { |
| 120 | set(name, { name, value }); |
| 121 | } |
| 122 | |
| 123 | void DOMFormData::set(const String& name, Blob& blob, const String& filename) |
| 124 | { |
| 125 | set(name, createFileEntry(name, blob, filename)); |
| 126 | } |
| 127 | |
| 128 | void DOMFormData::set(const String& name, Item&& item) |
| 129 | { |
| 130 | Optional<size_t> initialMatchLocation; |
| 131 | |
| 132 | // Find location of the first item with a matching name. |
| 133 | for (size_t i = 0; i < m_items.size(); ++i) { |
| 134 | if (name == m_items[i].name) { |
| 135 | initialMatchLocation = i; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (initialMatchLocation) { |
| 141 | m_items[*initialMatchLocation] = WTFMove(item); |
| 142 | |
| 143 | m_items.removeAllMatching([&name] (const auto& item) { |
| 144 | return item.name == name; |
| 145 | }, *initialMatchLocation + 1); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | m_items.append(WTFMove(item)); |
| 150 | } |
| 151 | |
| 152 | DOMFormData::Iterator::Iterator(DOMFormData& target) |
| 153 | : m_target(target) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | Optional<KeyValuePair<String, DOMFormData::FormDataEntryValue>> DOMFormData::Iterator::next() |
| 158 | { |
| 159 | auto& items = m_target->items(); |
| 160 | if (m_index >= items.size()) |
| 161 | return WTF::nullopt; |
| 162 | |
| 163 | auto& item = items[m_index++]; |
| 164 | return makeKeyValuePair(item.name, item.data); |
| 165 | } |
| 166 | |
| 167 | } // namespace WebCore |
| 168 | |