DOMImplementation.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/DOMImplementation.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/DocumentType.h>
  9. #include <LibWeb/DOM/ElementFactory.h>
  10. #include <LibWeb/DOM/Text.h>
  11. #include <LibWeb/Namespace.h>
  12. #include <LibWeb/Origin.h>
  13. namespace Web::DOM {
  14. DOMImplementation::DOMImplementation(Document& document)
  15. : RefCountForwarder(document)
  16. {
  17. }
  18. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
  19. ExceptionOr<NonnullRefPtr<Document>> DOMImplementation::create_document(const String& namespace_, const String& qualified_name) const
  20. {
  21. // FIXME: This should specifically be an XML document.
  22. auto xml_document = Document::create();
  23. xml_document->set_ready_for_post_load_tasks(true);
  24. RefPtr<Element> element;
  25. if (!qualified_name.is_empty()) {
  26. auto new_element = xml_document->create_element_ns(namespace_, qualified_name /* FIXME: and an empty dictionary */);
  27. if (new_element.is_exception())
  28. return new_element.exception();
  29. element = new_element.release_value();
  30. }
  31. // FIXME: If doctype is non-null, append doctype to document.
  32. if (element)
  33. xml_document->append_child(element.release_nonnull());
  34. xml_document->set_origin(document().origin());
  35. if (namespace_ == Namespace::HTML)
  36. xml_document->set_content_type("application/xhtml+xml");
  37. else if (namespace_ == Namespace::SVG)
  38. xml_document->set_content_type("image/svg+xml");
  39. else
  40. xml_document->set_content_type("application/xml");
  41. return xml_document;
  42. }
  43. // https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
  44. NonnullRefPtr<Document> DOMImplementation::create_html_document(const String& title) const
  45. {
  46. // FIXME: This should specifically be a HTML document.
  47. auto html_document = Document::create();
  48. html_document->set_content_type("text/html");
  49. html_document->set_ready_for_post_load_tasks(true);
  50. auto doctype = adopt_ref(*new DocumentType(html_document));
  51. doctype->set_name("html");
  52. html_document->append_child(doctype);
  53. auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML);
  54. html_document->append_child(html_element);
  55. auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML);
  56. html_element->append_child(head_element);
  57. if (!title.is_null()) {
  58. auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML);
  59. head_element->append_child(title_element);
  60. auto text_node = adopt_ref(*new Text(html_document, title));
  61. title_element->append_child(text_node);
  62. }
  63. auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML);
  64. html_element->append_child(body_element);
  65. html_document->set_origin(document().origin());
  66. return html_document;
  67. }
  68. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
  69. NonnullRefPtr<DocumentType> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id)
  70. {
  71. // FIXME: Validate qualified_name.
  72. auto document_type = DocumentType::create(document());
  73. document_type->set_name(qualified_name);
  74. document_type->set_public_id(public_id);
  75. document_type->set_system_id(system_id);
  76. return document_type;
  77. }
  78. }