XMLNodeHandlerHead.cpp 4.1 KB

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