showNodePath will be useful for debugging purpose.
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 May 2012 09:03:20 +0000 (09:03 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 May 2012 09:03:20 +0000 (09:03 +0000)
https://bugs.webkit.org/show_bug.cgi?id=86450

This patch implements showNodePath, which outputs node information in
a xpath-like format, e.g. /HTML/BODY/DIV[@id="test" and position()=0]/P[0]

Patch by Takashi Sakamoto <tasak@google.com> on 2012-05-17
Reviewed by Hajime Morita.

No new tests, just adding debugging interface.

* dom/Node.cpp:
(WebCore::Node::showNodePathForThis):
(WebCore):
(showNodePath):
* dom/Node.h:
(Node):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@117417 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/dom/Node.cpp
Source/WebCore/dom/Node.h

index 478f34d..6da4b40 100644 (file)
@@ -1,3 +1,22 @@
+2012-05-17  Takashi Sakamoto  <tasak@google.com>
+
+        showNodePath will be useful for debugging purpose.
+        https://bugs.webkit.org/show_bug.cgi?id=86450
+
+        This patch implements showNodePath, which outputs node information in
+        a xpath-like format, e.g. /HTML/BODY/DIV[@id="test" and position()=0]/P[0]
+
+        Reviewed by Hajime Morita.
+
+        No new tests, just adding debugging interface.
+
+        * dom/Node.cpp:
+        (WebCore::Node::showNodePathForThis):
+        (WebCore):
+        (showNodePath):
+        * dom/Node.h:
+        (Node):
+
 2012-05-17  Pravin D  <pravind.2k4@gmail.com>
 
         REGRESSION (r116331): RSS Headlines/links are missing (-webkit-box-flex broken?)
index e8ea932..0daf079 100644 (file)
@@ -2229,6 +2229,57 @@ void Node::showTreeForThis() const
     showTreeAndMark(this, "*");
 }
 
+void Node::showNodePathForThis() const
+{
+    Vector<const Node*, 16> chain;
+    const Node* node = this;
+    while (node->parentOrHostNode()) {
+        chain.append(node);
+        node = node->parentOrHostNode();
+    }
+    for (unsigned index = chain.size(); index > 0; --index) {
+        const Node* node = chain[index - 1];
+        if (node->isShadowRoot()) {
+            int count = 0;
+            for (ShadowRoot* shadowRoot = oldestShadowRootFor(node); shadowRoot && shadowRoot != node; shadowRoot = shadowRoot->youngerShadowRoot())
+                ++count;
+            fprintf(stderr, "/#shadow-root[%d]", count);
+            continue;
+        }
+
+        switch (node->nodeType()) {
+        case ELEMENT_NODE: {
+            fprintf(stderr, "/%s", node->nodeName().utf8().data());
+
+            const Element* element = toElement(node);
+            const AtomicString& idattr = element->getIdAttribute();
+            bool hasIdAttr = !idattr.isNull() && !idattr.isEmpty();
+            if (node->previousSibling() || node->nextSibling()) {
+                int count = 0;
+                for (Node* previous = node->previousSibling(); previous; previous = previous->previousSibling())
+                    if (previous->nodeName() == node->nodeName())
+                        ++count;
+                if (hasIdAttr)
+                    fprintf(stderr, "[@id=\"%s\" and position()=%d]", idattr.string().utf8().data(), count);
+                else
+                    fprintf(stderr, "[%d]", count);
+            } else if (hasIdAttr)
+                fprintf(stderr, "[@id=\"%s\"]", idattr.string().utf8().data());
+            break;
+        }
+        case TEXT_NODE:
+            fprintf(stderr, "/text()");
+            break;
+        case ATTRIBUTE_NODE:
+            fprintf(stderr, "/@%s", node->nodeName().utf8().data());
+            break;
+        default:
+            break;
+        }
+    }
+    fprintf(stderr, "\n");
+}
+
 static void traverseTreeAndMark(const String& baseIndent, const Node* rootNode, const Node* markedNode1, const char* markedLabel1, const Node* markedNode2, const char* markedLabel2)
 {
     for (const Node* node = rootNode; node; node = node->traverseNextNode()) {
@@ -2922,4 +2973,10 @@ void showTree(const WebCore::Node* node)
         node->showTreeForThis();
 }
 
+void showNodePath(const WebCore::Node* node)
+{
+    if (node)
+        node->showNodePathForThis();
+}
+
 #endif
index 9d34b97..d56e2ff 100644 (file)
@@ -551,6 +551,7 @@ public:
 
     void showNode(const char* prefix = "") const;
     void showTreeForThis() const;
+    void showNodePathForThis() const;
     void showTreeAndMark(const Node* markedNode1, const char* markedLabel1, const Node* markedNode2 = 0, const char* markedLabel2 = 0) const;
     void showTreeForThisAcrossFrame() const;
 #endif
@@ -861,6 +862,7 @@ inline void Node::reattachIfAttached()
 #ifndef NDEBUG
 // Outside the WebCore namespace for ease of invocation from gdb.
 void showTree(const WebCore::Node*);
+void showNodePath(const WebCore::Node*);
 #endif
 
 #endif