| 1 | /* |
| 2 | * Copyright (C) 2018 Igalia S.L. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "WebKitDOMElement.h" |
| 22 | |
| 23 | #include "DOMObjectCache.h" |
| 24 | #include "WebKitDOMElementPrivate.h" |
| 25 | #include "WebKitDOMNodePrivate.h" |
| 26 | #include "WebKitDOMPrivate.h" |
| 27 | #include <WebCore/HTMLInputElement.h> |
| 28 | #include <WebCore/HTMLTextAreaElement.h> |
| 29 | |
| 30 | #if PLATFORM(GTK) |
| 31 | #include "WebKitDOMEventTarget.h" |
| 32 | #endif |
| 33 | |
| 34 | namespace WebKit { |
| 35 | |
| 36 | WebKitDOMElement* kit(WebCore::Element* obj) |
| 37 | { |
| 38 | return WEBKIT_DOM_ELEMENT(kit(static_cast<WebCore::Node*>(obj))); |
| 39 | } |
| 40 | |
| 41 | WebCore::Element* core(WebKitDOMElement* element) |
| 42 | { |
| 43 | return element ? static_cast<WebCore::Element*>(webkitDOMNodeGetCoreObject(WEBKIT_DOM_NODE(element))) : nullptr; |
| 44 | } |
| 45 | |
| 46 | WebKitDOMElement* wrapElement(WebCore::Element* coreObject) |
| 47 | { |
| 48 | ASSERT(coreObject); |
| 49 | #if PLATFORM(GTK) |
| 50 | return WEBKIT_DOM_ELEMENT(g_object_new(WEBKIT_DOM_TYPE_ELEMENT, "core-object" , coreObject, nullptr)); |
| 51 | #else |
| 52 | auto* element = WEBKIT_DOM_ELEMENT(g_object_new(WEBKIT_DOM_TYPE_ELEMENT, nullptr)); |
| 53 | webkitDOMNodeSetCoreObject(WEBKIT_DOM_NODE(element), static_cast<WebCore::Node*>(coreObject)); |
| 54 | return element; |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | } // namespace WebKit |
| 59 | |
| 60 | #if PLATFORM(GTK) |
| 61 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
| 62 | G_DEFINE_TYPE_WITH_CODE(WebKitDOMElement, webkit_dom_element, WEBKIT_DOM_TYPE_NODE, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkitDOMElementDOMEventTargetInit)) |
| 63 | G_GNUC_END_IGNORE_DEPRECATIONS; |
| 64 | #else |
| 65 | G_DEFINE_TYPE(WebKitDOMElement, webkit_dom_element, WEBKIT_DOM_TYPE_NODE) |
| 66 | #endif |
| 67 | |
| 68 | static void webkit_dom_element_class_init(WebKitDOMElementClass* elementClass) |
| 69 | { |
| 70 | #if PLATFORM(GTK) |
| 71 | GObjectClass* gobjectClass = G_OBJECT_CLASS(elementClass); |
| 72 | webkitDOMElementInstallProperties(gobjectClass); |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | static void webkit_dom_element_init(WebKitDOMElement*) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * webkit_dom_element_html_input_element_is_user_edited: |
| 82 | * @element: a #WebKitDOMElement |
| 83 | * |
| 84 | * Get whether @element is an HTML text input element that has been edited by a user action. |
| 85 | * |
| 86 | * Returns: whether @element has been edited by a user action. |
| 87 | * |
| 88 | * Since: 2.22 |
| 89 | */ |
| 90 | gboolean webkit_dom_element_html_input_element_is_user_edited(WebKitDOMElement* element) |
| 91 | { |
| 92 | g_return_val_if_fail(WEBKIT_DOM_IS_ELEMENT(element), FALSE); |
| 93 | |
| 94 | auto* node = webkitDOMNodeGetCoreObject(WEBKIT_DOM_NODE(element)); |
| 95 | if (is<WebCore::HTMLInputElement>(node)) |
| 96 | return downcast<WebCore::HTMLInputElement>(*node).lastChangeWasUserEdit(); |
| 97 | |
| 98 | if (is<WebCore::HTMLTextAreaElement>(node)) |
| 99 | return downcast<WebCore::HTMLTextAreaElement>(*node).lastChangeWasUserEdit(); |
| 100 | |
| 101 | return FALSE; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * webkit_dom_element_is_html_input_element_auto_filled: |
| 106 | * @element: a #WebKitDOMElement |
| 107 | * |
| 108 | * Get whether the element is an HTML input element that has been filled automatically. |
| 109 | * |
| 110 | * Returns: whether @element has been filled automatically. |
| 111 | * |
| 112 | * Since: 2.22 |
| 113 | */ |
| 114 | gboolean webkit_dom_element_html_input_element_get_auto_filled(WebKitDOMElement* element) |
| 115 | { |
| 116 | g_return_val_if_fail(WEBKIT_DOM_IS_ELEMENT(element), FALSE); |
| 117 | |
| 118 | auto* node = webkitDOMNodeGetCoreObject(WEBKIT_DOM_NODE(element)); |
| 119 | if (!is<WebCore::HTMLInputElement>(node)) |
| 120 | return false; |
| 121 | |
| 122 | return downcast<WebCore::HTMLInputElement>(*node).isAutoFilled(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * webkit_dom_element_html_input_element_set_auto_filled: |
| 127 | * @element: a #WebKitDOMElement |
| 128 | * @auto_filled: value to set |
| 129 | * |
| 130 | * Set whether the element is an HTML input element that has been filled automatically. |
| 131 | * If @element is not an HTML input element this function does nothing. |
| 132 | * |
| 133 | * Since: 2.22 |
| 134 | */ |
| 135 | void webkit_dom_element_html_input_element_set_auto_filled(WebKitDOMElement* element, gboolean autoFilled) |
| 136 | { |
| 137 | g_return_if_fail(WEBKIT_DOM_IS_ELEMENT(element)); |
| 138 | |
| 139 | auto* node = webkitDOMNodeGetCoreObject(WEBKIT_DOM_NODE(element)); |
| 140 | if (!is<WebCore::HTMLInputElement>(node)) |
| 141 | return; |
| 142 | |
| 143 | downcast<WebCore::HTMLInputElement>(*node).setAutoFilled(autoFilled); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * webkit_dom_element_html_input_element_set_editing_value: |
| 148 | * @element: a #WebKitDOMElement |
| 149 | * @value: the text to set |
| 150 | * |
| 151 | * Set the value of an HTML input element as if it had been edited by |
| 152 | * the user, triggering a change event. If @element is not an HTML input |
| 153 | * element this function does nothing. |
| 154 | * |
| 155 | * Since: 2.22 |
| 156 | */ |
| 157 | void webkit_dom_element_html_input_element_set_editing_value(WebKitDOMElement* element, const char* value) |
| 158 | { |
| 159 | g_return_if_fail(WEBKIT_DOM_IS_ELEMENT(element)); |
| 160 | |
| 161 | auto* node = webkitDOMNodeGetCoreObject(WEBKIT_DOM_NODE(element)); |
| 162 | if (!is<WebCore::HTMLInputElement>(node)) |
| 163 | return; |
| 164 | |
| 165 | downcast<WebCore::HTMLInputElement>(*node).setValueForUser(String::fromUTF8(value)); |
| 166 | } |
| 167 | |