Reviewed by Anders.
authorthatcher <thatcher@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 9 May 2006 07:47:59 +0000 (07:47 +0000)
committerthatcher <thatcher@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 9 May 2006 07:47:59 +0000 (07:47 +0000)
        Bug 8804: Inspector should support searching by Xpath query
        http://bugzilla.opendarwin.org/show_bug.cgi?id=8804

        * WebInspector/WebInspector.m:
        (-[WebInspector _refreshSearch]):
        * WebInspector/webInspector/inspector.js:

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

WebKit/ChangeLog
WebKit/WebInspector/WebInspector.m
WebKit/WebInspector/webInspector/inspector.js

index 19bde634c3a54176ffa7f330ed90c6b9eaa7270a..afbde8bfd497bf3bad719df007acfb13ae96a975 100644 (file)
@@ -1,3 +1,14 @@
+2006-05-09  Timothy Hatcher  <timothy@apple.com>
+
+        Reviewed by Anders.
+
+        Bug 8804: Inspector should support searching by Xpath query
+        http://bugzilla.opendarwin.org/show_bug.cgi?id=8804
+
+        * WebInspector/WebInspector.m:
+        (-[WebInspector _refreshSearch]):
+        * WebInspector/webInspector/inspector.js:
+
 2006-05-08  Maciej Stachowiak  <mjs@apple.com>
 
         Reviewed by Tim Hatcher.
index d814a8c0a1316246c5653c3f95018c58db4e0884..9d8cbfe78e6cd1fd738ec953c0c1f5bc0c3c33f8 100644 (file)
@@ -624,22 +624,43 @@ static NSMapTable *lastChildIgnoringWhitespaceCache = NULL;
         return;
     }
 
-    BOOL matched = NO;
     unsigned count = 0;
-    DOMNode *node = [_private->webFrame DOMDocument];
-    while ((node = [node _traverseNextNodeStayingWithin:nil])) {
-        if ([[node nodeName] _webkit_hasCaseInsensitiveSubstring:query])
-            matched = YES;
-        else if ([node nodeType] == DOM_TEXT_NODE && [[node nodeValue] _webkit_hasCaseInsensitiveSubstring:query])
-            matched = YES;
-        else if ([node isKindOfClass:[DOMHTMLElement class]] && [[(DOMHTMLElement *)node idName] _webkit_hasCaseInsensitiveSubstring:query])
-            matched = YES;
-        else if ([node isKindOfClass:[DOMHTMLElement class]] && [[(DOMHTMLElement *)node className] _webkit_hasCaseInsensitiveSubstring:query])
-            matched = YES;
-        if (matched) {
-            [_private->searchResults addObject:node];
-            count++;
-            matched = NO;
+    DOMNode *node = nil;
+
+    if ([query hasPrefix:@"/"]) {
+        // search document by Xpath query
+        id nodeList = [[_private->webView windowScriptObject] callWebScriptMethod:@"resultsWithXpathQuery" withArguments:[NSArray arrayWithObject:query]];
+        if ([nodeList isKindOfClass:[WebScriptObject class]]) {
+            if ([[nodeList valueForKey:@"snapshotLength"] isKindOfClass:[NSNumber class]]) {
+                count = [[nodeList valueForKey:@"snapshotLength"] unsignedLongValue];
+                for( unsigned i = 0; i < count; i++ ) {
+                    NSNumber *index = [[NSNumber alloc] initWithUnsignedLong:i];
+                    NSArray *args = [[NSArray alloc] initWithObjects:&index count:1];
+                    node = [nodeList callWebScriptMethod:@"snapshotItem" withArguments:args];
+                    if (node)
+                        [_private->searchResults addObject:node];
+                    [args release];
+                    [index release];
+                }
+            }
+        }
+    } else {
+        // search nodes by node name, node value, id and class name
+        node = [_private->webFrame DOMDocument];
+        while ((node = [node _traverseNextNodeStayingWithin:nil])) {
+            BOOL matched = NO;
+            if ([[node nodeName] _webkit_hasCaseInsensitiveSubstring:query])
+                matched = YES;
+            else if ([node nodeType] == DOM_TEXT_NODE && [[node nodeValue] _webkit_hasCaseInsensitiveSubstring:query])
+                matched = YES;
+            else if ([node isKindOfClass:[DOMHTMLElement class]] && [[(DOMHTMLElement *)node idName] _webkit_hasCaseInsensitiveSubstring:query])
+                matched = YES;
+            else if ([node isKindOfClass:[DOMHTMLElement class]] && [[(DOMHTMLElement *)node className] _webkit_hasCaseInsensitiveSubstring:query])
+                matched = YES;
+            if (matched) {
+                [_private->searchResults addObject:node];
+                count++;
+            }
         }
     }
 
index 53edf0baf9bca89676d0ce3fa2529618d00a3bde..7df0710157f36bb7b2edb44fa870750597540fa0 100644 (file)
@@ -188,6 +188,18 @@ function performSearch(query)
     Inspector.searchPerformed(query);
 }
 
+function resultsWithXpathQuery(query)
+{
+    var nodeList = null;
+    try {
+        var focusedNode = Inspector.focusedDOMNode();
+        nodeList = focusedNode.document.evaluate(query, focusedNode.document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
+    } catch(err) {
+        // ignore any exceptions. the query might be malformed, but we allow that
+    }
+    return nodeList;
+}
+
 var tabNames = ["node","metrics","style","properties"];
 var currentPane = "node";
 var paneUpdateState = new Array();