Ver Fonte

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

Andreas Kling há 1 ano atrás
pai
commit
fb9f3f10f3

+ 1 - 0
Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp

@@ -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,

+ 12 - 0
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -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);
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/DOM/Element.h

@@ -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;

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Element.idl

@@ -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;

+ 13 - 0
Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp

@@ -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)

+ 2 - 0
Userland/Libraries/LibWeb/DOM/ShadowRoot.h

@@ -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;
 

+ 1 - 1
Userland/Libraries/LibWeb/DOM/ShadowRoot.idl

@@ -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;