XMLParseTools.cpp 5.4 KB

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