XMLParseTools.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "XMLParseTools.h"
  29. #include "../../Include/RmlUi/Core/ElementDocument.h"
  30. #include "../../Include/RmlUi/Core/StreamMemory.h"
  31. #include "../../Include/RmlUi/Core/StringUtilities.h"
  32. #include "../../Include/RmlUi/Core/Types.h"
  33. #include "Template.h"
  34. #include "TemplateCache.h"
  35. #include <ctype.h>
  36. #include <string.h>
  37. namespace Rml {
  38. const char* XMLParseTools::FindTag(const char* tag, const char* string, bool closing_tag)
  39. {
  40. const size_t length = strlen(tag);
  41. const char* ptr = string;
  42. bool found_closing = false;
  43. while (*ptr)
  44. {
  45. // Check if the first character matches
  46. if (tolower((*ptr)) == tag[0])
  47. {
  48. // If it does, check the whole word
  49. if (StringUtilities::StringCompareCaseInsensitive(StringView(ptr, ptr + length), StringView(tag, tag + length)))
  50. {
  51. // Check for opening <, loop back in the string skipping white space and forward slashes if
  52. // we're looking for the closing tag
  53. const char* tag_start = ptr - 1;
  54. while (tag_start > string && (StringUtilities::IsWhitespace(*tag_start) || *tag_start == '/'))
  55. {
  56. if (*tag_start == '/')
  57. found_closing = true;
  58. tag_start--;
  59. }
  60. // If the character we're looking at is a <, and found closing matches closing tag,
  61. // its the tag we're looking for
  62. if (*tag_start == '<' && found_closing == closing_tag)
  63. return tag_start;
  64. // Otherwise, keep looking
  65. }
  66. }
  67. ptr++;
  68. }
  69. return nullptr;
  70. }
  71. bool XMLParseTools::ReadAttribute(const char*& string, String& name, String& value)
  72. {
  73. const char* ptr = string;
  74. name = "";
  75. value = "";
  76. // Skip whitespace
  77. while (StringUtilities::IsWhitespace(*ptr))
  78. ptr++;
  79. // Look for the end of the attribute name
  80. bool found_whitespace = false;
  81. while (*ptr != '=' && *ptr != '>' && (!found_whitespace || StringUtilities::IsWhitespace(*ptr)))
  82. {
  83. if (StringUtilities::IsWhitespace(*ptr))
  84. found_whitespace = true;
  85. else
  86. name += *ptr;
  87. ptr++;
  88. }
  89. if (*ptr == '>')
  90. return false;
  91. // If we stopped on an equals, parse the value
  92. if (*ptr == '=')
  93. {
  94. // Skip over white space, ='s and quotes
  95. bool quoted = false;
  96. while (StringUtilities::IsWhitespace(*ptr) || *ptr == '\'' || *ptr == '"' || *ptr == '=')
  97. {
  98. if (*ptr == '\'' || *ptr == '"')
  99. quoted = true;
  100. ptr++;
  101. }
  102. if (*ptr == '>')
  103. return false;
  104. // Store the value
  105. while (*ptr != '\'' && *ptr != '"' && *ptr != '>' && (*ptr != ' ' || quoted))
  106. {
  107. value += *ptr++;
  108. }
  109. if (*ptr == '>')
  110. return false;
  111. // Advance passed the quote
  112. if (quoted)
  113. ptr++;
  114. }
  115. else
  116. {
  117. ptr--;
  118. }
  119. // Update the string pointer
  120. string = ptr;
  121. return true;
  122. }
  123. Element* XMLParseTools::ParseTemplate(Element* element, const String& template_name)
  124. {
  125. // Load the template, and parse it
  126. Template* parse_template = TemplateCache::GetTemplate(template_name);
  127. if (!parse_template)
  128. {
  129. Log::ParseError(element->GetOwnerDocument()->GetSourceURL(), -1, "Failed to find template '%s'.", template_name.c_str());
  130. return element;
  131. }
  132. return parse_template->ParseTemplate(element);
  133. }
  134. const char* XMLParseTools::ParseDataBrackets(bool& inside_brackets, bool& inside_string, char c, char previous)
  135. {
  136. if (inside_brackets)
  137. {
  138. if (c == '\'')
  139. inside_string = !inside_string;
  140. if (!inside_string)
  141. {
  142. if (c == '}' && previous == '}')
  143. inside_brackets = false;
  144. else if (c == '{' && previous == '{')
  145. return "Nested double curly brackets are illegal.";
  146. else if (previous == '}' && c != '}' && c != '\'')
  147. return "Single closing curly bracket encountered, use double curly brackets to close an expression.";
  148. else if (previous == '/' && c == '>')
  149. return "Closing double curly brackets not found, XML end node encountered first.";
  150. else if (previous == '<' && c == '/')
  151. return "Closing double curly brackets not found, XML end node encountered first.";
  152. }
  153. }
  154. else
  155. {
  156. if (c == '{' && previous == '{')
  157. {
  158. inside_brackets = true;
  159. }
  160. else if (c == '}' && previous == '}')
  161. {
  162. return "Closing double curly brackets encountered outside an expression.";
  163. }
  164. }
  165. return nullptr;
  166. }
  167. } // namespace Rml