From f12dae7ea422f080ec13102f3bc219d79101a25f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 27 May 2024 13:36:00 +0200 Subject: [PATCH] LibWeb: Update spec link & comments in Element.insertAdjacentHTML() This has moved from DOM Parsing to HTML, and the comments are slightly different. --- Userland/Libraries/LibWeb/DOM/Element.cpp | 37 +++++++++++------------ Userland/Libraries/LibWeb/DOM/Element.h | 4 +-- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 438f159aab1..ebb3793e93c 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2023, Andreas Kling + * Copyright (c) 2018-2024, Andreas Kling * Copyright (c) 2022-2023, San Atkins * * SPDX-License-Identifier: BSD-2-Clause @@ -1450,19 +1450,21 @@ WebIDL::ExceptionOr Element::set_outer_html(String const& value) return {}; } -// https://w3c.github.io/DOM-Parsing/#dom-element-insertadjacenthtml -WebIDL::ExceptionOr Element::insert_adjacent_html(String const& position, String const& text) +// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-insertadjacenthtml()-method +WebIDL::ExceptionOr Element::insert_adjacent_html(String const& position, String const& string) { + // 1. Let context be null. JS::GCPtr context; - // 1. Use the first matching item from this list: + + // 2. Use the first matching item from this list: // - If position is an ASCII case-insensitive match for the string "beforebegin" // - If position is an ASCII case-insensitive match for the string "afterend" if (Infra::is_ascii_case_insensitive_match(position, "beforebegin"sv) || Infra::is_ascii_case_insensitive_match(position, "afterend"sv)) { - // Let context be the context object's parent. + // 1. Set context to this's parent. context = this->parent(); - // If context is null or a Document, throw a "NoModificationAllowedError" DOMException. + // 2. If context is null or a Document, throw a "NoModificationAllowedError" DOMException. if (!context || context->is_document()) return WebIDL::NoModificationAllowedError::create(realm(), "insertAdjacentHTML: context is null or a Document"_fly_string); } @@ -1470,7 +1472,7 @@ WebIDL::ExceptionOr Element::insert_adjacent_html(String const& position, // - If position is an ASCII case-insensitive match for the string "beforeend" else if (Infra::is_ascii_case_insensitive_match(position, "afterbegin"sv) || Infra::is_ascii_case_insensitive_match(position, "beforeend"sv)) { - // Let context be the context object. + // Set context to this. context = this; } // Otherwise @@ -1479,7 +1481,7 @@ WebIDL::ExceptionOr Element::insert_adjacent_html(String const& position, return WebIDL::SyntaxError::create(realm(), "insertAdjacentHTML: invalid position argument"_fly_string); } - // 2. If context is not an Element or the following are all true: + // 3. If context is not an Element or the following are all true: // - context's node document is an HTML document, // - context's local name is "html", and // - context's namespace is the HTML namespace; @@ -1487,39 +1489,36 @@ WebIDL::ExceptionOr Element::insert_adjacent_html(String const& position, || (context->document().document_type() == Document::Type::HTML && static_cast(*context).local_name() == "html"sv && static_cast(*context).namespace_uri() == Namespace::HTML)) { - // FIXME: let context be a new Element with - // - body as its local name, - // - The HTML namespace as its namespace, and - // - The context object's node document as its node document. + // FIXME: set context to the result of creating an element given this's node document, body, and the HTML namespace. TODO(); } - // 3. Let fragment be the result of invoking the fragment parsing algorithm with text as markup, and context as the context element. - auto fragment = TRY(DOMParsing::parse_fragment(text, verify_cast(*context))); + // 4. Let fragment be the result of invoking the fragment parsing algorithm steps with context and string. + auto fragment = TRY(DOMParsing::parse_fragment(string, verify_cast(*context))); - // 4. Use the first matching item from this list: + // 5. Use the first matching item from this list: // - If position is an ASCII case-insensitive match for the string "beforebegin" if (Infra::is_ascii_case_insensitive_match(position, "beforebegin"sv)) { - // Insert fragment into the context object's parent before the context object. + // Insert fragment into this's parent before this. parent()->insert_before(fragment, this); } // - If position is an ASCII case-insensitive match for the string "afterbegin" else if (Infra::is_ascii_case_insensitive_match(position, "afterbegin"sv)) { - // Insert fragment into the context object before its first child. + // Insert fragment into this before its first child. insert_before(fragment, first_child()); } // - If position is an ASCII case-insensitive match for the string "beforeend" else if (Infra::is_ascii_case_insensitive_match(position, "beforeend"sv)) { - // Append fragment to the context object. + // Append fragment to this. TRY(append_child(fragment)); } // - If position is an ASCII case-insensitive match for the string "afterend" else if (Infra::is_ascii_case_insensitive_match(position, "afterend"sv)) { - // Insert fragment into the context object's parent before the context object's next sibling. + // Insert fragment into this's parent before this's next sibling. parent()->insert_before(fragment, next_sibling()); } return {}; diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 1f3ddb77307..6c0410ac86c 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2023, Andreas Kling + * Copyright (c) 2018-2024, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -179,7 +179,7 @@ public: WebIDL::ExceptionOr inner_html() const; WebIDL::ExceptionOr set_inner_html(StringView); - WebIDL::ExceptionOr insert_adjacent_html(String const& position, String const& text); + WebIDL::ExceptionOr insert_adjacent_html(String const& position, String const&); WebIDL::ExceptionOr outer_html() const; WebIDL::ExceptionOr set_outer_html(String const&);