CSSValueList.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * (C) 1999-2003 Lars Knoll (knoll@kde.org)
  3. * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #ifndef CSSValueList_h
  21. #define CSSValueList_h
  22. #include "CSSValue.h"
  23. #include <wtf/PassRefPtr.h>
  24. #include <wtf/Vector.h>
  25. namespace WebCore {
  26. class CSSParserValueList;
  27. class CSSValueList : public CSSValue {
  28. public:
  29. static PassRefPtr<CSSValueList> createCommaSeparated()
  30. {
  31. return adoptRef(new CSSValueList(CommaSeparator));
  32. }
  33. static PassRefPtr<CSSValueList> createSpaceSeparated()
  34. {
  35. return adoptRef(new CSSValueList(SpaceSeparator));
  36. }
  37. static PassRefPtr<CSSValueList> createSlashSeparated()
  38. {
  39. return adoptRef(new CSSValueList(SlashSeparator));
  40. }
  41. static PassRefPtr<CSSValueList> createFromParserValueList(CSSParserValueList* list)
  42. {
  43. return adoptRef(new CSSValueList(list));
  44. }
  45. size_t length() const { return m_values.size(); }
  46. CSSValue* item(size_t index) { return index < m_values.size() ? m_values[index].get() : 0; }
  47. const CSSValue* item(size_t index) const { return index < m_values.size() ? m_values[index].get() : 0; }
  48. CSSValue* itemWithoutBoundsCheck(size_t index) { return m_values[index].get(); }
  49. void append(PassRefPtr<CSSValue> value) { m_values.append(value); }
  50. void prepend(PassRefPtr<CSSValue> value) { m_values.insert(0, value); }
  51. bool removeAll(CSSValue*);
  52. bool hasValue(CSSValue*) const;
  53. PassRefPtr<CSSValueList> copy();
  54. String customCssText() const;
  55. bool equals(const CSSValueList&) const;
  56. bool equals(const CSSValue&) const;
  57. #if ENABLE(CSS_VARIABLES)
  58. String customSerializeResolvingVariables(const HashMap<AtomicString, String>&) const;
  59. #endif
  60. void addSubresourceStyleURLs(ListHashSet<KURL>&, const StyleSheetContents*) const;
  61. bool hasFailedOrCanceledSubresources() const;
  62. PassRefPtr<CSSValueList> cloneForCSSOM() const;
  63. protected:
  64. CSSValueList(ClassType, ValueListSeparator);
  65. CSSValueList(const CSSValueList& cloneFrom);
  66. private:
  67. explicit CSSValueList(ValueListSeparator);
  68. explicit CSSValueList(CSSParserValueList*);
  69. Vector<RefPtr<CSSValue>, 4> m_values;
  70. };
  71. // Objects of this class are intended to be stack-allocated and scoped to a single function.
  72. // Please take care not to pass these around as they do hold onto a raw pointer.
  73. class CSSValueListInspector {
  74. public:
  75. CSSValueListInspector(CSSValue* value) : m_list((value && value->isValueList()) ? static_cast<CSSValueList*>(value) : 0) { }
  76. CSSValue* item(size_t index) const { ASSERT_WITH_SECURITY_IMPLICATION(index < length()); return m_list->itemWithoutBoundsCheck(index); }
  77. CSSValue* first() const { return item(0); }
  78. CSSValue* second() const { return item(1); }
  79. size_t length() const { return m_list ? m_list->length() : 0; }
  80. private:
  81. CSSValueList* m_list;
  82. };
  83. // Wrapper that can be used to iterate over any CSSValue. Non-list values and 0 behave as zero-length lists.
  84. // Objects of this class are intended to be stack-allocated and scoped to a single function.
  85. // Please take care not to pass these around as they do hold onto a raw pointer.
  86. class CSSValueListIterator {
  87. public:
  88. CSSValueListIterator(CSSValue* value) : m_inspector(value), m_position(0) { }
  89. bool hasMore() const { return m_position < m_inspector.length(); }
  90. CSSValue* value() const { return m_inspector.item(m_position); }
  91. bool isPrimitiveValue() const { return value()->isPrimitiveValue(); }
  92. void advance() { m_position++; ASSERT(m_position <= m_inspector.length());}
  93. size_t index() const { return m_position; }
  94. private:
  95. CSSValueListInspector m_inspector;
  96. size_t m_position;
  97. };
  98. } // namespace WebCore
  99. #endif // CSSValueList_h