XMLNodeHandlerHead.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "XMLNodeHandlerHead.h"
  29. #include "../../Include/RmlUi/Core/Core.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. #include "../../Include/RmlUi/Core/ElementDocument.h"
  32. #include "../../Include/RmlUi/Core/StringUtilities.h"
  33. #include "../../Include/RmlUi/Core/SystemInterface.h"
  34. #include "../../Include/RmlUi/Core/URL.h"
  35. #include "../../Include/RmlUi/Core/XMLParser.h"
  36. #include "DocumentHeader.h"
  37. #include "TemplateCache.h"
  38. namespace Rml {
  39. static String Absolutepath(const String& source, const String& base)
  40. {
  41. String joined_path;
  42. ::Rml::GetSystemInterface()->JoinPath(joined_path, StringUtilities::Replace(base, '|', ':'), StringUtilities::Replace(source, '|', ':'));
  43. return StringUtilities::Replace(joined_path, ':', '|');
  44. }
  45. static DocumentHeader::Resource MakeInlineResource(XMLParser* parser, const String& data)
  46. {
  47. DocumentHeader::Resource resource;
  48. resource.is_inline = true;
  49. resource.content = data;
  50. resource.path = parser->GetSourceURL().GetURL();
  51. resource.line = parser->GetLineNumberOpenTag();
  52. return resource;
  53. }
  54. static DocumentHeader::Resource MakeExternalResource(XMLParser* parser, const String& path)
  55. {
  56. DocumentHeader::Resource resource;
  57. resource.is_inline = false;
  58. resource.path = Absolutepath(path, parser->GetSourceURL().GetURL());
  59. return resource;
  60. }
  61. XMLNodeHandlerHead::XMLNodeHandlerHead() {}
  62. XMLNodeHandlerHead::~XMLNodeHandlerHead() {}
  63. Element* XMLNodeHandlerHead::ElementStart(XMLParser* parser, const String& name, const XMLAttributes& attributes)
  64. {
  65. if (name == "head")
  66. {
  67. // Process the head attribute
  68. parser->GetDocumentHeader()->source = parser->GetSourceURL().GetURL();
  69. }
  70. // Is it a link tag?
  71. else if (name == "link")
  72. {
  73. // Lookup the type and href
  74. String type = StringUtilities::ToLower(Get<String>(attributes, "type", ""));
  75. String href = Get<String>(attributes, "href", "");
  76. if (!type.empty() && !href.empty())
  77. {
  78. // If its RCSS (... or CSS!), add to the RCSS fields.
  79. if (type == "text/rcss" || type == "text/css")
  80. {
  81. parser->GetDocumentHeader()->rcss.push_back(MakeExternalResource(parser, href));
  82. }
  83. // If its an template, add to the template fields
  84. else if (type == "text/template")
  85. {
  86. parser->GetDocumentHeader()->template_resources.push_back(href);
  87. }
  88. else
  89. {
  90. Log::ParseError(parser->GetSourceURL().GetURL(), parser->GetLineNumber(), "Invalid link type '%s'", type.c_str());
  91. }
  92. }
  93. else
  94. {
  95. Log::ParseError(parser->GetSourceURL().GetURL(), parser->GetLineNumber(), "Link tag requires type and href attributes");
  96. }
  97. }
  98. // Process script tags
  99. else if (name == "script")
  100. {
  101. // Check if its an external string
  102. String src = Get<String>(attributes, "src", "");
  103. if (src.size() > 0)
  104. {
  105. parser->GetDocumentHeader()->scripts.push_back(MakeExternalResource(parser, src));
  106. }
  107. }
  108. // No elements constructed
  109. return nullptr;
  110. }
  111. bool XMLNodeHandlerHead::ElementEnd(XMLParser* parser, const String& name)
  112. {
  113. // When the head tag closes, inject the header into the active document
  114. if (name == "head")
  115. {
  116. Element* element = parser->GetParseFrame()->element;
  117. if (!element)
  118. return true;
  119. ElementDocument* document = element->GetOwnerDocument();
  120. if (document)
  121. document->ProcessHeader(parser->GetDocumentHeader());
  122. }
  123. return true;
  124. }
  125. bool XMLNodeHandlerHead::ElementData(XMLParser* parser, const String& data, XMLDataType /*type*/)
  126. {
  127. const String& tag = parser->GetParseFrame()->tag;
  128. // Store the title
  129. if (tag == "title")
  130. {
  131. SystemInterface* system_interface = GetSystemInterface();
  132. if (system_interface != nullptr)
  133. system_interface->TranslateString(parser->GetDocumentHeader()->title, data);
  134. }
  135. // Store an inline script
  136. if (tag == "script" && data.size() > 0)
  137. {
  138. parser->GetDocumentHeader()->scripts.push_back(MakeInlineResource(parser, data));
  139. }
  140. // Store an inline style
  141. if (tag == "style" && data.size() > 0)
  142. {
  143. parser->GetDocumentHeader()->rcss.push_back(MakeInlineResource(parser, data));
  144. }
  145. return true;
  146. }
  147. } // namespace Rml