選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SCRIPTElement.cpp 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "SCRIPTElement.h"
  2. #include "../components/DocumentComponent.h"
  3. #include "../../tools/WebResource.h"
  4. #include "../../tools/Log.h"
  5. #include "../../parsers/scripting/javascript/JSParser.h"
  6. #include "../../app/app.h"
  7. #include <ctime>
  8. std::unique_ptr<Component> SCRIPTElement::renderer(const ElementRenderRequest &request) {
  9. if (!request.docComponent->win->openglWindow->app->jsEnable) {
  10. std::cout << "Js is disabled" << std::endl;
  11. return nullptr;
  12. }
  13. TagNode *tagNode = dynamic_cast<TagNode*>(request.node.get());
  14. if (tagNode) {
  15. //std::cout << "SCRIPTElement::renderer - start" << std::endl;
  16. bool gotSource = false;
  17. std::map<std::string, std::string>::const_iterator srcPair = tagNode->properties.find("src");
  18. std::string source = "";
  19. if (srcPair != tagNode->properties.end()) {
  20. //std::cout << "SCRIPTElement::renderer - src: " << srcPair->second << std::endl;
  21. URL uRequest = URL(srcPair->second);
  22. //std::cout << "SCRIPTElement::renderer - srcURL: " << uRequest << std::endl;
  23. //std::cout << "SCRIPTElement::renderer - bseURL: " << request.docComponent->currentURL << std::endl;
  24. // ok download this URL
  25. URL uScriptSrc = request.docComponent->currentURL.merge(uRequest);
  26. //std::cout << "SCRIPTElement::renderer - Canonical: " << uScriptSrc << std::endl;
  27. // download URL
  28. WebResource res = getWebResource(uScriptSrc);
  29. if (res.resourceType == ResourceType::INVALID) {
  30. logError() << "SCRIPTElement::renderer - Invalid resource type: " << res.raw << std::endl;
  31. } else {
  32. //std::cout << "body: " << res.raw << std::endl;
  33. //std::cout << "type: " << res.resourceType << std::endl;
  34. source = res.raw;
  35. gotSource = true;
  36. }
  37. }
  38. if (request.node->children.size()) {
  39. TextNode *textNode = dynamic_cast<TextNode*>(request.node->children.front().get());
  40. if (textNode) {
  41. //std::cout << "SCRIPTElement::renderer - source: " << textNode->text << std::endl;
  42. source = textNode->text;
  43. gotSource = true;
  44. }
  45. }
  46. if (!gotSource) {
  47. std::cout << "SCRIPTElement::renderer - no source!" << std::endl;
  48. return nullptr;
  49. }
  50. // parse JS
  51. //std::cout << "SCRIPTElement::renderer - source: " << source << std::endl;
  52. std::shared_ptr<JavaScript> newScript = std::make_shared<JavaScript>();
  53. // import current state (scope)
  54. newScript->applyScope(request.docComponent->mainScript);
  55. // parse it
  56. const std::clock_t begin = clock();
  57. newScript->parse(source);
  58. //newScript->tokens = parser.getTokens(source);
  59. //parser.parseTokens(newScript->tokens, &newScript->rootScope);
  60. newScript->execute();
  61. const std::clock_t end = clock();
  62. logDebug() << "SCRIPTElement::renderer - Parsed javascript in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
  63. // and flush our state back into mainScript
  64. //request.docComponent->mainScript = parser.append(request.docComponent->mainScript, newScript);
  65. request.docComponent->mainScript->applyScope(newScript);
  66. request.docComponent->mainScript->append(newScript);
  67. }
  68. return nullptr;
  69. }