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 "core/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
38namespace blink {
39
40using namespace HTMLNames;
41
42ShadowTreeStyleSheetCollection::ShadowTreeStyleSheetCollection(ShadowRoot& shadowRoot)
43    : TreeScopeStyleSheetCollection(shadowRoot)
44{
45}
46
47void 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 (!isHTMLStyleElement(*node))
57            continue;
58
59        HTMLStyleElement* element = toHTMLStyleElement(node);
60        const AtomicString& title = element->fastGetAttribute(titleAttr);
61        bool enabledViaScript = false;
62
63        sheet = element->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        if (!enabledViaScript && sheet && !title.isEmpty()) {
71            if (engine->preferredStylesheetSetName().isEmpty()) {
72                engine->setPreferredStylesheetSetName(title);
73                engine->setSelectedStylesheetSetName(title);
74            }
75            if (title != engine->preferredStylesheetSetName())
76                activeSheet = 0;
77        }
78
79        const AtomicString& rel = element->fastGetAttribute(relAttr);
80        if (rel.contains("alternate") && title.isEmpty())
81            activeSheet = 0;
82
83        if (sheet)
84            collection.appendSheetForList(sheet);
85        if (activeSheet)
86            collection.appendActiveStyleSheet(activeSheet);
87    }
88}
89
90void ShadowTreeStyleSheetCollection::updateActiveStyleSheets(StyleEngine* engine, StyleResolverUpdateMode updateMode)
91{
92    StyleSheetCollection collection;
93    collectStyleSheets(engine, collection);
94
95    StyleSheetChange change;
96    analyzeStyleSheetChange(updateMode, collection, change);
97
98    if (StyleResolver* styleResolver = engine->resolver()) {
99        if (change.styleResolverUpdateType != Additive) {
100            // We should not destroy StyleResolver when we find any stylesheet update in a shadow tree.
101            // In this case, we will reset rulesets created from style elements in the shadow tree.
102            styleResolver->resetAuthorStyle(treeScope());
103            styleResolver->removePendingAuthorStyleSheets(m_activeAuthorStyleSheets);
104            styleResolver->lazyAppendAuthorStyleSheets(0, collection.activeAuthorStyleSheets());
105        } else {
106            styleResolver->lazyAppendAuthorStyleSheets(m_activeAuthorStyleSheets.size(), collection.activeAuthorStyleSheets());
107        }
108    }
109    if (change.requiresFullStyleRecalc)
110        toShadowRoot(treeScope().rootNode()).host()->setNeedsStyleRecalc(SubtreeStyleChange);
111
112    collection.swap(*this);
113    updateUsesRemUnits();
114}
115
116}
117