Skip to content

Commit b2b2ace

Browse files
authored
Merge pull request #1052 from NativeScript/darind/inspector-fix
Add backendNodeId property to the ChildNodeInserted callback
2 parents bf083ff + 0f93a17 commit b2b2ace

File tree

3 files changed

+48
-37
lines changed

3 files changed

+48
-37
lines changed

test-app/runtime/src/main/cpp/DOMDomainCallbackHandlers.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ void DOMDomainCallbackHandlers::ChildNodeInsertedCallback(const v8::FunctionCall
3939
auto lastId = args[1]->ToNumber(isolate);
4040
auto node = args[2]->ToString(isolate);
4141

42-
auto nodeJson = protocol::StringUtil::parseJSON(v8_inspector::toProtocolString(node));
42+
auto resultString = V8DOMAgentImpl::AddBackendNodeIdProperty(isolate, node);
43+
auto resultUtf16Data = resultString.data();
44+
45+
auto nodeJson = protocol::StringUtil::parseJSON(String16((const uint16_t*) resultUtf16Data));
4346

4447
protocol::ErrorSupport errorSupport;
4548
auto domNode = protocol::DOM::Node::fromValue(nodeJson.get(), &errorSupport);
@@ -48,6 +51,7 @@ void DOMDomainCallbackHandlers::ChildNodeInsertedCallback(const v8::FunctionCall
4851
if (!errorSupportString.empty()) {
4952
auto errorMessage = "Error while parsing debug `DOM Node` object. ";
5053
DEBUG_WRITE_FORCE("%s Error: %s", errorMessage, errorSupportString.c_str());
54+
return;
5155
}
5256

5357
domAgentInstance->m_frontend.childNodeInserted(parentId->Int32Value(), lastId->Int32Value(), std::move(domNode));

test-app/runtime/src/main/cpp/v8_inspector/src/inspector/v8-dom-agent-impl.cpp

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -95,42 +95,7 @@ DispatchResponse V8DOMAgentImpl::getDocument(Maybe<int> in_depth, Maybe<bool> in
9595

9696
if (!outResult->ToObject()->Has(context, ArgConverter::ConvertToV8String(isolate, "backendNodeId")).FromMaybe(false)) {
9797
// Using an older version of the modules which doesn't set the backendNodeId required property
98-
auto scriptSource =
99-
"(function () {"
100-
" function addBackendNodeId(node) {"
101-
" if (!node.backendNodeId) {"
102-
" node.backendNodeId = 0;"
103-
" }"
104-
" if (node.children) {"
105-
" for (var i = 0; i < node.children.length; i++) {"
106-
" addBackendNodeId(node.children[i]);"
107-
" }"
108-
" }"
109-
" }"
110-
" return function(stringifiedNode) {"
111-
" try {"
112-
" const node = JSON.parse(stringifiedNode);"
113-
" addBackendNodeId(node);"
114-
" return JSON.stringify(node);"
115-
" } catch (e) {"
116-
" return stringifiedNode;"
117-
" }"
118-
" }"
119-
"})()";
120-
121-
auto source = ArgConverter::ConvertToV8String(isolate, scriptSource);
122-
v8::Local<v8::Script> script;
123-
v8::Script::Compile(context, source).ToLocal(&script);
124-
125-
v8::Local<v8::Value> result;
126-
script->Run(context).ToLocal(&result);
127-
auto addBackendNodeIdFunction = result.As<v8::Function>();
128-
129-
v8::Local<v8::Value> args[] = { outResult };
130-
v8::Local<v8::Value> scriptResult;
131-
addBackendNodeIdFunction->Call(context, context->Global(), 1, args).ToLocal(&scriptResult);
132-
133-
resultString = ArgConverter::ConvertToUtf16String(scriptResult->ToString());
98+
resultString = AddBackendNodeIdProperty(isolate, outResult);
13499
}
135100

136101
auto resultUtf16Data = resultString.data();
@@ -363,5 +328,46 @@ DispatchResponse V8DOMAgentImpl::getFrameOwner(const String& in_frameId, int* ou
363328
return utils::Common::protocolCommandNotSupportedDispatchResponse();
364329
}
365330

331+
std::u16string V8DOMAgentImpl::AddBackendNodeIdProperty(v8::Isolate* isolate, v8::Local<v8::Value> jsonInput) {
332+
auto scriptSource =
333+
"(function () {"
334+
" function addBackendNodeId(node) {"
335+
" if (!node.backendNodeId) {"
336+
" node.backendNodeId = 0;"
337+
" }"
338+
" if (node.children) {"
339+
" for (var i = 0; i < node.children.length; i++) {"
340+
" addBackendNodeId(node.children[i]);"
341+
" }"
342+
" }"
343+
" }"
344+
" return function(stringifiedNode) {"
345+
" try {"
346+
" const node = JSON.parse(stringifiedNode);"
347+
" addBackendNodeId(node);"
348+
" return JSON.stringify(node);"
349+
" } catch (e) {"
350+
" return stringifiedNode;"
351+
" }"
352+
" }"
353+
"})()";
354+
355+
auto source = ArgConverter::ConvertToV8String(isolate, scriptSource);
356+
v8::Local<v8::Script> script;
357+
auto context = isolate->GetCurrentContext();
358+
v8::Script::Compile(context, source).ToLocal(&script);
359+
360+
v8::Local<v8::Value> result;
361+
script->Run(context).ToLocal(&result);
362+
auto addBackendNodeIdFunction = result.As<v8::Function>();
363+
364+
v8::Local<v8::Value> funcArguments[] = { jsonInput };
365+
v8::Local<v8::Value> scriptResult;
366+
addBackendNodeIdFunction->Call(context, context->Global(), 1, funcArguments).ToLocal(&scriptResult);
367+
368+
auto resultString = ArgConverter::ConvertToUtf16String(scriptResult->ToString());
369+
return resultString;
370+
}
371+
366372
V8DOMAgentImpl* V8DOMAgentImpl::Instance = 0;
367373
}

test-app/runtime/src/main/cpp/v8_inspector/src/inspector/v8-dom-agent-impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class V8DOMAgentImpl : public protocol::DOM::Backend {
6969
static V8DOMAgentImpl* Instance;
7070
protocol::DOM::Frontend m_frontend;
7171

72+
static std::u16string AddBackendNodeIdProperty(v8::Isolate* isolate, v8::Local<v8::Value> jsonInput);
7273
private:
7374
V8InspectorSessionImpl* m_session;
7475
protocol::DictionaryValue* m_state;

0 commit comments

Comments
 (0)