XMLNodeHandlerHead.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 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 "DocumentHeader.h"
  30. #include "TemplateCache.h"
  31. #include "../../Include/RmlUi/Core/Core.h"
  32. #include "../../Include/RmlUi/Core/Element.h"
  33. #include "../../Include/RmlUi/Core/ElementDocument.h"
  34. #include "../../Include/RmlUi/Core/StringUtilities.h"
  35. #include "../../Include/RmlUi/Core/SystemInterface.h"
  36. #include "../../Include/RmlUi/Core/XMLParser.h"
  37. #include "../../Include/RmlUi/Core/URL.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. {
  63. }
  64. XMLNodeHandlerHead::~XMLNodeHandlerHead()
  65. {
  66. }
  67. Element* XMLNodeHandlerHead::ElementStart(XMLParser* parser, const String& name, const XMLAttributes& attributes)
  68. {
  69. if (name == "head")
  70. {
  71. // Process the head attribute
  72. parser->GetDocumentHeader()->source = parser->GetSourceURL().GetURL();
  73. }
  74. // Is it a link tag?
  75. else if (name == "link")
  76. {
  77. // Lookup the type and href
  78. String type = StringUtilities::ToLower(Get<String>(attributes, "type", ""));
  79. String href = Get<String>(attributes, "href", "");
  80. if (!type.empty() && !href.empty())
  81. {
  82. // If its RCSS (... or CSS!), add to the RCSS fields.
  83. if (type == "text/rcss" ||
  84. type == "text/css")
  85. {
  86. parser->GetDocumentHeader()->rcss.push_back(MakeExternalResource(parser, href));
  87. }
  88. // If its an template, add to the template fields
  89. else if (type == "text/template")
  90. {
  91. parser->GetDocumentHeader()->template_resources.push_back(href);
  92. }
  93. else
  94. {
  95. Log::ParseError(parser->GetSourceURL().GetURL(), parser->GetLineNumber(), "Invalid link type '%s'", type.c_str());
  96. }
  97. }
  98. else
  99. {
  100. Log::ParseError(parser->GetSourceURL().GetURL(), parser->GetLineNumber(), "Link tag requires type and href attributes");
  101. }
  102. }
  103. // Process script tags
  104. else if (name == "script")
  105. {
  106. // Check if its an external string
  107. String src = Get<String>(attributes, "src", "");
  108. if (src.size() > 0)
  109. {
  110. parser->GetDocumentHeader()->scripts.push_back(MakeExternalResource(parser, src));
  111. }
  112. }
  113. // No elements constructed
  114. return nullptr;
  115. }
  116. bool XMLNodeHandlerHead::ElementEnd(XMLParser* parser, const String& name)
  117. {
  118. // When the head tag closes, inject the header into the active document
  119. if (name == "head")
  120. {
  121. Element* element = parser->GetParseFrame()->element;
  122. if (!element)
  123. return true;
  124. ElementDocument* document = element->GetOwnerDocument();
  125. if (document)
  126. document->ProcessHeader(parser->GetDocumentHeader());
  127. }
  128. return true;
  129. }
  130. bool XMLNodeHandlerHead::ElementData(XMLParser* parser, const String& data, XMLDataType RMLUI_UNUSED_PARAMETER(type))
  131. {
  132. RMLUI_UNUSED(type);
  133. const String& tag = parser->GetParseFrame()->tag;
  134. // Store the title
  135. if (tag == "title")
  136. {
  137. SystemInterface* system_interface = GetSystemInterface();
  138. if (system_interface != nullptr)
  139. system_interface->TranslateString(parser->GetDocumentHeader()->title, data);
  140. }
  141. // Store an inline script
  142. if (tag == "script" && data.size() > 0)
  143. {
  144. parser->GetDocumentHeader()->scripts.push_back(MakeInlineResource(parser, data));
  145. }
  146. // Store an inline style
  147. if (tag == "style" && data.size() > 0)
  148. {
  149. parser->GetDocumentHeader()->rcss.push_back(MakeInlineResource(parser, data));
  150. }
  151. return true;
  152. }
  153. } // namespace Rml