1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 18:07:35 +00:00

LibWeb: Make DOMImplementation forward its ref count to DOM::Document

This allows document.implementation to keep the underlying document
alive for as long as we need it (for example, if someone holds on to a
DOMImplementation JS wrapper after the document is GC'd.)
This commit is contained in:
Andreas Kling 2021-12-09 12:51:05 +01:00
parent e1287a9a45
commit d368b08698
4 changed files with 22 additions and 14 deletions

View file

@ -15,7 +15,7 @@
namespace Web::DOM { namespace Web::DOM {
DOMImplementation::DOMImplementation(Document& document) DOMImplementation::DOMImplementation(Document& document)
: m_document(document) : RefCountForwarder(document)
{ {
} }
@ -37,7 +37,7 @@ NonnullRefPtr<Document> DOMImplementation::create_document(const String& namespa
if (element) if (element)
xml_document->append_child(element.release_nonnull()); xml_document->append_child(element.release_nonnull());
xml_document->set_origin(m_document.origin()); xml_document->set_origin(document().origin());
if (namespace_ == Namespace::HTML) if (namespace_ == Namespace::HTML)
xml_document->set_content_type("application/xhtml+xml"); xml_document->set_content_type("application/xhtml+xml");
@ -79,16 +79,16 @@ NonnullRefPtr<Document> DOMImplementation::create_html_document(const String& ti
auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML); auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML);
html_element->append_child(body_element); html_element->append_child(body_element);
html_document->set_origin(m_document.origin()); html_document->set_origin(document().origin());
return html_document; return html_document;
} }
// https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
NonnullRefPtr<DocumentType> DOMImplementation::create_document_type(const String& qualified_name, const String& public_id, const String& system_id) const NonnullRefPtr<DocumentType> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id)
{ {
// FIXME: Validate qualified_name. // FIXME: Validate qualified_name.
auto document_type = DocumentType::create(m_document); auto document_type = DocumentType::create(document());
document_type->set_name(qualified_name); document_type->set_name(qualified_name);
document_type->set_public_id(public_id); document_type->set_public_id(public_id);
document_type->set_system_id(system_id); document_type->set_system_id(system_id);

View file

@ -7,28 +7,30 @@
#pragma once #pragma once
#include <AK/NonnullRefPtr.h> #include <AK/NonnullRefPtr.h>
#include <AK/RefCountForwarder.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/Weakable.h> #include <AK/Weakable.h>
#include <LibWeb/Bindings/Wrappable.h> #include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/Document.h>
namespace Web::DOM { namespace Web::DOM {
class DOMImplementation final class DOMImplementation final
: public RefCounted<DOMImplementation> : public RefCountForwarder<Document>
, public Weakable<DOMImplementation> , public Weakable<DOMImplementation>
, public Bindings::Wrappable { , public Bindings::Wrappable {
public: public:
using WrapperType = Bindings::DOMImplementationWrapper; using WrapperType = Bindings::DOMImplementationWrapper;
static NonnullRefPtr<DOMImplementation> create(Document& document) static NonnullOwnPtr<DOMImplementation> create(Badge<Document>, Document& document)
{ {
return adopt_ref(*new DOMImplementation(document)); return adopt_own(*new DOMImplementation(document));
} }
// FIXME: Add optional DocumentType once supported by IDL // FIXME: Add optional DocumentType once supported by IDL
NonnullRefPtr<Document> create_document(const String&, const String&) const; NonnullRefPtr<Document> create_document(const String&, const String&) const;
NonnullRefPtr<Document> create_html_document(const String& title) const; NonnullRefPtr<Document> create_html_document(const String& title) const;
NonnullRefPtr<DocumentType> create_document_type(const String&, const String&, const String&) const; NonnullRefPtr<DocumentType> create_document_type(String const& qualified_name, String const& public_id, String const& system_id);
// https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature // https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature
bool has_feature() const { return true; } bool has_feature() const { return true; }
@ -36,7 +38,8 @@ public:
private: private:
explicit DOMImplementation(Document&); explicit DOMImplementation(Document&);
Document& m_document; Document& document() { return ref_count_target(); }
Document const& document() const { return ref_count_target(); }
}; };
} }

View file

@ -22,6 +22,7 @@
#include <LibWeb/DOM/Comment.h> #include <LibWeb/DOM/Comment.h>
#include <LibWeb/DOM/CustomEvent.h> #include <LibWeb/DOM/CustomEvent.h>
#include <LibWeb/DOM/DOMException.h> #include <LibWeb/DOM/DOMException.h>
#include <LibWeb/DOM/DOMImplementation.h>
#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentFragment.h> #include <LibWeb/DOM/DocumentFragment.h>
#include <LibWeb/DOM/DocumentType.h> #include <LibWeb/DOM/DocumentType.h>
@ -71,7 +72,7 @@ Document::Document(const AK::URL& url)
, m_style_sheets(CSS::StyleSheetList::create(*this)) , m_style_sheets(CSS::StyleSheetList::create(*this))
, m_url(url) , m_url(url)
, m_window(Window::create_with_document(*this)) , m_window(Window::create_with_document(*this))
, m_implementation(DOMImplementation::create(*this)) , m_implementation(DOMImplementation::create({}, *this))
, m_history(HTML::History::create(*this)) , m_history(HTML::History::create(*this))
{ {
HTML::main_thread_event_loop().register_document({}, *this); HTML::main_thread_event_loop().register_document({}, *this);
@ -1136,4 +1137,9 @@ void Document::evaluate_media_queries_and_report_changes()
} }
} }
NonnullRefPtr<DOMImplementation> Document::implementation() const
{
return *m_implementation;
}
} }

View file

@ -22,7 +22,6 @@
#include <LibWeb/CSS/StyleComputer.h> #include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleSheetList.h> #include <LibWeb/CSS/StyleSheetList.h>
#include <LibWeb/Cookie/Cookie.h> #include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/DOM/DOMImplementation.h>
#include <LibWeb/DOM/ExceptionOr.h> #include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/DOM/NonElementParentNode.h> #include <LibWeb/DOM/NonElementParentNode.h>
#include <LibWeb/DOM/ParentNode.h> #include <LibWeb/DOM/ParentNode.h>
@ -264,7 +263,7 @@ public:
void completely_finish_loading(); void completely_finish_loading();
const NonnullRefPtr<DOMImplementation> implementation() const { return m_implementation; } NonnullRefPtr<DOMImplementation> implementation() const;
RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; } RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; }
void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = move(script); } void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }
@ -376,7 +375,7 @@ private:
bool m_ready_for_post_load_tasks { false }; bool m_ready_for_post_load_tasks { false };
NonnullRefPtr<DOMImplementation> m_implementation; NonnullOwnPtr<DOMImplementation> m_implementation;
RefPtr<HTML::HTMLScriptElement> m_current_script; RefPtr<HTML::HTMLScriptElement> m_current_script;
bool m_should_invalidate_styles_on_attribute_changes { true }; bool m_should_invalidate_styles_on_attribute_changes { true };