mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-30 08:18:49 +00:00
LibHTML: Implement basic <form> and <input> element support
This patch adds "submit" inputs and default (text box) inputs, as well as form elements that can be submitted. Layout of input elements is implemented via a new LayoutWidget class that allows you to put an arbitrary GWidget in the layout tree. At the moment, the DOM node sets the initial size of the LayoutWidget, and then the positioning is done by the normal layout algorithm. We also now support submitting a <form method="GET">, which does a full replacing load with a URL based on the form's action + a query string built from the name/value of input elements within the submitted form. This is pretty neat! :^)
This commit is contained in:
parent
a91c17c0eb
commit
6d1c4ae5a9
Notes:
sideshowbarker
2024-07-19 11:04:51 +09:00
Author: https://github.com/awesomekling
Commit: 6d1c4ae5a9
11 changed files with 229 additions and 0 deletions
58
Libraries/LibHTML/DOM/HTMLFormElement.cpp
Normal file
58
Libraries/LibHTML/DOM/HTMLFormElement.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
#include <AK/StringBuilder.h>
|
||||
#include <LibHTML/DOM/HTMLFormElement.h>
|
||||
#include <LibHTML/DOM/HTMLInputElement.h>
|
||||
#include <LibHTML/Frame.h>
|
||||
#include <LibHTML/HtmlView.h>
|
||||
|
||||
HTMLFormElement::HTMLFormElement(Document& document, const String& tag_name)
|
||||
: HTMLElement(document, tag_name)
|
||||
{
|
||||
}
|
||||
|
||||
HTMLFormElement::~HTMLFormElement()
|
||||
{
|
||||
}
|
||||
|
||||
void HTMLFormElement::submit()
|
||||
{
|
||||
if (action().is_null()) {
|
||||
dbg() << "Unsupported form action ''";
|
||||
return;
|
||||
}
|
||||
|
||||
if (method().to_lowercase() != "get") {
|
||||
dbg() << "Unsupported form method '" << method() << "'";
|
||||
return;
|
||||
}
|
||||
|
||||
URL url(document().complete_url(action()));
|
||||
|
||||
struct NameAndValue {
|
||||
String name;
|
||||
String value;
|
||||
};
|
||||
|
||||
Vector<NameAndValue> parameters;
|
||||
|
||||
for_each_in_subtree([&](auto& node) {
|
||||
if (is<HTMLInputElement>(node)) {
|
||||
auto& input = to<HTMLInputElement>(node);
|
||||
if (!input.name().is_null())
|
||||
parameters.append({ input.name(), input.value() });
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
StringBuilder builder;
|
||||
for (int i = 0; i < parameters.size(); ++i) {
|
||||
builder.append(parameters[i].name);
|
||||
builder.append('=');
|
||||
builder.append(parameters[i].value);
|
||||
if (i != parameters.size() - 1)
|
||||
builder.append('&');
|
||||
}
|
||||
url.set_query(builder.to_string());
|
||||
|
||||
// FIXME: We shouldn't let the form just do this willy-nilly.
|
||||
document().frame()->html_view()->load(url);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue