/* * Copyright (c) 2021, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include namespace Web::DOMParsing { // https://w3c.github.io/DOM-Parsing/#dfn-fragment-parsing-algorithm WebIDL::ExceptionOr> parse_fragment(DeprecatedString const& markup, DOM::Element& context_element) { // FIXME: Handle XML documents. auto& realm = context_element.realm(); auto new_children = HTML::HTMLParser::parse_html_fragment(context_element, markup); auto fragment = MUST_OR_THROW_OOM(realm.heap().allocate(realm, context_element.document())); for (auto& child : new_children) { // I don't know if this can throw here, but let's be safe. (void)TRY(fragment->append_child(*child)); } return fragment; } // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml WebIDL::ExceptionOr inner_html_setter(JS::NonnullGCPtr context_object, DeprecatedString 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) JS::NonnullGCPtr 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 = TRY(parse_fragment(value, context_element)); // 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); // NOTE: We don't invalidate style & layout for