XMLNodeHandlerHead.cpp 4.1 KB

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