XMLNodeHandlerHead.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. XMLNodeHandlerHead::XMLNodeHandlerHead()
  40. {
  41. }
  42. XMLNodeHandlerHead::~XMLNodeHandlerHead()
  43. {
  44. }
  45. Element* XMLNodeHandlerHead::ElementStart(XMLParser* parser, const String& name, const XMLAttributes& attributes)
  46. {
  47. if (name == "head")
  48. {
  49. // Process the head attribute
  50. parser->GetDocumentHeader()->source = parser->GetSourceURL().GetURL();
  51. }
  52. // Is it a link tag?
  53. else if (name == "link")
  54. {
  55. // Lookup the type and href
  56. String type = StringUtilities::ToLower(Get<String>(attributes, "type", ""));
  57. String href = Get<String>(attributes, "href", "");
  58. if (!type.empty() && !href.empty())
  59. {
  60. // If its RCSS (... or CSS!), add to the RCSS fields.
  61. if (type == "text/rcss" ||
  62. type == "text/css")
  63. {
  64. parser->GetDocumentHeader()->rcss_external.push_back(href);
  65. }
  66. // If its an template, add to the template fields
  67. else if (type == "text/template")
  68. {
  69. parser->GetDocumentHeader()->template_resources.push_back(href);
  70. }
  71. else
  72. {
  73. Log::ParseError(parser->GetSourceURL().GetURL(), parser->GetLineNumber(), "Invalid link type '%s'", type.c_str());
  74. }
  75. }
  76. else
  77. {
  78. Log::ParseError(parser->GetSourceURL().GetURL(), parser->GetLineNumber(), "Link tag requires type and href attributes");
  79. }
  80. }
  81. // Process script tags
  82. else if (name == "script")
  83. {
  84. // Check if its an external string
  85. String src = Get<String>(attributes, "src", "");
  86. if (src.size() > 0)
  87. {
  88. parser->GetDocumentHeader()->scripts_external.push_back(src);
  89. }
  90. }
  91. // No elements constructed
  92. return nullptr;
  93. }
  94. bool XMLNodeHandlerHead::ElementEnd(XMLParser* parser, const String& name)
  95. {
  96. // When the head tag closes, inject the header into the active document
  97. if (name == "head")
  98. {
  99. Element* element = parser->GetParseFrame()->element;
  100. if (!element)
  101. return true;
  102. ElementDocument* document = element->GetOwnerDocument();
  103. if (document)
  104. document->ProcessHeader(parser->GetDocumentHeader());
  105. }
  106. return true;
  107. }
  108. bool XMLNodeHandlerHead::ElementData(XMLParser* parser, const String& data, XMLDataType RMLUI_UNUSED_PARAMETER(type))
  109. {
  110. RMLUI_UNUSED(type);
  111. const String& tag = parser->GetParseFrame()->tag;
  112. // Store the title
  113. if (tag == "title")
  114. {
  115. SystemInterface* system_interface = GetSystemInterface();
  116. if (system_interface != nullptr)
  117. system_interface->TranslateString(parser->GetDocumentHeader()->title, data);
  118. }
  119. // Store an inline script
  120. if (tag == "script" && data.size() > 0)
  121. parser->GetDocumentHeader()->scripts_inline.push_back(data);
  122. // Store an inline style
  123. if (tag == "style" && data.size() > 0)
  124. {
  125. parser->GetDocumentHeader()->rcss_inline.push_back(data);
  126. parser->GetDocumentHeader()->rcss_inline_line_numbers.push_back(parser->GetLineNumberOpenTag());
  127. }
  128. return true;
  129. }
  130. } // namespace Rml