DOMImplementation.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Bindings/MainThreadVM.h>
  9. #include <LibWeb/DOM/DOMImplementation.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/DocumentType.h>
  12. #include <LibWeb/DOM/ElementFactory.h>
  13. #include <LibWeb/DOM/Text.h>
  14. #include <LibWeb/HTML/Origin.h>
  15. #include <LibWeb/Namespace.h>
  16. namespace Web::DOM {
  17. JS::NonnullGCPtr<DOMImplementation> DOMImplementation::create(Document& document)
  18. {
  19. auto& realm = document.realm();
  20. return realm.heap().allocate<DOMImplementation>(realm, document);
  21. }
  22. DOMImplementation::DOMImplementation(Document& document)
  23. : PlatformObject(document.realm())
  24. , m_document(document)
  25. {
  26. }
  27. DOMImplementation::~DOMImplementation() = default;
  28. void DOMImplementation::initialize(JS::Realm& realm)
  29. {
  30. Base::initialize(realm);
  31. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMImplementationPrototype>(realm, "DOMImplementation"));
  32. }
  33. void DOMImplementation::visit_edges(Cell::Visitor& visitor)
  34. {
  35. Base::visit_edges(visitor);
  36. visitor.visit(m_document);
  37. }
  38. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
  39. WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> DOMImplementation::create_document(DeprecatedString const& namespace_, DeprecatedString const& qualified_name, JS::GCPtr<DocumentType> doctype) const
  40. {
  41. // FIXME: This should specifically be an XML document.
  42. auto xml_document = Document::create(realm());
  43. xml_document->set_ready_for_post_load_tasks(true);
  44. JS::GCPtr<Element> element;
  45. if (!qualified_name.is_empty())
  46. element = TRY(xml_document->create_element_ns(namespace_, qualified_name, ElementCreationOptions {}));
  47. if (doctype)
  48. TRY(xml_document->append_child(*doctype));
  49. if (element)
  50. TRY(xml_document->append_child(*element));
  51. xml_document->set_origin(document().origin());
  52. if (namespace_ == Namespace::HTML)
  53. xml_document->set_content_type("application/xhtml+xml");
  54. else if (namespace_ == Namespace::SVG)
  55. xml_document->set_content_type("image/svg+xml");
  56. else
  57. xml_document->set_content_type("application/xml");
  58. return xml_document;
  59. }
  60. // https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
  61. JS::NonnullGCPtr<Document> DOMImplementation::create_html_document(DeprecatedString const& title) const
  62. {
  63. auto html_document = Document::create(realm());
  64. html_document->set_content_type("text/html");
  65. html_document->set_ready_for_post_load_tasks(true);
  66. auto doctype = heap().allocate<DocumentType>(realm(), html_document);
  67. doctype->set_name("html");
  68. MUST(html_document->append_child(*doctype));
  69. auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  70. MUST(html_document->append_child(html_element));
  71. auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  72. MUST(html_element->append_child(head_element));
  73. if (!title.is_null()) {
  74. auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  75. MUST(head_element->append_child(title_element));
  76. auto text_node = heap().allocate<Text>(realm(), html_document, title);
  77. MUST(title_element->append_child(*text_node));
  78. }
  79. auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  80. MUST(html_element->append_child(body_element));
  81. html_document->set_origin(document().origin());
  82. return html_document;
  83. }
  84. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
  85. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentType>> DOMImplementation::create_document_type(DeprecatedString const& qualified_name, DeprecatedString const& public_id, DeprecatedString const& system_id)
  86. {
  87. TRY(Document::validate_qualified_name(realm(), qualified_name));
  88. auto document_type = DocumentType::create(document());
  89. document_type->set_name(qualified_name);
  90. document_type->set_public_id(public_id);
  91. document_type->set_system_id(system_id);
  92. return document_type;
  93. }
  94. }