DOMImplementation.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/HTML/Origin.h>
  12. #include <LibWeb/Namespace.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(String const& namespace_, String const& 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(String const& title) const
  42. {
  43. auto html_document = Document::create();
  44. html_document->set_content_type("text/html");
  45. html_document->set_ready_for_post_load_tasks(true);
  46. auto doctype = adopt_ref(*new DocumentType(html_document));
  47. doctype->set_name("html");
  48. html_document->append_child(doctype);
  49. auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML);
  50. html_document->append_child(html_element);
  51. auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML);
  52. html_element->append_child(head_element);
  53. if (!title.is_null()) {
  54. auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML);
  55. head_element->append_child(title_element);
  56. auto text_node = adopt_ref(*new Text(html_document, title));
  57. title_element->append_child(text_node);
  58. }
  59. auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML);
  60. html_element->append_child(body_element);
  61. html_document->set_origin(document().origin());
  62. return html_document;
  63. }
  64. // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
  65. ExceptionOr<NonnullRefPtr<DocumentType>> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id)
  66. {
  67. TRY(Document::validate_qualified_name(qualified_name));
  68. auto document_type = DocumentType::create(document());
  69. document_type->set_name(qualified_name);
  70. document_type->set_public_id(public_id);
  71. document_type->set_system_id(system_id);
  72. return document_type;
  73. }
  74. }