Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Unified Diff: sky/engine/core/dom/ContainerNode.cpp

Issue 924203002: Morph the APIs for Node, ParentNode, and Element closer to the specs (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sky/engine/core/dom/ContainerNode.cpp
diff --git a/sky/engine/core/dom/ContainerNode.cpp b/sky/engine/core/dom/ContainerNode.cpp
index aaf3be37000e22471a68c2ed4b14ce083790edc2..97f3708d03789a6e8d0fc22c1da1a2f450866ee8 100644
--- a/sky/engine/core/dom/ContainerNode.cpp
+++ b/sky/engine/core/dom/ContainerNode.cpp
@@ -51,7 +51,7 @@ static void collectChildrenAndRemoveFromOldParent(Node& node, NodeVector& nodes,
{
if (node.isDocumentFragment()) {
DocumentFragment& fragment = toDocumentFragment(node);
- getChildNodes(fragment, nodes);
+ appendChildNodes(fragment, nodes);
fragment.removeChildren();
return;
}
@@ -325,7 +325,7 @@ void ContainerNode::willRemoveChild(Node& child)
void ContainerNode::willRemoveChildren()
{
NodeVector children;
- getChildNodes(*this, children);
+ appendChildNodes(*this, children);
ChildListMutationScope mutation(*this);
for (NodeVector::const_iterator it = children.begin(); it != children.end(); ++it) {
@@ -869,6 +869,72 @@ void ContainerNode::setHovered(bool over)
}
}
+Element* ContainerNode::firstElementChild() const
+{
+ return ElementTraversal::firstChild(*this);
+}
+
+Element* ContainerNode::lastElementChild() const
+{
+ return ElementTraversal::lastChild(*this);
+}
+
+Vector<RefPtr<Node>> ContainerNode::getChildNodes() const
+{
+ Vector<RefPtr<Node>> result;
+ for (Node* node = firstChild(); node; node = node->nextSibling())
+ result.append(node);
+ return result;
eseidel 2015/02/14 00:12:23 This copies the vector unless c++ is smarter now..
+}
+
+Vector<RefPtr<Element>> ContainerNode::getChildElements() const
+{
+ Vector<RefPtr<Element>> result;
+ for (Element* element = ElementTraversal::firstWithin(*this); element; element = ElementTraversal::next(*element, this))
+ result.append(element);
+ return result;
+}
+
+void ContainerNode::append(Vector<RefPtr<Node>>& nodes, ExceptionState& es)
+{
+ RefPtr<ContainerNode> protect(this);
+ for (auto& node : nodes) {
+ appendChild(node.release(), es);
+ if (es.had_exception())
+ return;
+ }
+}
+
+void ContainerNode::prepend(Vector<RefPtr<Node>>& nodes, ExceptionState& es)
+{
+ RefPtr<ContainerNode> protect(this);
+ RefPtr<Node> refChild = m_firstChild;
+ for (auto& node : nodes) {
+ insertBefore(node.release(), refChild.get(), es);
+ if (es.had_exception())
+ return;
+ }
+}
+
+PassRefPtr<Node> ContainerNode::prependChild(PassRefPtr<Node> node, ExceptionState& es)
+{
+ return insertBefore(node, m_firstChild, es);
+}
+
+PassRefPtr<Node> ContainerNode::setChild(PassRefPtr<Node> node, ExceptionState& es)
+{
+ RefPtr<ContainerNode> protect(this);
+ removeChildren();
+ return appendChild(node, es);
+}
+
+void ContainerNode::setChildren(Vector<RefPtr<Node>>& nodes, ExceptionState& es)
+{
+ RefPtr<ContainerNode> protect(this);
+ removeChildren();
+ append(nodes, es);
+}
+
unsigned ContainerNode::countChildren() const
{
unsigned count = 0;

Powered by Google App Engine
This is Rietveld 408576698