2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/TextBox.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-05-30 12:59:10 +03:00
|
|
|
#include <LibWeb/DOM/Event.h>
|
2020-11-21 22:15:57 +03:00
|
|
|
#include <LibWeb/HTML/EventNames.h>
|
2020-07-26 16:08:16 +03:00
|
|
|
#include <LibWeb/HTML/HTMLFormElement.h>
|
|
|
|
#include <LibWeb/HTML/HTMLInputElement.h>
|
2020-09-11 19:17:39 +03:00
|
|
|
#include <LibWeb/InProcessWebView.h>
|
2020-11-22 17:53:01 +03:00
|
|
|
#include <LibWeb/Layout/ButtonBox.h>
|
|
|
|
#include <LibWeb/Layout/CheckBox.h>
|
|
|
|
#include <LibWeb/Layout/WidgetBox.h>
|
2020-07-28 20:27:41 +03:00
|
|
|
#include <LibWeb/Page/Frame.h>
|
2019-11-25 23:21:55 +03:00
|
|
|
|
2020-07-28 19:20:36 +03:00
|
|
|
namespace Web::HTML {
|
2020-03-07 12:27:02 +03:00
|
|
|
|
2020-10-10 04:48:05 +03:00
|
|
|
HTMLInputElement::HTMLInputElement(DOM::Document& document, const QualifiedName& qualified_name)
|
|
|
|
: HTMLElement(document, qualified_name)
|
2019-11-25 23:21:55 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLInputElement::~HTMLInputElement()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
void HTMLInputElement::did_click_button(Badge<Layout::ButtonBox>)
|
2020-09-12 18:56:11 +03:00
|
|
|
{
|
2020-11-21 22:15:57 +03:00
|
|
|
// FIXME: This should be a PointerEvent.
|
|
|
|
dispatch_event(DOM::Event::create(EventNames::click));
|
2020-09-12 18:56:11 +03:00
|
|
|
|
|
|
|
if (type().equals_ignoring_case("submit")) {
|
|
|
|
if (auto* form = first_ancestor_of_type<HTMLFormElement>()) {
|
2020-11-22 00:53:18 +03:00
|
|
|
form->submit_form(this);
|
2020-09-12 18:56:11 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
RefPtr<Layout::Node> HTMLInputElement::create_layout_node(const CSS::StyleProperties* parent_style)
|
2019-11-25 23:21:55 +03:00
|
|
|
{
|
2020-11-12 20:23:05 +03:00
|
|
|
ASSERT(document().page());
|
|
|
|
auto& page = *document().page();
|
|
|
|
auto& page_view = const_cast<InProcessWebView&>(static_cast<const InProcessWebView&>(page.client()));
|
2019-11-25 23:21:55 +03:00
|
|
|
|
2020-05-04 23:33:49 +03:00
|
|
|
if (type() == "hidden")
|
|
|
|
return nullptr;
|
|
|
|
|
2020-06-21 06:58:11 +03:00
|
|
|
auto style = document().style_resolver().resolve_style(*this, parent_style);
|
2020-06-24 17:22:16 +03:00
|
|
|
if (style->display() == CSS::Display::None)
|
2020-06-21 06:58:11 +03:00
|
|
|
return nullptr;
|
|
|
|
|
2020-09-12 18:56:11 +03:00
|
|
|
if (type().equals_ignoring_case("submit") || type().equals_ignoring_case("button"))
|
2020-11-22 17:53:01 +03:00
|
|
|
return adopt(*new Layout::ButtonBox(document(), *this, move(style)));
|
2020-09-12 18:56:11 +03:00
|
|
|
|
|
|
|
if (type() == "checkbox")
|
2020-11-22 17:53:01 +03:00
|
|
|
return adopt(*new Layout::CheckBox(document(), *this, move(style)));
|
2019-11-25 23:21:55 +03:00
|
|
|
|
2020-09-12 18:56:11 +03:00
|
|
|
auto& text_box = page_view.add<GUI::TextBox>();
|
|
|
|
text_box.set_text(value());
|
|
|
|
text_box.on_change = [this] {
|
2020-11-22 17:53:01 +03:00
|
|
|
auto& widget = downcast<Layout::WidgetBox>(layout_node())->widget();
|
2020-09-12 18:56:11 +03:00
|
|
|
const_cast<HTMLInputElement*>(this)->set_attribute(HTML::AttributeNames::value, static_cast<const GUI::TextBox&>(widget).text());
|
|
|
|
};
|
|
|
|
int text_width = Gfx::Font::default_font().width(value());
|
|
|
|
auto size_value = attribute(HTML::AttributeNames::size);
|
|
|
|
if (!size_value.is_null()) {
|
|
|
|
auto size = size_value.to_uint();
|
|
|
|
if (size.has_value())
|
|
|
|
text_width = Gfx::Font::default_font().glyph_width('x') * size.value();
|
|
|
|
}
|
|
|
|
text_box.set_relative_rect(0, 0, text_width + 20, 20);
|
2020-11-22 17:53:01 +03:00
|
|
|
return adopt(*new Layout::WidgetBox(document(), *this, text_box));
|
2019-11-25 23:21:55 +03:00
|
|
|
}
|
2020-03-07 12:27:02 +03:00
|
|
|
|
2020-09-11 19:17:39 +03:00
|
|
|
void HTMLInputElement::set_checked(bool checked)
|
|
|
|
{
|
|
|
|
if (m_checked == checked)
|
|
|
|
return;
|
|
|
|
m_checked = checked;
|
|
|
|
if (layout_node())
|
|
|
|
layout_node()->set_needs_display();
|
2020-09-11 19:25:15 +03:00
|
|
|
|
2020-11-21 22:15:57 +03:00
|
|
|
dispatch_event(DOM::Event::create(EventNames::change));
|
2020-09-11 19:17:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HTMLInputElement::enabled() const
|
|
|
|
{
|
|
|
|
return !has_attribute(HTML::AttributeNames::disabled);
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:27:02 +03:00
|
|
|
}
|