mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 15:27:42 +00:00
LibWeb: Make the innerHTML setter spec compliant
This adds innerHTML to ShadowRoot in the process.
This commit is contained in:
parent
f62477c093
commit
8e0f3436a2
8 changed files with 109 additions and 9 deletions
|
@ -17,6 +17,7 @@
|
||||||
#include <LibWeb/DOM/HTMLCollection.h>
|
#include <LibWeb/DOM/HTMLCollection.h>
|
||||||
#include <LibWeb/DOM/ShadowRoot.h>
|
#include <LibWeb/DOM/ShadowRoot.h>
|
||||||
#include <LibWeb/DOM/Text.h>
|
#include <LibWeb/DOM/Text.h>
|
||||||
|
#include <LibWeb/DOMParsing/InnerHTML.h>
|
||||||
#include <LibWeb/HTML/EventLoop/EventLoop.h>
|
#include <LibWeb/HTML/EventLoop/EventLoop.h>
|
||||||
#include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
|
#include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
|
||||||
#include <LibWeb/Layout/BlockBox.h>
|
#include <LibWeb/Layout/BlockBox.h>
|
||||||
|
@ -244,16 +245,15 @@ NonnullRefPtr<CSS::StyleProperties> Element::computed_style()
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Element::set_inner_html(StringView markup)
|
ExceptionOr<void> Element::set_inner_html(String const& markup)
|
||||||
{
|
{
|
||||||
auto new_children = HTML::HTMLDocumentParser::parse_html_fragment(*this, markup);
|
auto result = DOMParsing::InnerHTML::inner_html_setter(*this, markup);
|
||||||
remove_all_children();
|
if (result.is_exception())
|
||||||
while (!new_children.is_empty()) {
|
return result.exception();
|
||||||
append_child(new_children.take_first());
|
|
||||||
}
|
|
||||||
|
|
||||||
set_needs_style_update(true);
|
set_needs_style_update(true);
|
||||||
document().invalidate_layout();
|
document().invalidate_layout();
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
|
// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
|
||||||
|
|
|
@ -81,9 +81,8 @@ public:
|
||||||
|
|
||||||
NonnullRefPtr<CSS::CSSStyleDeclaration> style_for_bindings();
|
NonnullRefPtr<CSS::CSSStyleDeclaration> style_for_bindings();
|
||||||
|
|
||||||
// FIXME: innerHTML also appears on shadow roots. https://w3c.github.io/DOM-Parsing/#dom-innerhtml
|
|
||||||
String inner_html() const;
|
String inner_html() const;
|
||||||
void set_inner_html(StringView);
|
ExceptionOr<void> set_inner_html(String const&);
|
||||||
|
|
||||||
bool is_focused() const;
|
bool is_focused() const;
|
||||||
virtual bool is_focusable() const { return false; }
|
virtual bool is_focusable() const { return false; }
|
||||||
|
|
|
@ -13,7 +13,9 @@ interface Element : Node {
|
||||||
HTMLCollection getElementsByTagName(DOMString tagName);
|
HTMLCollection getElementsByTagName(DOMString tagName);
|
||||||
HTMLCollection getElementsByClassName(DOMString className);
|
HTMLCollection getElementsByClassName(DOMString className);
|
||||||
|
|
||||||
[LegacyNullToEmptyString] attribute DOMString innerHTML;
|
// FIXME: This should come from a InnerHTML mixin.
|
||||||
|
[LegacyNullToEmptyString, CEReactions] attribute DOMString innerHTML;
|
||||||
|
|
||||||
[Reflect] attribute DOMString id;
|
[Reflect] attribute DOMString id;
|
||||||
[Reflect=class] attribute DOMString className;
|
[Reflect=class] attribute DOMString className;
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,10 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibWeb/DOM/Document.h>
|
||||||
#include <LibWeb/DOM/Event.h>
|
#include <LibWeb/DOM/Event.h>
|
||||||
#include <LibWeb/DOM/ShadowRoot.h>
|
#include <LibWeb/DOM/ShadowRoot.h>
|
||||||
|
#include <LibWeb/DOMParsing/InnerHTML.h>
|
||||||
#include <LibWeb/Layout/BlockBox.h>
|
#include <LibWeb/Layout/BlockBox.h>
|
||||||
|
|
||||||
namespace Web::DOM {
|
namespace Web::DOM {
|
||||||
|
@ -33,4 +35,22 @@ RefPtr<Layout::Node> ShadowRoot::create_layout_node()
|
||||||
return adopt_ref(*new Layout::BlockBox(document(), this, CSS::ComputedValues {}));
|
return adopt_ref(*new Layout::BlockBox(document(), this, CSS::ComputedValues {}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
|
||||||
|
String ShadowRoot::inner_html() const
|
||||||
|
{
|
||||||
|
return serialize_fragment(/* FIXME: Providing true for the require well-formed flag (which may throw) */);
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
|
||||||
|
ExceptionOr<void> ShadowRoot::set_inner_html(String const& markup)
|
||||||
|
{
|
||||||
|
auto result = DOMParsing::InnerHTML::inner_html_setter(*this, markup);
|
||||||
|
if (result.is_exception())
|
||||||
|
return result.exception();
|
||||||
|
|
||||||
|
set_needs_style_update(true);
|
||||||
|
document().invalidate_layout();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,9 @@ public:
|
||||||
// NOTE: This is intended for the JS bindings.
|
// NOTE: This is intended for the JS bindings.
|
||||||
String mode() const { return m_closed ? "closed" : "open"; }
|
String mode() const { return m_closed ? "closed" : "open"; }
|
||||||
|
|
||||||
|
String inner_html() const;
|
||||||
|
ExceptionOr<void> set_inner_html(String const&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// ^Node
|
// ^Node
|
||||||
virtual FlyString node_name() const override { return "#shadow-root"; }
|
virtual FlyString node_name() const override { return "#shadow-root"; }
|
||||||
|
|
|
@ -3,4 +3,7 @@ interface ShadowRoot : DocumentFragment {
|
||||||
readonly attribute DOMString mode;
|
readonly attribute DOMString mode;
|
||||||
readonly attribute Element host;
|
readonly attribute Element host;
|
||||||
|
|
||||||
|
// FIXME: This should come from a InnerHTML mixin.
|
||||||
|
[LegacyNullToEmptyString] attribute DOMString innerHTML;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
33
Userland/Libraries/LibWeb/DOMParsing/Algorithms.h
Normal file
33
Userland/Libraries/LibWeb/DOMParsing/Algorithms.h
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibWeb/DOM/DocumentFragment.h>
|
||||||
|
#include <LibWeb/DOM/ExceptionOr.h>
|
||||||
|
#include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
|
||||||
|
|
||||||
|
namespace Web::DOMParsing {
|
||||||
|
|
||||||
|
// https://w3c.github.io/DOM-Parsing/#dfn-fragment-parsing-algorithm
|
||||||
|
static DOM::ExceptionOr<NonnullRefPtr<DOM::DocumentFragment>> parse_fragment(String const& markup, DOM::Element& context_element)
|
||||||
|
{
|
||||||
|
// FIXME: Handle XML documents.
|
||||||
|
|
||||||
|
auto new_children = HTML::HTMLDocumentParser::parse_html_fragment(context_element, markup);
|
||||||
|
auto fragment = make_ref_counted<DOM::DocumentFragment>(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
Userland/Libraries/LibWeb/DOMParsing/InnerHTML.h
Normal file
40
Userland/Libraries/LibWeb/DOMParsing/InnerHTML.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibWeb/DOM/Element.h>
|
||||||
|
#include <LibWeb/DOM/ExceptionOr.h>
|
||||||
|
#include <LibWeb/DOM/ShadowRoot.h>
|
||||||
|
#include <LibWeb/DOMParsing/Algorithms.h>
|
||||||
|
#include <LibWeb/HTML/HTMLTemplateElement.h>
|
||||||
|
|
||||||
|
namespace Web::DOMParsing::InnerHTML {
|
||||||
|
|
||||||
|
// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
|
||||||
|
static DOM::ExceptionOr<void> inner_html_setter(NonnullRefPtr<DOM::Node> 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<DOM::Element> context_element = is<DOM::ShadowRoot>(*context_object) ? *verify_cast<DOM::ShadowRoot>(*context_object).host() : verify_cast<DOM::Element>(*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<HTML::HTMLTemplateElement>(*context_object))
|
||||||
|
context_object = verify_cast<HTML::HTMLTemplateElement>(*context_object).content();
|
||||||
|
|
||||||
|
// 4. Replace all with fragment within the context object.
|
||||||
|
context_object->replace_all(fragment);
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue