1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:38:12 +00:00

LibWeb: Add support for the <base> element changing the base URL

Used by Google seemingly almost all around account sign in and
management. The modern sign in page has this near the beginning:
```html
<base href="https://accounts.google.com">
```
All of the XHRs performed by sign in are relative URLs to this
base URL. Previously we ignored this and did it relative to the
current URL, causing the XHRs to 404 and sign in to fall apart.

I presume they do this because you can access the sign in page
from multiple endpoints, such as `/ServiceLogin` and
`/o/oauth2/auth/identifier`
This commit is contained in:
Luke Wilde 2022-06-19 16:02:48 +01:00 committed by Linus Groh
parent b3d87f8e37
commit 1f820f8840
7 changed files with 111 additions and 8 deletions

View file

@ -43,6 +43,7 @@
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLAreaElement.h>
#include <LibWeb/HTML/HTMLBaseElement.h>
#include <LibWeb/HTML/HTMLBodyElement.h>
#include <LibWeb/HTML/HTMLEmbedElement.h>
#include <LibWeb/HTML/HTMLFormElement.h>
@ -523,11 +524,49 @@ Vector<CSS::BackgroundLayerData> const* Document::background_layers() const
return &body_layout_node->background_layers();
}
RefPtr<HTML::HTMLBaseElement> Document::first_base_element_with_href_in_tree_order() const
{
RefPtr<HTML::HTMLBaseElement> base_element;
for_each_in_subtree_of_type<HTML::HTMLBaseElement>([&base_element](HTML::HTMLBaseElement const& base_element_in_tree) {
if (base_element_in_tree.has_attribute(HTML::AttributeNames::href)) {
base_element = base_element_in_tree;
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
return base_element;
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#fallback-base-url
AK::URL Document::fallback_base_url() const
{
// FIXME: 1. If document is an iframe srcdoc document, then return the document base URL of document's browsing context's container document.
// FIXME: 2. If document's URL is about:blank, and document's browsing context's creator base URL is non-null, then return that creator base URL.
// 3. Return document's URL.
return m_url;
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-url
AK::URL Document::base_url() const
{
// 1. If there is no base element that has an href attribute in the Document, then return the Document's fallback base URL.
auto base_element = first_base_element_with_href_in_tree_order();
if (!base_element)
return fallback_base_url();
// 2. Otherwise, return the frozen base URL of the first base element in the Document that has an href attribute, in tree order.
return base_element->frozen_base_url();
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#parse-a-url
AK::URL Document::parse_url(String const& url) const
{
// FIXME: Make sure we do this according to spec.
return m_url.complete_url(url);
// FIXME: Pass in document's character encoding.
return base_url().complete_url(url);
}
void Document::set_needs_layout()