XMLNodeHandlerHead.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "XMLNodeHandlerHead.h"
  2. #include "../../Include/RmlUi/Core/Core.h"
  3. #include "../../Include/RmlUi/Core/Element.h"
  4. #include "../../Include/RmlUi/Core/ElementDocument.h"
  5. #include "../../Include/RmlUi/Core/StringUtilities.h"
  6. #include "../../Include/RmlUi/Core/SystemInterface.h"
  7. #include "../../Include/RmlUi/Core/URL.h"
  8. #include "../../Include/RmlUi/Core/XMLParser.h"
  9. #include "DocumentHeader.h"
  10. #include "TemplateCache.h"
  11. namespace Rml {
  12. static String Absolutepath(const String& source, const String& base)
  13. {
  14. String joined_path;
  15. ::Rml::GetSystemInterface()->JoinPath(joined_path, StringUtilities::Replace(base, '|', ':'), StringUtilities::Replace(source, '|', ':'));
  16. return StringUtilities::Replace(joined_path, ':', '|');
  17. }
  18. static DocumentHeader::Resource MakeInlineResource(XMLParser* parser, const String& data)
  19. {
  20. DocumentHeader::Resource resource;
  21. resource.is_inline = true;
  22. resource.content = data;
  23. resource.path = parser->GetSourceURL().GetURL();
  24. resource.line = parser->GetLineNumberOpenTag();
  25. return resource;
  26. }
  27. static DocumentHeader::Resource MakeExternalResource(XMLParser* parser, const String& path)
  28. {
  29. DocumentHeader::Resource resource;
  30. resource.is_inline = false;
  31. resource.path = Absolutepath(path, parser->GetSourceURL().GetURL());
  32. return resource;
  33. }
  34. XMLNodeHandlerHead::XMLNodeHandlerHead() {}
  35. XMLNodeHandlerHead::~XMLNodeHandlerHead() {}
  36. Element* XMLNodeHandlerHead::ElementStart(XMLParser* parser, const String& name, const XMLAttributes& attributes)
  37. {
  38. if (name == "head")
  39. {
  40. // Process the head attribute
  41. parser->GetDocumentHeader()->source = parser->GetSourceURL().GetURL();
  42. }
  43. // Is it a link tag?
  44. else if (name == "link")
  45. {
  46. // Lookup the type and href
  47. String type = StringUtilities::ToLower(Get<String>(attributes, "type", ""));
  48. String href = Get<String>(attributes, "href", "");
  49. if (!type.empty() && !href.empty())
  50. {
  51. // If its RCSS (... or CSS!), add to the RCSS fields.
  52. if (type == "text/rcss" || type == "text/css")
  53. {
  54. parser->GetDocumentHeader()->rcss.push_back(MakeExternalResource(parser, href));
  55. }
  56. // If its an template, add to the template fields
  57. else if (type == "text/template")
  58. {
  59. parser->GetDocumentHeader()->template_resources.push_back(href);
  60. }
  61. else
  62. {
  63. Log::ParseError(parser->GetSourceURL().GetURL(), parser->GetLineNumber(), "Invalid link type '%s'", type.c_str());
  64. }
  65. }
  66. else
  67. {
  68. Log::ParseError(parser->GetSourceURL().GetURL(), parser->GetLineNumber(), "Link tag requires type and href attributes");
  69. }
  70. }
  71. // Process script tags
  72. else if (name == "script")
  73. {
  74. // Check if its an external string
  75. String src = Get<String>(attributes, "src", "");
  76. if (src.size() > 0)
  77. {
  78. parser->GetDocumentHeader()->scripts.push_back(MakeExternalResource(parser, src));
  79. }
  80. }
  81. // No elements constructed
  82. return nullptr;
  83. }
  84. bool XMLNodeHandlerHead::ElementEnd(XMLParser* parser, const String& name)
  85. {
  86. // When the head tag closes, inject the header into the active document
  87. if (name == "head")
  88. {
  89. Element* element = parser->GetParseFrame()->element;
  90. if (!element)
  91. return true;
  92. ElementDocument* document = element->GetOwnerDocument();
  93. if (document)
  94. document->ProcessHeader(parser->GetDocumentHeader());
  95. }
  96. return true;
  97. }
  98. bool XMLNodeHandlerHead::ElementData(XMLParser* parser, const String& data, XMLDataType /*type*/)
  99. {
  100. const String& tag = parser->GetParseFrame()->tag;
  101. // Store the title
  102. if (tag == "title")
  103. {
  104. SystemInterface* system_interface = GetSystemInterface();
  105. if (system_interface != nullptr)
  106. system_interface->TranslateString(parser->GetDocumentHeader()->title, data);
  107. }
  108. // Store an inline script
  109. if (tag == "script" && data.size() > 0)
  110. {
  111. parser->GetDocumentHeader()->scripts.push_back(MakeInlineResource(parser, data));
  112. }
  113. // Store an inline style
  114. if (tag == "style" && data.size() > 0)
  115. {
  116. parser->GetDocumentHeader()->rcss.push_back(MakeInlineResource(parser, data));
  117. }
  118. return true;
  119. }
  120. } // namespace Rml