Browse Source

getDocumentFromScope(), jsConstruct_getElementsByTagName()

master
Odilitime 1 year ago
parent
commit
81b281095d
1 changed files with 46 additions and 1 deletions
  1. 46
    1
      src/parsers/scripting/javascript/BrowserJS.cpp

+ 46
- 1
src/parsers/scripting/javascript/BrowserJS.cpp View File

@@ -16,6 +16,24 @@ bool isConstruct(std::string construct) {
return it != constructs.end();
}

DocumentComponent *getDocumentFromScope(js_function &scope) {
if (scope.parentScope) {
return getDocumentFromScope(*scope.parentScope);
}
// no more parent
if (!scope.script) {
std::cout << "getDocumentFromScope couldnt find doc, no script\n";
return nullptr;
}
BrowserJavaScript *bScript = dynamic_cast<BrowserJavaScript *>(scope.script);
if (!bScript)
{
std::cout << "getDocumentFromScope rootScript isnt a browserJS\n";
return nullptr;
}
return bScript->document;
}

js_internal_storage *executeConstruct(std::string functionName, std::string params, js_function &scope) {
std::vector<std::string> constructs = getConstructs();
std::vector<std::string>::iterator it = std::find(constructs.begin(), constructs.end(), functionName);
@@ -29,7 +47,10 @@ js_internal_storage *executeConstruct(std::string functionName, std::string para
if (functionName == "document.addEventListener") {
return jsConstruct_addEventListener(params, scope);
}
std::cout << "executeConstruct hanlder needs to be written\n";
if (functionName == "document.getElementsByTagName") {
return jsConstruct_getElementsByTagName(params, scope);
}
std::cout << "executeConstruct handler needs to be written\n";
return nullptr;
}

@@ -45,3 +66,27 @@ js_internal_storage *jsConstruct_addEventListener(std::string params, js_functio
jb->value = false;
return jb;
}

js_internal_storage *jsConstruct_getElementsByTagName(std::string params, js_function &scope) {
//std::cout << "WARNING implement jsConstruct_getElementsByTagName\n";
js_array *ja = new js_array;
// how do we access the DOM?
DocumentComponent *doc = getDocumentFromScope(scope);
if (!doc) {
return ja; // return empty array
}
// iterate down (maybe flush tree to a list)
std::vector< std::shared_ptr<Node> > results;
Node::findTagNodeChild(params, doc->domRootNode, results);
std::cout << "Found [" << results.size() << "] for [" << params << "]\n";
for(auto foundTag : results) {
//TagNode *tag = dynamic_cast<TagNode *>(foundTag.get());
js_element *je = new js_element;
je->value = foundTag.get();
ja->value.push_back(je);
}
std::cout << "Found [" << ja->value.size() << "] of [" << params << "]\n";
// ja->value is a vector of js_internal_storage
return ja;
}


Loading…
Cancel
Save