Split default style-sheet statics out from StyleResolver into its own class
authorhayato@chromium.org <hayato@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 4 Feb 2013 04:53:39 +0000 (04:53 +0000)
committerhayato@chromium.org <hayato@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 4 Feb 2013 04:53:39 +0000 (04:53 +0000)
https://bugs.webkit.org/show_bug.cgi?id=107780

Reviewed by Dimitri Glazkov.

Factored static variables and logic about default style sheets out from StyleResolver into its own class
CSSDefaultStyleSheets. This is a following patch after r141373.

No new tests, refactoring only.

* CMakeLists.txt:
* GNUmakefile.list.am:
* Target.pri:
* WebCore.gypi:
* WebCore.xcodeproj/project.pbxproj:
* css/CSSAllInOne.cpp:
* css/CSSDefaultStyleSheets.cpp: Added.
(WebCore):
(WebCore::elementCanUseSimpleDefaultStyle):
(WebCore::screenEval):
(WebCore::printEval):
(WebCore::parseUASheet):
(WebCore::CSSDefaultStyleSheets::initDefaultStyle):
(WebCore::CSSDefaultStyleSheets::loadFullDefaultStyle):
(WebCore::CSSDefaultStyleSheets::loadSimpleDefaultStyle):
(WebCore::CSSDefaultStyleSheets::viewSourceStyle):
(WebCore::CSSDefaultStyleSheets::ensureDefaultStyleSheetsForElement):
* css/CSSDefaultStyleSheets.h: Added.
(WebCore):
(CSSDefaultStyleSheets):
* css/StyleResolver.cpp:
(WebCore):
(WebCore::StyleResolver::StyleResolver):
(WebCore::StyleResolver::matchUARules):
(WebCore::StyleResolver::styleForElement):
(WebCore::StyleResolver::styleForPage):
(WebCore::InspectorCSSOMWrappers::getWrapperForRuleInSheets):
(WebCore::StyleResolver::collectFeatures):
(WebCore::StyleResolver::reportMemoryUsage):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@141733 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/GNUmakefile.list.am
Source/WebCore/Target.pri
Source/WebCore/WebCore.gypi
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/css/CSSAllInOne.cpp
Source/WebCore/css/CSSDefaultStyleSheets.cpp [new file with mode: 0644]
Source/WebCore/css/CSSDefaultStyleSheets.h [new file with mode: 0644]
Source/WebCore/css/StyleResolver.cpp

index 0d72ca0..e01b166 100644 (file)
@@ -1028,6 +1028,7 @@ set(WebCore_SOURCES
     css/CSSComputedStyleDeclaration.cpp
     css/CSSCrossfadeValue.cpp
     css/CSSCursorImageValue.cpp
+    css/CSSDefaultStyleSheets.cpp
     css/CSSFontFace.cpp
     css/CSSFontFaceRule.cpp
     css/CSSFontFaceSource.cpp
index 33fb2a0..62b9d29 100644 (file)
@@ -1,3 +1,45 @@
+2013-02-03  Hayato Ito  <hayato@chromium.org>
+
+        Split default style-sheet statics out from StyleResolver into its own class
+        https://bugs.webkit.org/show_bug.cgi?id=107780
+
+        Reviewed by Dimitri Glazkov.
+
+        Factored static variables and logic about default style sheets out from StyleResolver into its own class
+        CSSDefaultStyleSheets. This is a following patch after r141373.
+
+        No new tests, refactoring only.
+
+        * CMakeLists.txt:
+        * GNUmakefile.list.am:
+        * Target.pri:
+        * WebCore.gypi:
+        * WebCore.xcodeproj/project.pbxproj:
+        * css/CSSAllInOne.cpp:
+        * css/CSSDefaultStyleSheets.cpp: Added.
+        (WebCore):
+        (WebCore::elementCanUseSimpleDefaultStyle):
+        (WebCore::screenEval):
+        (WebCore::printEval):
+        (WebCore::parseUASheet):
+        (WebCore::CSSDefaultStyleSheets::initDefaultStyle):
+        (WebCore::CSSDefaultStyleSheets::loadFullDefaultStyle):
+        (WebCore::CSSDefaultStyleSheets::loadSimpleDefaultStyle):
+        (WebCore::CSSDefaultStyleSheets::viewSourceStyle):
+        (WebCore::CSSDefaultStyleSheets::ensureDefaultStyleSheetsForElement):
+        * css/CSSDefaultStyleSheets.h: Added.
+        (WebCore):
+        (CSSDefaultStyleSheets):
+        * css/StyleResolver.cpp:
+        (WebCore):
+        (WebCore::StyleResolver::StyleResolver):
+        (WebCore::StyleResolver::matchUARules):
+        (WebCore::StyleResolver::styleForElement):
+        (WebCore::StyleResolver::styleForPage):
+        (WebCore::InspectorCSSOMWrappers::getWrapperForRuleInSheets):
+        (WebCore::StyleResolver::collectFeatures):
+        (WebCore::StyleResolver::reportMemoryUsage):
+
 2013-02-03  Kentaro Hara  <haraken@chromium.org>
 
         [V8] Simplify CodeGeneratorV8.pm by using InheritsExtendedAttribute("EventTarget")
index 1bca6a3..9e975a3 100644 (file)
@@ -2527,6 +2527,8 @@ webcore_sources += \
        Source/WebCore/css/CSSCrossfadeValue.h \
        Source/WebCore/css/CSSCursorImageValue.cpp \
        Source/WebCore/css/CSSCursorImageValue.h \
+       Source/WebCore/css/CSSDefaultStyleSheets.cpp \
+       Source/WebCore/css/CSSDefaultStyleSheets.h \
        Source/WebCore/css/CSSFontFace.cpp \
        Source/WebCore/css/CSSFontFace.h \
        Source/WebCore/css/CSSFontFaceRule.cpp \
index 90efc40..b9ff8eb 100644 (file)
@@ -256,6 +256,7 @@ SOURCES += \
     css/CSSCrossfadeValue.cpp \
     css/CSSCursorImageValue.cpp \
     css/CSSFontFace.cpp \
+    css/CSSDefaultStyleSheets.cpp \
     css/CSSFontFaceRule.cpp \
     css/CSSFontFaceSrcValue.cpp \
     css/CSSFontSelector.cpp \
index fa8da37..1f5d3aa 100644 (file)
             'css/CSSCrossfadeValue.cpp',
             'css/CSSCrossfadeValue.h',
             'css/CSSCursorImageValue.cpp',
+            'css/CSSDefaultStyleSheets.cpp',
+            'css/CSSDefaultStyleSheets.h',
             'css/CSSFontFace.cpp',
             'css/CSSFontFace.h',
             'css/CSSFontFaceRule.cpp',
index f6f65eb..f30c5a6 100644 (file)
                4A6E9FC713C17D570046A7F8 /* FontFeatureSettings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4A6E9FC513C17D570046A7F8 /* FontFeatureSettings.cpp */; };
                4A6E9FC813C17D570046A7F8 /* FontFeatureSettings.h in Headers */ = {isa = PBXBuildFile; fileRef = 4A6E9FC613C17D570046A7F8 /* FontFeatureSettings.h */; settings = {ATTRIBUTES = (Private, ); }; };
                4A8C96EB0BE69032004EEFF0 /* FrameSelectionMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4A8C96EA0BE69032004EEFF0 /* FrameSelectionMac.mm */; };
+               4A9CC81716BB9AC600EC645A /* CSSDefaultStyleSheets.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4A9CC81516BB9AC600EC645A /* CSSDefaultStyleSheets.cpp */; };
+               4A9CC81816BB9AC600EC645A /* CSSDefaultStyleSheets.h in Headers */ = {isa = PBXBuildFile; fileRef = 4A9CC81616BB9AC600EC645A /* CSSDefaultStyleSheets.h */; };
                4ABDFF0B14DBE385004D117D /* HTMLShadowElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4ABDFF0714DBE312004D117D /* HTMLShadowElement.cpp */; };
                4ABDFF0C14DBE385004D117D /* HTMLShadowElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 4ABDFF0814DBE312004D117D /* HTMLShadowElement.h */; };
                4ACBC0BE12713CBD0094F9B2 /* ClassList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4ACBC0BC12713CBD0094F9B2 /* ClassList.cpp */; };
                4A6E9FC513C17D570046A7F8 /* FontFeatureSettings.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FontFeatureSettings.cpp; sourceTree = "<group>"; };
                4A6E9FC613C17D570046A7F8 /* FontFeatureSettings.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 4; path = FontFeatureSettings.h; sourceTree = "<group>"; };
                4A8C96EA0BE69032004EEFF0 /* FrameSelectionMac.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; name = FrameSelectionMac.mm; path = mac/FrameSelectionMac.mm; sourceTree = "<group>"; };
+               4A9CC81516BB9AC600EC645A /* CSSDefaultStyleSheets.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CSSDefaultStyleSheets.cpp; sourceTree = "<group>"; };
+               4A9CC81616BB9AC600EC645A /* CSSDefaultStyleSheets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSDefaultStyleSheets.h; sourceTree = "<group>"; };
                4ABDFF0714DBE312004D117D /* HTMLShadowElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HTMLShadowElement.cpp; sourceTree = "<group>"; };
                4ABDFF0814DBE312004D117D /* HTMLShadowElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTMLShadowElement.h; sourceTree = "<group>"; };
                4ABDFF0914DBE312004D117D /* HTMLShadowElement.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = HTMLShadowElement.idl; sourceTree = "<group>"; };
                                2D8FEBDB143E3EF70072502B /* CSSCrossfadeValue.h */,
                                AA0978ED0ABAA6E100874480 /* CSSCursorImageValue.cpp */,
                                AA0978EE0ABAA6E100874480 /* CSSCursorImageValue.h */,
+                               4A9CC81516BB9AC600EC645A /* CSSDefaultStyleSheets.cpp */,
+                               4A9CC81616BB9AC600EC645A /* CSSDefaultStyleSheets.h */,
                                BC64B4CD0CB4298A005F2B62 /* CSSFontFace.cpp */,
                                BC64B4CE0CB4298A005F2B62 /* CSSFontFace.h */,
                                A80E6CBD0A1989CA007FB8C5 /* CSSFontFaceRule.cpp */,
                                BCEA4790097CAAC80094C9E4 /* CSSComputedStyleDeclaration.h in Headers */,
                                2D8FEBDD143E3EF70072502B /* CSSCrossfadeValue.h in Headers */,
                                AA21ECCD0ABF0FC6002B834C /* CSSCursorImageValue.h in Headers */,
+                               4A9CC81816BB9AC600EC645A /* CSSDefaultStyleSheets.h in Headers */,
                                BC64B4D60CB4298A005F2B62 /* CSSFontFace.h in Headers */,
                                A80E6CFD0A1989CA007FB8C5 /* CSSFontFaceRule.h in Headers */,
                                BC64B4D80CB4298A005F2B62 /* CSSFontFaceSource.h in Headers */,
                                BCEA478F097CAAC80094C9E4 /* CSSComputedStyleDeclaration.cpp in Sources */,
                                2D8FEBDC143E3EF70072502B /* CSSCrossfadeValue.cpp in Sources */,
                                AA21ECCA0ABF0FBA002B834C /* CSSCursorImageValue.cpp in Sources */,
+                               4A9CC81716BB9AC600EC645A /* CSSDefaultStyleSheets.cpp in Sources */,
                                BC64B4D50CB4298A005F2B62 /* CSSFontFace.cpp in Sources */,
                                A80E6CE70A1989CA007FB8C5 /* CSSFontFaceRule.cpp in Sources */,
                                BC64B4D70CB4298A005F2B62 /* CSSFontFaceSource.cpp in Sources */,
index 8646df6..076ab5c 100644 (file)
@@ -34,6 +34,7 @@
 #include "CSSComputedStyleDeclaration.cpp"
 #include "CSSCrossfadeValue.cpp"
 #include "CSSCursorImageValue.cpp"
+#include "CSSDefaultStyleSheets.cpp"
 #include "CSSFontFace.cpp"
 #include "CSSFontFaceRule.cpp"
 #include "CSSFontFaceSource.cpp"
diff --git a/Source/WebCore/css/CSSDefaultStyleSheets.cpp b/Source/WebCore/css/CSSDefaultStyleSheets.cpp
new file mode 100644 (file)
index 0000000..81f0478
--- /dev/null
@@ -0,0 +1,207 @@
+/*
+ * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
+ *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
+ * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
+ * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
+ * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
+ * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
+ * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
+ * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
+ * Copyright (C) Research In Motion Limited 2011. All rights reserved.
+ * Copyright (C) 2012 Google Inc. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "config.h"
+#include "CSSDefaultStyleSheets.h"
+
+#include "Chrome.h"
+#include "ChromeClient.h"
+#include "MediaQueryEvaluator.h"
+#include "Page.h"
+#include "RenderTheme.h"
+#include "RuleSet.h"
+#include "StyleSheetContents.h"
+#include "UserAgentStyleSheets.h"
+
+namespace WebCore {
+
+using namespace HTMLNames;
+
+RuleSet* CSSDefaultStyleSheets::defaultStyle;
+RuleSet* CSSDefaultStyleSheets::defaultQuirksStyle;
+RuleSet* CSSDefaultStyleSheets::defaultPrintStyle;
+RuleSet* CSSDefaultStyleSheets::defaultViewSourceStyle;
+
+StyleSheetContents* CSSDefaultStyleSheets::simpleDefaultStyleSheet;
+StyleSheetContents* CSSDefaultStyleSheets::defaultStyleSheet;
+StyleSheetContents* CSSDefaultStyleSheets::quirksStyleSheet;
+StyleSheetContents* CSSDefaultStyleSheets::svgStyleSheet;
+StyleSheetContents* CSSDefaultStyleSheets::mathMLStyleSheet;
+StyleSheetContents* CSSDefaultStyleSheets::mediaControlsStyleSheet;
+StyleSheetContents* CSSDefaultStyleSheets::fullscreenStyleSheet;
+StyleSheetContents* CSSDefaultStyleSheets::plugInsStyleSheet;
+
+// FIXME: It would be nice to use some mechanism that guarantees this is in sync with the real UA stylesheet.
+static const char* simpleUserAgentStyleSheet = "html,body,div{display:block}head{display:none}body{margin:8px}div:focus,span:focus{outline:auto 5px -webkit-focus-ring-color}a:-webkit-any-link{color:-webkit-link;text-decoration:underline}a:-webkit-any-link:active{color:-webkit-activelink}";
+
+static inline bool elementCanUseSimpleDefaultStyle(Element* e)
+{
+    return e->hasTagName(htmlTag) || e->hasTagName(headTag) || e->hasTagName(bodyTag) || e->hasTagName(divTag) || e->hasTagName(spanTag) || e->hasTagName(brTag) || e->hasTagName(aTag);
+}
+
+static const MediaQueryEvaluator& screenEval()
+{
+    DEFINE_STATIC_LOCAL(const MediaQueryEvaluator, staticScreenEval, ("screen"));
+    return staticScreenEval;
+}
+
+static const MediaQueryEvaluator& printEval()
+{
+    DEFINE_STATIC_LOCAL(const MediaQueryEvaluator, staticPrintEval, ("print"));
+    return staticPrintEval;
+}
+
+static StyleSheetContents* parseUASheet(const String& str)
+{
+    StyleSheetContents* sheet = StyleSheetContents::create().leakRef(); // leak the sheet on purpose
+    sheet->parseString(str);
+    return sheet;
+}
+
+static StyleSheetContents* parseUASheet(const char* characters, unsigned size)
+{
+    return parseUASheet(String(characters, size));
+}
+
+void CSSDefaultStyleSheets::initDefaultStyle(Element* root)
+{
+    if (!defaultStyle) {
+        if (!root || elementCanUseSimpleDefaultStyle(root))
+            loadSimpleDefaultStyle();
+        else
+            loadFullDefaultStyle();
+    }
+}
+
+void CSSDefaultStyleSheets::loadFullDefaultStyle()
+{
+    if (simpleDefaultStyleSheet) {
+        ASSERT(defaultStyle);
+        ASSERT(defaultPrintStyle == defaultStyle);
+        delete defaultStyle;
+        simpleDefaultStyleSheet->deref();
+        defaultStyle = RuleSet::create().leakPtr();
+        defaultPrintStyle = RuleSet::create().leakPtr();
+        simpleDefaultStyleSheet = 0;
+    } else {
+        ASSERT(!defaultStyle);
+        defaultStyle = RuleSet::create().leakPtr();
+        defaultPrintStyle = RuleSet::create().leakPtr();
+        defaultQuirksStyle = RuleSet::create().leakPtr();
+    }
+
+    // Strict-mode rules.
+    String defaultRules = String(htmlUserAgentStyleSheet, sizeof(htmlUserAgentStyleSheet)) + RenderTheme::defaultTheme()->extraDefaultStyleSheet();
+    defaultStyleSheet = parseUASheet(defaultRules);
+    defaultStyle->addRulesFromSheet(defaultStyleSheet, screenEval());
+    defaultPrintStyle->addRulesFromSheet(defaultStyleSheet, printEval());
+
+    // Quirks-mode rules.
+    String quirksRules = String(quirksUserAgentStyleSheet, sizeof(quirksUserAgentStyleSheet)) + RenderTheme::defaultTheme()->extraQuirksStyleSheet();
+    quirksStyleSheet = parseUASheet(quirksRules);
+    defaultQuirksStyle->addRulesFromSheet(quirksStyleSheet, screenEval());
+}
+
+void CSSDefaultStyleSheets::loadSimpleDefaultStyle()
+{
+    ASSERT(!defaultStyle);
+    ASSERT(!simpleDefaultStyleSheet);
+
+    defaultStyle = RuleSet::create().leakPtr();
+    // There are no media-specific rules in the simple default style.
+    defaultPrintStyle = defaultStyle;
+    defaultQuirksStyle = RuleSet::create().leakPtr();
+
+    simpleDefaultStyleSheet = parseUASheet(simpleUserAgentStyleSheet, strlen(simpleUserAgentStyleSheet));
+    defaultStyle->addRulesFromSheet(simpleDefaultStyleSheet, screenEval());
+
+    // No need to initialize quirks sheet yet as there are no quirk rules for elements allowed in simple default style.
+}
+
+RuleSet* CSSDefaultStyleSheets::viewSourceStyle()
+{
+    if (!defaultViewSourceStyle) {
+        defaultViewSourceStyle = RuleSet::create().leakPtr();
+        defaultViewSourceStyle->addRulesFromSheet(parseUASheet(sourceUserAgentStyleSheet, sizeof(sourceUserAgentStyleSheet)), screenEval());
+    }
+    return defaultViewSourceStyle;
+}
+
+
+void CSSDefaultStyleSheets::ensureDefaultStyleSheetsForElement(Element* element)
+{
+    if (simpleDefaultStyleSheet && !elementCanUseSimpleDefaultStyle(element))
+        loadFullDefaultStyle();
+
+#if ENABLE(SVG)
+    if (element->isSVGElement() && !svgStyleSheet) {
+        // SVG rules.
+        svgStyleSheet = parseUASheet(svgUserAgentStyleSheet, sizeof(svgUserAgentStyleSheet));
+        defaultStyle->addRulesFromSheet(svgStyleSheet, screenEval());
+        defaultPrintStyle->addRulesFromSheet(svgStyleSheet, printEval());
+    }
+#endif
+
+#if ENABLE(MATHML)
+    if (element->isMathMLElement() && !mathMLStyleSheet) {
+        // MathML rules.
+        mathMLStyleSheet = parseUASheet(mathmlUserAgentStyleSheet, sizeof(mathmlUserAgentStyleSheet));
+        defaultStyle->addRulesFromSheet(mathMLStyleSheet, screenEval());
+        defaultPrintStyle->addRulesFromSheet(mathMLStyleSheet, printEval());
+    }
+#endif
+
+#if ENABLE(VIDEO)
+    if (!mediaControlsStyleSheet && (element->hasTagName(videoTag) || element->hasTagName(audioTag))) {
+        String mediaRules = String(mediaControlsUserAgentStyleSheet, sizeof(mediaControlsUserAgentStyleSheet)) + RenderTheme::themeForPage(element->document()->page())->extraMediaControlsStyleSheet();
+        mediaControlsStyleSheet = parseUASheet(mediaRules);
+        defaultStyle->addRulesFromSheet(mediaControlsStyleSheet, screenEval());
+        defaultPrintStyle->addRulesFromSheet(mediaControlsStyleSheet, printEval());
+    }
+#endif
+
+#if ENABLE(FULLSCREEN_API)
+    if (!fullscreenStyleSheet && element->document()->webkitIsFullScreen()) {
+        String fullscreenRules = String(fullscreenUserAgentStyleSheet, sizeof(fullscreenUserAgentStyleSheet)) + RenderTheme::defaultTheme()->extraFullScreenStyleSheet();
+        fullscreenStyleSheet = parseUASheet(fullscreenRules);
+        defaultStyle->addRulesFromSheet(fullscreenStyleSheet, screenEval());
+        defaultQuirksStyle->addRulesFromSheet(fullscreenStyleSheet, screenEval());
+    }
+#endif
+
+    if (!plugInsStyleSheet && (element->hasTagName(objectTag) || element->hasTagName(embedTag))) {
+        String plugInsRules = String(plugInsUserAgentStyleSheet, sizeof(plugInsUserAgentStyleSheet)) + RenderTheme::themeForPage(element->document()->page())->extraPlugInsStyleSheet() + element->document()->page()->chrome()->client()->plugInExtraStyleSheet();
+        plugInsStyleSheet = parseUASheet(plugInsRules);
+        defaultStyle->addRulesFromSheet(plugInsStyleSheet, screenEval());
+    }
+
+    ASSERT(defaultStyle->features().idsInRules.isEmpty());
+    ASSERT(mathMLStyleSheet || defaultStyle->features().siblingRules.isEmpty());
+}
+
+} // namespace WebCore
diff --git a/Source/WebCore/css/CSSDefaultStyleSheets.h b/Source/WebCore/css/CSSDefaultStyleSheets.h
new file mode 100644 (file)
index 0000000..b51010c
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
+ * Copyright (C) 2013 Google Inc. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef CSSDefaultStyleSheets_h
+#define CSSDefaultStyleSheets_h
+
+namespace WebCore {
+
+class Element;
+class RuleSet;
+class StyleSheetContents;
+
+class CSSDefaultStyleSheets {
+public:
+    static RuleSet* defaultStyle;
+    static RuleSet* defaultQuirksStyle;
+    static RuleSet* defaultPrintStyle;
+    static RuleSet* defaultViewSourceStyle;
+
+    static StyleSheetContents* simpleDefaultStyleSheet;
+    static StyleSheetContents* defaultStyleSheet;
+    static StyleSheetContents* quirksStyleSheet;
+    static StyleSheetContents* svgStyleSheet;
+    static StyleSheetContents* mathMLStyleSheet;
+    static StyleSheetContents* mediaControlsStyleSheet;
+    static StyleSheetContents* fullscreenStyleSheet;
+    static StyleSheetContents* plugInsStyleSheet;
+
+    static void ensureDefaultStyleSheetsForElement(Element*);
+    static void loadFullDefaultStyle();
+    static void loadSimpleDefaultStyle();
+    static void initDefaultStyle(Element*);
+    static RuleSet* viewSourceStyle();
+};
+
+} // namespace WebCore
+
+#endif // CSSDefaultStyleSheets_h
index fb74e27..e3c98dd 100644 (file)
@@ -33,6 +33,7 @@
 #include "CSSBorderImage.h"
 #include "CSSCalculationValue.h"
 #include "CSSCursorImageValue.h"
+#include "CSSDefaultStyleSheets.h"
 #include "CSSFontFaceRule.h"
 #include "CSSFontSelector.h"
 #include "CSSHostRule.h"
@@ -57,8 +58,6 @@
 #endif
 #include "CachedImage.h"
 #include "CalculationValue.h"
-#include "Chrome.h"
-#include "ChromeClient.h"
 #include "ContentData.h"
 #include "ContextFeatures.h"
 #include "Counter.h"
@@ -211,44 +210,8 @@ if (isInitial) { \
     return; \
 }
 
-static RuleSet* defaultStyle;
-static RuleSet* defaultQuirksStyle;
-static RuleSet* defaultPrintStyle;
-static RuleSet* defaultViewSourceStyle;
-static StyleSheetContents* simpleDefaultStyleSheet;
-static StyleSheetContents* defaultStyleSheet;
-static StyleSheetContents* quirksStyleSheet;
-static StyleSheetContents* svgStyleSheet;
-static StyleSheetContents* mathMLStyleSheet;
-static StyleSheetContents* mediaControlsStyleSheet;
-static StyleSheetContents* fullscreenStyleSheet;
-static StyleSheetContents* plugInsStyleSheet;
-
 RenderStyle* StyleResolver::s_styleNotYetAvailable;
 
-static void loadFullDefaultStyle();
-static void loadSimpleDefaultStyle();
-
-// FIXME: It would be nice to use some mechanism that guarantees this is in sync with the real UA stylesheet.
-static const char* simpleUserAgentStyleSheet = "html,body,div{display:block}head{display:none}body{margin:8px}div:focus,span:focus{outline:auto 5px -webkit-focus-ring-color}a:-webkit-any-link{color:-webkit-link;text-decoration:underline}a:-webkit-any-link:active{color:-webkit-activelink}";
-
-static inline bool elementCanUseSimpleDefaultStyle(Element* e)
-{
-    return e->hasTagName(htmlTag) || e->hasTagName(headTag) || e->hasTagName(bodyTag) || e->hasTagName(divTag) || e->hasTagName(spanTag) || e->hasTagName(brTag) || e->hasTagName(aTag);
-}
-
-static const MediaQueryEvaluator& screenEval()
-{
-    DEFINE_STATIC_LOCAL(const MediaQueryEvaluator, staticScreenEval, ("screen"));
-    return staticScreenEval;
-}
-
-static const MediaQueryEvaluator& printEval()
-{
-    DEFINE_STATIC_LOCAL(const MediaQueryEvaluator, staticPrintEval, ("print"));
-    return staticPrintEval;
-}
-
 static StylePropertySet* leftToRightDeclaration()
 {
     DEFINE_STATIC_LOCAL(RefPtr<StylePropertySet>, leftToRightDecl, (StylePropertySet::create()));
@@ -299,12 +262,7 @@ StyleResolver::StyleResolver(Document* document, bool matchAuthorAndUserStyles)
 {
     Element* root = document->documentElement();
 
-    if (!defaultStyle) {
-        if (!root || elementCanUseSimpleDefaultStyle(root))
-            loadSimpleDefaultStyle();
-        else
-            loadFullDefaultStyle();
-    }
+    CSSDefaultStyleSheets::initDefaultStyle(root);
 
     // construct document root element default style. this is needed
     // to evaluate media queries that contain relative constraints, like "screen and (max-width: 10em)"
@@ -486,123 +444,6 @@ void StyleResolver::sweepMatchedPropertiesCache(Timer<StyleResolver>*)
     m_matchedPropertiesCacheAdditionsSinceLastSweep = 0;
 }
 
-static StyleSheetContents* parseUASheet(const String& str)
-{
-    StyleSheetContents* sheet = StyleSheetContents::create().leakRef(); // leak the sheet on purpose
-    sheet->parseString(str);
-    return sheet;
-}
-
-static StyleSheetContents* parseUASheet(const char* characters, unsigned size)
-{
-    return parseUASheet(String(characters, size));
-}
-
-static void loadFullDefaultStyle()
-{
-    if (simpleDefaultStyleSheet) {
-        ASSERT(defaultStyle);
-        ASSERT(defaultPrintStyle == defaultStyle);
-        delete defaultStyle;
-        simpleDefaultStyleSheet->deref();
-        defaultStyle = RuleSet::create().leakPtr();
-        defaultPrintStyle = RuleSet::create().leakPtr();
-        simpleDefaultStyleSheet = 0;
-    } else {
-        ASSERT(!defaultStyle);
-        defaultStyle = RuleSet::create().leakPtr();
-        defaultPrintStyle = RuleSet::create().leakPtr();
-        defaultQuirksStyle = RuleSet::create().leakPtr();
-    }
-
-    // Strict-mode rules.
-    String defaultRules = String(htmlUserAgentStyleSheet, sizeof(htmlUserAgentStyleSheet)) + RenderTheme::defaultTheme()->extraDefaultStyleSheet();
-    defaultStyleSheet = parseUASheet(defaultRules);
-    defaultStyle->addRulesFromSheet(defaultStyleSheet, screenEval());
-    defaultPrintStyle->addRulesFromSheet(defaultStyleSheet, printEval());
-
-    // Quirks-mode rules.
-    String quirksRules = String(quirksUserAgentStyleSheet, sizeof(quirksUserAgentStyleSheet)) + RenderTheme::defaultTheme()->extraQuirksStyleSheet();
-    quirksStyleSheet = parseUASheet(quirksRules);
-    defaultQuirksStyle->addRulesFromSheet(quirksStyleSheet, screenEval());
-}
-
-static void loadSimpleDefaultStyle()
-{
-    ASSERT(!defaultStyle);
-    ASSERT(!simpleDefaultStyleSheet);
-
-    defaultStyle = RuleSet::create().leakPtr();
-    // There are no media-specific rules in the simple default style.
-    defaultPrintStyle = defaultStyle;
-    defaultQuirksStyle = RuleSet::create().leakPtr();
-
-    simpleDefaultStyleSheet = parseUASheet(simpleUserAgentStyleSheet, strlen(simpleUserAgentStyleSheet));
-    defaultStyle->addRulesFromSheet(simpleDefaultStyleSheet, screenEval());
-
-    // No need to initialize quirks sheet yet as there are no quirk rules for elements allowed in simple default style.
-}
-
-static RuleSet* viewSourceStyle()
-{
-    if (!defaultViewSourceStyle) {
-        defaultViewSourceStyle = RuleSet::create().leakPtr();
-        defaultViewSourceStyle->addRulesFromSheet(parseUASheet(sourceUserAgentStyleSheet, sizeof(sourceUserAgentStyleSheet)), screenEval());
-    }
-    return defaultViewSourceStyle;
-}
-
-static void ensureDefaultStyleSheetsForElement(Element* element)
-{
-    if (simpleDefaultStyleSheet && !elementCanUseSimpleDefaultStyle(element))
-        loadFullDefaultStyle();
-
-#if ENABLE(SVG)
-    if (element->isSVGElement() && !svgStyleSheet) {
-        // SVG rules.
-        svgStyleSheet = parseUASheet(svgUserAgentStyleSheet, sizeof(svgUserAgentStyleSheet));
-        defaultStyle->addRulesFromSheet(svgStyleSheet, screenEval());
-        defaultPrintStyle->addRulesFromSheet(svgStyleSheet, printEval());
-    }
-#endif
-
-#if ENABLE(MATHML)
-    if (element->isMathMLElement() && !mathMLStyleSheet) {
-        // MathML rules.
-        mathMLStyleSheet = parseUASheet(mathmlUserAgentStyleSheet, sizeof(mathmlUserAgentStyleSheet));
-        defaultStyle->addRulesFromSheet(mathMLStyleSheet, screenEval());
-        defaultPrintStyle->addRulesFromSheet(mathMLStyleSheet, printEval());
-    }
-#endif
-
-#if ENABLE(VIDEO)
-    if (!mediaControlsStyleSheet && (element->hasTagName(videoTag) || element->hasTagName(audioTag))) {
-        String mediaRules = String(mediaControlsUserAgentStyleSheet, sizeof(mediaControlsUserAgentStyleSheet)) + RenderTheme::themeForPage(element->document()->page())->extraMediaControlsStyleSheet();
-        mediaControlsStyleSheet = parseUASheet(mediaRules);
-        defaultStyle->addRulesFromSheet(mediaControlsStyleSheet, screenEval());
-        defaultPrintStyle->addRulesFromSheet(mediaControlsStyleSheet, printEval());
-    }
-#endif
-
-#if ENABLE(FULLSCREEN_API)
-    if (!fullscreenStyleSheet && element->document()->webkitIsFullScreen()) {
-        String fullscreenRules = String(fullscreenUserAgentStyleSheet, sizeof(fullscreenUserAgentStyleSheet)) + RenderTheme::defaultTheme()->extraFullScreenStyleSheet();
-        fullscreenStyleSheet = parseUASheet(fullscreenRules);
-        defaultStyle->addRulesFromSheet(fullscreenStyleSheet, screenEval());
-        defaultQuirksStyle->addRulesFromSheet(fullscreenStyleSheet, screenEval());
-    }
-#endif
-
-    if (!plugInsStyleSheet && (element->hasTagName(objectTag) || element->hasTagName(embedTag))) {
-        String plugInsRules = String(plugInsUserAgentStyleSheet, sizeof(plugInsUserAgentStyleSheet)) + RenderTheme::themeForPage(element->document()->page())->extraPlugInsStyleSheet() + element->document()->page()->chrome()->client()->plugInExtraStyleSheet();
-        plugInsStyleSheet = parseUASheet(plugInsRules);
-        defaultStyle->addRulesFromSheet(plugInsStyleSheet, screenEval());
-    }
-
-    ASSERT(defaultStyle->features().idsInRules.isEmpty());
-    ASSERT(mathMLStyleSheet || defaultStyle->features().siblingRules.isEmpty());
-}
-
 void StyleResolver::addMatchedProperties(MatchResult& matchResult, const StylePropertySet* properties, StyleRule* rule, unsigned linkMatchType, PropertyWhitelistType propertyWhitelistType)
 {
     matchResult.matchedProperties.grow(matchResult.matchedProperties.size() + 1);
@@ -1354,19 +1195,19 @@ void StyleResolver::matchUARules(MatchResult& result)
     MatchingUARulesScope scope;
 
     // First we match rules from the user agent sheet.
-    if (simpleDefaultStyleSheet)
+    if (CSSDefaultStyleSheets::simpleDefaultStyleSheet)
         result.isCacheable = false;
     RuleSet* userAgentStyleSheet = m_medium->mediaTypeMatchSpecific("print")
-        ? defaultPrintStyle : defaultStyle;
+        ? CSSDefaultStyleSheets::defaultPrintStyle : CSSDefaultStyleSheets::defaultStyle;
     matchUARules(result, userAgentStyleSheet);
 
     // In quirks mode, we match rules from the quirks user agent sheet.
     if (!m_selectorChecker.strictParsing())
-        matchUARules(result, defaultQuirksStyle);
+        matchUARules(result, CSSDefaultStyleSheets::defaultQuirksStyle);
 
     // If document uses view source styles (in view source mode or in xml viewer mode), then we match rules from the view source style sheet.
     if (document()->isViewSource())
-        matchUARules(result, viewSourceStyle());
+        matchUARules(result, CSSDefaultStyleSheets::viewSourceStyle());
 }
 
 static void setStylesForPaginationMode(Pagination::Mode paginationMode, RenderStyle* style)
@@ -1604,7 +1445,7 @@ PassRefPtr<RenderStyle> StyleResolver::styleForElement(Element* element, RenderS
         m_style->setInsideLink(linkState);
     }
 
-    ensureDefaultStyleSheetsForElement(element);
+    CSSDefaultStyleSheets::ensureDefaultStyleSheetsForElement(element);
 
     MatchResult matchResult;
     if (matchingBehavior == MatchOnlyUserAgentRules)
@@ -1800,7 +1641,7 @@ PassRefPtr<RenderStyle> StyleResolver::styleForPage(int pageIndex)
     const String page = pageName(pageIndex);
     
     MatchResult result;
-    matchPageRules(result, defaultPrintStyle, isLeft, isFirst, page);
+    matchPageRules(result, CSSDefaultStyleSheets::defaultPrintStyle, isLeft, isFirst, page);
     matchPageRules(result, m_userStyle.get(), isLeft, isFirst, page);
     // Only consider the global author RuleSet for @page rules, as per the HTML5 spec.
     matchPageRules(result, m_authorStyle.get(), isLeft, isFirst, page);
@@ -2743,13 +2584,13 @@ void InspectorCSSOMWrappers::collectFromDocumentStyleSheetCollection(DocumentSty
 CSSStyleRule* InspectorCSSOMWrappers::getWrapperForRuleInSheets(StyleRule* rule, DocumentStyleSheetCollection* styleSheetCollection)
 {
     if (m_styleRuleToCSSOMWrapperMap.isEmpty()) {
-        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, simpleDefaultStyleSheet);
-        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, defaultStyleSheet);
-        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, quirksStyleSheet);
-        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, svgStyleSheet);
-        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, mathMLStyleSheet);
-        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, mediaControlsStyleSheet);
-        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, fullscreenStyleSheet);
+        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::simpleDefaultStyleSheet);
+        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::defaultStyleSheet);
+        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::quirksStyleSheet);
+        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::svgStyleSheet);
+        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::mathMLStyleSheet);
+        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::mediaControlsStyleSheet);
+        collectFromStyleSheetContents(m_styleSheetCSSOMWrapperSet, CSSDefaultStyleSheets::fullscreenStyleSheet);
 
         collectFromDocumentStyleSheetCollection(styleSheetCollection);
     }
@@ -5418,10 +5259,10 @@ void StyleResolver::collectFeatures()
     // Collect all ids and rules using sibling selectors (:first-child and similar)
     // in the current set of stylesheets. Style sharing code uses this information to reject
     // sharing candidates.
-    m_features.add(defaultStyle->features());
+    m_features.add(CSSDefaultStyleSheets::defaultStyle->features());
     m_features.add(m_authorStyle->features());
     if (document()->isViewSource())
-        m_features.add(viewSourceStyle()->features());
+        m_features.add(CSSDefaultStyleSheets::viewSourceStyle()->features());
 
     if (m_scopeResolver)
         m_scopeResolver->collectFeaturesTo(m_features);
@@ -5492,10 +5333,10 @@ void StyleResolver::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
     info.addMember(m_scopeResolver, "scopeResolver");
 
     // FIXME: move this to a place where it would be called only once?
-    info.addMember(defaultStyle, "defaultStyle");
-    info.addMember(defaultQuirksStyle, "defaultQuirksStyle");
-    info.addMember(defaultPrintStyle, "defaultPrintStyle");
-    info.addMember(defaultViewSourceStyle, "defaultViewSourceStyle");
+    info.addMember(CSSDefaultStyleSheets::defaultStyle, "defaultStyle");
+    info.addMember(CSSDefaultStyleSheets::defaultQuirksStyle, "defaultQuirksStyle");
+    info.addMember(CSSDefaultStyleSheets::defaultPrintStyle,"defaultPrintStyle");
+    info.addMember(CSSDefaultStyleSheets::defaultViewSourceStyle, "defaultViewSourceStyle");
 }
 
 } // namespace WebCore