PropertyIterators.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "../../Include/Rocket/Core/PropertyIterators.h"
  29. #include "ElementDefinition.h"
  30. namespace Rocket {
  31. namespace Core {
  32. // Return the list of pseudo classes which defines the current property, possibly null
  33. ElementDefinitionIterator::ElementDefinitionIterator() : pseudo_classes(nullptr) {}
  34. ElementDefinitionIterator::ElementDefinitionIterator(const StringList& pseudo_classes, PropertyIt it_properties, PseudoIt it_pseudo_class_properties, PropertyIt it_properties_end, PseudoIt it_pseudo_class_properties_end)
  35. : pseudo_classes(&pseudo_classes), it_properties(it_properties), it_pseudo_class_properties(it_pseudo_class_properties), it_properties_end(it_properties_end), it_pseudo_class_properties_end(it_pseudo_class_properties_end)
  36. {
  37. proceed_to_next_valid();
  38. }
  39. const PseudoClassList* ElementDefinitionIterator::pseudo_class_list() const
  40. {
  41. if (it_properties != it_properties_end)
  42. return nullptr;
  43. return &it_pseudo_class_properties->second[i_pseudo_class].first;
  44. }
  45. ElementDefinitionIterator& ElementDefinitionIterator::operator++()
  46. {
  47. // The iteration proceeds as follows:
  48. // 1. Iterate over all the default properties of the element (with no pseudo classes)
  49. // 2. Iterate over each pseudo class that has a definition for this property,
  50. // testing each one to see if it matches the currently set pseudo classes.
  51. if (it_properties != it_properties_end)
  52. {
  53. ++it_properties;
  54. proceed_to_next_valid();
  55. return *this;
  56. }
  57. ++i_pseudo_class;
  58. proceed_to_next_valid();
  59. return *this;
  60. }
  61. void ElementDefinitionIterator::proceed_to_next_valid()
  62. {
  63. if (it_properties == it_properties_end)
  64. {
  65. // Iterate over all the pseudo classes and match the applicable rules
  66. for (; it_pseudo_class_properties != it_pseudo_class_properties_end; ++it_pseudo_class_properties)
  67. {
  68. const PseudoClassPropertyList& pseudo_list = it_pseudo_class_properties->second;
  69. for (; i_pseudo_class < pseudo_list.size(); ++i_pseudo_class)
  70. {
  71. if (ElementDefinition::IsPseudoClassRuleApplicable(pseudo_list[i_pseudo_class].first, *pseudo_classes))
  72. {
  73. return;
  74. }
  75. }
  76. i_pseudo_class = 0;
  77. }
  78. }
  79. }
  80. ElementStyleIterator& ElementStyleIterator::operator++()
  81. {
  82. // First, we iterate over the local properties
  83. if (it_properties != it_properties_end)
  84. {
  85. ++it_properties;
  86. proceed_to_next_valid();
  87. return *this;
  88. }
  89. // Then, we iterate over the properties given by the element's definition
  90. ++it_definition;
  91. proceed_to_next_valid();
  92. return *this;
  93. }
  94. void ElementStyleIterator::proceed_to_next_valid()
  95. {
  96. // If we've reached the end of the local properties, continue iteration on the definition
  97. if (it_properties == it_properties_end)
  98. {
  99. for (; it_definition != it_definition_end; ++it_definition)
  100. {
  101. // Skip this property if it has been overridden by the element's local properties
  102. if (property_map && property_map->count((*it_definition).first))
  103. continue;
  104. return;
  105. }
  106. }
  107. }
  108. // Return the list of pseudo classes which defines the current property, possibly null
  109. const PseudoClassList* ElementStyleIterator::pseudo_class_list() const
  110. {
  111. if (it_properties != it_properties_end)
  112. return nullptr;
  113. return it_definition.pseudo_class_list();
  114. }
  115. ElementStyleIterator::ElementStyleIterator() : property_map(nullptr) {}
  116. ElementStyleIterator::ElementStyleIterator(const PropertyMap* property_map, PropertyIt it_properties, DefinitionIt it_definition, PropertyIt it_properties_end, DefinitionIt it_definition_end)
  117. : property_map(property_map), it_properties(it_properties), it_definition(it_definition), it_properties_end(it_properties_end), it_definition_end(it_definition_end)
  118. {
  119. proceed_to_next_valid();
  120. }
  121. }
  122. }