ladybird/Libraries/LibHTML/DOM/HTMLFormElement.cpp
Andreas Kling 1aea8f116b LibHTML: Add TreeNode::for_each_in_subtree_of_type<T>()
This allows you to iterate a subtree and get a callback for every node
where is<T>(node) == true. This makes for quite pleasant DOM traversal.
2019-12-18 21:34:03 +01:00

56 lines
1.4 KiB
C++

#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_of_type<HTMLInputElement>([&](auto& 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);
}