DOMImplementation.cpp 3.3 KB

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