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 "InspectorCSSOMWrappers.h"
31
32#include "CSSDefaultStyleSheets.h"
33#include "CSSHostRule.h"
34#include "CSSImportRule.h"
35#include "CSSMediaRule.h"
36#include "CSSRule.h"
37#include "CSSStyleRule.h"
38#include "CSSStyleSheet.h"
39#include "CSSSupportsRule.h"
40#include "DocumentStyleSheetCollection.h"
41#include "StyleSheetContents.h"
42#include "WebKitCSSRegionRule.h"
43
44namespace WebCore {
45
46void InspectorCSSOMWrappers::collectFromStyleSheetIfNeeded(CSSStyleSheet* styleSheet)
47{
48    if (!m_styleRuleToCSSOMWrapperMap.isEmpty())
49        collect(styleSheet);
50}
51
52template <class ListType>
53void InspectorCSSOMWrappers::collect(ListType* listType)
54{
55    if (!listType)
56        return;
57    unsigned size = listType->length();
58    for (unsigned i = 0; i < size; ++i) {
59        CSSRule* cssRule = listType->item(i);
60        switch (cssRule->type()) {
61        case CSSRule::IMPORT_RULE:
62            collect(static_cast<CSSImportRule*>(cssRule)->styleSheet());
63            break;
64        case CSSRule::MEDIA_RULE:
65            collect(static_cast<CSSMediaRule*>(cssRule));
66            break;
67#if ENABLE(CSS3_CONDITIONAL_RULES)
68        case CSSRule::SUPPORTS_RULE:
69            collect(static_cast<CSSSupportsRule*>(cssRule));
70            break;
71#endif
72#if ENABLE(CSS_REGIONS)
73        case CSSRule::WEBKIT_REGION_RULE:
74            collect(static_cast<WebKitCSSRegionRule*>(cssRule));
75            break;
76#endif
77#if ENABLE(SHADOW_DOM)
78        case CSSRule::HOST_RULE:
79            collect(static_cast<CSSHostRule*>(cssRule));
80            break;
81#endif
82        case CSSRule::STYLE_RULE:
83            m_styleRuleToCSSOMWrapperMap.add(static_cast<CSSStyleRule*>(cssRule)->styleRule(), static_cast<CSSStyleRule*>(cssRule));
84            break;
85        default:
86            break;
87        }
88    }
89}
90
91void InspectorCSSOMWrappers::collectFromStyleSheetContents(HashSet<RefPtr<CSSStyleSheet> >& sheetWrapperSet, StyleSheetContents* styleSheet)
92{
93    if (!styleSheet)
94        return;
95    RefPtr<CSSStyleSheet> styleSheetWrapper = CSSStyleSheet::create(styleSheet);
96    sheetWrapperSet.add(styleSheetWrapper);
97    collect(styleSheetWrapper.get());
98}
99
100void InspectorCSSOMWrappers::collectFromStyleSheets(const Vector<RefPtr<CSSStyleSheet> >& sheets)
101{
102    for (unsigned i = 0; i < sheets.size(); ++i)
103        collect(sheets[i].get());
104}
105
106void InspectorCSSOMWrappers::collectFromDocumentStyleSheetCollection(DocumentStyleSheetCollection* styleSheetCollection)
107{
108    collectFromStyleSheets(styleSheetCollection->activeAuthorStyleSheets());
109    collect(styleSheetCollection->pageUserSheet());
110    collectFromStyleSheets(styleSheetCollection->injectedUserStyleSheets());
111    collectFromStyleSheets(styleSheetCollection->documentUserStyleSheets());
112}
113
114CSSStyleRule* InspectorCSSOMWrappers::getWrapperForRuleInSheets(StyleRule* rule, DocumentStyleSheetCollection* styleSheetCollection)
115{
116    if (m_styleRuleToCSSOMWrapperMap.isEmpty()) {
117        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::simpleDefaultStyleSheet);
118        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::defaultStyleSheet);
119        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::quirksStyleSheet);
120        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::svgStyleSheet);
121        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::mathMLStyleSheet);
122        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::mediaControlsStyleSheet);
123        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::fullscreenStyleSheet);
124        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::plugInsStyleSheet);
125
126        collectFromDocumentStyleSheetCollection(styleSheetCollection);
127    }
128    return m_styleRuleToCSSOMWrapperMap.get(rule);
129}
130
131} // namespace WebCore
132