Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / DocumentStyleSheetCollection.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/DocumentStyleSheetCollection.h"
29
30 #include "HTMLNames.h"
31 #include "RuntimeEnabledFeatures.h"
32 #include "SVGNames.h"
33 #include "core/css/CSSStyleSheet.h"
34 #include "core/css/resolver/StyleResolver.h"
35 #include "core/dom/Document.h"
36 #include "core/dom/DocumentStyleSheetCollector.h"
37 #include "core/dom/Element.h"
38 #include "core/dom/ProcessingInstruction.h"
39 #include "core/dom/StyleEngine.h"
40 #include "core/dom/StyleSheetCandidate.h"
41 #include "core/html/HTMLIFrameElement.h"
42 #include "core/html/HTMLLinkElement.h"
43 #include "core/html/HTMLStyleElement.h"
44 #include "core/frame/Settings.h"
45 #include "core/svg/SVGStyleElement.h"
46
47 namespace WebCore {
48
49 using namespace HTMLNames;
50
51 DocumentStyleSheetCollection::DocumentStyleSheetCollection(TreeScope& treeScope)
52     : TreeScopeStyleSheetCollection(treeScope)
53 {
54     ASSERT(treeScope.rootNode() == treeScope.rootNode().document());
55 }
56
57 void DocumentStyleSheetCollection::collectStyleSheetsFromCandidates(StyleEngine* engine, DocumentStyleSheetCollector& collector)
58 {
59     DocumentOrderedList::iterator begin = m_styleSheetCandidateNodes.begin();
60     DocumentOrderedList::iterator end = m_styleSheetCandidateNodes.end();
61     for (DocumentOrderedList::iterator it = begin; it != end; ++it) {
62         Node* n = *it;
63         StyleSheetCandidate candidate(*n);
64
65         if (candidate.isXSL()) {
66             // Processing instruction (XML documents only).
67             // We don't support linking to embedded CSS stylesheets, see <https://bugs.webkit.org/show_bug.cgi?id=49281> for discussion.
68             // Don't apply XSL transforms to already transformed documents -- <rdar://problem/4132806>
69             if (RuntimeEnabledFeatures::xsltEnabled() && !document().transformSourceDocument()) {
70                 ProcessingInstruction* pi = toProcessingInstruction(n);
71                 // Don't apply XSL transforms until loading is finished.
72                 if (!document().parsing() && !pi->isLoading())
73                     document().applyXSLTransform(pi);
74                 return;
75             }
76
77             continue;
78         }
79
80         if (candidate.isImport()) {
81             Document* document = candidate.importedDocument();
82             if (!document)
83                 continue;
84             document->styleEngine()->updateStyleSheetsInImport(collector);
85             continue;
86         }
87
88         if (candidate.isEnabledAndLoading()) {
89             // it is loading but we should still decide which style sheet set to use
90             if (candidate.hasPreferrableName(engine->preferredStylesheetSetName()))
91                 engine->selectStylesheetSetName(candidate.title());
92             continue;
93         }
94
95         StyleSheet* sheet = candidate.sheet();
96         if (!sheet)
97             continue;
98
99         if (candidate.hasPreferrableName(engine->preferredStylesheetSetName()))
100             engine->selectStylesheetSetName(candidate.title());
101         collector.appendSheetForList(sheet);
102         if (candidate.canBeActivated(engine->preferredStylesheetSetName()))
103             collector.appendActiveStyleSheet(toCSSStyleSheet(sheet));
104     }
105 }
106
107 void DocumentStyleSheetCollection::collectStyleSheets(StyleEngine* engine, DocumentStyleSheetCollector& collector)
108 {
109     ASSERT(document().styleEngine() == engine);
110     collector.appendActiveStyleSheets(engine->injectedAuthorStyleSheets());
111     collector.appendActiveStyleSheets(engine->documentAuthorStyleSheets());
112     collectStyleSheetsFromCandidates(engine, collector);
113 }
114
115 bool DocumentStyleSheetCollection::updateActiveStyleSheets(StyleEngine* engine, StyleResolverUpdateMode updateMode)
116 {
117     StyleSheetCollection collection;
118     ActiveDocumentStyleSheetCollector collector(collection);
119     collectStyleSheets(engine, collector);
120
121     StyleSheetChange change;
122     analyzeStyleSheetChange(updateMode, collection, change);
123
124     if (change.styleResolverUpdateType == Reconstruct) {
125         engine->clearMasterResolver();
126         // FIMXE: The following depends on whether StyleRuleFontFace was modified or not.
127         // No need to always-clear font cache.
128         engine->clearFontCache();
129     } else if (StyleResolver* styleResolver = engine->resolver()) {
130         // FIXME: We might have already had styles in child treescope. In this case, we cannot use buildScopedStyleTreeInDocumentOrder.
131         // Need to change "false" to some valid condition.
132         styleResolver->setBuildScopedStyleTreeInDocumentOrder(false);
133         if (change.styleResolverUpdateType != Additive) {
134             ASSERT(change.styleResolverUpdateType == Reset);
135             resetAllRuleSetsInTreeScope(styleResolver);
136             engine->removeFontFaceRules(change.fontFaceRulesToRemove);
137             styleResolver->removePendingAuthorStyleSheets(m_activeAuthorStyleSheets);
138             styleResolver->lazyAppendAuthorStyleSheets(0, collection.activeAuthorStyleSheets());
139         } else {
140             styleResolver->lazyAppendAuthorStyleSheets(m_activeAuthorStyleSheets.size(), collection.activeAuthorStyleSheets());
141         }
142     }
143     m_scopingNodesForStyleScoped.didRemoveScopingNodes();
144
145     collection.swap(*this);
146
147     updateUsesRemUnits();
148
149     return change.requiresFullStyleRecalc;
150 }
151
152 }