| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2022-08-08 15:52:48 +02:00
										 |  |  |  * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 16:15:49 -06:00
										 |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | #include <LibWeb/Bindings/MainThreadVM.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | #include <LibWeb/DOM/DOMImplementation.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/DocumentType.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/ElementFactory.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Text.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-12 20:37:43 +01:00
										 |  |  | #include <LibWeb/HTML/Origin.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | #include <LibWeb/Namespace.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::DOM { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 15:52:48 +02:00
										 |  |  | JS::NonnullGCPtr<DOMImplementation> DOMImplementation::create(Document& document) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-25 16:15:49 -06:00
										 |  |  |     auto& realm = document.realm(); | 
					
						
							| 
									
										
										
										
											2022-12-14 17:40:33 +00:00
										 |  |  |     return realm.heap().allocate<DOMImplementation>(realm, document); | 
					
						
							| 
									
										
										
										
											2022-08-08 15:52:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | DOMImplementation::DOMImplementation(Document& document) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  |     : PlatformObject(document.realm()) | 
					
						
							| 
									
										
										
										
											2022-08-08 15:52:48 +02:00
										 |  |  |     , m_document(document) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DOMImplementation::~DOMImplementation() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  | JS::ThrowCompletionOr<void> DOMImplementation::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  |     MUST_OR_THROW_OOM(Base::initialize(realm)); | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  |     set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMImplementationPrototype>(realm, "DOMImplementation")); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 15:52:48 +02:00
										 |  |  | void DOMImplementation::visit_edges(Cell::Visitor& visitor) | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-08 15:52:48 +02:00
										 |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_document); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | // https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> DOMImplementation::create_document(DeprecatedString const& namespace_, DeprecatedString const& qualified_name, JS::GCPtr<DocumentType> doctype) const | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  |     // FIXME: This should specifically be an XML document.
 | 
					
						
							| 
									
										
										
										
											2022-09-25 16:15:49 -06:00
										 |  |  |     auto xml_document = Document::create(realm()); | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     xml_document->set_ready_for_post_load_tasks(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::GCPtr<Element> element; | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 12:37:19 +00:00
										 |  |  |     if (!qualified_name.is_empty()) | 
					
						
							|  |  |  |         element = TRY(xml_document->create_element_ns(namespace_, qualified_name /* FIXME: and an empty dictionary */)); | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-02 08:56:17 +01:00
										 |  |  |     if (doctype) | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |         TRY(xml_document->append_child(*doctype)); | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (element) | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |         TRY(xml_document->append_child(*element)); | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 12:51:05 +01:00
										 |  |  |     xml_document->set_origin(document().origin()); | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (namespace_ == Namespace::HTML) | 
					
						
							| 
									
										
										
										
											2021-12-04 14:38:16 +01:00
										 |  |  |         xml_document->set_content_type("application/xhtml+xml"); | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  |     else if (namespace_ == Namespace::SVG) | 
					
						
							| 
									
										
										
										
											2021-12-04 14:38:16 +01:00
										 |  |  |         xml_document->set_content_type("image/svg+xml"); | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-12-04 14:38:16 +01:00
										 |  |  |         xml_document->set_content_type("application/xml"); | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return xml_document; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | JS::NonnullGCPtr<Document> DOMImplementation::create_html_document(DeprecatedString const& title) const | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-25 16:15:49 -06:00
										 |  |  |     auto html_document = Document::create(realm()); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     html_document->set_content_type("text/html"); | 
					
						
							| 
									
										
										
										
											2020-11-21 18:32:39 +00:00
										 |  |  |     html_document->set_ready_for_post_load_tasks(true); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     auto doctype = heap().allocate<DocumentType>(realm(), html_document); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  |     doctype->set_name("html"); | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |     MUST(html_document->append_child(*doctype)); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML); | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |     MUST(html_document->append_child(html_element)); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML); | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |     MUST(html_element->append_child(head_element)); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!title.is_null()) { | 
					
						
							|  |  |  |         auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML); | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |         MUST(head_element->append_child(title_element)); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |         auto text_node = heap().allocate<Text>(realm(), html_document, title); | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |         MUST(title_element->append_child(*text_node)); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML); | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |     MUST(html_element->append_child(body_element)); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 12:51:05 +01:00
										 |  |  |     html_document->set_origin(document().origin()); | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return html_document; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentType>> DOMImplementation::create_document_type(DeprecatedString const& qualified_name, DeprecatedString const& public_id, DeprecatedString const& system_id) | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-25 16:15:49 -06:00
										 |  |  |     TRY(Document::validate_qualified_name(realm(), qualified_name)); | 
					
						
							| 
									
										
										
										
											2021-12-09 12:51:05 +01:00
										 |  |  |     auto document_type = DocumentType::create(document()); | 
					
						
							| 
									
										
										
										
											2021-05-04 21:40:31 +01:00
										 |  |  |     document_type->set_name(qualified_name); | 
					
						
							|  |  |  |     document_type->set_public_id(public_id); | 
					
						
							|  |  |  |     document_type->set_system_id(system_id); | 
					
						
							|  |  |  |     return document_type; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 06:08:06 +00:00
										 |  |  | } |