Skip to content

Commit

Permalink
LibWeb/HTML: Update cloning steps to current spec algorithms
Browse files Browse the repository at this point in the history
Reflects the changes in whatwg/html#10859

I've also added missing calls to the Base::cloned() method, and modified
a couple of spec links to point to the multipage version.

I took the liberty to fix a spec typo, and submitted a PR for it:
whatwg/html#10892
  • Loading branch information
AtkinsSJ committed Jan 5, 2025
1 parent 9884cd0 commit 8fb7c70
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
6 changes: 4 additions & 2 deletions Libraries/LibWeb/HTML/HTMLInputElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1660,9 +1660,11 @@ void HTMLInputElement::apply_presentational_hints(GC::Ref<CSS::CascadedPropertie
}

// https://html.spec.whatwg.org/multipage/input.html#the-input-element%3Aconcept-node-clone-ext
WebIDL::ExceptionOr<void> HTMLInputElement::cloned(DOM::Node& copy, bool)
WebIDL::ExceptionOr<void> HTMLInputElement::cloned(DOM::Node& copy, bool subtree)
{
// The cloning steps for input elements must propagate the value, dirty value flag, checkedness, and dirty checkedness flag from the node being cloned to the copy.
TRY(Base::cloned(copy, subtree));

// The cloning steps for input elements given node, copy, and subtree are to propagate the value, dirty value flag, checkedness, and dirty checkedness flag from node to copy.
auto& input_clone = verify_cast<HTMLInputElement>(copy);
input_clone.m_value = m_value;
input_clone.m_dirty_value = m_dirty_value;
Expand Down
10 changes: 5 additions & 5 deletions Libraries/LibWeb/HTML/HTMLOrSVGElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void HTMLOrSVGElement<ElementBase>::blur()
// User agents may selectively or uniformly ignore calls to this method for usability reasons.
}

// https://html.spec.whatwg.org/#dom-noncedelement-nonce
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#dom-noncedelement-nonce
template<typename ElementBase>
void HTMLOrSVGElement<ElementBase>::attribute_changed(FlyString const& local_name, Optional<String> const&, Optional<String> const& value, Optional<FlyString> const& namespace_)
{
Expand All @@ -76,17 +76,17 @@ void HTMLOrSVGElement<ElementBase>::attribute_changed(FlyString const& local_nam
}
}

// https://html.spec.whatwg.org/#dom-noncedelement-nonce
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#dom-noncedelement-nonce
template<typename ElementBase>
WebIDL::ExceptionOr<void> HTMLOrSVGElement<ElementBase>::cloned(DOM::Node& copy, bool)
{
// The cloning steps for elements that include HTMLOrSVGElement must set the
// [[CryptographicNonce]] slot on the copy to the value of the slot on the element being cloned.
// The cloning steps for elements that include HTMLOrSVGElement given node, copy, and subtree
// are to set copy's [[CryptographicNonce]] to node's [[CryptographicNonce]].
static_cast<ElementBase&>(copy).m_cryptographic_nonce = m_cryptographic_nonce;
return {};
}

// https://html.spec.whatwg.org/#dom-noncedelement-nonce
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#dom-noncedelement-nonce
template<typename ElementBase>
void HTMLOrSVGElement<ElementBase>::inserted()
{
Expand Down
22 changes: 13 additions & 9 deletions Libraries/LibWeb/HTML/HTMLTemplateElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,24 @@ void HTMLTemplateElement::adopted_from(DOM::Document&)
}

// https://html.spec.whatwg.org/multipage/scripting.html#the-template-element:concept-node-clone-ext
WebIDL::ExceptionOr<void> HTMLTemplateElement::cloned(Node& copy, bool clone_children)
WebIDL::ExceptionOr<void> HTMLTemplateElement::cloned(Node& copy, bool subtree)
{
// 1. If the clone children flag is not set in the calling clone algorithm, return.
if (!clone_children)
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)
return {};

// 2. Let copied contents be the result of cloning all the children of node's template contents,
// with document set to copy's template contents's node document, and with the clone children flag set.
// 3. Append copied contents to copy's template contents.
auto& template_clone = verify_cast<HTMLTemplateElement>(copy);
// 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.
auto& template_copy = verify_cast<HTMLTemplateElement>(copy);
for (auto child = content()->first_child(); child; child = child->next_sibling()) {
auto cloned_child = TRY(child->clone_node(&template_clone.content()->document(), true));
TRY(template_clone.content()->append_child(cloned_child));
TRY(child->clone_node(&template_copy.content()->document(), true, template_copy.content()));
}

return {};
}

Expand Down
6 changes: 4 additions & 2 deletions Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ void HTMLTextAreaElement::clear_algorithm()
}

// https://html.spec.whatwg.org/multipage/forms.html#the-textarea-element:concept-node-clone-ext
WebIDL::ExceptionOr<void> HTMLTextAreaElement::cloned(DOM::Node& copy, bool)
WebIDL::ExceptionOr<void> HTMLTextAreaElement::cloned(DOM::Node& copy, bool subtree)
{
// The cloning steps for textarea elements must propagate the raw value and dirty value flag from the node being cloned to the copy.
TRY(Base::cloned(copy, subtree));

// The cloning steps for textarea elements given node, copy, and subtree are to propagate the raw value and dirty value flag from node to copy.
auto& textarea_copy = verify_cast<HTMLTextAreaElement>(copy);
textarea_copy.m_raw_value = m_raw_value;
textarea_copy.m_dirty_value = m_dirty_value;
Expand Down

0 comments on commit 8fb7c70

Please sign in to comment.