DOMImplementation.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/MainThreadVM.h>
  8. #include <LibWeb/DOM/DOMImplementation.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/DocumentType.h>
  11. #include <LibWeb/DOM/ElementFactory.h>
  12. #include <LibWeb/DOM/Text.h>
  13. #include <LibWeb/HTML/Origin.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/Namespace.h>
  16. namespace Web::DOM {
  17. JS::NonnullGCPtr<DOMImplementation> DOMImplementation::create(Document& document)
  18. {
  19. auto& window = document.window();
  20. return *window.heap().allocate<DOMImplementation>(document.realm(), document);
  21. }
  22. DOMImplementation::DOMImplementation(Document& document)
  23. : PlatformObject(document.window().cached_web_prototype("DOMImplementation"))
  24. , m_document(document)
  25. {
  26. }
  27. DOMImplementation::~DOMImplementation() = default;
  28. void DOMImplementation::visit_edges(Cell::Visitor& visitor)
  29. {
  30. Base::visit_edges(visitor);
  31. visitor.visit(m_document);
  32. }
  33. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
  34. WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> DOMImplementation::create_document(String const& namespace_, String const& qualified_name, JS::GCPtr<DocumentType> doctype) const
  35. {
  36. // FIXME: This should specifically be an XML document.
  37. auto xml_document = Document::create(Bindings::main_thread_internal_window_object());
  38. xml_document->set_ready_for_post_load_tasks(true);
  39. JS::GCPtr<Element> element;
  40. if (!qualified_name.is_empty())
  41. element = TRY(xml_document->create_element_ns(namespace_, qualified_name /* FIXME: and an empty dictionary */));
  42. if (doctype)
  43. xml_document->append_child(*doctype);
  44. if (element)
  45. xml_document->append_child(*element);
  46. xml_document->set_origin(document().origin());
  47. if (namespace_ == Namespace::HTML)
  48. xml_document->set_content_type("application/xhtml+xml");
  49. else if (namespace_ == Namespace::SVG)
  50. xml_document->set_content_type("image/svg+xml");
  51. else
  52. xml_document->set_content_type("application/xml");
  53. return xml_document;
  54. }
  55. // https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
  56. JS::NonnullGCPtr<Document> DOMImplementation::create_html_document(String const& title) const
  57. {
  58. auto html_document = Document::create(Bindings::main_thread_internal_window_object());
  59. html_document->set_content_type("text/html");
  60. html_document->set_ready_for_post_load_tasks(true);
  61. auto doctype = heap().allocate<DocumentType>(realm(), html_document);
  62. doctype->set_name("html");
  63. html_document->append_child(*doctype);
  64. auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML);
  65. html_document->append_child(html_element);
  66. auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML);
  67. html_element->append_child(head_element);
  68. if (!title.is_null()) {
  69. auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML);
  70. head_element->append_child(title_element);
  71. auto text_node = heap().allocate<Text>(realm(), html_document, title);
  72. title_element->append_child(*text_node);
  73. }
  74. auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML);
  75. html_element->append_child(body_element);
  76. html_document->set_origin(document().origin());
  77. return html_document;
  78. }
  79. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
  80. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentType>> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id)
  81. {
  82. TRY(Document::validate_qualified_name(global_object(), qualified_name));
  83. auto document_type = DocumentType::create(document());
  84. document_type->set_name(qualified_name);
  85. document_type->set_public_id(public_id);
  86. document_type->set_system_id(system_id);
  87. return document_type;
  88. }
  89. }