From 7619dbdbb4ce1d38b17ac5fd97af16a83e7c2f6a Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 2 Oct 2021 22:45:37 +0200 Subject: [PATCH] LibWeb: Clean up static function in header 'static' for a function means that the symbol shall not be made public for the result of the current compilation unit. This does not make sense in a header, especially not if it's a large function that is used in more than one place and not that performance-sensitive. --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + Userland/Libraries/LibWeb/DOM/Element.cpp | 2 +- Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp | 2 +- .../Libraries/LibWeb/DOMParsing/Algorithms.h | 33 ----------- .../Libraries/LibWeb/DOMParsing/InnerHTML.cpp | 55 +++++++++++++++++++ .../Libraries/LibWeb/DOMParsing/InnerHTML.h | 25 +-------- 6 files changed, 60 insertions(+), 58 deletions(-) delete mode 100644 Userland/Libraries/LibWeb/DOMParsing/Algorithms.h create mode 100644 Userland/Libraries/LibWeb/DOMParsing/InnerHTML.cpp diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 1708ef5a3b..ad49eedfaa 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -78,6 +78,7 @@ set(SOURCES DOM/Text.idl DOM/Timer.cpp DOM/Window.cpp + DOMParsing/InnerHTML.cpp DOMTreeModel.cpp Dump.cpp FontCache.cpp diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 9cd7d08c1d..565630af33 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -265,7 +265,7 @@ DOM::ExceptionOr Element::matches(StringView selectors) const ExceptionOr Element::set_inner_html(String const& markup) { - auto result = DOMParsing::InnerHTML::inner_html_setter(*this, markup); + auto result = DOMParsing::inner_html_setter(*this, markup); if (result.is_exception()) return result.exception(); diff --git a/Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp b/Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp index 7c7759f453..02f2c02970 100644 --- a/Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp +++ b/Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp @@ -44,7 +44,7 @@ String ShadowRoot::inner_html() const // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml ExceptionOr ShadowRoot::set_inner_html(String const& markup) { - auto result = DOMParsing::InnerHTML::inner_html_setter(*this, markup); + auto result = DOMParsing::inner_html_setter(*this, markup); if (result.is_exception()) return result.exception(); diff --git a/Userland/Libraries/LibWeb/DOMParsing/Algorithms.h b/Userland/Libraries/LibWeb/DOMParsing/Algorithms.h deleted file mode 100644 index cf25e13083..0000000000 --- a/Userland/Libraries/LibWeb/DOMParsing/Algorithms.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2021, Luke Wilde - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include - -namespace Web::DOMParsing { - -// https://w3c.github.io/DOM-Parsing/#dfn-fragment-parsing-algorithm -static DOM::ExceptionOr> parse_fragment(String const& markup, DOM::Element& context_element) -{ - // FIXME: Handle XML documents. - - auto new_children = HTML::HTMLParser::parse_html_fragment(context_element, markup); - auto fragment = make_ref_counted(context_element.document()); - - for (auto& child : new_children) { - // I don't know if this can throw here, but let's be safe. - auto result = fragment->append_child(child); - if (result.is_exception()) - return result.exception(); - } - - return fragment; -} - -} diff --git a/Userland/Libraries/LibWeb/DOMParsing/InnerHTML.cpp b/Userland/Libraries/LibWeb/DOMParsing/InnerHTML.cpp new file mode 100644 index 0000000000..6831d9f0c9 --- /dev/null +++ b/Userland/Libraries/LibWeb/DOMParsing/InnerHTML.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021, Luke Wilde + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web::DOMParsing { + +// https://w3c.github.io/DOM-Parsing/#dfn-fragment-parsing-algorithm +static DOM::ExceptionOr> parse_fragment(String const& markup, DOM::Element& context_element) +{ + // FIXME: Handle XML documents. + + auto new_children = HTML::HTMLParser::parse_html_fragment(context_element, markup); + auto fragment = make_ref_counted(context_element.document()); + + for (auto& child : new_children) { + // I don't know if this can throw here, but let's be safe. + auto result = fragment->append_child(child); + if (result.is_exception()) + return result.exception(); + } + + return fragment; +} + +// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml +DOM::ExceptionOr inner_html_setter(NonnullRefPtr context_object, String const& value) +{ + // 1. Let context element be the context object's host if the context object is a ShadowRoot object, or the context object otherwise. + // (This is handled in Element and ShadowRoot) + NonnullRefPtr context_element = is(*context_object) ? *verify_cast(*context_object).host() : verify_cast(*context_object); + + // 2. Let fragment be the result of invoking the fragment parsing algorithm with the new value as markup, and with context element. + auto fragment_or_exception = parse_fragment(value, context_element); + if (fragment_or_exception.is_exception()) + return fragment_or_exception.exception(); + auto fragment = fragment_or_exception.release_value(); + + // 3. If the context object is a template element, then let context object be the template's template contents (a DocumentFragment). + if (is(*context_object)) + context_object = verify_cast(*context_object).content(); + + // 4. Replace all with fragment within the context object. + context_object->replace_all(fragment); + + return {}; +} + +} diff --git a/Userland/Libraries/LibWeb/DOMParsing/InnerHTML.h b/Userland/Libraries/LibWeb/DOMParsing/InnerHTML.h index ef95a21e4d..55175e9d47 100644 --- a/Userland/Libraries/LibWeb/DOMParsing/InnerHTML.h +++ b/Userland/Libraries/LibWeb/DOMParsing/InnerHTML.h @@ -9,32 +9,11 @@ #include #include #include -#include #include -namespace Web::DOMParsing::InnerHTML { +namespace Web::DOMParsing { // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml -static DOM::ExceptionOr inner_html_setter(NonnullRefPtr context_object, String const& value) -{ - // 1. Let context element be the context object's host if the context object is a ShadowRoot object, or the context object otherwise. - // (This is handled in Element and ShadowRoot) - NonnullRefPtr context_element = is(*context_object) ? *verify_cast(*context_object).host() : verify_cast(*context_object); - - // 2. Let fragment be the result of invoking the fragment parsing algorithm with the new value as markup, and with context element. - auto fragment_or_exception = parse_fragment(value, context_element); - if (fragment_or_exception.is_exception()) - return fragment_or_exception.exception(); - auto fragment = fragment_or_exception.release_value(); - - // 3. If the context object is a template element, then let context object be the template's template contents (a DocumentFragment). - if (is(*context_object)) - context_object = verify_cast(*context_object).content(); - - // 4. Replace all with fragment within the context object. - context_object->replace_all(fragment); - - return {}; -} +DOM::ExceptionOr inner_html_setter(NonnullRefPtr context_object, String const& value); }