Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / ShadowTreeStyleSheetCollection.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
7  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
9  * Copyright (C) 2013 Google Inc. All rights reserved.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public License
22  * along with this library; see the file COPYING.LIB.  If not, write to
23  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  */
26
27 #include "config.h"
28 #include "core/dom/ShadowTreeStyleSheetCollection.h"
29
30 #include "HTMLNames.h"
31 #include "core/css/CSSStyleSheet.h"
32 #include "core/css/resolver/StyleResolver.h"
33 #include "core/dom/Element.h"
34 #include "core/dom/StyleEngine.h"
35 #include "core/dom/shadow/ShadowRoot.h"
36 #include "core/html/HTMLStyleElement.h"
37
38 namespace WebCore {
39
40 using namespace HTMLNames;
41
42 ShadowTreeStyleSheetCollection::ShadowTreeStyleSheetCollection(ShadowRoot& shadowRoot)
43     : TreeScopeStyleSheetCollection(shadowRoot)
44 {
45 }
46
47 void ShadowTreeStyleSheetCollection::collectStyleSheets(StyleEngine* engine, StyleSheetCollection& collection)
48 {
49     DocumentOrderedList::iterator begin = m_styleSheetCandidateNodes.begin();
50     DocumentOrderedList::iterator end = m_styleSheetCandidateNodes.end();
51     for (DocumentOrderedList::iterator it = begin; it != end; ++it) {
52         Node* node = *it;
53         StyleSheet* sheet = 0;
54         CSSStyleSheet* activeSheet = 0;
55
56         if (!node->isHTMLElement() || !node->hasTagName(styleTag))
57             continue;
58
59         Element* element = toElement(node);
60         const AtomicString& title = element->fastGetAttribute(titleAttr);
61         bool enabledViaScript = false;
62
63         sheet = toHTMLStyleElement(node)->sheet();
64         if (sheet && !sheet->disabled() && sheet->isCSSStyleSheet())
65             activeSheet = toCSSStyleSheet(sheet);
66
67         // FIXME: clarify how PREFERRED or ALTERNATE works in shadow trees.
68         // Should we set preferred/selected stylesheets name in shadow trees and
69         // use the name in document?
70         const AtomicString& rel = element->fastGetAttribute(relAttr);
71         if (!enabledViaScript && sheet && !title.isEmpty()) {
72             if (engine->preferredStylesheetSetName().isEmpty()) {
73                 if (element->hasLocalName(styleTag) || !rel.contains("alternate")) {
74                     engine->setPreferredStylesheetSetName(title);
75                     engine->setSelectedStylesheetSetName(title);
76                 }
77             }
78             if (title != engine->preferredStylesheetSetName())
79                 activeSheet = 0;
80         }
81
82         if (rel.contains("alternate") && title.isEmpty())
83             activeSheet = 0;
84
85         if (sheet)
86             collection.appendSheetForList(sheet);
87         if (activeSheet)
88             collection.appendActiveStyleSheet(activeSheet);
89     }
90 }
91
92 bool ShadowTreeStyleSheetCollection::updateActiveStyleSheets(StyleEngine* engine, StyleResolverUpdateMode updateMode)
93 {
94     StyleSheetCollection collection;
95     collectStyleSheets(engine, collection);
96
97     StyleSheetChange change;
98     analyzeStyleSheetChange(updateMode, collection, change);
99
100     if (StyleResolver* styleResolver = engine->resolver()) {
101         // FIXME: We might have already had styles in child treescope. In this case, we cannot use buildScopedStyleTreeInDocumentOrder.
102         // Need to change "false" to some valid condition.
103         styleResolver->setBuildScopedStyleTreeInDocumentOrder(false);
104         if (change.styleResolverUpdateType != Additive) {
105             // We should not destroy StyleResolver when we find any stylesheet update in a shadow tree.
106             // In this case, we will reset rulesets created from style elements in the shadow tree.
107             resetAllRuleSetsInTreeScope(styleResolver);
108             styleResolver->removePendingAuthorStyleSheets(m_activeAuthorStyleSheets);
109             styleResolver->lazyAppendAuthorStyleSheets(0, collection.activeAuthorStyleSheets());
110         } else {
111             styleResolver->lazyAppendAuthorStyleSheets(m_activeAuthorStyleSheets.size(), collection.activeAuthorStyleSheets());
112         }
113     }
114     if (change.requiresFullStyleRecalc)
115         toShadowRoot(m_treeScope.rootNode()).host()->setNeedsStyleRecalc(SubtreeStyleChange);
116
117     m_scopingNodesForStyleScoped.didRemoveScopingNodes();
118     collection.swap(*this);
119     updateUsesRemUnits();
120
121     return change.requiresFullStyleRecalc;
122 }
123
124 }