1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-06-29 07:35:26 +00:00

LibWeb: Add Element.getHTML() and ShadowRoot.getHTML()

(cherry picked from commit fb9f3f10f3090cc2822d2c681bd7350dda17830e)
This commit is contained in:
Andreas Kling 2024-06-25 14:52:06 +02:00 committed by Nico Weber
parent e4feb43e86
commit 83d0599d1c
7 changed files with 32 additions and 2 deletions

View File

@ -79,6 +79,7 @@ static bool is_platform_object(Type const& type)
"Request"sv,
"Selection"sv,
"SVGTransform"sv,
"ShadowRoot"sv,
"Table"sv,
"Text"sv,
"TextMetrics"sv,

View File

@ -2591,4 +2591,16 @@ CSS::StyleSheetList& Element::document_or_shadow_root_style_sheets()
return document().style_sheets();
}
// https://html.spec.whatwg.org/#dom-element-gethtml
WebIDL::ExceptionOr<String> Element::get_html(GetHTMLOptions const& options) const
{
// Element's getHTML(options) method steps are to return the result
// of HTML fragment serialization algorithm with this,
// options["serializableShadowRoots"], and options["shadowRoots"].
return HTML::HTMLParser::serialize_html_fragment(
*this,
options.serializable_shadow_roots ? HTML::HTMLParser::SerializableShadowRoots::Yes : HTML::HTMLParser::SerializableShadowRoots::No,
options.shadow_roots);
}
}

View File

@ -187,6 +187,8 @@ public:
WebIDL::ExceptionOr<String> inner_html() const;
WebIDL::ExceptionOr<void> set_inner_html(StringView);
WebIDL::ExceptionOr<String> get_html(GetHTMLOptions const&) const;
WebIDL::ExceptionOr<void> insert_adjacent_html(String const& position, String const&);
WebIDL::ExceptionOr<String> outer_html() const;

View File

@ -95,7 +95,7 @@ interface Element : Node {
// https://html.spec.whatwg.org/#dom-parsing-and-serialization
[FIXME, CEReactions] undefined setHTMLUnsafe((TrustedHTML or DOMString) html);
[FIXME] DOMString getHTML(optional GetHTMLOptions options = {});
DOMString getHTML(optional GetHTMLOptions options = {});
// FIXME: [CEReactions] attribute (TrustedHTML or [LegacyNullToEmptyString] DOMString) innerHTML;
[CEReactions, LegacyNullToEmptyString] attribute DOMString innerHTML;

View File

@ -10,6 +10,7 @@
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOMParsing/InnerHTML.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/Layout/BlockContainer.h>
namespace Web::DOM {
@ -75,6 +76,18 @@ WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(StringView markup)
return {};
}
// https://html.spec.whatwg.org/#dom-element-gethtml
WebIDL::ExceptionOr<String> ShadowRoot::get_html(GetHTMLOptions const& options) const
{
// ShadowRoot's getHTML(options) method steps are to return the result
// of HTML fragment serialization algorithm with this,
// options["serializableShadowRoots"], and options["shadowRoots"].
return HTML::HTMLParser::serialize_html_fragment(
*this,
options.serializable_shadow_roots ? HTML::HTMLParser::SerializableShadowRoots::Yes : HTML::HTMLParser::SerializableShadowRoots::No,
options.shadow_roots);
}
CSS::StyleSheetList& ShadowRoot::style_sheets()
{
if (!m_style_sheets)

View File

@ -46,6 +46,8 @@ public:
WebIDL::ExceptionOr<String> inner_html() const;
WebIDL::ExceptionOr<void> set_inner_html(StringView);
WebIDL::ExceptionOr<String> get_html(GetHTMLOptions const&) const;
CSS::StyleSheetList& style_sheets();
CSS::StyleSheetList const& style_sheets() const;

View File

@ -16,7 +16,7 @@ interface ShadowRoot : DocumentFragment {
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-parsing-and-serialization
[FIXME, CEReactions] undefined setHTMLUnsafe((TrustedHTML or DOMString) html);
[FIXME] DOMString getHTML(optional GetHTMLOptions options = {});
DOMString getHTML(optional GetHTMLOptions options = {});
// FIXME: [CEReactions] attribute (TrustedHTML or [LegacyNullToEmptyString] DOMString) innerHTML;
[CEReactions, LegacyNullToEmptyString] attribute DOMString innerHTML;