DOMImplementation.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, RefPtr<DocumentType> doctype) 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. if (doctype)
  32. xml_document->append_child(doctype.release_nonnull());
  33. if (element)
  34. xml_document->append_child(element.release_nonnull());
  35. xml_document->set_origin(document().origin());
  36. if (namespace_ == Namespace::HTML)
  37. xml_document->set_content_type("application/xhtml+xml");
  38. else if (namespace_ == Namespace::SVG)
  39. xml_document->set_content_type("image/svg+xml");
  40. else
  41. xml_document->set_content_type("application/xml");
  42. return xml_document;
  43. }
  44. // https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
  45. NonnullRefPtr<Document> DOMImplementation::create_html_document(const String& title) const
  46. {
  47. // FIXME: This should specifically be a HTML document.
  48. auto html_document = Document::create();
  49. html_document->set_content_type("text/html");
  50. html_document->set_ready_for_post_load_tasks(true);
  51. auto doctype = adopt_ref(*new DocumentType(html_document));
  52. doctype->set_name("html");
  53. html_document->append_child(doctype);
  54. auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML);
  55. html_document->append_child(html_element);
  56. auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML);
  57. html_element->append_child(head_element);
  58. if (!title.is_null()) {
  59. auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML);
  60. head_element->append_child(title_element);
  61. auto text_node = adopt_ref(*new Text(html_document, title));
  62. title_element->append_child(text_node);
  63. }
  64. auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML);
  65. html_element->append_child(body_element);
  66. html_document->set_origin(document().origin());
  67. return html_document;
  68. }
  69. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
  70. ExceptionOr<NonnullRefPtr<DocumentType>> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id)
  71. {
  72. auto result = Document::validate_qualified_name(qualified_name);
  73. if (result.is_exception())
  74. return result.exception();
  75. auto document_type = DocumentType::create(document());
  76. document_type->set_name(qualified_name);
  77. document_type->set_public_id(public_id);
  78. document_type->set_system_id(system_id);
  79. return document_type;
  80. }
  81. }