Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 2004-2005 Allan Sandfeld Jensen (kde (at) carewolf.com)
      4  * Copyright (C) 2006, 2007 Nicholas Shanks (webkit (at) 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 (at) webkit.org>
      7  * Copyright (C) 2007, 2008 Eric Seidel <eric (at) 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/CSSHostRule.h"
     34 #include "core/css/CSSImportRule.h"
     35 #include "core/css/CSSMediaRule.h"
     36 #include "core/css/CSSRegionRule.h"
     37 #include "core/css/CSSRule.h"
     38 #include "core/css/CSSStyleRule.h"
     39 #include "core/css/CSSStyleSheet.h"
     40 #include "core/css/CSSSupportsRule.h"
     41 #include "core/css/StyleSheetContents.h"
     42 #include "core/dom/DocumentStyleSheetCollection.h"
     43 
     44 namespace WebCore {
     45 
     46 void InspectorCSSOMWrappers::collectFromStyleSheetIfNeeded(CSSStyleSheet* styleSheet)
     47 {
     48     if (!m_styleRuleToCSSOMWrapperMap.isEmpty())
     49         collect(styleSheet);
     50 }
     51 
     52 void InspectorCSSOMWrappers::reset()
     53 {
     54     m_styleRuleToCSSOMWrapperMap.clear();
     55     m_styleSheetCSSOMWrapperSet.clear();
     56 }
     57 
     58 template <class ListType>
     59 void InspectorCSSOMWrappers::collect(ListType* listType)
     60 {
     61     if (!listType)
     62         return;
     63     unsigned size = listType->length();
     64     for (unsigned i = 0; i < size; ++i) {
     65         CSSRule* cssRule = listType->item(i);
     66         switch (cssRule->type()) {
     67         case CSSRule::IMPORT_RULE:
     68             collect(static_cast<CSSImportRule*>(cssRule)->styleSheet());
     69             break;
     70         case CSSRule::MEDIA_RULE:
     71             collect(static_cast<CSSMediaRule*>(cssRule));
     72             break;
     73         case CSSRule::SUPPORTS_RULE:
     74             collect(static_cast<CSSSupportsRule*>(cssRule));
     75             break;
     76         case CSSRule::WEBKIT_REGION_RULE:
     77             collect(static_cast<CSSRegionRule*>(cssRule));
     78             break;
     79         case CSSRule::HOST_RULE:
     80             collect(static_cast<CSSHostRule*>(cssRule));
     81             break;
     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 
     91 void 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 
    100 void InspectorCSSOMWrappers::collectFromStyleSheets(const Vector<RefPtr<CSSStyleSheet> >& sheets)
    101 {
    102     for (unsigned i = 0; i < sheets.size(); ++i)
    103         collect(sheets[i].get());
    104 }
    105 
    106 void InspectorCSSOMWrappers::collectFromDocumentStyleSheetCollection(DocumentStyleSheetCollection* styleSheetCollection)
    107 {
    108     collectFromStyleSheets(styleSheetCollection->activeAuthorStyleSheets());
    109     collect(styleSheetCollection->pageUserSheet());
    110     collectFromStyleSheets(styleSheetCollection->injectedUserStyleSheets());
    111     collectFromStyleSheets(styleSheetCollection->documentUserStyleSheets());
    112 }
    113 
    114 CSSStyleRule* 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 
    125         collectFromDocumentStyleSheetCollection(styleSheetCollection);
    126     }
    127     return m_styleRuleToCSSOMWrapperMap.get(rule);
    128 }
    129 
    130 } // namespace WebCore
    131