2020-07-31 23:07:00 -03:00
|
|
|
/*
|
2021-04-28 17:46:44 -03:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-07-31 23:07:00 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 23:07:00 -03:00
|
|
|
*/
|
|
|
|
|
|
2024-04-26 21:09:58 -03:00
|
|
|
#include <LibWeb/Bindings/HTMLTemplateElementPrototype.h>
|
2022-08-28 08:42:07 -03:00
|
|
|
#include <LibWeb/Bindings/MainThreadVM.h>
|
2020-08-19 18:30:33 -03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-07-31 23:07:00 -03:00
|
|
|
#include <LibWeb/HTML/HTMLTemplateElement.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(HTMLTemplateElement);
|
2023-11-19 15:47:52 -03:00
|
|
|
|
2022-02-18 17:00:52 -03:00
|
|
|
HTMLTemplateElement::HTMLTemplateElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 07:20:15 -03:00
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-07-31 23:07:00 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 16:21:51 -03:00
|
|
|
HTMLTemplateElement::~HTMLTemplateElement() = default;
|
2020-07-31 23:07:00 -03:00
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void HTMLTemplateElement::initialize(JS::Realm& realm)
|
2022-08-28 08:42:07 -03:00
|
|
|
{
|
2024-03-16 09:13:08 -03:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLTemplateElement);
|
2025-04-20 11:22:57 -03:00
|
|
|
Base::initialize(realm);
|
2023-01-10 08:28:20 -03:00
|
|
|
|
2025-11-27 06:44:12 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/scripting.html#template-contents
|
|
|
|
|
// When a template element is created, the user agent must run the following steps to establish the template contents:
|
|
|
|
|
// 1. Let document be the template element's node document's appropriate template contents owner document.
|
|
|
|
|
auto document = m_document->appropriate_template_contents_owner_document();
|
|
|
|
|
|
|
|
|
|
// 2. Create a DocumentFragment object whose node document is document and host is the template element.
|
|
|
|
|
auto document_fragment = realm.create<DOM::DocumentFragment>(document);
|
|
|
|
|
document_fragment->set_host(this);
|
|
|
|
|
|
|
|
|
|
// 3. Set the template element's template contents to the newly created DocumentFragment object.
|
|
|
|
|
m_content = document_fragment;
|
2022-08-28 08:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
2022-10-29 08:06:24 -03:00
|
|
|
void HTMLTemplateElement::visit_edges(Cell::Visitor& visitor)
|
2020-08-19 18:30:33 -03:00
|
|
|
{
|
2022-10-29 08:06:24 -03:00
|
|
|
Base::visit_edges(visitor);
|
2023-11-19 00:18:00 -03:00
|
|
|
visitor.visit(m_content);
|
2020-08-19 18:30:33 -03:00
|
|
|
}
|
|
|
|
|
|
2021-07-05 01:40:47 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/scripting.html#the-template-element:concept-node-adopt-ext
|
|
|
|
|
void HTMLTemplateElement::adopted_from(DOM::Document&)
|
|
|
|
|
{
|
2025-11-27 06:44:12 -03:00
|
|
|
// 1. Let document be node's node document's appropriate template contents owner document.
|
|
|
|
|
auto document = this->document().appropriate_template_contents_owner_document();
|
2022-10-29 08:06:24 -03:00
|
|
|
|
2025-11-27 06:44:12 -03:00
|
|
|
// 2. Adopt node's template contents (a DocumentFragment object) into document.
|
|
|
|
|
document->adopt_node(content());
|
2021-07-05 01:40:47 -03:00
|
|
|
}
|
|
|
|
|
|
2021-07-05 01:40:07 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/scripting.html#the-template-element:concept-node-clone-ext
|
2025-01-11 14:37:08 -03:00
|
|
|
WebIDL::ExceptionOr<void> HTMLTemplateElement::cloned(Node& copy, bool subtree) const
|
2021-07-05 01:40:07 -03:00
|
|
|
{
|
2025-01-05 12:46:37 -03:00
|
|
|
TRY(Base::cloned(copy, subtree));
|
|
|
|
|
|
|
|
|
|
// The cloning steps for template elements given node, copy, and subtree are:
|
|
|
|
|
|
|
|
|
|
// 1. If subtree is false, then return.
|
|
|
|
|
if (!subtree)
|
2024-06-25 06:06:41 -03:00
|
|
|
return {};
|
2021-07-05 01:40:07 -03:00
|
|
|
|
2025-01-05 12:46:37 -03:00
|
|
|
// 2. For each child of node's template contents's children, in tree order:
|
|
|
|
|
// clone a node given child with document set to copy's template contents's node document,
|
|
|
|
|
// subtree set to true, and parent set to copy's template contents.
|
2025-01-21 11:12:05 -03:00
|
|
|
auto& template_copy = as<HTMLTemplateElement>(copy);
|
2024-06-25 06:06:41 -03:00
|
|
|
for (auto child = content()->first_child(); child; child = child->next_sibling()) {
|
2025-01-05 12:46:37 -03:00
|
|
|
TRY(child->clone_node(&template_copy.content()->document(), true, template_copy.content()));
|
2024-06-25 06:06:41 -03:00
|
|
|
}
|
2025-01-05 12:46:37 -03:00
|
|
|
|
2024-06-25 06:06:41 -03:00
|
|
|
return {};
|
2021-07-05 01:40:07 -03:00
|
|
|
}
|
|
|
|
|
|
2025-11-27 06:54:47 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/scripting.html#dom-template-content
|
|
|
|
|
GC::Ref<DOM::DocumentFragment> HTMLTemplateElement::content_for_bindings() const
|
|
|
|
|
{
|
|
|
|
|
// 1. Assert: this's template contents is not a ShadowRoot node.
|
|
|
|
|
VERIFY(!m_content->is_shadow_root());
|
|
|
|
|
|
|
|
|
|
// 2. Return this's template contents.
|
|
|
|
|
return *m_content;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
void HTMLTemplateElement::set_template_contents(GC::Ref<DOM::DocumentFragment> contents)
|
2024-06-25 04:43:50 -03:00
|
|
|
{
|
|
|
|
|
m_content = contents;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 23:07:00 -03:00
|
|
|
}
|