StyleSheetParser.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "StyleSheetParser.h"
  29. #include <algorithm>
  30. #include "StyleSheetFactory.h"
  31. #include "StyleSheetNode.h"
  32. #include <Rocket/Core/Log.h>
  33. #include <Rocket/Core/StreamMemory.h>
  34. #include <Rocket/Core/StyleSheet.h>
  35. #include <Rocket/Core/StyleSheetSpecification.h>
  36. namespace Rocket {
  37. namespace Core {
  38. StyleSheetParser::StyleSheetParser()
  39. {
  40. line_number = 0;
  41. stream = NULL;
  42. parse_buffer_pos = 0;
  43. }
  44. StyleSheetParser::~StyleSheetParser()
  45. {
  46. }
  47. int StyleSheetParser::Parse(StyleSheetNode* node, Stream* _stream)
  48. {
  49. int rule_count = 0;
  50. line_number = 0;
  51. stream = _stream;
  52. stream_file_name = stream->GetSourceURL().GetURL().Replace("|", ":");
  53. // Look for more styles while data is available
  54. while (FillBuffer())
  55. {
  56. String style_names;
  57. while (FindToken(style_names, "{", true))
  58. {
  59. // Read the attributes
  60. PropertyDictionary properties;
  61. if (!ReadProperties(properties))
  62. {
  63. continue;
  64. }
  65. StringList style_name_list;
  66. StringUtilities::ExpandString(style_name_list, style_names);
  67. // Add style nodes to the root of the tree
  68. for (size_t i = 0; i < style_name_list.size(); i++)
  69. ImportProperties(node, style_name_list[i], properties, rule_count);
  70. rule_count++;
  71. }
  72. }
  73. return rule_count;
  74. }
  75. bool StyleSheetParser::ParseProperties(PropertyDictionary& parsed_properties, const String& properties)
  76. {
  77. stream = new StreamMemory((const byte*)properties.CString(), properties.Length());
  78. bool success = ReadProperties(parsed_properties);
  79. stream->Close();
  80. return success;
  81. }
  82. bool StyleSheetParser::ReadProperties(PropertyDictionary& properties)
  83. {
  84. int rule_line_number = line_number;
  85. String name;
  86. String value;
  87. enum ParseState { NAME, VALUE, QUOTE };
  88. ParseState state = NAME;
  89. char character;
  90. char previous_character = 0;
  91. while (ReadCharacter(character))
  92. {
  93. parse_buffer_pos++;
  94. switch (state)
  95. {
  96. case NAME:
  97. {
  98. if (character == ';')
  99. {
  100. name = StringUtilities::StripWhitespace(name);
  101. if (!name.Empty())
  102. {
  103. Log::Message(Log::LT_WARNING, "Found name with no value parsing property declaration '%s' at %s:%d", name.CString(), stream_file_name.CString(), line_number);
  104. name.Clear();
  105. }
  106. }
  107. else if (character == '}')
  108. {
  109. name = StringUtilities::StripWhitespace(name);
  110. if (!StringUtilities::StripWhitespace(name).Empty())
  111. Log::Message(Log::LT_WARNING, "End of rule encountered while parsing property declaration '%s' at %s:%d", name.CString(), stream_file_name.CString(), line_number);
  112. return true;
  113. }
  114. else if (character == ':')
  115. {
  116. name = StringUtilities::StripWhitespace(name);
  117. state = VALUE;
  118. }
  119. else
  120. name.Append(character);
  121. }
  122. break;
  123. case VALUE:
  124. {
  125. if (character == ';')
  126. {
  127. value = StringUtilities::StripWhitespace(value);
  128. if (!StyleSheetSpecification::ParsePropertyDeclaration(properties, name, value, stream_file_name, rule_line_number))
  129. Log::Message(Log::LT_WARNING, "Syntax error parsing property declaration '%s: %s;' in %s: %d.", name.CString(), value.CString(), stream_file_name.CString(), line_number);
  130. name.Clear();
  131. value.Clear();
  132. state = NAME;
  133. }
  134. else if (character == '}')
  135. {
  136. Log::Message(Log::LT_WARNING, "End of rule encountered while parsing property declaration '%s: %s;' in %s: %d.", name.CString(), value.CString(), stream_file_name.CString(), line_number);
  137. return true;
  138. }
  139. else
  140. {
  141. value.Append(character);
  142. if (character == '"')
  143. state = QUOTE;
  144. }
  145. }
  146. break;
  147. case QUOTE:
  148. {
  149. value.Append(character);
  150. if (character == '"' && previous_character != '/')
  151. state = VALUE;
  152. }
  153. break;
  154. }
  155. previous_character = character;
  156. }
  157. if (!name.Empty() || !value.Empty())
  158. Log::Message(Log::LT_WARNING, "Invalid property declaration at %s:%d", stream_file_name.CString(), line_number);
  159. return true;
  160. }
  161. // Updates the StyleNode tree, creating new nodes as necessary, setting the definition index
  162. bool StyleSheetParser::ImportProperties(StyleSheetNode* node, const String& names, const PropertyDictionary& properties, int rule_specificity)
  163. {
  164. StyleSheetNode* tag_node = NULL;
  165. StyleSheetNode* leaf_node = node;
  166. StringList nodes;
  167. StringUtilities::ExpandString(nodes, names, ' ');
  168. // Create each node going down the tree
  169. for (size_t i = 0; i < nodes.size(); i++)
  170. {
  171. String name = nodes[i];
  172. String tag;
  173. String id;
  174. StringList classes;
  175. StringList pseudo_classes;
  176. StringList structural_pseudo_classes;
  177. size_t index = 0;
  178. while (index < name.Length())
  179. {
  180. size_t start_index = index;
  181. size_t end_index = index + 1;
  182. // Read until we hit the next identifier.
  183. while (end_index < name.Length() &&
  184. name[end_index] != '#' &&
  185. name[end_index] != '.' &&
  186. name[end_index] != ':')
  187. end_index++;
  188. String identifier = name.Substring(start_index, end_index - start_index);
  189. if (!identifier.Empty())
  190. {
  191. switch (identifier[0])
  192. {
  193. case '#': id = identifier.Substring(1); break;
  194. case '.': classes.push_back(identifier.Substring(1)); break;
  195. case ':':
  196. {
  197. String pseudo_class_name = identifier.Substring(1);
  198. if (StyleSheetFactory::GetSelector(pseudo_class_name) != NULL)
  199. structural_pseudo_classes.push_back(pseudo_class_name);
  200. else
  201. pseudo_classes.push_back(pseudo_class_name);
  202. }
  203. break;
  204. default: tag = identifier;
  205. }
  206. }
  207. index = end_index;
  208. }
  209. // Sort the classes and pseudo-classes so they are consistent across equivalent declarations that shuffle the
  210. // order around.
  211. std::sort(classes.begin(), classes.end());
  212. std::sort(pseudo_classes.begin(), pseudo_classes.end());
  213. std::sort(structural_pseudo_classes.begin(), structural_pseudo_classes.end());
  214. // Get the named child node.
  215. leaf_node = leaf_node->GetChildNode(tag, StyleSheetNode::TAG);
  216. tag_node = leaf_node;
  217. if (!id.Empty())
  218. leaf_node = leaf_node->GetChildNode(id, StyleSheetNode::ID);
  219. for (size_t j = 0; j < classes.size(); ++j)
  220. leaf_node = leaf_node->GetChildNode(classes[j], StyleSheetNode::CLASS);
  221. for (size_t j = 0; j < structural_pseudo_classes.size(); ++j)
  222. leaf_node = leaf_node->GetChildNode(structural_pseudo_classes[j], StyleSheetNode::STRUCTURAL_PSEUDO_CLASS);
  223. for (size_t j = 0; j < pseudo_classes.size(); ++j)
  224. leaf_node = leaf_node->GetChildNode(pseudo_classes[j], StyleSheetNode::PSEUDO_CLASS);
  225. }
  226. // Merge the new properties with those already on the leaf node.
  227. leaf_node->ImportProperties(properties, rule_specificity);
  228. return true;
  229. }
  230. bool StyleSheetParser::FindToken(String& buffer, const char* tokens, bool remove_token)
  231. {
  232. buffer.Clear();
  233. char character;
  234. while (ReadCharacter(character))
  235. {
  236. if (strchr(tokens, character) != NULL)
  237. {
  238. if (remove_token)
  239. parse_buffer_pos++;
  240. return true;
  241. }
  242. else
  243. {
  244. buffer.Append(character);
  245. parse_buffer_pos++;
  246. }
  247. }
  248. return false;
  249. }
  250. // Attempts to find the next character in the active stream.
  251. bool StyleSheetParser::ReadCharacter(char& buffer)
  252. {
  253. bool comment = false;
  254. // Continuously fill the buffer until either we run out of
  255. // stream or we find the requested token
  256. do
  257. {
  258. while (parse_buffer_pos < parse_buffer.Length())
  259. {
  260. if (parse_buffer[parse_buffer_pos] == '\n')
  261. line_number++;
  262. else if (comment)
  263. {
  264. // Check for closing comment
  265. if (parse_buffer[parse_buffer_pos] == '*')
  266. {
  267. parse_buffer_pos++;
  268. if (parse_buffer_pos >= parse_buffer.Length())
  269. {
  270. if (!FillBuffer())
  271. return false;
  272. }
  273. if (parse_buffer[parse_buffer_pos] == '/')
  274. comment = false;
  275. }
  276. }
  277. else
  278. {
  279. // Check for an opening comment
  280. if (parse_buffer[parse_buffer_pos] == '/')
  281. {
  282. parse_buffer_pos++;
  283. if (parse_buffer_pos >= parse_buffer.Length())
  284. {
  285. if (!FillBuffer())
  286. {
  287. buffer = '/';
  288. parse_buffer = "/";
  289. return true;
  290. }
  291. }
  292. if (parse_buffer[parse_buffer_pos] == '*')
  293. comment = true;
  294. else
  295. {
  296. buffer = '/';
  297. if (parse_buffer_pos == 0)
  298. parse_buffer.Insert(parse_buffer_pos, '/');
  299. else
  300. parse_buffer_pos--;
  301. return true;
  302. }
  303. }
  304. if (!comment)
  305. {
  306. // If we find a character, return it
  307. buffer = parse_buffer[parse_buffer_pos];
  308. return true;
  309. }
  310. }
  311. parse_buffer_pos++;
  312. }
  313. }
  314. while (FillBuffer());
  315. return false;
  316. }
  317. // Fills the internal buffer with more content
  318. bool StyleSheetParser::FillBuffer()
  319. {
  320. // If theres no data to process, abort
  321. if (stream->IsEOS())
  322. return false;
  323. // Read in some data (4092 instead of 4096 to avoid the buffer growing when we have to add back
  324. // a character after a failed comment parse.)
  325. parse_buffer.Clear();
  326. bool read = stream->Read(parse_buffer, 4092) > 0;
  327. parse_buffer_pos = 0;
  328. return read;
  329. }
  330. }
  331. }