XmlNodeHandlers.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "XmlNodeHandlers.h"
  29. #include <RmlUi/Core/Types.h>
  30. #include <RmlUi/Core/XMLNodeHandler.h>
  31. #include <RmlUi/Core/XMLParser.h>
  32. #include <doctest.h>
  33. using namespace Rml;
  34. class XMLNodeHandlerMeta : public Rml::XMLNodeHandler
  35. {
  36. public:
  37. XMLNodeHandlerMeta(MetaList* meta_list) : meta_list(meta_list)
  38. {}
  39. ~XMLNodeHandlerMeta()
  40. {}
  41. /// Called when a new element start is opened
  42. Element* ElementStart(XMLParser* /*parser*/, const String& /*name*/, const XMLAttributes& attributes) override
  43. {
  44. MetaItem item;
  45. auto it_name = attributes.find("name");
  46. if (it_name != attributes.end())
  47. item.name = it_name->second.Get<String>();
  48. auto it_content = attributes.find("content");
  49. if (it_content != attributes.end())
  50. item.content = it_content->second.Get<String>();
  51. if (!item.name.empty() && !item.content.empty())
  52. meta_list->push_back(std::move(item));
  53. return nullptr;
  54. }
  55. /// Called when an element is closed
  56. bool ElementEnd(XMLParser* /*parser*/, const String& /*name*/) override
  57. {
  58. return true;
  59. }
  60. /// Called for element data
  61. bool ElementData(XMLParser* /*parser*/, const String& /*data*/, XMLDataType /*type*/) override
  62. {
  63. return true;
  64. }
  65. private:
  66. MetaList* meta_list;
  67. };
  68. class XMLNodeHandlerLink : public Rml::XMLNodeHandler
  69. {
  70. public:
  71. XMLNodeHandlerLink(LinkList* link_list) : link_list(link_list)
  72. {
  73. node_handler_head = XMLParser::GetNodeHandler("head");
  74. RMLUI_ASSERT(node_handler_head);
  75. }
  76. ~XMLNodeHandlerLink() {}
  77. Element* ElementStart(XMLParser* parser, const String& name, const XMLAttributes& attributes) override
  78. {
  79. RMLUI_ASSERT(name == "link");
  80. const String rel = StringUtilities::ToLower(Get<String>(attributes, "rel", ""));
  81. const String type = StringUtilities::ToLower(Get<String>(attributes, "type", ""));
  82. const String href = Get<String>(attributes, "href", "");
  83. if (!type.empty() && !href.empty())
  84. {
  85. // Pass it on to the head handler if it's a type it handles.
  86. if (type == "text/rcss" || type == "text/css" || type == "text/template")
  87. {
  88. return node_handler_head->ElementStart(parser, name, attributes);
  89. }
  90. }
  91. LinkItem item{ rel, href };
  92. if (rel == "match")
  93. item.href = StringUtilities::Replace(href, '/', '\\');
  94. else
  95. item.href = href;
  96. link_list->push_back(std::move(item));
  97. return nullptr;
  98. }
  99. bool ElementEnd(XMLParser* parser, const String& name) override
  100. {
  101. return node_handler_head->ElementEnd(parser, name);
  102. }
  103. bool ElementData(XMLParser* parser, const String& data, XMLDataType type) override
  104. {
  105. return node_handler_head->ElementData(parser, data, type);
  106. }
  107. private:
  108. LinkList* link_list;
  109. Rml::XMLNodeHandler* node_handler_head;
  110. };
  111. static SharedPtr<XMLNodeHandlerMeta> meta_handler;
  112. static SharedPtr<XMLNodeHandlerLink> link_handler;
  113. void InitializeXmlNodeHandlers(MetaList* meta_list, LinkList* link_list)
  114. {
  115. meta_handler = MakeShared<XMLNodeHandlerMeta>(meta_list);
  116. REQUIRE(meta_handler);
  117. Rml::XMLParser::RegisterNodeHandler("meta", meta_handler);
  118. link_handler = MakeShared<XMLNodeHandlerLink>(link_list);
  119. REQUIRE(link_handler);
  120. Rml::XMLParser::RegisterNodeHandler("link", link_handler);
  121. }