StyleSheetParser.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 "../../Include/Rocket/Core/Log.h"
  33. #include "../../Include/Rocket/Core/StreamMemory.h"
  34. #include "../../Include/Rocket/Core/StyleSheet.h"
  35. #include "../../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->RemoveReference();
  80. stream = NULL;
  81. return success;
  82. }
  83. bool StyleSheetParser::ReadProperties(PropertyDictionary& properties)
  84. {
  85. int rule_line_number = (int)line_number;
  86. String name;
  87. String value;
  88. enum ParseState { NAME, VALUE, QUOTE };
  89. ParseState state = NAME;
  90. char character;
  91. char previous_character = 0;
  92. while (ReadCharacter(character))
  93. {
  94. parse_buffer_pos++;
  95. switch (state)
  96. {
  97. case NAME:
  98. {
  99. if (character == ';')
  100. {
  101. name = StringUtilities::StripWhitespace(name);
  102. if (!name.Empty())
  103. {
  104. 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);
  105. name.Clear();
  106. }
  107. }
  108. else if (character == '}')
  109. {
  110. name = StringUtilities::StripWhitespace(name);
  111. if (!StringUtilities::StripWhitespace(name).Empty())
  112. 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);
  113. return true;
  114. }
  115. else if (character == ':')
  116. {
  117. name = StringUtilities::StripWhitespace(name);
  118. state = VALUE;
  119. }
  120. else
  121. name.Append(character);
  122. }
  123. break;
  124. case VALUE:
  125. {
  126. if (character == ';')
  127. {
  128. value = StringUtilities::StripWhitespace(value);
  129. if (!StyleSheetSpecification::ParsePropertyDeclaration(properties, name, value, stream_file_name, rule_line_number))
  130. 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);
  131. name.Clear();
  132. value.Clear();
  133. state = NAME;
  134. }
  135. else if (character == '}')
  136. {
  137. 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);
  138. return true;
  139. }
  140. else
  141. {
  142. value.Append(character);
  143. if (character == '"')
  144. state = QUOTE;
  145. }
  146. }
  147. break;
  148. case QUOTE:
  149. {
  150. value.Append(character);
  151. if (character == '"' && previous_character != '/')
  152. state = VALUE;
  153. }
  154. break;
  155. }
  156. previous_character = character;
  157. }
  158. if (!name.Empty() || !value.Empty())
  159. Log::Message(Log::LT_WARNING, "Invalid property declaration at %s:%d", stream_file_name.CString(), line_number);
  160. return true;
  161. }
  162. // Updates the StyleNode tree, creating new nodes as necessary, setting the definition index
  163. bool StyleSheetParser::ImportProperties(StyleSheetNode* node, const String& names, const PropertyDictionary& properties, int rule_specificity)
  164. {
  165. StyleSheetNode* tag_node = NULL;
  166. StyleSheetNode* leaf_node = node;
  167. StringList nodes;
  168. StringUtilities::ExpandString(nodes, names, ' ');
  169. // Create each node going down the tree
  170. for (size_t i = 0; i < nodes.size(); i++)
  171. {
  172. String name = nodes[i];
  173. String tag;
  174. String id;
  175. StringList classes;
  176. StringList pseudo_classes;
  177. StringList structural_pseudo_classes;
  178. size_t index = 0;
  179. while (index < name.Length())
  180. {
  181. size_t start_index = index;
  182. size_t end_index = index + 1;
  183. // Read until we hit the next identifier.
  184. while (end_index < name.Length() &&
  185. name[end_index] != '#' &&
  186. name[end_index] != '.' &&
  187. name[end_index] != ':')
  188. end_index++;
  189. String identifier = name.Substring(start_index, end_index - start_index);
  190. if (!identifier.Empty())
  191. {
  192. switch (identifier[0])
  193. {
  194. case '#': id = identifier.Substring(1); break;
  195. case '.': classes.push_back(identifier.Substring(1)); break;
  196. case ':':
  197. {
  198. String pseudo_class_name = identifier.Substring(1);
  199. if (StyleSheetFactory::GetSelector(pseudo_class_name) != NULL)
  200. structural_pseudo_classes.push_back(pseudo_class_name);
  201. else
  202. pseudo_classes.push_back(pseudo_class_name);
  203. }
  204. break;
  205. default: tag = identifier;
  206. }
  207. }
  208. index = end_index;
  209. }
  210. // Sort the classes and pseudo-classes so they are consistent across equivalent declarations that shuffle the
  211. // order around.
  212. std::sort(classes.begin(), classes.end());
  213. std::sort(pseudo_classes.begin(), pseudo_classes.end());
  214. std::sort(structural_pseudo_classes.begin(), structural_pseudo_classes.end());
  215. // Get the named child node.
  216. leaf_node = leaf_node->GetChildNode(tag, StyleSheetNode::TAG);
  217. tag_node = leaf_node;
  218. if (!id.Empty())
  219. leaf_node = leaf_node->GetChildNode(id, StyleSheetNode::ID);
  220. for (size_t j = 0; j < classes.size(); ++j)
  221. leaf_node = leaf_node->GetChildNode(classes[j], StyleSheetNode::CLASS);
  222. for (size_t j = 0; j < structural_pseudo_classes.size(); ++j)
  223. leaf_node = leaf_node->GetChildNode(structural_pseudo_classes[j], StyleSheetNode::STRUCTURAL_PSEUDO_CLASS);
  224. for (size_t j = 0; j < pseudo_classes.size(); ++j)
  225. leaf_node = leaf_node->GetChildNode(pseudo_classes[j], StyleSheetNode::PSEUDO_CLASS);
  226. }
  227. // Merge the new properties with those already on the leaf node.
  228. leaf_node->ImportProperties(properties, rule_specificity);
  229. return true;
  230. }
  231. bool StyleSheetParser::FindToken(String& buffer, const char* tokens, bool remove_token)
  232. {
  233. buffer.Clear();
  234. char character;
  235. while (ReadCharacter(character))
  236. {
  237. if (strchr(tokens, character) != NULL)
  238. {
  239. if (remove_token)
  240. parse_buffer_pos++;
  241. return true;
  242. }
  243. else
  244. {
  245. buffer.Append(character);
  246. parse_buffer_pos++;
  247. }
  248. }
  249. return false;
  250. }
  251. // Attempts to find the next character in the active stream.
  252. bool StyleSheetParser::ReadCharacter(char& buffer)
  253. {
  254. bool comment = false;
  255. // Continuously fill the buffer until either we run out of
  256. // stream or we find the requested token
  257. do
  258. {
  259. while (parse_buffer_pos < parse_buffer.Length())
  260. {
  261. if (parse_buffer[parse_buffer_pos] == '\n')
  262. line_number++;
  263. else if (comment)
  264. {
  265. // Check for closing comment
  266. if (parse_buffer[parse_buffer_pos] == '*')
  267. {
  268. parse_buffer_pos++;
  269. if (parse_buffer_pos >= parse_buffer.Length())
  270. {
  271. if (!FillBuffer())
  272. return false;
  273. }
  274. if (parse_buffer[parse_buffer_pos] == '/')
  275. comment = false;
  276. }
  277. }
  278. else
  279. {
  280. // Check for an opening comment
  281. if (parse_buffer[parse_buffer_pos] == '/')
  282. {
  283. parse_buffer_pos++;
  284. if (parse_buffer_pos >= parse_buffer.Length())
  285. {
  286. if (!FillBuffer())
  287. {
  288. buffer = '/';
  289. parse_buffer = "/";
  290. return true;
  291. }
  292. }
  293. if (parse_buffer[parse_buffer_pos] == '*')
  294. comment = true;
  295. else
  296. {
  297. buffer = '/';
  298. if (parse_buffer_pos == 0)
  299. parse_buffer.Insert(parse_buffer_pos, '/');
  300. else
  301. parse_buffer_pos--;
  302. return true;
  303. }
  304. }
  305. if (!comment)
  306. {
  307. // If we find a character, return it
  308. buffer = parse_buffer[parse_buffer_pos];
  309. return true;
  310. }
  311. }
  312. parse_buffer_pos++;
  313. }
  314. }
  315. while (FillBuffer());
  316. return false;
  317. }
  318. // Fills the internal buffer with more content
  319. bool StyleSheetParser::FillBuffer()
  320. {
  321. // If theres no data to process, abort
  322. if (stream->IsEOS())
  323. return false;
  324. // Read in some data (4092 instead of 4096 to avoid the buffer growing when we have to add back
  325. // a character after a failed comment parse.)
  326. parse_buffer.Clear();
  327. bool read = stream->Read(parse_buffer, 4092) > 0;
  328. parse_buffer_pos = 0;
  329. return read;
  330. }
  331. }
  332. }