XMLNodeHandlerHead.cpp 4.4 KB

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