XMLNodeHandlerHead.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "XMLNodeHandlerHead.h"
  29. #include "DocumentHeader.h"
  30. #include "TemplateCache.h"
  31. #include "../../Include/Rocket/Core.h"
  32. namespace Rocket {
  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 = 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 NULL;
  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 != NULL)
  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. parser->GetDocumentHeader()->rcss_inline.push_back(data);
  119. return true;
  120. }
  121. void XMLNodeHandlerHead::Release()
  122. {
  123. delete this;
  124. }
  125. }
  126. }