2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11 * Copyright (C) 2012 Google Inc. All rights reserved.
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
23 * You should have received a copy of the GNU Library General Public License
24 * along with this library; see the file COPYING.LIB. If not, write to
25 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
30 #include "core/css/CSSDefaultStyleSheets.h"
32 #include "core/MathMLNames.h"
33 #include "core/css/MediaQueryEvaluator.h"
34 #include "core/css/RuleSet.h"
35 #include "core/css/StyleSheetContents.h"
36 #include "core/dom/Fullscreen.h"
37 #include "core/html/HTMLAnchorElement.h"
38 #include "core/html/HTMLHtmlElement.h"
39 #include "core/layout/LayoutTheme.h"
40 #include "platform/PlatformResourceLoader.h"
41 #include "wtf/LeakAnnotations.h"
45 using namespace HTMLNames
;
47 CSSDefaultStyleSheets
& CSSDefaultStyleSheets::instance()
49 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent
<CSSDefaultStyleSheets
>, cssDefaultStyleSheets
, (adoptPtrWillBeNoop(new CSSDefaultStyleSheets())));
50 return *cssDefaultStyleSheets
;
53 static const MediaQueryEvaluator
& screenEval()
55 DEFINE_STATIC_LOCAL(const MediaQueryEvaluator
, staticScreenEval
, ("screen"));
56 return staticScreenEval
;
59 static const MediaQueryEvaluator
& printEval()
61 DEFINE_STATIC_LOCAL(const MediaQueryEvaluator
, staticPrintEval
, ("print"));
62 return staticPrintEval
;
65 static PassRefPtrWillBeRawPtr
<StyleSheetContents
> parseUASheet(const String
& str
)
67 RefPtrWillBeRawPtr
<StyleSheetContents
> sheet
= StyleSheetContents::create(CSSParserContext(UASheetMode
, 0));
68 sheet
->parseString(str
);
69 // User Agent stylesheets are parsed once for the lifetime of the renderer
70 // process and are intentionally leaked.
71 WTF_ANNOTATE_LEAKING_OBJECT_PTR(sheet
.get());
72 return sheet
.release();
75 CSSDefaultStyleSheets::CSSDefaultStyleSheets()
76 : m_defaultStyle(nullptr)
77 , m_defaultMobileViewportStyle(nullptr)
78 , m_defaultQuirksStyle(nullptr)
79 , m_defaultPrintStyle(nullptr)
80 , m_defaultViewSourceStyle(nullptr)
81 , m_defaultXHTMLMobileProfileStyle(nullptr)
82 , m_defaultStyleSheet(nullptr)
83 , m_mobileViewportStyleSheet(nullptr)
84 , m_quirksStyleSheet(nullptr)
85 , m_svgStyleSheet(nullptr)
86 , m_mathmlStyleSheet(nullptr)
87 , m_mediaControlsStyleSheet(nullptr)
88 , m_fullscreenStyleSheet(nullptr)
90 m_defaultStyle
= RuleSet::create();
91 m_defaultPrintStyle
= RuleSet::create();
92 m_defaultQuirksStyle
= RuleSet::create();
95 String defaultRules
= loadResourceAsASCIIString("html.css") + LayoutTheme::theme().extraDefaultStyleSheet();
96 m_defaultStyleSheet
= parseUASheet(defaultRules
);
97 m_defaultStyle
->addRulesFromSheet(defaultStyleSheet(), screenEval());
98 m_defaultPrintStyle
->addRulesFromSheet(defaultStyleSheet(), printEval());
100 // Quirks-mode rules.
101 String quirksRules
= loadResourceAsASCIIString("quirks.css") + LayoutTheme::theme().extraQuirksStyleSheet();
102 m_quirksStyleSheet
= parseUASheet(quirksRules
);
103 m_defaultQuirksStyle
->addRulesFromSheet(quirksStyleSheet(), screenEval());
106 RuleSet
* CSSDefaultStyleSheets::defaultViewSourceStyle()
108 if (!m_defaultViewSourceStyle
) {
109 m_defaultViewSourceStyle
= RuleSet::create();
110 // Loaded stylesheet is leaked on purpose.
111 RefPtrWillBeRawPtr
<StyleSheetContents
> stylesheet
= parseUASheet(loadResourceAsASCIIString("view-source.css"));
112 m_defaultViewSourceStyle
->addRulesFromSheet(stylesheet
.release().leakRef(), screenEval());
114 return m_defaultViewSourceStyle
.get();
117 RuleSet
* CSSDefaultStyleSheets::defaultXHTMLMobileProfileStyle()
119 if (!m_defaultXHTMLMobileProfileStyle
) {
120 m_defaultXHTMLMobileProfileStyle
= RuleSet::create();
121 // Loaded stylesheet is leaked on purpose.
122 RefPtrWillBeRawPtr
<StyleSheetContents
> stylesheet
= parseUASheet(loadResourceAsASCIIString("xhtmlmp.css"));
123 m_defaultXHTMLMobileProfileStyle
->addRulesFromSheet(stylesheet
.release().leakRef(), screenEval());
125 return m_defaultXHTMLMobileProfileStyle
.get();
128 RuleSet
* CSSDefaultStyleSheets::defaultMobileViewportStyle()
130 if (!m_defaultMobileViewportStyle
) {
131 m_defaultMobileViewportStyle
= RuleSet::create();
132 m_mobileViewportStyleSheet
= parseUASheet(loadResourceAsASCIIString("viewportAndroid.css"));
133 m_defaultMobileViewportStyle
->addRulesFromSheet(m_mobileViewportStyleSheet
.get(), screenEval());
135 return m_defaultMobileViewportStyle
.get();
138 void CSSDefaultStyleSheets::ensureDefaultStyleSheetsForElement(const Element
& element
, bool& changedDefaultStyle
)
140 // FIXME: We should assert that the sheet only styles SVG elements.
141 if (element
.isSVGElement() && !m_svgStyleSheet
) {
142 m_svgStyleSheet
= parseUASheet(loadResourceAsASCIIString("svg.css"));
143 m_defaultStyle
->addRulesFromSheet(svgStyleSheet(), screenEval());
144 m_defaultPrintStyle
->addRulesFromSheet(svgStyleSheet(), printEval());
145 changedDefaultStyle
= true;
148 // FIXME: We should assert that the sheet only styles MathML elements.
149 if (element
.namespaceURI() == MathMLNames::mathmlNamespaceURI
150 && !m_mathmlStyleSheet
) {
151 m_mathmlStyleSheet
= parseUASheet(loadResourceAsASCIIString("mathml.css"));
152 m_defaultStyle
->addRulesFromSheet(mathmlStyleSheet(), screenEval());
153 m_defaultPrintStyle
->addRulesFromSheet(mathmlStyleSheet(), printEval());
154 changedDefaultStyle
= true;
157 // FIXME: We should assert that this sheet only contains rules for <video> and <audio>.
158 if (!m_mediaControlsStyleSheet
&& (isHTMLVideoElement(element
) || isHTMLAudioElement(element
))) {
159 String mediaRules
= loadResourceAsASCIIString(
160 RuntimeEnabledFeatures::newMediaPlaybackUiEnabled() ?
161 "mediaControlsNew.css" : "mediaControls.css") +
162 LayoutTheme::theme().extraMediaControlsStyleSheet();
163 m_mediaControlsStyleSheet
= parseUASheet(mediaRules
);
164 m_defaultStyle
->addRulesFromSheet(mediaControlsStyleSheet(), screenEval());
165 m_defaultPrintStyle
->addRulesFromSheet(mediaControlsStyleSheet(), printEval());
166 changedDefaultStyle
= true;
169 // FIXME: This only works because we Force recalc the entire document so the new sheet
170 // is loaded for <html> and the correct styles apply to everyone.
171 if (!m_fullscreenStyleSheet
&& Fullscreen::isFullScreen(element
.document())) {
172 String fullscreenRules
= loadResourceAsASCIIString("fullscreen.css") + LayoutTheme::theme().extraFullScreenStyleSheet();
173 m_fullscreenStyleSheet
= parseUASheet(fullscreenRules
);
174 m_defaultStyle
->addRulesFromSheet(fullscreenStyleSheet(), screenEval());
175 m_defaultQuirksStyle
->addRulesFromSheet(fullscreenStyleSheet(), screenEval());
176 changedDefaultStyle
= true;
179 ASSERT(!m_defaultStyle
->features().hasIdsInSelectors());
180 ASSERT(m_defaultStyle
->features().siblingRules
.isEmpty());
183 DEFINE_TRACE(CSSDefaultStyleSheets
)
185 visitor
->trace(m_defaultStyle
);
186 visitor
->trace(m_defaultMobileViewportStyle
);
187 visitor
->trace(m_defaultQuirksStyle
);
188 visitor
->trace(m_defaultPrintStyle
);
189 visitor
->trace(m_defaultViewSourceStyle
);
190 visitor
->trace(m_defaultXHTMLMobileProfileStyle
);
191 visitor
->trace(m_defaultStyleSheet
);
192 visitor
->trace(m_mobileViewportStyleSheet
);
193 visitor
->trace(m_quirksStyleSheet
);
194 visitor
->trace(m_svgStyleSheet
);
195 visitor
->trace(m_mathmlStyleSheet
);
196 visitor
->trace(m_mediaControlsStyleSheet
);
197 visitor
->trace(m_fullscreenStyleSheet
);