StyleSheetNode.h

00001 /*
00002  * This source file is part of libRocket, the HTML/CSS Interface Middleware
00003  *
00004  * For the latest information, see http://www.librocket.com
00005  *
00006  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  */
00027 
00028 #ifndef ROCKETCORESTYLESHEETNODE_H
00029 #define ROCKETCORESTYLESHEETNODE_H
00030 
00031 #include <Rocket/Core/PropertyDictionary.h>
00032 #include <Rocket/Core/StyleSheet.h>
00033 #include <Rocket/Core/Types.h>
00034 
00035 namespace Rocket {
00036 namespace Core {
00037 
00038 class StyleSheetNodeSelector;
00039 
00040 typedef std::map< StringList, PropertyDictionary > PseudoClassPropertyMap;
00041 
00048 class StyleSheetNode
00049 {
00050 public:
00051         enum NodeType
00052         {
00053                 TAG = 0,
00054                 CLASS,
00055                 ID,
00056                 PSEUDO_CLASS,
00057                 STRUCTURAL_PSEUDO_CLASS,
00058                 NUM_NODE_TYPES, // only counts the listed node types
00059                 ROOT                    // special node type we don't keep in a list
00060         };
00061 
00063         StyleSheetNode(const String& name, NodeType type, StyleSheetNode* parent = NULL);
00065         StyleSheetNode(const String& name, StyleSheetNode* parent, StyleSheetNodeSelector* selector, int a, int b);
00066         ~StyleSheetNode();
00067 
00069         void Write(Stream* stream);
00070 
00072         bool MergeHierarchy(StyleSheetNode* node, int specificity_offset = 0);
00074         void BuildIndex(StyleSheet::NodeIndex& styled_index, StyleSheet::NodeIndex& complete_index);
00075 
00077         const String& GetName() const;
00079         int GetSpecificity() const;
00080 
00086         void ImportProperties(const PropertyDictionary& properties, int rule_specificity);
00092         void MergeProperties(const PropertyDictionary& properties, int rule_specificity_offset);
00094         const PropertyDictionary& GetProperties() const;
00095 
00098         void GetPseudoClassProperties(PseudoClassPropertyMap& pseudo_class_properties) const;
00102         bool GetVolatilePseudoClasses(PseudoClassList& volatile_pseudo_classes) const;
00103 
00108         StyleSheetNode* GetChildNode(const String& name, NodeType type, bool create = true);
00109 
00111         bool IsApplicable(const Element* element) const;
00113         void GetApplicableDescendants(std::vector< const StyleSheetNode* >& applicable_nodes, const Element* element) const;
00114 
00118         bool IsStructurallyVolatile(bool check_ancestors = true) const;
00119 
00120 private:
00121         // Constructs a structural pseudo-class child node.
00122         StyleSheetNode* CreateStructuralChild(const String& child_name);
00123         // Recursively builds up a list of all pseudo-classes branching from a single node.
00124         void GetPseudoClassProperties(PseudoClassPropertyMap& pseudo_class_properties, const StringList& ancestor_pseudo_classes);
00125 
00126         int CalculateSpecificity();
00127 
00128         // The parent of this node; is NULL for the root node.
00129         StyleSheetNode* parent;
00130 
00131         // The name and type.
00132         String name;
00133         NodeType type;
00134 
00135         // The complex selector for this node; only used for structural nodes.
00136         StyleSheetNodeSelector* selector;
00137         int a;
00138         int b;
00139 
00140         // A measure of specificity of this node; the attribute in a node with a higher value will override those of a
00141         // node with a lower value.
00142         int specificity;
00143 
00144         // The generic properties for this node.
00145         PropertyDictionary properties;
00146 
00147         // This node's child nodes, whether standard tagged children, or further derivations of this tag by ID or class.
00148         typedef std::map< String, StyleSheetNode* > NodeMap;
00149         NodeMap children[NUM_NODE_TYPES];
00150 };
00151 
00152 }
00153 }
00154 
00155 #endif