| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781 |
- /*
- * 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 "ComputeProperty.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 {
- class AbstractPropertyParser {
- public:
- virtual bool Parse(const String& name, const String& value, const String& stream_file_name, int rule_line_number) = 0;
- };
- /*
- * PropertySpecificationParser just passes the parsing to a property specification. Usually
- * the main stylesheet specification, except for e.g. @decorator blocks.
- */
- class PropertySpecificationParser : public AbstractPropertyParser {
- private:
- PropertyDictionary& properties;
- const PropertySpecification& specification;
- public:
- PropertySpecificationParser(PropertyDictionary& properties, const PropertySpecification& specification) : properties(properties), specification(specification) {}
- bool Parse(const String& name, const String& value, const String& stream_file_name, int rule_line_number) override
- {
- return specification.ParsePropertyDeclaration(properties, name, value, stream_file_name, rule_line_number);
- }
- };
- /*
- * Spritesheets need a special parser because its property names are arbitrary keys,
- * while its values are always rectangles. Thus, it must be parsed with a special "rectangle" parser
- * for every name-value pair. We can probably optimize this for @performance.
- */
- class SpritesheetPropertyParser : public AbstractPropertyParser {
- private:
- String image_source;
- SpriteDefinitionList sprite_definitions;
- PropertyDictionary properties;
- PropertySpecification specification;
- PropertyId id_rx, id_ry, id_rw, id_rh;
- ShorthandId id_rectangle;
- public:
- SpritesheetPropertyParser() : specification(4, 1)
- {
- id_rx = specification.RegisterProperty("rectangle-x", "", false, false).AddParser("length").GetId();
- id_ry = specification.RegisterProperty("rectangle-y", "", false, false).AddParser("length").GetId();
- id_rw = specification.RegisterProperty("rectangle-w", "", false, false).AddParser("length").GetId();
- id_rh = specification.RegisterProperty("rectangle-h", "", false, false).AddParser("length").GetId();
- id_rectangle = specification.RegisterShorthand("rectangle", "rectangle-x, rectangle-y, rectangle-w, rectangle-h", ShorthandType::FallThrough);
- }
- const String& GetImageSource() const
- {
- return image_source;
- }
- const SpriteDefinitionList& GetSpriteDefinitions() const
- {
- return sprite_definitions;
- }
- void Clear() {
- image_source.clear();
- sprite_definitions.clear();
- }
- bool Parse(const String& name, const String& value, const String& stream_file_name, int rule_line_number) override
- {
- static const String str_src = "src";
- static const String str_rectangle = "rectangle";
- if (name == str_src)
- {
- image_source = value;
- }
- else
- {
- if (!specification.ParseShorthandDeclaration(properties, id_rectangle, value, stream_file_name, rule_line_number))
- return false;
- Rectangle rectangle;
- if (auto property = properties.GetProperty(id_rx))
- rectangle.x = ComputeAbsoluteLength(*property, 1.f);
- if (auto property = properties.GetProperty(id_ry))
- rectangle.y = ComputeAbsoluteLength(*property, 1.f);
- if (auto property = properties.GetProperty(id_rw))
- rectangle.width = ComputeAbsoluteLength(*property, 1.f);
- if (auto property = properties.GetProperty(id_rh))
- rectangle.height = ComputeAbsoluteLength(*property, 1.f);
- sprite_definitions.emplace_back(name, rectangle);
- }
- return true;
- }
- };
- StyleSheetParser::StyleSheetParser()
- {
- line_number = 0;
- stream = NULL;
- parse_buffer_pos = 0;
- }
- StyleSheetParser::~StyleSheetParser()
- {
- }
- static bool IsValidIdentifier(const String& str)
- {
- if (str.empty())
- return false;
- for (int i = 0; i < str.size(); i++)
- {
- char c = str[i];
- bool valid = (
- (c >= 'a' && c <= 'z')
- || (c >= 'A' && c <= 'Z')
- || (c >= '0' && c <= '9')
- || (c == '-')
- || (c == '_')
- );
- if (!valid)
- return false;
- }
- return true;
- }
- static void PostprocessKeyframes(KeyframesMap& keyframes_map)
- {
- for (auto& keyframes_pair : keyframes_map)
- {
- Keyframes& keyframes = keyframes_pair.second;
- auto& blocks = keyframes.blocks;
- auto& property_ids = keyframes.property_ids;
- // Sort keyframes on selector value.
- std::sort(blocks.begin(), blocks.end(), [](const KeyframeBlock& a, const KeyframeBlock& b) { return a.normalized_time < b.normalized_time; });
- // Add all property names specified by any block
- if(blocks.size() > 0) property_ids.reserve(blocks.size() * blocks[0].properties.GetNumProperties());
- for(auto& block : blocks)
- {
- for (auto& property : block.properties.GetProperties())
- property_ids.push_back(property.first);
- }
- // Remove duplicate property names
- std::sort(property_ids.begin(), property_ids.end());
- property_ids.erase(std::unique(property_ids.begin(), property_ids.end()), property_ids.end());
- property_ids.shrink_to_fit();
- }
- }
- bool StyleSheetParser::ParseKeyframeBlock(KeyframesMap& keyframes_map, const String& identifier, const String& rules, const PropertyDictionary& properties)
- {
- if (!IsValidIdentifier(identifier))
- {
- Log::Message(Log::LT_WARNING, "Invalid keyframes identifier '%s' at %s:%d", identifier.c_str(), stream_file_name.c_str(), line_number);
- return false;
- }
- if (properties.GetNumProperties() == 0)
- return true;
- StringList rule_list;
- StringUtilities::ExpandString(rule_list, rules);
- std::vector<float> rule_values;
- rule_values.reserve(rule_list.size());
- for (auto rule : rule_list)
- {
- float value = 0.0f;
- int count = 0;
- rule = ToLower(rule);
- if (rule == "from")
- rule_values.push_back(0.0f);
- else if (rule == "to")
- rule_values.push_back(1.0f);
- else if(sscanf(rule.c_str(), "%f%%%n", &value, &count) == 1)
- if(count > 0 && value >= 0.0f && value <= 100.0f)
- rule_values.push_back(0.01f * value);
- }
- if (rule_values.empty())
- {
- Log::Message(Log::LT_WARNING, "Invalid keyframes rule(s) '%s' at %s:%d", rules.c_str(), stream_file_name.c_str(), line_number);
- return false;
- }
- Keyframes& keyframes = keyframes_map[identifier];
- for(float selector : rule_values)
- {
- auto it = std::find_if(keyframes.blocks.begin(), keyframes.blocks.end(), [selector](const KeyframeBlock& keyframe_block) { return Math::AbsoluteValue(keyframe_block.normalized_time - selector) < 0.0001f; });
- if (it == keyframes.blocks.end())
- {
- keyframes.blocks.push_back(KeyframeBlock{ selector });
- it = (keyframes.blocks.end() - 1);
- }
- else
- {
- // In case of duplicate keyframes, we only use the latest definition as per CSS rules
- it->properties = PropertyDictionary();
- }
- it->properties.Import(properties);
- }
- return true;
- }
- bool StyleSheetParser::ParseDecoratorBlock(const String& at_name, DecoratorSpecificationMap& decorator_map, const StyleSheet& style_sheet)
- {
- StringList name_type;
- StringUtilities::ExpandString(name_type, at_name, ':');
- if (name_type.size() != 2 || name_type[0].empty() || name_type[1].empty())
- {
- Log::Message(Log::LT_WARNING, "Decorator syntax error at %s:%d. Use syntax: '@decorator name : type { ... }'.", stream_file_name.c_str(), line_number);
- return false;
- }
- const String& name = name_type[0];
- String decorator_type = name_type[1];
- auto it_find = decorator_map.find(name);
- if (it_find != decorator_map.end())
- {
- Log::Message(Log::LT_WARNING, "Decorator with name '%s' already declared, ignoring decorator at %s:%d.", name.c_str(), stream_file_name.c_str(), line_number);
- return false;
- }
- // Get the instancer associated with the decorator type
- DecoratorInstancer* decorator_instancer = Factory::GetDecoratorInstancer(decorator_type);
- PropertyDictionary properties;
- if(!decorator_instancer)
- {
- // Type is not a declared decorator type, instead, see if it is another decorator name, then we inherit its properties.
- auto it = decorator_map.find(decorator_type);
- if (it != decorator_map.end())
- {
- // Yes, try to retrieve the instancer from the parent type, and add its property values.
- decorator_instancer = Factory::GetDecoratorInstancer(it->second.decorator_type);
- properties = it->second.properties;
- decorator_type = it->second.decorator_type;
- }
- // If we still don't have an instancer, we cannot continue.
- if (!decorator_instancer)
- {
- Log::Message(Log::LT_WARNING, "Invalid decorator type '%s' declared at %s:%d.", decorator_type.c_str(), stream_file_name.c_str(), line_number);
- return false;
- }
- }
- const PropertySpecification& property_specification = decorator_instancer->GetPropertySpecification();
- PropertySpecificationParser parser(properties, property_specification);
- if (!ReadProperties(parser))
- return false;
- // Set non-defined properties to their defaults
- property_specification.SetPropertyDefaults(properties);
- std::shared_ptr<Decorator> decorator = decorator_instancer->InstanceDecorator(decorator_type, properties, DecoratorInstancerInterface(style_sheet));
- if (!decorator)
- {
- Log::Message(Log::LT_WARNING, "Could not instance decorator of type '%s' declared at %s:%d.", decorator_type.c_str(), stream_file_name.c_str(), line_number);
- return false;
- }
- decorator_map.emplace(name, DecoratorSpecification{ std::move(decorator_type), std::move(properties), std::move(decorator) });
- return true;
- }
- int StyleSheetParser::Parse(StyleSheetNode* node, Stream* _stream, const StyleSheet& style_sheet, KeyframesMap& keyframes, DecoratorSpecificationMap& decorator_map, SpritesheetList& spritesheet_list)
- {
- int rule_count = 0;
- line_number = 0;
- stream = _stream;
- stream_file_name = Replace(stream->GetSourceURL().GetURL(), '|', ':');
- enum class State { Global, AtRuleIdentifier, KeyframeBlock, Invalid };
- State state = State::Global;
- // At-rules given by the following syntax in global space: @identifier name { block }
- String at_rule_name;
- // Look for more styles while data is available
- while (FillBuffer())
- {
- String pre_token_str;
-
- while (char token = FindToken(pre_token_str, "{@}", true))
- {
- switch (state)
- {
- case State::Global:
- {
- if (token == '{')
- {
- // Read the attributes
- PropertyDictionary properties;
- PropertySpecificationParser parser(properties, StyleSheetSpecification::GetPropertySpecification());
- if (!ReadProperties(parser))
- continue;
- StringList style_name_list;
- StringUtilities::ExpandString(style_name_list, pre_token_str);
- // 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++;
- }
- else if (token == '@')
- {
- state = State::AtRuleIdentifier;
- }
- else
- {
- Log::Message(Log::LT_WARNING, "Invalid character '%c' found while parsing stylesheet at %s:%d. Trying to proceed.", token, stream_file_name.c_str(), line_number);
- }
- }
- break;
- case State::AtRuleIdentifier:
- {
- if (token == '{')
- {
- String at_rule_identifier = pre_token_str.substr(0, pre_token_str.find(' '));
- at_rule_name = StringUtilities::StripWhitespace(pre_token_str.substr(at_rule_identifier.size()));
- if (at_rule_identifier == KEYFRAMES)
- {
- state = State::KeyframeBlock;
- }
- else if (at_rule_identifier == "decorator")
- {
- ParseDecoratorBlock(at_rule_name, decorator_map, style_sheet);
-
- at_rule_name.clear();
- state = State::Global;
- }
- else if (at_rule_identifier == "spritesheet")
- {
- // This is reasonably heavy to initialize, so we make it static
- static SpritesheetPropertyParser spritesheet_property_parser;
- spritesheet_property_parser.Clear();
- ReadProperties(spritesheet_property_parser);
- const String& image_source = spritesheet_property_parser.GetImageSource();
- const SpriteDefinitionList& sprite_definitions = spritesheet_property_parser.GetSpriteDefinitions();
-
- if (at_rule_name.empty())
- {
- Log::Message(Log::LT_WARNING, "No name given for @spritesheet at %s:%d", stream_file_name.c_str(), line_number);
- }
- else if (sprite_definitions.empty())
- {
- Log::Message(Log::LT_WARNING, "Spritesheet with name '%s' has no sprites defined, ignored. At %s:%d", at_rule_name.c_str(), stream_file_name.c_str(), line_number);
- }
- else if (image_source.empty())
- {
- Log::Message(Log::LT_WARNING, "No image source (property 'src') specified for spritesheet '%s'. At %s:%d", at_rule_name.c_str(), stream_file_name.c_str(), line_number);
- }
- else
- {
- spritesheet_list.AddSpriteSheet(at_rule_name, image_source, stream_file_name, (int)line_number, sprite_definitions);
- }
- spritesheet_property_parser.Clear();
- at_rule_name.clear();
- state = State::Global;
- }
- else
- {
- // Invalid identifier, should ignore
- at_rule_name.clear();
- state = State::Global;
- Log::Message(Log::LT_WARNING, "Invalid at-rule identifier '%s' found in stylesheet at %s:%d", at_rule_identifier.c_str(), stream_file_name.c_str(), line_number);
- }
- }
- else
- {
- Log::Message(Log::LT_WARNING, "Invalid character '%c' found while parsing at-rule identifier in stylesheet at %s:%d", token, stream_file_name.c_str(), line_number);
- state = State::Invalid;
- }
- }
- break;
- case State::KeyframeBlock:
- {
- if (token == '{')
- {
- // Each keyframe in keyframes has its own block which is processed here
- PropertyDictionary properties;
- PropertySpecificationParser parser(properties, StyleSheetSpecification::GetPropertySpecification());
- continue;
- if (!ParseKeyframeBlock(keyframes, at_rule_name, pre_token_str, properties))
- continue;
- }
- else if (token == '}')
- {
- at_rule_name.clear();
- state = State::Global;
- }
- else
- {
- Log::Message(Log::LT_WARNING, "Invalid character '%c' found while parsing keyframe block in stylesheet at %s:%d", token, stream_file_name.c_str(), line_number);
- state = State::Invalid;
- }
- }
- break;
- default:
- ROCKET_ERROR;
- state = State::Invalid;
- break;
- }
- if (state == State::Invalid)
- break;
- }
- if (state == State::Invalid)
- break;
- }
- PostprocessKeyframes(keyframes);
- return rule_count;
- }
- bool StyleSheetParser::ParseProperties(PropertyDictionary& parsed_properties, const String& properties)
- {
- stream = new StreamMemory((const byte*)properties.c_str(), properties.size());
- PropertySpecificationParser parser(parsed_properties, StyleSheetSpecification::GetPropertySpecification());
- bool success = ReadProperties(parser);
- stream->RemoveReference();
- stream = NULL;
- return success;
- }
- bool StyleSheetParser::ReadProperties(AbstractPropertyParser& property_parser)
- {
- 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 while parsing property declaration '%s' at %s:%d", name.c_str(), stream_file_name.c_str(), 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.c_str(), stream_file_name.c_str(), line_number);
- return true;
- }
- else if (character == ':')
- {
- name = StringUtilities::StripWhitespace(name);
- state = VALUE;
- }
- else
- name += character;
- }
- break;
-
- case VALUE:
- {
- if (character == ';')
- {
- value = StringUtilities::StripWhitespace(value);
- if (!property_parser.Parse(name, value, stream_file_name, rule_line_number))
- Log::Message(Log::LT_WARNING, "Syntax error parsing property declaration '%s: %s;' in %s: %d.", name.c_str(), value.c_str(), stream_file_name.c_str(), 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.c_str(), value.c_str(), stream_file_name.c_str(), line_number);
- return true;
- }
- else
- {
- value += character;
- if (character == '"')
- state = QUOTE;
- }
- }
- break;
- case QUOTE:
- {
- value += character;
- if (character == '"' && previous_character != '/')
- state = VALUE;
- }
- break;
- }
- previous_character = character;
- }
- if (!name.empty() || !value.empty())
- Log::Message(Log::LT_WARNING, "Invalid property declaration '%s':'%s' at %s:%d", name.c_str(), value.c_str(), stream_file_name.c_str(), 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.size())
- {
- size_t start_index = index;
- size_t end_index = index + 1;
- // Read until we hit the next identifier.
- while (end_index < name.size() &&
- name[end_index] != '#' &&
- name[end_index] != '.' &&
- name[end_index] != ':')
- end_index++;
- String identifier = name.substr(start_index, end_index - start_index);
- if (!identifier.empty())
- {
- switch (identifier[0])
- {
- case '#': id = identifier.substr(1); break;
- case '.': classes.push_back(identifier.substr(1)); break;
- case ':':
- {
- String pseudo_class_name = identifier.substr(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;
- }
- char 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 character;
- }
- else
- {
- buffer += character;
- parse_buffer_pos++;
- }
- }
- return 0;
- }
- // 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.size())
- {
- 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.size())
- {
- 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.size())
- {
- 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, 1, '/');
- 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;
- }
- }
- }
|