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.
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.
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.
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.
28 #include "core/dom/ShadowTreeStyleSheetCollection.h"
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"
40 using namespace HTMLNames;
42 ShadowTreeStyleSheetCollection::ShadowTreeStyleSheetCollection(ShadowRoot& shadowRoot)
43 : TreeScopeStyleSheetCollection(shadowRoot)
47 void ShadowTreeStyleSheetCollection::collectStyleSheets(StyleEngine* engine, StyleSheetCollection& collection)
49 DocumentOrderedList::iterator begin = m_styleSheetCandidateNodes.begin();
50 DocumentOrderedList::iterator end = m_styleSheetCandidateNodes.end();
51 for (DocumentOrderedList::iterator it = begin; it != end; ++it) {
53 StyleSheet* sheet = 0;
54 CSSStyleSheet* activeSheet = 0;
56 if (!node->isHTMLElement() || !node->hasTagName(styleTag))
59 Element* element = toElement(node);
60 const AtomicString& title = element->fastGetAttribute(titleAttr);
61 bool enabledViaScript = false;
63 sheet = toHTMLStyleElement(node)->sheet();
64 if (sheet && !sheet->disabled() && sheet->isCSSStyleSheet())
65 activeSheet = toCSSStyleSheet(sheet);
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);
78 if (title != engine->preferredStylesheetSetName())
82 if (rel.contains("alternate") && title.isEmpty())
86 collection.appendSheetForList(sheet);
88 collection.appendActiveStyleSheet(activeSheet);
92 bool ShadowTreeStyleSheetCollection::updateActiveStyleSheets(StyleEngine* engine, StyleResolverUpdateMode updateMode)
94 StyleSheetCollection collection;
95 collectStyleSheets(engine, collection);
97 StyleSheetChange change;
98 analyzeStyleSheetChange(updateMode, collection, change);
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());
111 styleResolver->lazyAppendAuthorStyleSheets(m_activeAuthorStyleSheets.size(), collection.activeAuthorStyleSheets());
114 if (change.requiresFullStyleRecalc)
115 toShadowRoot(m_treeScope.rootNode()).host()->setNeedsStyleRecalc(SubtreeStyleChange);
117 m_scopingNodesForStyleScoped.didRemoveScopingNodes();
118 collection.swap(*this);
119 updateUsesRemUnits();
121 return change.requiresFullStyleRecalc;