tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / css / CSSStyleRule.cpp
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, 2008 Apple Inc. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23 #include "CSSStyleRule.h"
24
25 #include "CSSMutableStyleDeclaration.h"
26 #include "CSSPageRule.h"
27 #include "CSSParser.h"
28 #include "CSSSelector.h"
29 #include "CSSStyleSheet.h"
30 #include "Document.h"
31 #include "StyledElement.h"
32 #include "StyleSheet.h"
33
34 namespace WebCore {
35
36 CSSStyleRule::CSSStyleRule(CSSStyleSheet* parent, int sourceLine, CSSRule::Type type)
37     : CSSRule(parent, type)
38 {
39     m_sourceLine = sourceLine;
40
41     // m_sourceLine is a bitfield, so let's catch any overflow early in debug mode.
42     ASSERT(m_sourceLine == sourceLine);
43 }
44
45 CSSStyleRule::~CSSStyleRule()
46 {
47     if (m_style)
48         m_style->setParentRule(0);
49 }
50
51 String CSSStyleRule::selectorText() const
52 {
53     if (isPageRule())
54         return static_cast<const CSSPageRule*>(this)->pageSelectorText();
55
56     String str;
57     for (CSSSelector* s = selectorList().first(); s; s = CSSSelectorList::next(s)) {
58         if (s != selectorList().first())
59             str += ", ";
60         str += s->selectorText();
61     }
62     return str;
63 }
64
65 void CSSStyleRule::setSelectorText(const String& selectorText)
66 {
67     Document* doc = 0;
68
69     if (CSSStyleSheet* styleSheet = m_style->parentStyleSheet())
70         doc = styleSheet->findDocument();
71
72     if (!doc && m_style->isElementStyleDeclaration()) {
73         if (StyledElement* element = static_cast<CSSElementStyleDeclaration*>(m_style.get())->element())
74             doc = element->document();
75     }
76
77     if (!doc)
78         return;
79
80     CSSParser p;
81     CSSSelectorList selectorList;
82     p.parseSelector(selectorText, doc, selectorList);
83     if (!selectorList.first())
84         return;
85
86     String oldSelectorText = this->selectorText();
87     m_selectorList.adopt(selectorList);
88     if (this->selectorText() == oldSelectorText)
89         return;
90
91     doc->styleSelectorChanged(DeferRecalcStyle);
92 }
93
94 String CSSStyleRule::cssText() const
95 {
96     String result = selectorText();
97
98     result += " { ";
99     result += m_style->cssText();
100     result += "}";
101
102     return result;
103 }
104
105 void CSSStyleRule::addSubresourceStyleURLs(ListHashSet<KURL>& urls)
106 {
107     if (m_style)
108         m_style->addSubresourceStyleURLs(urls);
109 }
110
111 } // namespace WebCore