You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 lines
3.4 KiB

#include "SCRIPTElement.h"
#include "../components/DocumentComponent.h"
#include "../../tools/WebResource.h"
#include "../../tools/Log.h"
#include "../../parsers/scripting/javascript/JSParser.h"
#include "../../app/app.h"
#include <ctime>
std::unique_ptr<Component> SCRIPTElement::renderer(const ElementRenderRequest &request) {
if (!request.docComponent->win->openglWindow->app->jsEnable) {
std::cout << "Js is disabled" << std::endl;
return nullptr;
}
TagNode *tagNode = dynamic_cast<TagNode*>(request.node.get());
if (tagNode) {
//std::cout << "SCRIPTElement::renderer - start" << std::endl;
bool gotSource = false;
std::map<std::string, std::string>::const_iterator srcPair = tagNode->properties.find("src");
std::string source = "";
if (srcPair != tagNode->properties.end()) {
//std::cout << "SCRIPTElement::renderer - src: " << srcPair->second << std::endl;
URL uRequest = URL(srcPair->second);
//std::cout << "SCRIPTElement::renderer - srcURL: " << uRequest << std::endl;
//std::cout << "SCRIPTElement::renderer - bseURL: " << request.docComponent->currentURL << std::endl;
// ok download this URL
URL uScriptSrc = request.docComponent->currentURL.merge(uRequest);
//std::cout << "SCRIPTElement::renderer - Canonical: " << uScriptSrc << std::endl;
// download URL
WebResource res = getWebResource(uScriptSrc);
if (res.resourceType == ResourceType::INVALID) {
logError() << "SCRIPTElement::renderer - Invalid resource type: " << res.raw << std::endl;
} else {
//std::cout << "body: " << res.raw << std::endl;
//std::cout << "type: " << res.resourceType << std::endl;
source = res.raw;
gotSource = true;
}
}
if (request.node->children.size()) {
TextNode *textNode = dynamic_cast<TextNode*>(request.node->children.front().get());
if (textNode) {
//std::cout << "SCRIPTElement::renderer - source: " << textNode->text << std::endl;
source = textNode->text;
gotSource = true;
}
}
if (!gotSource) {
std::cout << "SCRIPTElement::renderer - no source!" << std::endl;
return nullptr;
}
// parse JS
//std::cout << "SCRIPTElement::renderer - source: " << source << std::endl;
std::shared_ptr<JavaScript> newScript = std::make_shared<JavaScript>();
// import current state (scope)
newScript->applyScope(request.docComponent->mainScript);
// parse it
const std::clock_t begin = clock();
newScript->parse(source);
//newScript->tokens = parser.getTokens(source);
//parser.parseTokens(newScript->tokens, &newScript->rootScope);
newScript->execute();
const std::clock_t end = clock();
logDebug() << "SCRIPTElement::renderer - Parsed javascript in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
// and flush our state back into mainScript
//request.docComponent->mainScript = parser.append(request.docComponent->mainScript, newScript);
request.docComponent->mainScript->applyScope(newScript);
request.docComponent->mainScript->append(newScript);
}
return nullptr;
}