1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11 * Copyright (C) 2012 Google Inc. All rights reserved.
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
22 *
23 * You should have received a copy of the GNU Library General Public License
24 * along with this library; see the file COPYING.LIB. If not, write to
25 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
27 */
28
29 #include "config.h"
30 #include "core/css/InspectorCSSOMWrappers.h"
31
32 #include "core/css/CSSDefaultStyleSheets.h"
33 #include "core/css/CSSImportRule.h"
34 #include "core/css/CSSMediaRule.h"
35 #include "core/css/CSSRegionRule.h"
36 #include "core/css/CSSRule.h"
37 #include "core/css/CSSStyleRule.h"
38 #include "core/css/CSSStyleSheet.h"
39 #include "core/css/CSSSupportsRule.h"
40 #include "core/css/StyleSheetContents.h"
41 #include "core/dom/StyleEngine.h"
42
43 namespace WebCore {
44
collectFromStyleSheetIfNeeded(CSSStyleSheet * styleSheet)45 void InspectorCSSOMWrappers::collectFromStyleSheetIfNeeded(CSSStyleSheet* styleSheet)
46 {
47 if (!m_styleRuleToCSSOMWrapperMap.isEmpty())
48 collect(styleSheet);
49 }
50
reset()51 void InspectorCSSOMWrappers::reset()
52 {
53 m_styleRuleToCSSOMWrapperMap.clear();
54 m_styleSheetCSSOMWrapperSet.clear();
55 }
56
57 template <class ListType>
collect(ListType * listType)58 void InspectorCSSOMWrappers::collect(ListType* listType)
59 {
60 if (!listType)
61 return;
62 unsigned size = listType->length();
63 for (unsigned i = 0; i < size; ++i) {
64 CSSRule* cssRule = listType->item(i);
65 switch (cssRule->type()) {
66 case CSSRule::IMPORT_RULE:
67 collect(toCSSImportRule(cssRule)->styleSheet());
68 break;
69 case CSSRule::MEDIA_RULE:
70 collect(toCSSMediaRule(cssRule));
71 break;
72 case CSSRule::SUPPORTS_RULE:
73 collect(toCSSSupportsRule(cssRule));
74 break;
75 case CSSRule::WEBKIT_REGION_RULE:
76 collect(toCSSRegionRule(cssRule));
77 break;
78 case CSSRule::STYLE_RULE:
79 m_styleRuleToCSSOMWrapperMap.add(toCSSStyleRule(cssRule)->styleRule(), toCSSStyleRule(cssRule));
80 break;
81 default:
82 break;
83 }
84 }
85 }
86
collectFromStyleSheetContents(HashSet<RefPtr<CSSStyleSheet>> & sheetWrapperSet,StyleSheetContents * styleSheet)87 void InspectorCSSOMWrappers::collectFromStyleSheetContents(HashSet<RefPtr<CSSStyleSheet> >& sheetWrapperSet, StyleSheetContents* styleSheet)
88 {
89 if (!styleSheet)
90 return;
91 RefPtr<CSSStyleSheet> styleSheetWrapper = CSSStyleSheet::create(styleSheet);
92 sheetWrapperSet.add(styleSheetWrapper);
93 collect(styleSheetWrapper.get());
94 }
95
collectFromStyleSheets(const Vector<RefPtr<CSSStyleSheet>> & sheets)96 void InspectorCSSOMWrappers::collectFromStyleSheets(const Vector<RefPtr<CSSStyleSheet> >& sheets)
97 {
98 for (unsigned i = 0; i < sheets.size(); ++i)
99 collect(sheets[i].get());
100 }
101
collectFromStyleEngine(StyleEngine * styleSheetCollection)102 void InspectorCSSOMWrappers::collectFromStyleEngine(StyleEngine* styleSheetCollection)
103 {
104 Vector<const Vector<RefPtr<CSSStyleSheet> >*> activeAuthorStyleSheets;
105 styleSheetCollection->getActiveAuthorStyleSheets(activeAuthorStyleSheets);
106 for (size_t i = 0; i < activeAuthorStyleSheets.size(); ++i)
107 collectFromStyleSheets(*activeAuthorStyleSheets[i]);
108 }
109
getWrapperForRuleInSheets(StyleRule * rule,StyleEngine * styleSheetCollection)110 CSSStyleRule* InspectorCSSOMWrappers::getWrapperForRuleInSheets(StyleRule* rule, StyleEngine* styleSheetCollection)
111 {
112 if (m_styleRuleToCSSOMWrapperMap.isEmpty()) {
113 collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::defaultStyleSheet);
114 collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::viewportStyleSheet);
115 collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::quirksStyleSheet);
116 collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::svgStyleSheet);
117 collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::mediaControlsStyleSheet);
118 collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::fullscreenStyleSheet);
119
120 collectFromStyleEngine(styleSheetCollection);
121 }
122 return m_styleRuleToCSSOMWrapperMap.get(rule);
123 }
124
125 } // namespace WebCore
126