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