|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Apple 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 |
| 6 | + * are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include "config.h" |
| 27 | +#include "AutofillElements.h" |
| 28 | + |
| 29 | +#include "FocusController.h" |
| 30 | +#include "Page.h" |
| 31 | + |
| 32 | +namespace WebCore { |
| 33 | + |
| 34 | +static inline bool isAutofillableElement(Element& node) |
| 35 | +{ |
| 36 | + if (!is<HTMLInputElement>(node)) |
| 37 | + return false; |
| 38 | + |
| 39 | + auto inputElement = &downcast<HTMLInputElement>(node); |
| 40 | + return inputElement->isTextField() || inputElement->isEmailField(); |
| 41 | +} |
| 42 | + |
| 43 | +static inline RefPtr<HTMLInputElement> nextAutofillableElement(Node* startNode, FocusController& focusController) |
| 44 | +{ |
| 45 | + if (!is<Element>(startNode)) |
| 46 | + return nullptr; |
| 47 | + |
| 48 | + RefPtr<Element> nextElement = downcast<Element>(startNode); |
| 49 | + do { |
| 50 | + nextElement = focusController.nextFocusableElement(*nextElement.get()); |
| 51 | + } while (nextElement && !isAutofillableElement(*nextElement.get())); |
| 52 | + |
| 53 | + if (!nextElement) |
| 54 | + return nullptr; |
| 55 | + |
| 56 | + return &downcast<HTMLInputElement>(*nextElement); |
| 57 | +} |
| 58 | + |
| 59 | +static inline RefPtr<HTMLInputElement> previousAutofillableElement(Node* startNode, FocusController& focusController) |
| 60 | +{ |
| 61 | + if (!is<Element>(startNode)) |
| 62 | + return nullptr; |
| 63 | + |
| 64 | + RefPtr<Element> previousElement = downcast<Element>(startNode); |
| 65 | + do { |
| 66 | + previousElement = focusController.previousFocusableElement(*previousElement.get()); |
| 67 | + } while (previousElement && !isAutofillableElement(*previousElement.get())); |
| 68 | + |
| 69 | + if (!previousElement) |
| 70 | + return nullptr; |
| 71 | + |
| 72 | + return &downcast<HTMLInputElement>(*previousElement); |
| 73 | +} |
| 74 | + |
| 75 | +AutofillElements::AutofillElements(RefPtr<HTMLInputElement>&& username, RefPtr<HTMLInputElement>&& password) |
| 76 | + : m_username(username) |
| 77 | + , m_password(password) |
| 78 | +{ |
| 79 | +} |
| 80 | + |
| 81 | +std::optional<AutofillElements> AutofillElements::computeAutofillElements(Ref<HTMLInputElement> start) |
| 82 | +{ |
| 83 | + if (!start->document().page()) |
| 84 | + return std::nullopt; |
| 85 | + FocusController& focusController = start->document().page()->focusController(); |
| 86 | + if (start->isPasswordField()) { |
| 87 | + RefPtr<HTMLInputElement> previousElement = previousAutofillableElement(start.ptr(), focusController); |
| 88 | + RefPtr<HTMLInputElement> nextElement = nextAutofillableElement(start.ptr(), focusController); |
| 89 | + bool hasDuplicatePasswordElements = (nextElement && nextElement->isPasswordField()) || (previousElement && previousElement->isPasswordField()); |
| 90 | + if (hasDuplicatePasswordElements) |
| 91 | + return std::nullopt; |
| 92 | + |
| 93 | + if (previousElement && is<HTMLInputElement>(*previousElement)) { |
| 94 | + if (previousElement->isTextField()) |
| 95 | + return AutofillElements(WTFMove(previousElement), WTFMove(start)); |
| 96 | + } |
| 97 | + } else { |
| 98 | + RefPtr<HTMLInputElement> nextElement = nextAutofillableElement(start.ptr(), focusController); |
| 99 | + if (nextElement && is<HTMLInputElement>(*nextElement)) { |
| 100 | + if (nextElement->isPasswordField()) { |
| 101 | + RefPtr<HTMLInputElement> elementAfternextElement = nextAutofillableElement(nextElement.get(), focusController); |
| 102 | + bool hasDuplicatePasswordElements = elementAfternextElement && elementAfternextElement->isPasswordField(); |
| 103 | + if (hasDuplicatePasswordElements) |
| 104 | + return std::nullopt; |
| 105 | + |
| 106 | + return AutofillElements(WTFMove(start), WTFMove(nextElement)); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (start->isPasswordField()) { |
| 112 | + RefPtr<HTMLInputElement> previousElement = previousAutofillableElement(start.ptr(), focusController); |
| 113 | + RefPtr<HTMLInputElement> nextElement = nextAutofillableElement(start.ptr(), focusController); |
| 114 | + if (!previousElement && !nextElement) |
| 115 | + return AutofillElements(nullptr, start.ptr()); |
| 116 | + } |
| 117 | + return std::nullopt; |
| 118 | +} |
| 119 | + |
| 120 | +void AutofillElements::autofill(String username, String password) |
| 121 | +{ |
| 122 | + if (m_username) { |
| 123 | + m_username->setValueForUser(username); |
| 124 | + m_username->setAutoFilled(); |
| 125 | + } |
| 126 | + if (m_password) { |
| 127 | + m_password->setValueForUser(password); |
| 128 | + m_password->setAutoFilled(); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace WebCore |
0 commit comments