Template.cpp 4.2 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-2023 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 "Template.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../Include/RmlUi/Core/XMLParser.h"
  32. #include "XMLParseTools.h"
  33. #include <string.h>
  34. namespace Rml {
  35. Template::Template() {}
  36. Template::~Template() {}
  37. const String& Template::GetName() const
  38. {
  39. return name;
  40. }
  41. bool Template::Load(Stream* stream)
  42. {
  43. // Load the entire template into memory so we can pull out
  44. // the header and body tags
  45. String buffer;
  46. stream->Read(buffer, stream->Length());
  47. // Pull out the header
  48. const char* head_start = XMLParseTools::FindTag("head", buffer.c_str());
  49. if (!head_start)
  50. return false;
  51. const char* head_end = XMLParseTools::FindTag("head", head_start, true);
  52. if (!head_end)
  53. return false;
  54. // Advance to the end of the tag
  55. head_end = strchr(head_end, '>') + 1;
  56. // Pull out the body
  57. const char* body_start = XMLParseTools::FindTag("body", head_end);
  58. if (!body_start)
  59. return false;
  60. const char* body_end = XMLParseTools::FindTag("body", body_start, true);
  61. if (!body_end)
  62. return false;
  63. // Advance to the end of the tag
  64. body_end = strchr(body_end, '>') + 1;
  65. // Find the RML tag, skip over it and read the attributes,
  66. // storing the ones we're interested in.
  67. String attribute_name;
  68. String attribute_value;
  69. const char* ptr = XMLParseTools::FindTag("template", buffer.c_str());
  70. if (!ptr)
  71. return false;
  72. while (XMLParseTools::ReadAttribute(++ptr, attribute_name, attribute_value))
  73. {
  74. if (attribute_name == "name")
  75. name = attribute_value;
  76. if (attribute_name == "content")
  77. content = attribute_value;
  78. }
  79. // Create a stream around the header, parse it and store it
  80. auto header_stream = MakeUnique<StreamMemory>((const byte*)head_start, head_end - head_start);
  81. header_stream->SetSourceURL(stream->GetSourceURL());
  82. XMLParser parser(nullptr);
  83. parser.Parse(header_stream.get());
  84. header_stream.reset();
  85. header = *parser.GetDocumentHeader();
  86. // Store the body in stream form
  87. body = MakeUnique<StreamMemory>(body_end - body_start);
  88. body->SetSourceURL(stream->GetSourceURL());
  89. body->PushBack(body_start, body_end - body_start);
  90. return true;
  91. }
  92. Element* Template::ParseTemplate(Element* element)
  93. {
  94. body->Seek(0, SEEK_SET);
  95. const int num_children_before = element->GetNumChildren();
  96. XMLParser parser(element);
  97. parser.Parse(body.get());
  98. // If there's an inject attribute on the template, attempt to find the required element.
  99. if (!content.empty())
  100. {
  101. const int num_children_after = element->GetNumChildren();
  102. // We look through the newly added elements (and only those), and look for the desired id.
  103. for (int i = num_children_before; i < num_children_after; ++i)
  104. {
  105. Element* content_element = ElementUtilities::GetElementById(element->GetChild(i), content);
  106. if (content_element)
  107. {
  108. element = content_element;
  109. break;
  110. }
  111. }
  112. }
  113. return element;
  114. }
  115. const DocumentHeader* Template::GetHeader()
  116. {
  117. return &header;
  118. }
  119. } // namespace Rml