DOMImplementation.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/DocumentType.h>
  11. #include <LibWeb/DOM/ElementFactory.h>
  12. #include <LibWeb/DOM/Text.h>
  13. #include <LibWeb/DOM/XMLDocument.h>
  14. #include <LibWeb/HTML/Origin.h>
  15. #include <LibWeb/Namespace.h>
  16. namespace Web::DOM {
  17. JS_DEFINE_ALLOCATOR(DOMImplementation);
  18. JS::NonnullGCPtr<DOMImplementation> DOMImplementation::create(Document& document)
  19. {
  20. auto& realm = document.realm();
  21. return realm.heap().allocate<DOMImplementation>(realm, document);
  22. }
  23. DOMImplementation::DOMImplementation(Document& document)
  24. : PlatformObject(document.realm())
  25. , m_document(document)
  26. {
  27. }
  28. DOMImplementation::~DOMImplementation() = default;
  29. void DOMImplementation::initialize(JS::Realm& realm)
  30. {
  31. Base::initialize(realm);
  32. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMImplementationPrototype>(realm, "DOMImplementation"_fly_string));
  33. }
  34. void DOMImplementation::visit_edges(Cell::Visitor& visitor)
  35. {
  36. Base::visit_edges(visitor);
  37. visitor.visit(m_document);
  38. }
  39. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
  40. WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> DOMImplementation::create_document(Optional<FlyString> const& namespace_, String const& qualified_name, JS::GCPtr<DocumentType> doctype) const
  41. {
  42. // 1. Let document be a new XMLDocument
  43. auto xml_document = XMLDocument::create(realm());
  44. xml_document->set_ready_for_post_load_tasks(true);
  45. // 2. Let element be null.
  46. JS::GCPtr<Element> element;
  47. // 3. If qualifiedName is not the empty string, then set element to the result of running the internal createElementNS steps, given document, namespace, qualifiedName, and an empty dictionary.
  48. if (!qualified_name.is_empty())
  49. element = TRY(xml_document->create_element_ns(namespace_, qualified_name, ElementCreationOptions {}));
  50. // 4. If doctype is non-null, append doctype to document.
  51. if (doctype)
  52. TRY(xml_document->append_child(*doctype));
  53. // 5. If element is non-null, append element to document.
  54. if (element)
  55. TRY(xml_document->append_child(*element));
  56. // 6. document’s origin is this’s associated document’s origin.
  57. xml_document->set_origin(document().origin());
  58. // 7. document’s content type is determined by namespace:
  59. if (namespace_ == Namespace::HTML) {
  60. // -> HTML namespace
  61. xml_document->set_content_type("application/xhtml+xml"_string);
  62. } else if (namespace_ == Namespace::SVG) {
  63. // -> SVG namespace
  64. xml_document->set_content_type("image/svg+xml"_string);
  65. } else {
  66. // -> Any other namespace
  67. xml_document->set_content_type("application/xml"_string);
  68. }
  69. // 8. Return document.
  70. return xml_document;
  71. }
  72. // https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
  73. JS::NonnullGCPtr<Document> DOMImplementation::create_html_document(Optional<String> const& title) const
  74. {
  75. // 1. Let doc be a new document that is an HTML document.
  76. auto html_document = Document::create(realm());
  77. // 2. Set doc’s content type to "text/html".
  78. html_document->set_content_type("text/html"_string);
  79. html_document->set_ready_for_post_load_tasks(true);
  80. // 3. Append a new doctype, with "html" as its name and with its node document set to doc, to doc.
  81. auto doctype = heap().allocate<DocumentType>(realm(), html_document);
  82. doctype->set_name("html"_string);
  83. MUST(html_document->append_child(*doctype));
  84. // 4. Append the result of creating an element given doc, html, and the HTML namespace, to doc.
  85. auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  86. MUST(html_document->append_child(html_element));
  87. // 5. Append the result of creating an element given doc, head, and the HTML namespace, to the html element created earlier.
  88. auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  89. MUST(html_element->append_child(head_element));
  90. // 6. If title is given:
  91. if (title.has_value()) {
  92. // 1. Append the result of creating an element given doc, title, and the HTML namespace, to the head element created earlier.
  93. auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  94. MUST(head_element->append_child(title_element));
  95. // 2. Append a new Text node, with its data set to title (which could be the empty string) and its node document set to doc, to the title element created earlier.
  96. auto text_node = heap().allocate<Text>(realm(), html_document, title.value());
  97. MUST(title_element->append_child(*text_node));
  98. }
  99. // 7. Append the result of creating an element given doc, body, and the HTML namespace, to the html element created earlier.
  100. auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  101. MUST(html_element->append_child(body_element));
  102. // 8. doc’s origin is this’s associated document’s origin.
  103. html_document->set_origin(document().origin());
  104. // 9. Return doc.
  105. return html_document;
  106. }
  107. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
  108. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentType>> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id)
  109. {
  110. // 1. Validate qualifiedName.
  111. TRY(Document::validate_qualified_name(realm(), qualified_name));
  112. // 2. Return a new doctype, with qualifiedName as its name, publicId as its public ID, and systemId as its system ID, and with its node document set to the associated document of this.
  113. auto document_type = DocumentType::create(document());
  114. document_type->set_name(qualified_name);
  115. document_type->set_public_id(public_id);
  116. document_type->set_system_id(system_id);
  117. return document_type;
  118. }
  119. }