diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index dc79aa6b6c4..f9c535dec8c 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -1158,6 +1158,33 @@ GC::Ptr Document::first_base_element_with_target_in_tree_ 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 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 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(); 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. + // 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(); } diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h index 127c72b28d4..ad6a8b17016 100644 --- a/Libraries/LibWeb/DOM/Document.h +++ b/Libraries/LibWeb/DOM/Document.h @@ -222,7 +222,7 @@ public: String referrer() const; 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 fallback_base_url() const; URL::URL base_url() const; @@ -230,6 +230,7 @@ public: void update_base_element(Badge); GC::Ptr first_base_element_with_href_in_tree_order() const; GC::Ptr 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 document_uri() const { return url_string(); } diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h index 1952a9baf05..af9e8730b54 100644 --- a/Libraries/LibWeb/DOM/Node.h +++ b/Libraries/LibWeb/DOM/Node.h @@ -50,6 +50,7 @@ enum class ShouldComputeRole { #define ENUMERATE_STYLE_INVALIDATION_REASONS(X) \ X(AdoptedStyleSheetsList) \ + X(BaseURLChanged) \ X(CSSFontLoaded) \ X(CSSImportRule) \ X(CSSStylePropertiesRemoveProperty) \ diff --git a/Libraries/LibWeb/HTML/HTMLBaseElement.cpp b/Libraries/LibWeb/HTML/HTMLBaseElement.cpp index cf6673f083e..a6e7bf63e65 100644 --- a/Libraries/LibWeb/HTML/HTMLBaseElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLBaseElement.cpp @@ -95,6 +95,9 @@ void HTMLBaseElement::set_the_frozen_base_url() // 4. Set element's frozen base URL to urlRecord. 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 diff --git a/Libraries/LibWeb/HTML/Navigable.cpp b/Libraries/LibWeb/HTML/Navigable.cpp index 68b80ada239..c2d5e634019 100644 --- a/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Libraries/LibWeb/HTML/Navigable.cpp @@ -2286,7 +2286,7 @@ void perform_url_and_history_update_steps(DOM::Document& document, URL::URL new_ if (serialized_data.has_value()) 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); // 9. Set document's latest entry to newEntry.