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#include "core/frame/Settings.h"
38
39namespace WebCore {
40
41using namespace HTMLNames;
42
43ShadowTreeStyleSheetCollection::ShadowTreeStyleSheetCollection(ShadowRoot& shadowRoot)
44    : StyleSheetCollection(shadowRoot)
45{
46}
47
48void ShadowTreeStyleSheetCollection::collectStyleSheets(StyleEngine* engine, StyleSheetCollectionBase& collection)
49{
50    DocumentOrderedList::iterator begin = m_styleSheetCandidateNodes.begin();
51    DocumentOrderedList::iterator end = m_styleSheetCandidateNodes.end();
52    for (DocumentOrderedList::iterator it = begin; it != end; ++it) {
53        Node* node = *it;
54        StyleSheet* sheet = 0;
55        CSSStyleSheet* activeSheet = 0;
56
57        if (!node->isHTMLElement() || !node->hasTagName(styleTag))
58            continue;
59
60        Element* element = toElement(node);
61        AtomicString title = element->getAttribute(titleAttr);
62        bool enabledViaScript = false;
63
64        sheet = toHTMLStyleElement(node)->sheet();
65        if (sheet && !sheet->disabled() && sheet->isCSSStyleSheet())
66            activeSheet = toCSSStyleSheet(sheet);
67
68        // FIXME: clarify how PREFERRED or ALTERNATE works in shadow trees.
69        // Should we set preferred/selected stylesheets name in shadow trees and
70        // use the name in document?
71        AtomicString rel = element->getAttribute(relAttr);
72        if (!enabledViaScript && sheet && !title.isEmpty()) {
73            if (engine->preferredStylesheetSetName().isEmpty()) {
74                if (element->hasLocalName(styleTag) || !rel.contains("alternate")) {
75                    engine->setPreferredStylesheetSetName(title);
76                    engine->setSelectedStylesheetSetName(title);
77                }
78            }
79            if (title != engine->preferredStylesheetSetName())
80                activeSheet = 0;
81        }
82
83        if (rel.contains("alternate") && title.isEmpty())
84            activeSheet = 0;
85
86        if (sheet)
87            collection.appendSheetForList(sheet);
88        if (activeSheet)
89            collection.appendActiveStyleSheet(activeSheet);
90    }
91}
92
93bool ShadowTreeStyleSheetCollection::updateActiveStyleSheets(StyleEngine* engine, StyleResolverUpdateMode updateMode)
94{
95    StyleSheetCollectionBase collection;
96    collectStyleSheets(engine, collection);
97
98    StyleSheetChange change;
99    analyzeStyleSheetChange(updateMode, collection, change);
100
101    if (StyleResolver* styleResolver = engine->resolver()) {
102        // FIXME: We might have already had styles in child treescope. In this case, we cannot use buildScopedStyleTreeInDocumentOrder.
103        // Need to change "false" to some valid condition.
104        styleResolver->setBuildScopedStyleTreeInDocumentOrder(false);
105        if (change.styleResolverUpdateType != Additive) {
106            // We should not destroy StyleResolver when we find any stylesheet update in a shadow tree.
107            // In this case, we will reset rulesets created from style elements in the shadow tree.
108            resetAllRuleSetsInTreeScope(styleResolver);
109            styleResolver->removePendingAuthorStyleSheets(m_activeAuthorStyleSheets);
110            styleResolver->lazyAppendAuthorStyleSheets(0, collection.activeAuthorStyleSheets());
111        } else {
112            styleResolver->lazyAppendAuthorStyleSheets(m_activeAuthorStyleSheets.size(), collection.activeAuthorStyleSheets());
113        }
114    }
115    if (change.requiresFullStyleRecalc)
116        toShadowRoot(m_treeScope.rootNode())->host()->setNeedsStyleRecalc();
117
118    m_scopingNodesForStyleScoped.didRemoveScopingNodes();
119    collection.swap(*this);
120    updateUsesRemUnits();
121
122    return change.requiresFullStyleRecalc;
123}
124
125}
126