XMLParseTools.cpp 5.3 KB

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