mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
LibWeb: Implement ChildNode.before
This commit is contained in:
parent
d1bc9358c1
commit
1bbe40c5af
4 changed files with 71 additions and 3 deletions
|
@ -6,7 +6,8 @@ interface CharacterData : Node {
|
||||||
readonly attribute Element? nextElementSibling;
|
readonly attribute Element? nextElementSibling;
|
||||||
readonly attribute Element? previousElementSibling;
|
readonly attribute Element? previousElementSibling;
|
||||||
|
|
||||||
// FIXME: This should come from a ChildNode mixin
|
// FIXME: These should come from a ChildNode mixin
|
||||||
|
[CEReactions, Unscopable] undefined before((Node or DOMString)... nodes);
|
||||||
[CEReactions, Unscopable, ImplementedAs=remove_binding] undefined remove();
|
[CEReactions, Unscopable, ImplementedAs=remove_binding] undefined remove();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,12 +6,51 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibWeb/DOM/ExceptionOr.h>
|
||||||
|
#include <LibWeb/DOM/NodeOperations.h>
|
||||||
|
|
||||||
namespace Web::DOM {
|
namespace Web::DOM {
|
||||||
|
|
||||||
// https://dom.spec.whatwg.org/#childnode
|
// https://dom.spec.whatwg.org/#childnode
|
||||||
template<typename NodeType>
|
template<typename NodeType>
|
||||||
class ChildNode {
|
class ChildNode {
|
||||||
public:
|
public:
|
||||||
|
// https://dom.spec.whatwg.org/#dom-childnode-before
|
||||||
|
ExceptionOr<void> before(Vector<Variant<NonnullRefPtr<Node>, String>> const& nodes)
|
||||||
|
{
|
||||||
|
auto* node = static_cast<NodeType*>(this);
|
||||||
|
|
||||||
|
// 1. Let parent be this’s parent.
|
||||||
|
auto* parent = node->parent();
|
||||||
|
|
||||||
|
// 2. If parent is null, then return.
|
||||||
|
if (!parent)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. Let viablePreviousSibling be this’s first preceding sibling not in nodes; otherwise null.
|
||||||
|
auto viable_previous_sibling = viable_previous_sibling_for_insertion(nodes);
|
||||||
|
|
||||||
|
// 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
|
||||||
|
auto node_or_exception = convert_nodes_to_single_node(nodes, node->document());
|
||||||
|
if (node_or_exception.is_exception())
|
||||||
|
return node_or_exception.exception();
|
||||||
|
|
||||||
|
auto node_to_insert = node_or_exception.release_value();
|
||||||
|
|
||||||
|
// 5. If viablePreviousSibling is null, then set it to parent’s first child; otherwise to viablePreviousSibling’s next sibling.
|
||||||
|
if (!viable_previous_sibling)
|
||||||
|
viable_previous_sibling = parent->first_child();
|
||||||
|
else
|
||||||
|
viable_previous_sibling = viable_previous_sibling->next_sibling();
|
||||||
|
|
||||||
|
// 6. Pre-insert node into parent before viablePreviousSibling.
|
||||||
|
auto result = parent->pre_insert(node_to_insert, viable_previous_sibling);
|
||||||
|
if (result.is_exception())
|
||||||
|
return result.exception();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
// https://dom.spec.whatwg.org/#dom-childnode-remove
|
// https://dom.spec.whatwg.org/#dom-childnode-remove
|
||||||
void remove_binding()
|
void remove_binding()
|
||||||
{
|
{
|
||||||
|
@ -27,6 +66,32 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ChildNode() = default;
|
ChildNode() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
RefPtr<Node> viable_previous_sibling_for_insertion(Vector<Variant<NonnullRefPtr<Node>, String>> const& nodes) const
|
||||||
|
{
|
||||||
|
auto* node = static_cast<NodeType const*>(this);
|
||||||
|
|
||||||
|
while (auto* previous_sibling = node->previous_sibling()) {
|
||||||
|
bool contained_in_nodes = false;
|
||||||
|
|
||||||
|
for (auto const& node_or_string : nodes) {
|
||||||
|
if (!node_or_string.template has<NonnullRefPtr<Node>>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto node_in_vector = node_or_string.template get<NonnullRefPtr<Node>>();
|
||||||
|
if (node_in_vector.ptr() == previous_sibling) {
|
||||||
|
contained_in_nodes = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!contained_in_nodes)
|
||||||
|
return previous_sibling;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,8 @@ interface DocumentType : Node {
|
||||||
readonly attribute DOMString publicId;
|
readonly attribute DOMString publicId;
|
||||||
readonly attribute DOMString systemId;
|
readonly attribute DOMString systemId;
|
||||||
|
|
||||||
// FIXME: This should come from a ChildNode mixin
|
// FIXME: These should come from a ChildNode mixin
|
||||||
|
[CEReactions, Unscopable] undefined before((Node or DOMString)... nodes);
|
||||||
[CEReactions, Unscopable, ImplementedAs=remove_binding] undefined remove();
|
[CEReactions, Unscopable, ImplementedAs=remove_binding] undefined remove();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,7 +54,8 @@ interface Element : Node {
|
||||||
readonly attribute long clientWidth;
|
readonly attribute long clientWidth;
|
||||||
readonly attribute long clientHeight;
|
readonly attribute long clientHeight;
|
||||||
|
|
||||||
// FIXME: This should come from a ChildNode mixin
|
// FIXME: These should come from a ChildNode mixin
|
||||||
|
[CEReactions, Unscopable] undefined before((Node or DOMString)... nodes);
|
||||||
[CEReactions, Unscopable, ImplementedAs=remove_binding] undefined remove();
|
[CEReactions, Unscopable, ImplementedAs=remove_binding] undefined remove();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue