/platform/external/webkit/WebCore/css/CSSMediaRule.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 131 lines · 76 code · 22 blank · 33 comment · 10 complexity · ee023b1e460e9c5eb8df3d21e2bac53c MD5 · raw file

  1. /**
  2. * (C) 1999-2003 Lars Knoll (knoll@kde.org)
  3. * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
  4. * Copyright (C) 2002, 2005, 2006 Apple Computer, Inc.
  5. * Copyright (C) 2006 Samuel Weinig (sam@webkit.org)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public License
  18. * along with this library; see the file COPYING.LIB. If not, write to
  19. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #include "config.h"
  23. #include "CSSMediaRule.h"
  24. #include "CSSParser.h"
  25. #include "ExceptionCode.h"
  26. namespace WebCore {
  27. CSSMediaRule::CSSMediaRule(CSSStyleSheet* parent, PassRefPtr<MediaList> media, PassRefPtr<CSSRuleList> rules)
  28. : CSSRule(parent)
  29. , m_lstMedia(media)
  30. , m_lstCSSRules(rules)
  31. {
  32. }
  33. CSSMediaRule::~CSSMediaRule()
  34. {
  35. if (m_lstMedia)
  36. m_lstMedia->setParent(0);
  37. int length = m_lstCSSRules->length();
  38. for (int i = 0; i < length; i++)
  39. m_lstCSSRules->item(i)->setParent(0);
  40. }
  41. unsigned CSSMediaRule::append(CSSRule* rule)
  42. {
  43. if (!rule)
  44. return 0;
  45. rule->setParent(this);
  46. return m_lstCSSRules->insertRule(rule, m_lstCSSRules->length());
  47. }
  48. unsigned CSSMediaRule::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
  49. {
  50. if (index > m_lstCSSRules->length()) {
  51. // INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.
  52. ec = INDEX_SIZE_ERR;
  53. return 0;
  54. }
  55. CSSParser p(useStrictParsing());
  56. RefPtr<CSSRule> newRule = p.parseRule(parentStyleSheet(), rule);
  57. if (!newRule) {
  58. // SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable.
  59. ec = SYNTAX_ERR;
  60. return 0;
  61. }
  62. if (newRule->isImportRule()) {
  63. // FIXME: an HIERARCHY_REQUEST_ERR should also be thrown for a @charset or a nested
  64. // @media rule. They are currently not getting parsed, resulting in a SYNTAX_ERR
  65. // to get raised above.
  66. // HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified
  67. // index, e.g., if an @import rule is inserted after a standard rule set or other
  68. // at-rule.
  69. ec = HIERARCHY_REQUEST_ERR;
  70. return 0;
  71. }
  72. newRule->setParent(this);
  73. unsigned returnedIndex = m_lstCSSRules->insertRule(newRule.get(), index);
  74. // stylesheet() can only return 0 for computed style declarations.
  75. stylesheet()->styleSheetChanged();
  76. return returnedIndex;
  77. }
  78. void CSSMediaRule::deleteRule(unsigned index, ExceptionCode& ec)
  79. {
  80. if (index >= m_lstCSSRules->length()) {
  81. // INDEX_SIZE_ERR: Raised if the specified index does not correspond to a
  82. // rule in the media rule list.
  83. ec = INDEX_SIZE_ERR;
  84. return;
  85. }
  86. m_lstCSSRules->deleteRule(index);
  87. // stylesheet() can only return 0 for computed style declarations.
  88. stylesheet()->styleSheetChanged();
  89. }
  90. String CSSMediaRule::cssText() const
  91. {
  92. String result = "@media ";
  93. if (m_lstMedia) {
  94. result += m_lstMedia->mediaText();
  95. result += " ";
  96. }
  97. result += "{ \n";
  98. if (m_lstCSSRules) {
  99. unsigned len = m_lstCSSRules->length();
  100. for (unsigned i = 0; i < len; i++) {
  101. result += " ";
  102. result += m_lstCSSRules->item(i)->cssText();
  103. result += "\n";
  104. }
  105. }
  106. result += "}";
  107. return result;
  108. }
  109. } // namespace WebCore