diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-03-23 20:13:34 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-24 03:35:11 +0100 |
commit | f733385cc43a41b3db4390adba9b8ccb2ba1cd6a (patch) | |
tree | 385859da8d832ff3a8c155b8ed4ed47834222900 | |
parent | 5608bc4eaf6bbc72def4cdb09052a6e91e14a3c1 (diff) | |
download | serenity-f733385cc43a41b3db4390adba9b8ccb2ba1cd6a.zip |
LibWeb: Move automatic browsing context creation to HTMLIFrameElement
We will soon have two DOM nodes which contain nested browsing contexts:
HTMLIFrameElement and HTMLObjectElement. Only HTMLIFrameElement should
have its nested context created automatically upon insertion, so move
the invocation of that logic to HTMLIFrameElement.
4 files changed, 50 insertions, 19 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp index 680499777d..6512e774ac 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp @@ -21,25 +21,38 @@ BrowsingContextContainer::BrowsingContextContainer(DOM::Document& document, DOM: BrowsingContextContainer::~BrowsingContextContainer() = default; -void BrowsingContextContainer::inserted() +// https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-nested-browsing-context +void BrowsingContextContainer::create_new_nested_browsing_context() { - HTMLElement::inserted(); - if (!is_connected()) + // 1. Let group be element's node document's browsing context's top-level browsing context's group. + // FIXME: We do not have a concept of "browsing context groups" yet. + auto* group = document().browsing_context(); + if (!group) return; - if (auto* browsing_context = document().browsing_context()) { - VERIFY(browsing_context->page()); - m_nested_browsing_context = BrowsingContext::create_nested(*browsing_context->page(), *this); - browsing_context->append_child(*m_nested_browsing_context); - m_nested_browsing_context->set_frame_nesting_levels(browsing_context->frame_nesting_levels()); - m_nested_browsing_context->register_frame_nesting(document().url()); - } + + VERIFY(group->page()); + + // 2. Let browsingContext be the result of creating a new browsing context with element's node document, element, and group. + // 3. Set element's nested browsing context to browsingContext. + m_nested_browsing_context = BrowsingContext::create_nested(*group->page(), *this); + group->append_child(*m_nested_browsing_context); + m_nested_browsing_context->set_frame_nesting_levels(group->frame_nesting_levels()); + m_nested_browsing_context->register_frame_nesting(document().url()); + + // 4. If element has a name attribute, then set browsingContext's name to the value of this attribute. + if (auto name = attribute(HTML::AttributeNames::name); !name.is_empty()) + m_nested_browsing_context->set_name(name); } -void BrowsingContextContainer::removed_from(Node* old_parent) +// https://html.spec.whatwg.org/multipage/window-object.html#a-browsing-context-is-discarded +void BrowsingContextContainer::discard_nested_browsing_context() { - HTMLElement::removed_from(old_parent); + // 1. Discard all Document objects for all the entries in browsingContext's session history. if (m_nested_browsing_context && m_nested_browsing_context->parent()) m_nested_browsing_context->parent()->remove_child(*m_nested_browsing_context); + + // 2. If browsingContext is a top-level browsing context, then remove browsingContext. + // NOTE: We skip this here because this is by definition a nested browsing context, not top-level. } // https://html.spec.whatwg.org/multipage/browsers.html#concept-bcc-content-document @@ -55,7 +68,7 @@ const DOM::Document* BrowsingContextContainer::content_document() const // 3. Let document be context's active document. auto const* document = context.active_document(); - //FIXME: This should not be here, as we're expected to have a document at this point. + // FIXME: This should not be here, as we're expected to have a document at this point. if (!document) return nullptr; diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.h b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.h index 9b49ba952e..4d22443279 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.h +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.h @@ -21,10 +21,10 @@ public: const DOM::Document* content_document() const; DOM::Document const* content_document_without_origin_check() const; - virtual void inserted() override; - virtual void removed_from(Node*) override; - protected: + void create_new_nested_browsing_context(); + void discard_nested_browsing_context(); + RefPtr<BrowsingContext> m_nested_browsing_context; private: diff --git a/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp index 8e50412bb1..5879b7b4cf 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp @@ -32,11 +32,28 @@ void HTMLIFrameElement::parse_attribute(const FlyString& name, const String& val load_src(value); } +// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-6 void HTMLIFrameElement::inserted() { - BrowsingContextContainer::inserted(); - if (is_connected()) - load_src(attribute(HTML::AttributeNames::src)); + HTMLElement::inserted(); + + if (!is_connected()) + return; + + // 1. Create a new nested browsing context for element. + create_new_nested_browsing_context(); + + // 2. FIXME: If element has a sandbox attribute, then parse the sandboxing directive given the attribute's value and element's iframe sandboxing flag set. + + // 3. Process the iframe attributes for element, with initialInsertion set to true. + load_src(attribute(HTML::AttributeNames::src)); +} + +// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-7 +void HTMLIFrameElement::removed_from(DOM::Node* node) +{ + HTMLElement::removed_from(node); + discard_nested_browsing_context(); } void HTMLIFrameElement::load_src(const String& value) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.h b/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.h index f977d2fe1f..1276480fc9 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.h @@ -21,6 +21,7 @@ public: private: virtual void inserted() override; + virtual void removed_from(Node*) override; virtual void parse_attribute(const FlyString& name, const String& value) override; void load_src(const String&); |