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

This commit is contained in:
Andreas Kling 2024-06-25 14:52:06 +02:00 committed by Andreas Kling
parent 0c47b3ff97
commit fb9f3f10f3
Notes: sideshowbarker 2024-07-17 07:14:09 +09:00
7 changed files with 32 additions and 2 deletions

View file

@ -80,6 +80,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

@ -2583,4 +2583,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;