XMLParseTools.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "precompiled.h"
  29. #include "XMLParseTools.h"
  30. #include "../../Include/RmlUi/Core/StreamMemory.h"
  31. #include "Template.h"
  32. #include "TemplateCache.h"
  33. #include "../../Include/RmlUi/Core.h"
  34. #include <ctype.h>
  35. namespace Rml {
  36. namespace Core {
  37. // Searchs a string for the specified tag
  38. // NOTE: tag *MUST* be in lowercase
  39. const char* XMLParseTools::FindTag(const char* tag, const char* string, bool closing_tag)
  40. {
  41. int length = (int)strlen(tag);
  42. const char* ptr = string;
  43. bool found_closing = false;
  44. while (*ptr)
  45. {
  46. // Check if the first character matches
  47. if (tolower((*ptr)) == tag[0])
  48. {
  49. // If it does, check the whole word
  50. if (strncasecmp(ptr,tag,length) == 0)
  51. {
  52. // Check for opening <, loop back in the string skipping white space and forward slashes if
  53. // we're looking for the closing tag
  54. const char* tag_start = ptr - 1;
  55. while (tag_start > string && (StringUtilities::IsWhitespace(*tag_start) || *tag_start == '/'))
  56. {
  57. if (*tag_start == '/')
  58. found_closing = true;
  59. tag_start--;
  60. }
  61. // If the character we're looking at is a <, and found closing matches closing tag,
  62. // its the tag we're looking for
  63. if (*tag_start == '<' && found_closing == closing_tag)
  64. return tag_start;
  65. // Otherwise, keep looking
  66. }
  67. }
  68. ptr++;
  69. }
  70. return nullptr;
  71. }
  72. bool XMLParseTools::ReadAttribute(const char* &string, String& name, String& value)
  73. {
  74. const char* ptr = string;
  75. name = "";
  76. value = "";
  77. // Skip whitespace
  78. while (StringUtilities::IsWhitespace(*ptr))
  79. ptr++;
  80. // Look for the end of the attribute name
  81. bool found_whitespace = false;
  82. while (*ptr != '=' && *ptr != '>' && (!found_whitespace || StringUtilities::IsWhitespace(*ptr)))
  83. {
  84. if (StringUtilities::IsWhitespace(*ptr))
  85. found_whitespace = true;
  86. else
  87. name += *ptr;
  88. ptr++;
  89. }
  90. if (*ptr == '>')
  91. return false;
  92. // If we stopped on an equals, parse the value
  93. if (*ptr == '=')
  94. {
  95. // Skip over white space, ='s and quotes
  96. bool quoted = false;
  97. while (StringUtilities::IsWhitespace(*ptr) || *ptr == '\'' || *ptr == '"' || *ptr == '=')
  98. {
  99. if (*ptr == '\'' || *ptr == '"')
  100. quoted = true;
  101. ptr++;
  102. }
  103. if (*ptr == '>')
  104. return false;
  105. // Store the value
  106. while (*ptr != '\'' && *ptr != '"' && *ptr != '>' && (*ptr != ' ' || quoted))
  107. {
  108. value += *ptr++;
  109. }
  110. if (*ptr == '>')
  111. return false;
  112. // Advance passed the quote
  113. if (quoted)
  114. ptr++;
  115. }
  116. else
  117. {
  118. ptr--;
  119. }
  120. // Update the string pointer
  121. string = ptr;
  122. return true;
  123. }
  124. Element* XMLParseTools::ParseTemplate(Element* element, const String& template_name)
  125. {
  126. // Load the template, and parse it
  127. Template* parse_template = TemplateCache::GetTemplate(template_name);
  128. if (!parse_template)
  129. {
  130. Log::ParseError(element->GetOwnerDocument()->GetSourceURL(), -1, "Failed to find template '%s'.", template_name.c_str());
  131. return element;
  132. }
  133. return parse_template->ParseTemplate(element);
  134. }
  135. }
  136. }