Index: third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp |
diff --git a/third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp b/third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp |
index ff6e8c82292a042be85c2105c36b004b9f06bed3..426e42568df303f076cbb59a45a62efbeed987c0 100644 |
--- a/third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp |
+++ b/third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp |
@@ -33,8 +33,13 @@ |
#include "bindings/core/v8/BindingSecurity.h" |
#include "bindings/core/v8/DOMWrapperWorld.h" |
#include "bindings/core/v8/ScriptController.h" |
+#include "bindings/core/v8/V8Node.h" |
#include "bindings/core/v8/V8Window.h" |
+#include "core/dom/ContainerNode.h" |
+#include "core/dom/Document.h" |
+#include "core/dom/Element.h" |
#include "core/dom/ExecutionContext.h" |
+#include "core/dom/StaticNodeList.h" |
#include "core/frame/FrameConsole.h" |
#include "core/frame/LocalDOMWindow.h" |
#include "core/frame/LocalFrame.h" |
@@ -234,4 +239,69 @@ v8::MaybeLocal<v8::Value> MainThreadDebugger::memoryInfo(v8::Isolate* isolate, v |
return toV8(MemoryInfo::create(), creationContext, isolate); |
} |
+static void createFunctionProperty(v8::Local<v8::Context> context, v8::Local<v8::Object> object, const char* name, v8::FunctionCallback callback) |
+{ |
+ v8::Local<v8::String> funcName = v8String(context->GetIsolate(), name); |
+ v8::Local<v8::Function> func; |
+ if (!v8::Function::New(context, callback).ToLocal(&func)) |
+ return; |
+ func->SetName(funcName); |
+ if (!object->Set(context, funcName, func).FromMaybe(false)) |
+ return; |
+} |
+ |
+void MainThreadDebugger::willInstallCommandLineAPI(v8::Local<v8::Context> context, v8::Local<v8::Object> object) |
+{ |
+ createFunctionProperty(context, object, "$", MainThreadDebugger::querySelectorCallback); |
+ createFunctionProperty(context, object, "$$", MainThreadDebugger::querySelectorAllCallback); |
+} |
+ |
+static Node* secondArgumentAsNode(const v8::FunctionCallbackInfo<v8::Value>& info) |
+{ |
+ if (info.Length() < 2) { |
+ ExecutionContext* executionContext = toExecutionContext(info.GetIsolate()->GetCurrentContext()); |
+ if (executionContext->isDocument()) |
+ return toDocument(executionContext); |
+ } else { |
+ return V8Node::toImplWithTypeCheck(info.GetIsolate(), info[1]); |
+ } |
+ return nullptr; |
+} |
+ |
+void MainThreadDebugger::querySelectorCallback(const v8::FunctionCallbackInfo<v8::Value>& info) |
+{ |
+ if (info.Length() < 1) |
+ return; |
+ String selector = toCoreStringWithUndefinedOrNullCheck(info[0]); |
+ if (selector.isEmpty()) |
+ return; |
+ Node* node = secondArgumentAsNode(info); |
+ if (!node || !node->isContainerNode()) |
+ return; |
+ if (Element* element = toContainerNode(node)->querySelector(AtomicString(selector), IGNORE_EXCEPTION)) |
+ info.GetReturnValue().Set(toV8(static_cast<Node*>(element), info.Holder(), info.GetIsolate())); |
dgozman
2016/05/18 01:51:26
I think static_cast is redundant.
kozy
2016/05/18 02:18:56
Removed.
|
+} |
+ |
+void MainThreadDebugger::querySelectorAllCallback(const v8::FunctionCallbackInfo<v8::Value>& info) |
+{ |
+ if (info.Length() < 1) |
+ return; |
+ String selector = toCoreStringWithUndefinedOrNullCheck(info[0]); |
+ if (selector.isEmpty()) |
+ return; |
+ Node* node = secondArgumentAsNode(info); |
+ if (!node || !node->isContainerNode()) |
+ return; |
+ StaticElementList* elementList = toContainerNode(node)->querySelectorAll(AtomicString(selector), IGNORE_EXCEPTION); |
dgozman
2016/05/18 01:51:26
// toV8(elementList) doesn't work here, since we n
kozy
2016/05/18 02:18:56
Done.
|
+ v8::Isolate* isolate = info.GetIsolate(); |
+ v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
+ v8::Local<v8::Array> nodes = v8::Array::New(isolate, elementList->length()); |
+ for (size_t i = 0; i < elementList->length(); ++i) { |
+ Element* element = elementList->item(i); |
+ if (!nodes->Set(context, i, toV8(static_cast<Node*>(element), info.Holder(), info.GetIsolate())).FromMaybe(false)) |
+ return; |
+ } |
+ info.GetReturnValue().Set(nodes); |
+} |
+ |
} // namespace blink |