/* * 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 * Copyright (C) 2007, 2008 Eric Seidel * 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 "StyleResolver.h" #include "Attribute.h" #include "CSSBorderImage.h" #include "CSSCalculationValue.h" #include "CSSCursorImageValue.h" #include "CSSFontFaceRule.h" #include "CSSFontSelector.h" #include "CSSImportRule.h" #include "CSSLineBoxContainValue.h" #include "CSSMediaRule.h" #include "CSSPageRule.h" #include "CSSParser.h" #include "CSSPrimitiveValueMappings.h" #include "CSSPropertyNames.h" #include "CSSReflectValue.h" #include "CSSRuleList.h" #include "CSSSelector.h" #include "CSSSelectorList.h" #include "CSSStyleRule.h" #include "CSSStyleSheet.h" #include "CSSTimingFunctionValue.h" #include "CSSValueList.h" #if ENABLE(CSS_VARIABLES) #include "CSSVariableValue.h" #endif #include "CachedImage.h" #include "CalculationValue.h" #include "ContentData.h" #include "ContextFeatures.h" #include "Counter.h" #include "CounterContent.h" #include "CursorList.h" #include "FontFeatureValue.h" #include "FontValue.h" #include "Frame.h" #include "FrameSelection.h" #include "FrameView.h" #include "HTMLDocument.h" #include "HTMLIFrameElement.h" #include "HTMLInputElement.h" #include "HTMLNames.h" #include "HTMLOptionElement.h" #include "HTMLProgressElement.h" #include "HTMLStyleElement.h" #include "HTMLTextAreaElement.h" #include "InspectorInstrumentation.h" #include "KeyframeList.h" #include "LinkHash.h" #include "LocaleToScriptMapping.h" #include "MathMLNames.h" #include "Matrix3DTransformOperation.h" #include "MatrixTransformOperation.h" #include "MediaList.h" #include "MediaQueryEvaluator.h" #include "NodeRenderStyle.h" #include "NodeRenderingContext.h" #include "Page.h" #include "PageGroup.h" #include "Pair.h" #include "PerspectiveTransformOperation.h" #include "QuotesData.h" #include "Rect.h" #include "RenderRegion.h" #include "RenderScrollbar.h" #include "RenderScrollbarTheme.h" #include "RenderStyleConstants.h" #include "RenderTheme.h" #include "RenderView.h" #include "RotateTransformOperation.h" #include "SVGDocumentExtensions.h" #include "SVGFontFaceElement.h" #include "ScaleTransformOperation.h" #include "SecurityOrigin.h" #include "Settings.h" #include "ShadowData.h" #include "ShadowRoot.h" #include "ShadowValue.h" #include "SkewTransformOperation.h" #include "StyleBuilder.h" #include "StyleCachedImage.h" #include "StyleGeneratedImage.h" #include "StylePendingImage.h" #include "StyleRule.h" #include "StyleRuleImport.h" #include "StyleSheetContents.h" #include "StyleSheetList.h" #include "Text.h" #include "TransformationMatrix.h" #include "TranslateTransformOperation.h" #include "UserAgentStyleSheets.h" #include "WebCoreMemoryInstrumentation.h" #include "WebKitCSSKeyframeRule.h" #include "WebKitCSSKeyframesRule.h" #include "WebKitCSSRegionRule.h" #include "WebKitCSSTransformValue.h" #include "WebKitFontFamilyNames.h" #include "XMLNames.h" #include #include #if ENABLE(CSS_FILTERS) #include "FilterOperation.h" #include "WebKitCSSFilterValue.h" #endif #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(WIDGET_REGION) #include "DashboardRegion.h" #endif #if ENABLE(SVG) #include "CachedSVGDocument.h" #include "SVGDocument.h" #include "SVGElement.h" #include "SVGNames.h" #include "SVGURIReference.h" #include "WebKitCSSSVGDocumentValue.h" #endif #if ENABLE(CSS_SHADERS) #include "CustomFilterArrayParameter.h" #include "CustomFilterNumberParameter.h" #include "CustomFilterOperation.h" #include "CustomFilterParameter.h" #include "CustomFilterTransformParameter.h" #include "StyleCachedShader.h" #include "StyleCustomFilterProgram.h" #include "StylePendingShader.h" #include "StyleShader.h" #include "WebKitCSSMixFunctionValue.h" #include "WebKitCSSShaderValue.h" #endif #if ENABLE(CSS_IMAGE_SET) #include "CSSImageSetValue.h" #include "StyleCachedImageSet.h" #endif #if PLATFORM(BLACKBERRY) #define FIXED_POSITION_CREATES_STACKING_CONTEXT 1 #endif using namespace std; namespace WebCore { using namespace HTMLNames; #define HANDLE_INHERIT(prop, Prop) \ if (isInherit) { \ m_style->set##Prop(m_parentStyle->prop()); \ return; \ } #define HANDLE_INHERIT_AND_INITIAL(prop, Prop) \ HANDLE_INHERIT(prop, Prop) \ if (isInitial) { \ m_style->set##Prop(RenderStyle::initial##Prop()); \ return; \ } #define HANDLE_INHERIT_AND_INITIAL_WITH_VALUE(prop, Prop, Value) \ HANDLE_INHERIT(prop, Prop) \ if (isInitial) { \ m_style->set##Prop(RenderStyle::initial##Value());\ return;\ } #define HANDLE_INHERIT_AND_INITIAL_AND_PRIMITIVE(prop, Prop) \ HANDLE_INHERIT_AND_INITIAL(prop, Prop) \ if (primitiveValue) \ m_style->set##Prop(*primitiveValue); class RuleData { public: RuleData(StyleRule*, unsigned selectorIndex, unsigned position, bool hasDocumentSecurityOrigin, bool canUseFastCheckSelector, bool inRegionRule); unsigned position() const { return m_position; } StyleRule* rule() const { return m_rule; } CSSSelector* selector() const { return m_rule->selectorList().selectorAt(m_selectorIndex); } unsigned selectorIndex() const { return m_selectorIndex; } bool hasFastCheckableSelector() const { return m_hasFastCheckableSelector; } bool hasMultipartSelector() const { return m_hasMultipartSelector; } bool hasRightmostSelectorMatchingHTMLBasedOnRuleHash() const { return m_hasRightmostSelectorMatchingHTMLBasedOnRuleHash; } bool containsUncommonAttributeSelector() const { return m_containsUncommonAttributeSelector; } unsigned specificity() const { return m_specificity; } unsigned linkMatchType() const { return m_linkMatchType; } bool hasDocumentSecurityOrigin() const { return m_hasDocumentSecurityOrigin; } bool isInRegionRule() const { return m_isInRegionRule; } // Try to balance between memory usage (there can be lots of RuleData objects) and good filtering performance. static const unsigned maximumIdentifierCount = 4; const unsigned* descendantSelectorIdentifierHashes() const { return m_descendantSelectorIdentifierHashes; } void reportMemoryUsage(MemoryObjectInfo*) const; private: StyleRule* m_rule; unsigned m_selectorIndex : 12; // This number was picked fairly arbitrarily. We can probably lower it if we need to. // Some simple testing showed <100,000 RuleData's on large sites. unsigned m_position : 20; unsigned m_specificity : 24; unsigned m_hasFastCheckableSelector : 1; unsigned m_hasMultipartSelector : 1; unsigned m_hasRightmostSelectorMatchingHTMLBasedOnRuleHash : 1; unsigned m_containsUncommonAttributeSelector : 1; unsigned m_linkMatchType : 2; // SelectorChecker::LinkMatchMask unsigned m_hasDocumentSecurityOrigin : 1; unsigned m_isInRegionRule : 1; // Use plain array instead of a Vector to minimize memory overhead. unsigned m_descendantSelectorIdentifierHashes[maximumIdentifierCount]; }; struct SameSizeAsRuleData { void* a; unsigned b; unsigned c; unsigned d[4]; }; COMPILE_ASSERT(sizeof(RuleData) == sizeof(SameSizeAsRuleData), RuleData_should_stay_small); class RuleSet { WTF_MAKE_NONCOPYABLE(RuleSet); WTF_MAKE_FAST_ALLOCATED; public: static PassOwnPtr create() { return adoptPtr(new RuleSet); } typedef HashMap > > AtomRuleMap; void addRulesFromSheet(StyleSheetContents*, const MediaQueryEvaluator&, StyleResolver* = 0, const ContainerNode* = 0); void addStyleRule(StyleRule*, bool hasDocumentSecurityOrigin, bool canUseFastCheckSelector, bool isInRegionRule = false); void addRule(StyleRule*, unsigned selectorIndex, bool hasDocumentSecurityOrigin, bool canUseFastCheckSelector, bool isInRegionRule = false); void addPageRule(StyleRulePage*); void addToRuleSet(AtomicStringImpl* key, AtomRuleMap&, const RuleData&); void addRegionRule(StyleRuleRegion*, bool hasDocumentSecurityOrigin); void shrinkToFit(); void disableAutoShrinkToFit() { m_autoShrinkToFitEnabled = false; } const StyleResolver::Features& features() const { return m_features; } const Vector* idRules(AtomicStringImpl* key) const { return m_idRules.get(key); } const Vector* classRules(AtomicStringImpl* key) const { return m_classRules.get(key); } const Vector* tagRules(AtomicStringImpl* key) const { return m_tagRules.get(key); } const Vector* shadowPseudoElementRules(AtomicStringImpl* key) const { return m_shadowPseudoElementRules.get(key); } const Vector* linkPseudoClassRules() const { return &m_linkPseudoClassRules; } const Vector* focusPseudoClassRules() const { return &m_focusPseudoClassRules; } const Vector* universalRules() const { return &m_universalRules; } const Vector& pageRules() const { return m_pageRules; } void reportMemoryUsage(MemoryObjectInfo*) const; public: RuleSet(); AtomRuleMap m_idRules; AtomRuleMap m_classRules; AtomRuleMap m_tagRules; AtomRuleMap m_shadowPseudoElementRules; Vector m_linkPseudoClassRules; Vector m_focusPseudoClassRules; Vector m_universalRules; Vector m_pageRules; unsigned m_ruleCount; bool m_autoShrinkToFitEnabled; StyleResolver::Features m_features; struct RuleSetSelectorPair { RuleSetSelectorPair(CSSSelector* selector, PassOwnPtr ruleSet) : selector(selector), ruleSet(ruleSet) { } RuleSetSelectorPair(const RuleSetSelectorPair& rs) : selector(rs.selector), ruleSet(const_cast(&rs)->ruleSet.release()) { } void reportMemoryUsage(MemoryObjectInfo*) const; CSSSelector* selector; OwnPtr ruleSet; }; Vector m_regionSelectorsAndRuleSets; }; 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; RenderStyle* StyleResolver::s_styleNotYetAvailable; static void loadFullDefaultStyle(); static void loadSimpleDefaultStyle(); template static void collectCSSOMWrappers(HashMap >&, ListType*); // 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, leftToRightDecl, (StylePropertySet::create())); if (leftToRightDecl->isEmpty()) leftToRightDecl->setProperty(CSSPropertyDirection, CSSValueLtr); return leftToRightDecl.get(); } static StylePropertySet* rightToLeftDeclaration() { DEFINE_STATIC_LOCAL(RefPtr, rightToLeftDecl, (StylePropertySet::create())); if (rightToLeftDecl->isEmpty()) rightToLeftDecl->setProperty(CSSPropertyDirection, CSSValueRtl); return rightToLeftDecl.get(); } StyleResolver::StyleResolver(Document* document, bool matchAuthorAndUserStyles) : m_hasUAAppearance(false) , m_backgroundData(BackgroundFillLayer) , m_matchedPropertiesCacheAdditionsSinceLastSweep(0) , m_checker(document, !document->inQuirksMode()) , m_parentStyle(0) , m_rootElementStyle(0) , m_element(0) , m_styledElement(0) , m_regionForStyling(0) , m_elementLinkState(NotInsideLink) , m_parentNode(0) , m_lineHeightValue(0) , m_fontDirty(false) , m_matchAuthorAndUserStyles(matchAuthorAndUserStyles) , m_sameOriginOnly(false) , m_distributedToInsertionPoint(false) , m_hasUnknownPseudoElements(false) , m_fontSelector(CSSFontSelector::create(document)) , m_applyPropertyToRegularStyle(true) , m_applyPropertyToVisitedLinkStyle(false) , m_styleBuilder(StyleBuilder::sharedStyleBuilder()) #if ENABLE(CSS_SHADERS) , m_hasPendingShaders(false) #endif #if ENABLE(STYLE_SCOPED) , m_scopeStackParent(0) , m_scopeStackParentBoundsIndex(0) #endif , m_styleMap(this) { Element* root = document->documentElement(); if (!defaultStyle) { if (!root || elementCanUseSimpleDefaultStyle(root)) loadSimpleDefaultStyle(); else loadFullDefaultStyle(); } // construct document root element default style. this is needed // to evaluate media queries that contain relative constraints, like "screen and (max-width: 10em)" // This is here instead of constructor, because when constructor is run, // document doesn't have documentElement // NOTE: this assumes that element that gets passed to styleForElement -call // is always from the document that owns the style selector FrameView* view = document->view(); if (view) m_medium = adoptPtr(new MediaQueryEvaluator(view->mediaType())); else m_medium = adoptPtr(new MediaQueryEvaluator("all")); if (root) m_rootDefaultStyle = styleForElement(root, 0, DisallowStyleSharing, MatchOnlyUserAgentRules); if (m_rootDefaultStyle && view) m_medium = adoptPtr(new MediaQueryEvaluator(view->mediaType(), view->frame(), m_rootDefaultStyle.get())); m_authorStyle = RuleSet::create(); // Adding rules from multiple sheets, shrink at the end. // Adding global rules from multiple sheets, shrink at the end. // Note that there usually is only 1 sheet for scoped rules, so auto-shrink-to-fit is fine. m_authorStyle->disableAutoShrinkToFit(); // FIXME: This sucks! The user sheet is reparsed every time! OwnPtr tempUserStyle = RuleSet::create(); if (CSSStyleSheet* pageUserSheet = document->pageUserSheet()) tempUserStyle->addRulesFromSheet(pageUserSheet->contents(), *m_medium, this); addAuthorRulesAndCollectUserRulesFromSheets(document->pageGroupUserSheets(), *tempUserStyle); addAuthorRulesAndCollectUserRulesFromSheets(document->documentUserSheets(), *tempUserStyle); if (tempUserStyle->m_ruleCount > 0 || tempUserStyle->m_pageRules.size() > 0) m_userStyle = tempUserStyle.release(); #if ENABLE(SVG_FONTS) if (document->svgExtensions()) { const HashSet& svgFontFaceElements = document->svgExtensions()->svgFontFaceElements(); HashSet::const_iterator end = svgFontFaceElements.end(); for (HashSet::const_iterator it = svgFontFaceElements.begin(); it != end; ++it) fontSelector()->addFontFaceRule((*it)->fontFaceRule()); } #endif addStylesheetsFromSeamlessParents(); appendAuthorStylesheets(0, document->styleSheets()->vector()); } void StyleResolver::addStylesheetsFromSeamlessParents() { // Build a list of stylesheet lists from our ancestors, and walk that // list in reverse order so that the root-most sheets are appended first. Document* childDocument = document(); Vector ancestorSheets; while (HTMLIFrameElement* parentIFrame = childDocument->seamlessParentIFrame()) { Document* parentDocument = parentIFrame->document(); ancestorSheets.append(parentDocument->styleSheets()); childDocument = parentDocument; } for (int i = ancestorSheets.size() - 1; i >= 0; i--) appendAuthorStylesheets(0, ancestorSheets.at(i)->vector()); } void StyleResolver::addAuthorRulesAndCollectUserRulesFromSheets(const Vector >* userSheets, RuleSet& userStyle) { if (!userSheets) return; unsigned length = userSheets->size(); for (unsigned i = 0; i < length; i++) { StyleSheetContents* sheet = userSheets->at(i)->contents(); if (sheet->isUserStyleSheet()) userStyle.addRulesFromSheet(sheet, *m_medium, this); else m_authorStyle->addRulesFromSheet(sheet, *m_medium, this); } } static PassOwnPtr makeRuleSet(const Vector& rules) { size_t size = rules.size(); if (!size) return nullptr; OwnPtr ruleSet = RuleSet::create(); for (size_t i = 0; i < size; ++i) ruleSet->addRule(rules[i].rule, rules[i].selectorIndex, rules[i].hasDocumentSecurityOrigin, false); ruleSet->shrinkToFit(); return ruleSet.release(); } void StyleResolver::collectFeatures() { m_features.clear(); // 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(m_authorStyle->features()); #if ENABLE(STYLE_SCOPED) for (ScopedRuleSetMap::iterator it = m_scopedAuthorStyles.begin(); it != m_scopedAuthorStyles.end(); ++it) m_features.add(it->second->features()); #endif if (m_userStyle) m_features.add(m_userStyle->features()); m_siblingRuleSet = makeRuleSet(m_features.siblingRules); m_uncommonAttributeRuleSet = makeRuleSet(m_features.uncommonAttributeRules); } #if ENABLE(STYLE_SCOPED) const ContainerNode* StyleResolver::determineScope(const CSSStyleSheet* sheet) { ASSERT(sheet); if (!ContextFeatures::styleScopedEnabled(document())) return 0; Node* ownerNode = sheet->ownerNode(); if (!ownerNode || !ownerNode->isHTMLElement() || !ownerNode->hasTagName(HTMLNames::styleTag)) return 0; HTMLStyleElement* styleElement = static_cast(ownerNode); if (!styleElement->scoped()) return styleElement->isInShadowTree()? styleElement->shadowRoot() : 0; ContainerNode* parent = styleElement->parentNode(); if (!parent) return 0; return (parent->isElementNode() || parent->isShadowRoot()) ? parent : 0; } inline RuleSet* StyleResolver::ruleSetForScope(const ContainerNode* scope) const { if (!scope->hasScopedHTMLStyleChild()) return 0; ScopedRuleSetMap::const_iterator it = m_scopedAuthorStyles.find(scope); return it != m_scopedAuthorStyles.end() ? it->second.get() : 0; } #endif void StyleResolver::appendAuthorStylesheets(unsigned firstNew, const Vector >& stylesheets) { // This handles sheets added to the end of the stylesheet list only. In other cases the style resolver // needs to be reconstructed. To handle insertions too the rule order numbers would need to be updated. unsigned size = stylesheets.size(); for (unsigned i = firstNew; i < size; ++i) { if (!stylesheets[i]->isCSSStyleSheet()) continue; CSSStyleSheet* cssSheet = static_cast(stylesheets[i].get()); if (cssSheet->disabled()) continue; if (cssSheet->mediaQueries() && !m_medium->eval(cssSheet->mediaQueries(), this)) continue; StyleSheetContents* sheet = cssSheet->contents(); #if ENABLE(STYLE_SCOPED) const ContainerNode* scope = determineScope(cssSheet); if (scope) { ScopedRuleSetMap::AddResult addResult = m_scopedAuthorStyles.add(scope, nullptr); if (addResult.isNewEntry) addResult.iterator->second = RuleSet::create(); addResult.iterator->second->addRulesFromSheet(sheet, *m_medium, this, scope); continue; } #endif m_authorStyle->addRulesFromSheet(sheet, *m_medium, this); if (!m_styleRuleToCSSOMWrapperMap.isEmpty()) collectCSSOMWrappers(m_styleRuleToCSSOMWrapperMap, cssSheet); } m_authorStyle->shrinkToFit(); collectFeatures(); if (document()->renderer() && document()->renderer()->style()) document()->renderer()->style()->font().update(fontSelector()); } #if ENABLE(STYLE_SCOPED) void StyleResolver::setupScopeStack(const ContainerNode* parent) { // The scoping element stack shouldn't be used if