LibWeb: Update base URL change processing

Corresponds to 49f5cd381e
This commit is contained in:
Sam Atkins 2025-08-07 13:37:11 +01:00
commit 0b998b8379
Notes: github-actions[bot] 2025-08-11 11:24:22 +00:00
5 changed files with 36 additions and 4 deletions

View file

@ -1158,6 +1158,33 @@ GC::Ptr<HTML::HTMLBaseElement> Document::first_base_element_with_target_in_tree_
return m_first_base_element_with_target_in_tree_order; return m_first_base_element_with_target_in_tree_order;
} }
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#respond-to-base-url-changes
void Document::respond_to_base_url_changes()
{
// To respond to base URL changes for a Document document:
// 1. The user agent should update any user interface elements which are displaying affected URLs, or data derived
// from such URLs, to the user. Examples of such user interface elements would be a status bar that displays a
// hyperlink's url, or some user interface which displays the URL specified by a q, blockquote, ins, or del
// element's cite attribute.
// FIXME: Update those UI elements.
// 2. Ensure that the CSS :link/:visited/etc. pseudo-classes are updated appropriately.
invalidate_style(StyleInvalidationReason::BaseURLChanged);
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#set-the-url
void Document::set_url(URL::URL const& url)
{
// To set the URL for a Document document to a URL record url:
// 1. Set document's URL to url.
m_url = url;
// 2. Respond to base URL changes given document.
respond_to_base_url_changes();
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#fallback-base-url // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#fallback-base-url
URL::URL Document::fallback_base_url() const URL::URL Document::fallback_base_url() const
{ {
@ -1181,12 +1208,12 @@ URL::URL Document::fallback_base_url() const
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-url // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-url
URL::URL Document::base_url() const URL::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. // 1. If document has no descendant base element that has an href attribute, then return document's fallback base URL.
auto base_element = first_base_element_with_href_in_tree_order(); auto base_element = first_base_element_with_href_in_tree_order();
if (!base_element) if (!base_element)
return fallback_base_url(); 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. // 2. Otherwise, return the frozen base URL of the first base element in document that has an href attribute, in tree order.
return base_element->frozen_base_url(); return base_element->frozen_base_url();
} }

View file

@ -222,7 +222,7 @@ public:
String referrer() const; String referrer() const;
void set_referrer(String); void set_referrer(String);
void set_url(const URL::URL& url) { m_url = url; } void set_url(URL::URL const& url);
URL::URL url() const { return m_url; } URL::URL url() const { return m_url; }
URL::URL fallback_base_url() const; URL::URL fallback_base_url() const;
URL::URL base_url() const; URL::URL base_url() const;
@ -230,6 +230,7 @@ public:
void update_base_element(Badge<HTML::HTMLBaseElement>); void update_base_element(Badge<HTML::HTMLBaseElement>);
GC::Ptr<HTML::HTMLBaseElement> first_base_element_with_href_in_tree_order() const; GC::Ptr<HTML::HTMLBaseElement> first_base_element_with_href_in_tree_order() const;
GC::Ptr<HTML::HTMLBaseElement> first_base_element_with_target_in_tree_order() const; GC::Ptr<HTML::HTMLBaseElement> first_base_element_with_target_in_tree_order() const;
void respond_to_base_url_changes();
String url_string() const { return m_url.to_string(); } String url_string() const { return m_url.to_string(); }
String document_uri() const { return url_string(); } String document_uri() const { return url_string(); }

View file

@ -50,6 +50,7 @@ enum class ShouldComputeRole {
#define ENUMERATE_STYLE_INVALIDATION_REASONS(X) \ #define ENUMERATE_STYLE_INVALIDATION_REASONS(X) \
X(AdoptedStyleSheetsList) \ X(AdoptedStyleSheetsList) \
X(BaseURLChanged) \
X(CSSFontLoaded) \ X(CSSFontLoaded) \
X(CSSImportRule) \ X(CSSImportRule) \
X(CSSStylePropertiesRemoveProperty) \ X(CSSStylePropertiesRemoveProperty) \

View file

@ -95,6 +95,9 @@ void HTMLBaseElement::set_the_frozen_base_url()
// 4. Set element's frozen base URL to urlRecord. // 4. Set element's frozen base URL to urlRecord.
m_frozen_base_url = url_record.release_value(); m_frozen_base_url = url_record.release_value();
// 5. Respond to base URL changes given document.
document.respond_to_base_url_changes();
} }
// https://html.spec.whatwg.org/multipage/semantics.html#dom-base-href // https://html.spec.whatwg.org/multipage/semantics.html#dom-base-href

View file

@ -2286,7 +2286,7 @@ void perform_url_and_history_update_steps(DOM::Document& document, URL::URL new_
if (serialized_data.has_value()) if (serialized_data.has_value())
document.restore_the_history_object_state(new_entry); document.restore_the_history_object_state(new_entry);
// 8. Set document's URL to newURL. // 8. Set the URL given document to newURL.
document.set_url(new_url); document.set_url(new_url);
// 9. Set document's latest entry to newEntry. // 9. Set document's latest entry to newEntry.