e377801117ee1a31c320489bc811b3a251560d36
[WebKit-https.git] / Source / WebCore / css / StyleResolver.cpp
1 /*
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, 2013 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.
12  *
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.
17  *
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.
22  *
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.
27  */
28
29 #include "config.h"
30 #include "StyleResolver.h"
31
32 #include "Attribute.h"
33 #include "CSSBorderImage.h"
34 #include "CSSCalculationValue.h"
35 #include "CSSCursorImageValue.h"
36 #include "CSSDefaultStyleSheets.h"
37 #include "CSSFilterImageValue.h"
38 #include "CSSFontFaceRule.h"
39 #include "CSSFontSelector.h"
40 #include "CSSLineBoxContainValue.h"
41 #include "CSSPageRule.h"
42 #include "CSSParser.h"
43 #include "CSSPrimitiveValueMappings.h"
44 #include "CSSPropertyNames.h"
45 #include "CSSReflectValue.h"
46 #include "CSSSelector.h"
47 #include "CSSSelectorList.h"
48 #include "CSSStyleRule.h"
49 #include "CSSSupportsRule.h"
50 #include "CSSTimingFunctionValue.h"
51 #include "CSSValueList.h"
52 #include "CachedImage.h"
53 #include "CalculationValue.h"
54 #include "ContentData.h"
55 #include "ContextFeatures.h"
56 #include "Counter.h"
57 #include "CounterContent.h"
58 #include "CursorList.h"
59 #include "DeprecatedStyleBuilder.h"
60 #include "DocumentStyleSheetCollection.h"
61 #include "ElementRuleCollector.h"
62 #include "FontFeatureValue.h"
63 #include "FontValue.h"
64 #include "Frame.h"
65 #include "FrameSelection.h"
66 #include "FrameView.h"
67 #include "HTMLDocument.h"
68 #include "HTMLIFrameElement.h"
69 #include "HTMLInputElement.h"
70 #include "HTMLNames.h"
71 #include "HTMLOptGroupElement.h"
72 #include "HTMLOptionElement.h"
73 #include "HTMLProgressElement.h"
74 #include "HTMLStyleElement.h"
75 #include "HTMLTableElement.h"
76 #include "HTMLTextAreaElement.h"
77 #include "InsertionPoint.h"
78 #include "InspectorInstrumentation.h"
79 #include "KeyframeList.h"
80 #include "LinkHash.h"
81 #include "LocaleToScriptMapping.h"
82 #include "MathMLNames.h"
83 #include "MediaList.h"
84 #include "MediaQueryEvaluator.h"
85 #include "NodeRenderStyle.h"
86 #include "NodeRenderingTraversal.h"
87 #include "Page.h"
88 #include "PageRuleCollector.h"
89 #include "Pair.h"
90 #include "QuotesData.h"
91 #include "Rect.h"
92 #include "RenderRegion.h"
93 #include "RenderScrollbar.h"
94 #include "RenderScrollbarTheme.h"
95 #include "RenderStyleConstants.h"
96 #include "RenderTheme.h"
97 #include "RenderView.h"
98 #include "RuleSet.h"
99 #include "SVGDocumentExtensions.h"
100 #include "SVGFontFaceElement.h"
101 #include "SecurityOrigin.h"
102 #include "SelectorCheckerFastPath.h"
103 #include "Settings.h"
104 #include "ShadowData.h"
105 #include "ShadowRoot.h"
106 #include "ShadowValue.h"
107 #include "StyleCachedImage.h"
108 #include "StyleFontSizeFunctions.h"
109 #include "StyleGeneratedImage.h"
110 #include "StylePendingImage.h"
111 #include "StylePropertySet.h"
112 #include "StylePropertyShorthand.h"
113 #include "StyleRule.h"
114 #include "StyleRuleImport.h"
115 #include "StyleSheetContents.h"
116 #include "StyleSheetList.h"
117 #include "Text.h"
118 #include "TransformFunctions.h"
119 #include "TransformOperations.h"
120 #include "UserAgentStyleSheets.h"
121 #include "ViewportStyleResolver.h"
122 #include "VisitedLinkState.h"
123 #include "WebKitCSSKeyframeRule.h"
124 #include "WebKitCSSKeyframesRule.h"
125 #include "WebKitCSSRegionRule.h"
126 #include "WebKitCSSTransformValue.h"
127 #include "WebKitFontFamilyNames.h"
128 #include "XMLNames.h"
129 #include <wtf/StdLibExtras.h>
130 #include <wtf/Vector.h>
131
132 #if ENABLE(CSS_FILTERS)
133 #include "FilterOperation.h"
134 #include "WebKitCSSFilterValue.h"
135 #endif
136
137 #if ENABLE(CSS_IMAGE_SET)
138 #include "CSSImageSetValue.h"
139 #include "StyleCachedImageSet.h"
140 #endif
141
142 #if ENABLE(CSS_SHADERS)
143 #include "CustomFilterArrayParameter.h"
144 #include "CustomFilterColorParameter.h"
145 #include "CustomFilterConstants.h"
146 #include "CustomFilterNumberParameter.h"
147 #include "CustomFilterOperation.h"
148 #include "CustomFilterParameter.h"
149 #include "CustomFilterProgramInfo.h"
150 #include "CustomFilterTransformParameter.h"
151 #include "StyleCachedShader.h"
152 #include "StyleCustomFilterProgram.h"
153 #include "StyleCustomFilterProgramCache.h"
154 #include "StylePendingShader.h"
155 #include "StyleShader.h"
156 #include "WebKitCSSMixFunctionValue.h"
157 #include "WebKitCSSShaderValue.h"
158 #endif
159
160 #if ENABLE(CSS_SHAPES)
161 #include "CachedResourceLoader.h"
162 #endif
163
164 #if ENABLE(CSS_VARIABLES)
165 #include "CSSVariableValue.h"
166 #endif
167
168 #if ENABLE(DASHBOARD_SUPPORT)
169 #include "DashboardRegion.h"
170 #endif
171
172 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
173 #include "HTMLAudioElement.h"
174 #endif
175
176 #if ENABLE(SVG)
177 #include "CachedSVGDocument.h"
178 #include "CachedSVGDocumentReference.h"
179 #include "SVGDocument.h"
180 #include "SVGElement.h"
181 #include "SVGNames.h"
182 #include "SVGURIReference.h"
183 #include "WebKitCSSSVGDocumentValue.h"
184 #endif
185
186 #if ENABLE(VIDEO_TRACK)
187 #include "WebVTTElement.h"
188 #endif
189
190 using namespace std;
191
192 namespace WebCore {
193
194 using namespace HTMLNames;
195
196 #define HANDLE_INHERIT(prop, Prop) \
197 if (isInherit) { \
198     m_state.style()->set##Prop(m_state.parentStyle()->prop()); \
199     return; \
200 }
201
202 #define HANDLE_INHERIT_AND_INITIAL(prop, Prop) \
203 HANDLE_INHERIT(prop, Prop) \
204 if (isInitial) { \
205     m_state.style()->set##Prop(RenderStyle::initial##Prop()); \
206     return; \
207 }
208
209 RenderStyle* StyleResolver::s_styleNotYetAvailable;
210
211 inline void StyleResolver::State::cacheBorderAndBackground()
212 {
213     m_hasUAAppearance = m_style->hasAppearance();
214     if (m_hasUAAppearance) {
215         m_borderData = m_style->border();
216         m_backgroundData = *m_style->backgroundLayers();
217         m_backgroundColor = m_style->backgroundColor();
218     }
219 }
220
221 inline void StyleResolver::State::clear()
222 {
223     m_element = 0;
224     m_styledElement = 0;
225     m_parentStyle = 0;
226     m_parentNode = 0;
227     m_regionForStyling = 0;
228     m_pendingImageProperties.clear();
229 #if ENABLE(CSS_SHADERS)
230     m_hasPendingShaders = false;
231 #endif
232 #if ENABLE(CSS_FILTERS) && ENABLE(SVG)
233     m_pendingSVGDocuments.clear();
234 #endif
235 }
236
237 void StyleResolver::MatchResult::addMatchedProperties(const StylePropertySet& properties, StyleRule* rule, unsigned linkMatchType, PropertyWhitelistType propertyWhitelistType)
238 {
239     matchedProperties.grow(matchedProperties.size() + 1);
240     StyleResolver::MatchedProperties& newProperties = matchedProperties.last();
241     newProperties.properties = const_cast<StylePropertySet*>(&properties);
242     newProperties.linkMatchType = linkMatchType;
243     newProperties.whitelistType = propertyWhitelistType;
244     matchedRules.append(rule);
245 }
246
247 StyleResolver::StyleResolver(Document& document, bool matchAuthorAndUserStyles)
248     : m_matchedPropertiesCacheAdditionsSinceLastSweep(0)
249     , m_matchedPropertiesCacheSweepTimer(this, &StyleResolver::sweepMatchedPropertiesCache)
250     , m_document(document)
251     , m_matchAuthorAndUserStyles(matchAuthorAndUserStyles)
252     , m_fontSelector(CSSFontSelector::create(&m_document))
253 #if ENABLE(CSS_DEVICE_ADAPTATION)
254     , m_viewportStyleResolver(ViewportStyleResolver::create(&document))
255 #endif
256     , m_deprecatedStyleBuilder(DeprecatedStyleBuilder::sharedStyleBuilder())
257     , m_styleMap(this)
258 {
259     Element* root = m_document.documentElement();
260
261     CSSDefaultStyleSheets::initDefaultStyle(root);
262
263     // construct document root element default style. this is needed
264     // to evaluate media queries that contain relative constraints, like "screen and (max-width: 10em)"
265     // This is here instead of constructor, because when constructor is run,
266     // document doesn't have documentElement
267     // NOTE: this assumes that element that gets passed to styleForElement -call
268     // is always from the document that owns the style selector
269     FrameView* view = m_document.view();
270     if (view)
271         m_medium = adoptPtr(new MediaQueryEvaluator(view->mediaType()));
272     else
273         m_medium = adoptPtr(new MediaQueryEvaluator("all"));
274
275     if (root)
276         m_rootDefaultStyle = styleForElement(root, 0, DisallowStyleSharing, MatchOnlyUserAgentRules);
277
278     if (m_rootDefaultStyle && view)
279         m_medium = adoptPtr(new MediaQueryEvaluator(view->mediaType(), &view->frame(), m_rootDefaultStyle.get()));
280
281     m_ruleSets.resetAuthorStyle();
282
283     DocumentStyleSheetCollection* styleSheetCollection = m_document.styleSheetCollection();
284     m_ruleSets.initUserStyle(styleSheetCollection, *m_medium, *this);
285
286 #if ENABLE(SVG_FONTS)
287     if (m_document.svgExtensions()) {
288         const HashSet<SVGFontFaceElement*>& svgFontFaceElements = m_document.svgExtensions()->svgFontFaceElements();
289         HashSet<SVGFontFaceElement*>::const_iterator end = svgFontFaceElements.end();
290         for (HashSet<SVGFontFaceElement*>::const_iterator it = svgFontFaceElements.begin(); it != end; ++it)
291             fontSelector()->addFontFaceRule((*it)->fontFaceRule());
292     }
293 #endif
294
295     appendAuthorStyleSheets(0, styleSheetCollection->activeAuthorStyleSheets());
296 }
297
298 void StyleResolver::appendAuthorStyleSheets(unsigned firstNew, const Vector<RefPtr<CSSStyleSheet> >& styleSheets)
299 {
300     m_ruleSets.appendAuthorStyleSheets(firstNew, styleSheets, m_medium.get(), m_inspectorCSSOMWrappers, document().isViewSource(), this);
301     if (document().renderer() && document().renderer()->style())
302         document().renderer()->style()->font().update(fontSelector());
303
304 #if ENABLE(CSS_DEVICE_ADAPTATION)
305     viewportStyleResolver()->resolve();
306 #endif
307 }
308
309 void StyleResolver::pushParentElement(Element* parent)
310 {
311     const ContainerNode* parentsParent = parent->parentOrShadowHostElement();
312
313     // We are not always invoked consistently. For example, script execution can cause us to enter
314     // style recalc in the middle of tree building. We may also be invoked from somewhere within the tree.
315     // Reset the stack in this case, or if we see a new root element.
316     // Otherwise just push the new parent.
317     if (!parentsParent || m_selectorFilter.parentStackIsEmpty())
318         m_selectorFilter.setupParentStack(parent);
319     else
320         m_selectorFilter.pushParent(parent);
321
322     // Note: We mustn't skip ShadowRoot nodes for the scope stack.
323     if (m_scopeResolver)
324         m_scopeResolver->push(parent, parent->parentOrShadowHostNode());
325 }
326
327 void StyleResolver::popParentElement(Element* parent)
328 {
329     // Note that we may get invoked for some random elements in some wacky cases during style resolve.
330     // Pause maintaining the stack in this case.
331     if (m_selectorFilter.parentStackIsConsistent(parent))
332         m_selectorFilter.popParent();
333     if (m_scopeResolver)
334         m_scopeResolver->pop(parent);
335 }
336
337 void StyleResolver::pushParentShadowRoot(const ShadowRoot* shadowRoot)
338 {
339     ASSERT(shadowRoot->hostElement());
340     if (m_scopeResolver)
341         m_scopeResolver->push(shadowRoot, shadowRoot->hostElement());
342 }
343
344 void StyleResolver::popParentShadowRoot(const ShadowRoot* shadowRoot)
345 {
346     ASSERT(shadowRoot->hostElement());
347     if (m_scopeResolver)
348         m_scopeResolver->pop(shadowRoot);
349 }
350
351 // This is a simplified style setting function for keyframe styles
352 void StyleResolver::addKeyframeStyle(PassRefPtr<StyleRuleKeyframes> rule)
353 {
354     AtomicString s(rule->name());
355     m_keyframesRuleMap.set(s.impl(), rule);
356 }
357
358 StyleResolver::~StyleResolver()
359 {
360     m_fontSelector->clearDocument();
361
362 #if ENABLE(CSS_DEVICE_ADAPTATION)
363     m_viewportStyleResolver->clearDocument();
364 #endif
365 }
366
367 void StyleResolver::sweepMatchedPropertiesCache(Timer<StyleResolver>*)
368 {
369     // Look for cache entries containing a style declaration with a single ref and remove them.
370     // This may happen when an element attribute mutation causes it to generate a new inlineStyle()
371     // or presentationAttributeStyle(), potentially leaving this cache with the last ref on the old one.
372     Vector<unsigned, 16> toRemove;
373     MatchedPropertiesCache::iterator it = m_matchedPropertiesCache.begin();
374     MatchedPropertiesCache::iterator end = m_matchedPropertiesCache.end();
375     for (; it != end; ++it) {
376         Vector<MatchedProperties>& matchedProperties = it->value.matchedProperties;
377         for (size_t i = 0; i < matchedProperties.size(); ++i) {
378             if (matchedProperties[i].properties->hasOneRef()) {
379                 toRemove.append(it->key);
380                 break;
381             }
382         }
383     }
384     for (size_t i = 0; i < toRemove.size(); ++i)
385         m_matchedPropertiesCache.remove(toRemove[i]);
386
387     m_matchedPropertiesCacheAdditionsSinceLastSweep = 0;
388 }
389
390 inline bool StyleResolver::styleSharingCandidateMatchesHostRules()
391 {
392 #if ENABLE(SHADOW_DOM)
393     return m_scopeResolver && m_scopeResolver->styleSharingCandidateMatchesHostRules(m_state.element());
394 #else
395     return false;
396 #endif
397 }
398
399 bool StyleResolver::classNamesAffectedByRules(const SpaceSplitString& classNames) const
400 {
401     for (unsigned i = 0; i < classNames.size(); ++i) {
402         if (m_ruleSets.features().classesInRules.contains(classNames[i].impl()))
403             return true;
404     }
405     return false;
406 }
407
408 inline void StyleResolver::State::initElement(Element* e)
409 {
410     m_element = e;
411     m_styledElement = e && e->isStyledElement() ? static_cast<StyledElement*>(e) : 0;
412     m_elementLinkState = e ? e->document().visitedLinkState().determineLinkState(e) : NotInsideLink;
413 }
414
415 inline void StyleResolver::initElement(Element* e)
416 {
417     if (m_state.element() != e) {
418         m_state.initElement(e);
419         if (e && e == e->document().documentElement()) {
420             e->document().setDirectionSetOnDocumentElement(false);
421             e->document().setWritingModeSetOnDocumentElement(false);
422         }
423     }
424 }
425
426 inline void StyleResolver::State::initForStyleResolve(Document& document, Element* e, RenderStyle* parentStyle, RenderRegion* regionForStyling)
427 {
428     m_regionForStyling = regionForStyling;
429
430     if (e) {
431         m_parentNode = NodeRenderingTraversal::parent(e);
432         bool resetStyleInheritance = hasShadowRootParent(e) && toShadowRoot(e->parentNode())->resetStyleInheritance();
433         m_parentStyle = resetStyleInheritance ? 0 :
434             parentStyle ? parentStyle :
435             m_parentNode ? m_parentNode->renderStyle() : 0;
436     } else {
437         m_parentNode = 0;
438         m_parentStyle = parentStyle;
439     }
440
441     Node* docElement = e ? e->document().documentElement() : 0;
442     RenderStyle* docStyle = document.renderStyle();
443     m_rootElementStyle = docElement && e != docElement ? docElement->renderStyle() : docStyle;
444
445     m_style = 0;
446     m_pendingImageProperties.clear();
447     m_fontDirty = false;
448 }
449
450 static const unsigned cStyleSearchThreshold = 10;
451 static const unsigned cStyleSearchLevelThreshold = 10;
452
453 static inline bool parentElementPreventsSharing(const Element* parentElement)
454 {
455     if (!parentElement)
456         return false;
457     return parentElement->hasFlagsSetDuringStylingOfChildren();
458 }
459
460 Node* StyleResolver::locateCousinList(Element* parent, unsigned& visitedNodeCount) const
461 {
462     if (visitedNodeCount >= cStyleSearchThreshold * cStyleSearchLevelThreshold)
463         return 0;
464     if (!parent || !parent->isStyledElement())
465         return 0;
466     if (parent->hasScopedHTMLStyleChild())
467         return 0;
468     StyledElement* p = static_cast<StyledElement*>(parent);
469     if (p->inlineStyle())
470         return 0;
471 #if ENABLE(SVG)
472     if (p->isSVGElement() && toSVGElement(p)->animatedSMILStyleProperties())
473         return 0;
474 #endif
475     if (p->hasID() && m_ruleSets.features().idsInRules.contains(p->idForStyleResolution().impl()))
476         return 0;
477
478     RenderStyle* parentStyle = p->renderStyle();
479     unsigned subcount = 0;
480     Node* thisCousin = p;
481     Node* currentNode = p->previousSibling();
482
483     // Reserve the tries for this level. This effectively makes sure that the algorithm
484     // will never go deeper than cStyleSearchLevelThreshold levels into recursion.
485     visitedNodeCount += cStyleSearchThreshold;
486     while (thisCousin) {
487         while (currentNode) {
488             ++subcount;
489             if (currentNode->renderStyle() == parentStyle && currentNode->lastChild()
490                 && currentNode->isElementNode() && !parentElementPreventsSharing(toElement(currentNode))
491 #if ENABLE(SHADOW_DOM)
492                 && !toElement(currentNode)->authorShadowRoot()
493 #endif
494                 ) {
495                 // Adjust for unused reserved tries.
496                 visitedNodeCount -= cStyleSearchThreshold - subcount;
497                 return currentNode->lastChild();
498             }
499             if (subcount >= cStyleSearchThreshold)
500                 return 0;
501             currentNode = currentNode->previousSibling();
502         }
503         currentNode = locateCousinList(thisCousin->parentElement(), visitedNodeCount);
504         thisCousin = currentNode;
505     }
506
507     return 0;
508 }
509
510 bool StyleResolver::styleSharingCandidateMatchesRuleSet(RuleSet* ruleSet)
511 {
512     if (!ruleSet)
513         return false;
514
515     ElementRuleCollector collector(this, m_state);
516     return collector.hasAnyMatchingRules(ruleSet);
517 }
518
519 bool StyleResolver::canShareStyleWithControl(StyledElement* element) const
520 {
521     const State& state = m_state;
522     HTMLInputElement* thisInputElement = element->toInputElement();
523     HTMLInputElement* otherInputElement = state.element()->toInputElement();
524
525     if (!thisInputElement || !otherInputElement)
526         return false;
527
528     if (thisInputElement->elementData() != otherInputElement->elementData()) {
529         if (thisInputElement->fastGetAttribute(typeAttr) != otherInputElement->fastGetAttribute(typeAttr))
530             return false;
531         if (thisInputElement->fastGetAttribute(readonlyAttr) != otherInputElement->fastGetAttribute(readonlyAttr))
532             return false;
533     }
534
535     if (thisInputElement->isAutofilled() != otherInputElement->isAutofilled())
536         return false;
537     if (thisInputElement->shouldAppearChecked() != otherInputElement->shouldAppearChecked())
538         return false;
539     if (thisInputElement->shouldAppearIndeterminate() != otherInputElement->shouldAppearIndeterminate())
540         return false;
541     if (thisInputElement->isRequired() != otherInputElement->isRequired())
542         return false;
543
544     if (element->isDisabledFormControl() != state.element()->isDisabledFormControl())
545         return false;
546
547     if (element->isDefaultButtonForForm() != state.element()->isDefaultButtonForForm())
548         return false;
549
550     if (state.document().containsValidityStyleRules()) {
551         bool willValidate = element->willValidate();
552
553         if (willValidate != state.element()->willValidate())
554             return false;
555
556         if (willValidate && (element->isValidFormControlElement() != state.element()->isValidFormControlElement()))
557             return false;
558
559         if (element->isInRange() != state.element()->isInRange())
560             return false;
561
562         if (element->isOutOfRange() != state.element()->isOutOfRange())
563             return false;
564     }
565
566     return true;
567 }
568
569 static inline bool elementHasDirectionAuto(Element* element)
570 {
571     // FIXME: This line is surprisingly hot, we may wish to inline hasDirectionAuto into StyleResolver.
572     return element->isHTMLElement() && toHTMLElement(element)->hasDirectionAuto();
573 }
574
575 bool StyleResolver::sharingCandidateHasIdenticalStyleAffectingAttributes(StyledElement* sharingCandidate) const
576 {
577     const State& state = m_state;
578     if (state.element()->elementData() == sharingCandidate->elementData())
579         return true;
580     if (state.element()->fastGetAttribute(XMLNames::langAttr) != sharingCandidate->fastGetAttribute(XMLNames::langAttr))
581         return false;
582     if (state.element()->fastGetAttribute(langAttr) != sharingCandidate->fastGetAttribute(langAttr))
583         return false;
584
585     if (!state.elementAffectedByClassRules()) {
586         if (sharingCandidate->hasClass() && classNamesAffectedByRules(sharingCandidate->classNames()))
587             return false;
588     } else if (sharingCandidate->hasClass()) {
589 #if ENABLE(SVG)
590         // SVG elements require a (slow!) getAttribute comparision because "class" is an animatable attribute for SVG.
591         if (state.element()->isSVGElement()) {
592             if (state.element()->getAttribute(classAttr) != sharingCandidate->getAttribute(classAttr))
593                 return false;
594         } else {
595 #endif
596             if (state.element()->classNames() != sharingCandidate->classNames())
597                 return false;
598 #if ENABLE(SVG)
599         }
600 #endif
601     } else
602         return false;
603
604     if (state.styledElement()->presentationAttributeStyle() != sharingCandidate->presentationAttributeStyle())
605         return false;
606
607 #if ENABLE(PROGRESS_ELEMENT)
608     if (state.element()->hasTagName(progressTag)) {
609         if (state.element()->shouldAppearIndeterminate() != sharingCandidate->shouldAppearIndeterminate())
610             return false;
611     }
612 #endif
613
614     return true;
615 }
616
617 bool StyleResolver::canShareStyleWithElement(StyledElement* element) const
618 {
619     RenderStyle* style = element->renderStyle();
620     const State& state = m_state;
621
622     if (!style)
623         return false;
624     if (style->unique())
625         return false;
626     if (style->hasUniquePseudoStyle())
627         return false;
628     if (element->tagQName() != state.element()->tagQName())
629         return false;
630     if (element->inlineStyle())
631         return false;
632     if (element->needsStyleRecalc())
633         return false;
634 #if ENABLE(SVG)
635     if (element->isSVGElement() && toSVGElement(element)->animatedSMILStyleProperties())
636         return false;
637 #endif
638     if (element->isLink() != state.element()->isLink())
639         return false;
640     if (element->hovered() != state.element()->hovered())
641         return false;
642     if (element->active() != state.element()->active())
643         return false;
644     if (element->focused() != state.element()->focused())
645         return false;
646     if (element->shadowPseudoId() != state.element()->shadowPseudoId())
647         return false;
648     if (element == element->document().cssTarget())
649         return false;
650     if (!sharingCandidateHasIdenticalStyleAffectingAttributes(element))
651         return false;
652     if (element->additionalPresentationAttributeStyle() != state.styledElement()->additionalPresentationAttributeStyle())
653         return false;
654
655     if (element->hasID() && m_ruleSets.features().idsInRules.contains(element->idForStyleResolution().impl()))
656         return false;
657     if (element->hasScopedHTMLStyleChild())
658         return false;
659
660     // FIXME: We should share style for option and optgroup whenever possible.
661     // Before doing so, we need to resolve issues in HTMLSelectElement::recalcListItems
662     // and RenderMenuList::setText. See also https://bugs.webkit.org/show_bug.cgi?id=88405
663     if (isHTMLOptionElement(element) || isHTMLOptGroupElement(element))
664         return false;
665
666     bool isControl = element->isFormControlElement();
667
668     if (isControl != state.element()->isFormControlElement())
669         return false;
670
671     if (isControl && !canShareStyleWithControl(element))
672         return false;
673
674     if (style->transitions() || style->animations())
675         return false;
676
677 #if USE(ACCELERATED_COMPOSITING)
678     // Turn off style sharing for elements that can gain layers for reasons outside of the style system.
679     // See comments in RenderObject::setStyle().
680     if (element->hasTagName(iframeTag) || element->hasTagName(frameTag) || element->hasTagName(embedTag) || element->hasTagName(objectTag) || element->hasTagName(appletTag) || element->hasTagName(canvasTag)
681 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
682         // With proxying, the media elements are backed by a RenderEmbeddedObject.
683         || element->hasTagName(videoTag) || isHTMLAudioElement(element)
684 #endif
685         )
686         return false;
687 #endif
688
689     if (elementHasDirectionAuto(element))
690         return false;
691
692     if (element->isLink() && state.elementLinkState() != style->insideLink())
693         return false;
694
695 #if ENABLE(VIDEO_TRACK)
696     // Deny sharing styles between WebVTT and non-WebVTT nodes.
697     if (element->isWebVTTElement() != state.element()->isWebVTTElement())
698         return false;
699
700     if (element->isWebVTTElement() && state.element()->isWebVTTElement() && toWebVTTElement(element)->isPastNode() != toWebVTTElement(state.element())->isPastNode())
701         return false;
702 #endif
703
704 #if ENABLE(FULLSCREEN_API)
705     if (element == element->document().webkitCurrentFullScreenElement() || state.element() == state.document().webkitCurrentFullScreenElement())
706         return false;
707 #endif
708     return true;
709 }
710
711 inline StyledElement* StyleResolver::findSiblingForStyleSharing(Node* node, unsigned& count) const
712 {
713     for (; node; node = node->previousSibling()) {
714         if (!node->isStyledElement())
715             continue;
716         if (canShareStyleWithElement(static_cast<StyledElement*>(node)))
717             break;
718         if (count++ == cStyleSearchThreshold)
719             return 0;
720     }
721     return static_cast<StyledElement*>(node);
722 }
723
724 RenderStyle* StyleResolver::locateSharedStyle()
725 {
726     State& state = m_state;
727     if (!state.styledElement() || !state.parentStyle())
728         return 0;
729
730     // If the element has inline style it is probably unique.
731     if (state.styledElement()->inlineStyle())
732         return 0;
733 #if ENABLE(SVG)
734     if (state.styledElement()->isSVGElement() && toSVGElement(state.styledElement())->animatedSMILStyleProperties())
735         return 0;
736 #endif
737     // Ids stop style sharing if they show up in the stylesheets.
738     if (state.styledElement()->hasID() && m_ruleSets.features().idsInRules.contains(state.styledElement()->idForStyleResolution().impl()))
739         return 0;
740     if (parentElementPreventsSharing(state.element()->parentElement()))
741         return 0;
742     if (state.styledElement()->hasScopedHTMLStyleChild())
743         return 0;
744     if (state.element() == state.document().cssTarget())
745         return 0;
746     if (elementHasDirectionAuto(state.element()))
747         return 0;
748
749     // Cache whether state.element is affected by any known class selectors.
750     // FIXME: This shouldn't be a member variable. The style sharing code could be factored out of StyleResolver.
751     state.setElementAffectedByClassRules(state.element() && state.element()->hasClass() && classNamesAffectedByRules(state.element()->classNames()));
752
753     // Check previous siblings and their cousins.
754     unsigned count = 0;
755     unsigned visitedNodeCount = 0;
756     StyledElement* shareElement = 0;
757     Node* cousinList = state.styledElement()->previousSibling();
758     while (cousinList) {
759         shareElement = findSiblingForStyleSharing(cousinList, count);
760         if (shareElement)
761             break;
762         cousinList = locateCousinList(cousinList->parentElement(), visitedNodeCount);
763     }
764
765     // If we have exhausted all our budget or our cousins.
766     if (!shareElement)
767         return 0;
768
769     // Can't share if sibling rules apply. This is checked at the end as it should rarely fail.
770     if (styleSharingCandidateMatchesRuleSet(m_ruleSets.sibling()))
771         return 0;
772     // Can't share if attribute rules apply.
773     if (styleSharingCandidateMatchesRuleSet(m_ruleSets.uncommonAttribute()))
774         return 0;
775     // Can't share if @host @-rules apply.
776     if (styleSharingCandidateMatchesHostRules())
777         return 0;
778     // Tracking child index requires unique style for each node. This may get set by the sibling rule match above.
779     if (parentElementPreventsSharing(state.element()->parentElement()))
780         return 0;
781     return shareElement->renderStyle();
782 }
783
784 static inline bool isAtShadowBoundary(const Element* element)
785 {
786     if (!element)
787         return false;
788     ContainerNode* parentNode = element->parentNode();
789     return parentNode && parentNode->isShadowRoot();
790 }
791
792 PassRefPtr<RenderStyle> StyleResolver::styleForElement(Element* element, RenderStyle* defaultParent,
793     StyleSharingBehavior sharingBehavior, RuleMatchingBehavior matchingBehavior, RenderRegion* regionForStyling)
794 {
795     // Once an element has a renderer, we don't try to destroy it, since otherwise the renderer
796     // will vanish if a style recalc happens during loading.
797     if (sharingBehavior == AllowStyleSharing && !element->document().haveStylesheetsLoaded() && !element->renderer()) {
798         if (!s_styleNotYetAvailable) {
799             s_styleNotYetAvailable = RenderStyle::create().leakRef();
800             s_styleNotYetAvailable->setDisplay(NONE);
801             s_styleNotYetAvailable->font().update(m_fontSelector);
802         }
803         element->document().setHasNodesWithPlaceholderStyle();
804         return s_styleNotYetAvailable;
805     }
806
807     State& state = m_state;
808     initElement(element);
809     state.initForStyleResolve(document(), element, defaultParent, regionForStyling);
810     if (sharingBehavior == AllowStyleSharing) {
811         RenderStyle* sharedStyle = locateSharedStyle();
812         if (sharedStyle) {
813             state.clear();
814             return sharedStyle;
815         }
816     }
817
818     if (state.parentStyle()) {
819         state.setStyle(RenderStyle::create());
820         state.style()->inheritFrom(state.parentStyle(), isAtShadowBoundary(element) ? RenderStyle::AtShadowBoundary : RenderStyle::NotAtShadowBoundary);
821     } else {
822         state.setStyle(defaultStyleForElement());
823         state.setParentStyle(RenderStyle::clone(state.style()));
824     }
825
826     if (element->isLink()) {
827         state.style()->setIsLink(true);
828         EInsideLink linkState = state.elementLinkState();
829         if (linkState != NotInsideLink) {
830             bool forceVisited = InspectorInstrumentation::forcePseudoState(element, CSSSelector::PseudoVisited);
831             if (forceVisited)
832                 linkState = InsideVisitedLink;
833         }
834         state.style()->setInsideLink(linkState);
835     }
836
837     bool needsCollection = false;
838     CSSDefaultStyleSheets::ensureDefaultStyleSheetsForElement(element, needsCollection);
839     if (needsCollection)
840         m_ruleSets.collectFeatures(document().isViewSource(), m_scopeResolver.get());
841
842     ElementRuleCollector collector(this, state);
843     collector.setRegionForStyling(regionForStyling);
844     collector.setMedium(m_medium.get());
845
846     if (matchingBehavior == MatchOnlyUserAgentRules)
847         collector.matchUARules();
848     else
849         collector.matchAllRules(m_matchAuthorAndUserStyles, matchingBehavior != MatchAllRulesExcludingSMIL);
850
851     applyMatchedProperties(collector.matchedResult(), element);
852
853     // Clean up our style object's display and text decorations (among other fixups).
854     adjustRenderStyle(state.style(), state.parentStyle(), element);
855
856     state.clear(); // Clear out for the next resolve.
857
858     document().didAccessStyleResolver();
859
860     // Now return the style.
861     return state.takeStyle();
862 }
863
864 PassRefPtr<RenderStyle> StyleResolver::styleForKeyframe(const RenderStyle* elementStyle, const StyleKeyframe* keyframe, KeyframeValue& keyframeValue)
865 {
866     MatchResult result;
867     result.addMatchedProperties(keyframe->properties());
868
869     ASSERT(!m_state.style());
870
871     State& state = m_state;
872
873     // Create the style
874     state.setStyle(RenderStyle::clone(elementStyle));
875     state.setLineHeightValue(0);
876
877     // We don't need to bother with !important. Since there is only ever one
878     // decl, there's nothing to override. So just add the first properties.
879     bool inheritedOnly = false;
880     applyMatchedProperties<HighPriorityProperties>(result, false, 0, result.matchedProperties.size() - 1, inheritedOnly);
881
882     // If our font got dirtied, go ahead and update it now.
883     updateFont();
884
885     // Line-height is set when we are sure we decided on the font-size
886     if (state.lineHeightValue())
887         applyProperty(CSSPropertyLineHeight, state.lineHeightValue());
888
889     // Now do rest of the properties.
890     applyMatchedProperties<LowPriorityProperties>(result, false, 0, result.matchedProperties.size() - 1, inheritedOnly);
891
892     // If our font got dirtied by one of the non-essential font props,
893     // go ahead and update it a second time.
894     updateFont();
895
896     // Start loading resources referenced by this style.
897     loadPendingResources();
898     
899     // Add all the animating properties to the keyframe.
900     unsigned propertyCount = keyframe->properties().propertyCount();
901     for (unsigned i = 0; i < propertyCount; ++i) {
902         CSSPropertyID property = keyframe->properties().propertyAt(i).id();
903         // Timing-function within keyframes is special, because it is not animated; it just
904         // describes the timing function between this keyframe and the next.
905         if (property != CSSPropertyWebkitAnimationTimingFunction)
906             keyframeValue.addProperty(property);
907     }
908
909     document().didAccessStyleResolver();
910
911     return state.takeStyle();
912 }
913
914 void StyleResolver::keyframeStylesForAnimation(Element* e, const RenderStyle* elementStyle, KeyframeList& list)
915 {
916     list.clear();
917
918     // Get the keyframesRule for this name
919     if (!e || list.animationName().isEmpty())
920         return;
921
922     m_keyframesRuleMap.checkConsistency();
923
924     KeyframesRuleMap::iterator it = m_keyframesRuleMap.find(list.animationName().impl());
925     if (it == m_keyframesRuleMap.end())
926         return;
927
928     const StyleRuleKeyframes* keyframesRule = it->value.get();
929
930     // Construct and populate the style for each keyframe
931     const Vector<RefPtr<StyleKeyframe> >& keyframes = keyframesRule->keyframes();
932     for (unsigned i = 0; i < keyframes.size(); ++i) {
933         // Apply the declaration to the style. This is a simplified version of the logic in styleForElement
934         initElement(e);
935         m_state.initForStyleResolve(document(), e);
936
937         const StyleKeyframe* keyframe = keyframes[i].get();
938
939         KeyframeValue keyframeValue(0, 0);
940         keyframeValue.setStyle(styleForKeyframe(elementStyle, keyframe, keyframeValue));
941
942         // Add this keyframe style to all the indicated key times
943         Vector<double> keys;
944         keyframe->getKeys(keys);
945         for (size_t keyIndex = 0; keyIndex < keys.size(); ++keyIndex) {
946             keyframeValue.setKey(keys[keyIndex]);
947             list.insert(keyframeValue);
948         }
949     }
950
951     // If the 0% keyframe is missing, create it (but only if there is at least one other keyframe)
952     int initialListSize = list.size();
953     if (initialListSize > 0 && list[0].key()) {
954         static StyleKeyframe* zeroPercentKeyframe;
955         if (!zeroPercentKeyframe) {
956             zeroPercentKeyframe = StyleKeyframe::create(MutableStylePropertySet::create()).leakRef();
957             zeroPercentKeyframe->setKeyText("0%");
958         }
959         KeyframeValue keyframeValue(0, 0);
960         keyframeValue.setStyle(styleForKeyframe(elementStyle, zeroPercentKeyframe, keyframeValue));
961         list.insert(keyframeValue);
962     }
963
964     // If the 100% keyframe is missing, create it (but only if there is at least one other keyframe)
965     if (initialListSize > 0 && (list[list.size() - 1].key() != 1)) {
966         static StyleKeyframe* hundredPercentKeyframe;
967         if (!hundredPercentKeyframe) {
968             hundredPercentKeyframe = StyleKeyframe::create(MutableStylePropertySet::create()).leakRef();
969             hundredPercentKeyframe->setKeyText("100%");
970         }
971         KeyframeValue keyframeValue(1, 0);
972         keyframeValue.setStyle(styleForKeyframe(elementStyle, hundredPercentKeyframe, keyframeValue));
973         list.insert(keyframeValue);
974     }
975 }
976
977 PassRefPtr<RenderStyle> StyleResolver::pseudoStyleForElement(Element* e, const PseudoStyleRequest& pseudoStyleRequest, RenderStyle* parentStyle)
978 {
979     ASSERT(parentStyle);
980     if (!e)
981         return 0;
982
983     State& state = m_state;
984
985     initElement(e);
986
987     state.initForStyleResolve(document(), e, parentStyle);
988
989     if (m_state.parentStyle()) {
990         state.setStyle(RenderStyle::create());
991         state.style()->inheritFrom(m_state.parentStyle());
992     } else {
993         state.setStyle(defaultStyleForElement());
994         state.setParentStyle(RenderStyle::clone(state.style()));
995     }
996
997     // Since we don't use pseudo-elements in any of our quirk/print user agent rules, don't waste time walking
998     // those rules.
999
1000     // Check UA, user and author rules.
1001     ElementRuleCollector collector(this, state);
1002     collector.setPseudoStyleRequest(pseudoStyleRequest);
1003     collector.setMedium(m_medium.get());
1004     collector.matchUARules();
1005
1006     if (m_matchAuthorAndUserStyles) {
1007         collector.matchUserRules(false);
1008         collector.matchAuthorRules(false);
1009     }
1010
1011     if (collector.matchedResult().matchedProperties.isEmpty())
1012         return 0;
1013
1014     state.style()->setStyleType(pseudoStyleRequest.pseudoId);
1015
1016     applyMatchedProperties(collector.matchedResult(), e);
1017
1018     // Clean up our style object's display and text decorations (among other fixups).
1019     adjustRenderStyle(state.style(), m_state.parentStyle(), 0);
1020
1021     // Start loading resources referenced by this style.
1022     loadPendingResources();
1023
1024     document().didAccessStyleResolver();
1025
1026     // Now return the style.
1027     return state.takeStyle();
1028 }
1029
1030 PassRefPtr<RenderStyle> StyleResolver::styleForPage(int pageIndex)
1031 {
1032     m_state.initForStyleResolve(document(), document().documentElement()); // m_rootElementStyle will be set to the document style.
1033
1034     m_state.setStyle(RenderStyle::create());
1035     m_state.style()->inheritFrom(m_state.rootElementStyle());
1036
1037     PageRuleCollector collector(m_state, m_ruleSets);
1038     collector.matchAllPageRules(pageIndex);
1039     m_state.setLineHeightValue(0);
1040     bool inheritedOnly = false;
1041
1042     MatchResult& result = collector.matchedResult();
1043 #if ENABLE(CSS_VARIABLES)
1044     applyMatchedProperties<VariableDefinitions>(result, false, 0, result.matchedProperties.size() - 1, inheritedOnly);
1045 #endif
1046     applyMatchedProperties<HighPriorityProperties>(result, false, 0, result.matchedProperties.size() - 1, inheritedOnly);
1047
1048     // If our font got dirtied, go ahead and update it now.
1049     updateFont();
1050
1051     // Line-height is set when we are sure we decided on the font-size.
1052     if (m_state.lineHeightValue())
1053         applyProperty(CSSPropertyLineHeight, m_state.lineHeightValue());
1054
1055     applyMatchedProperties<LowPriorityProperties>(result, false, 0, result.matchedProperties.size() - 1, inheritedOnly);
1056
1057     // Start loading resources referenced by this style.
1058     loadPendingResources();
1059
1060     document().didAccessStyleResolver();
1061
1062     // Now return the style.
1063     return m_state.takeStyle();
1064 }
1065
1066 PassRefPtr<RenderStyle> StyleResolver::defaultStyleForElement()
1067 {
1068     m_state.setStyle(RenderStyle::create());
1069     // Make sure our fonts are initialized if we don't inherit them from our parent style.
1070     if (Settings* settings = documentSettings()) {
1071         initializeFontStyle(settings);
1072         m_state.style()->font().update(fontSelector());
1073     } else
1074         m_state.style()->font().update(0);
1075
1076     return m_state.takeStyle();
1077 }
1078
1079 static void addIntrinsicMargins(RenderStyle* style)
1080 {
1081     // Intrinsic margin value.
1082     const int intrinsicMargin = 2 * style->effectiveZoom();
1083
1084     // FIXME: Using width/height alone and not also dealing with min-width/max-width is flawed.
1085     // FIXME: Using "quirk" to decide the margin wasn't set is kind of lame.
1086     if (style->width().isIntrinsicOrAuto()) {
1087         if (style->marginLeft().quirk())
1088             style->setMarginLeft(Length(intrinsicMargin, Fixed));
1089         if (style->marginRight().quirk())
1090             style->setMarginRight(Length(intrinsicMargin, Fixed));
1091     }
1092
1093     if (style->height().isAuto()) {
1094         if (style->marginTop().quirk())
1095             style->setMarginTop(Length(intrinsicMargin, Fixed));
1096         if (style->marginBottom().quirk())
1097             style->setMarginBottom(Length(intrinsicMargin, Fixed));
1098     }
1099 }
1100
1101 static EDisplay equivalentBlockDisplay(EDisplay display, bool isFloating, bool strictParsing)
1102 {
1103     switch (display) {
1104     case BLOCK:
1105     case TABLE:
1106     case BOX:
1107     case FLEX:
1108     case GRID:
1109         return display;
1110
1111     case LIST_ITEM:
1112         // It is a WinIE bug that floated list items lose their bullets, so we'll emulate the quirk, but only in quirks mode.
1113         if (!strictParsing && isFloating)
1114             return BLOCK;
1115         return display;
1116     case INLINE_TABLE:
1117         return TABLE;
1118     case INLINE_BOX:
1119         return BOX;
1120     case INLINE_FLEX:
1121         return FLEX;
1122     case INLINE_GRID:
1123         return GRID;
1124
1125     case INLINE:
1126     case RUN_IN:
1127     case COMPACT:
1128     case INLINE_BLOCK:
1129     case TABLE_ROW_GROUP:
1130     case TABLE_HEADER_GROUP:
1131     case TABLE_FOOTER_GROUP:
1132     case TABLE_ROW:
1133     case TABLE_COLUMN_GROUP:
1134     case TABLE_COLUMN:
1135     case TABLE_CELL:
1136     case TABLE_CAPTION:
1137         return BLOCK;
1138     case NONE:
1139         ASSERT_NOT_REACHED();
1140         return NONE;
1141     }
1142     ASSERT_NOT_REACHED();
1143     return BLOCK;
1144 }
1145
1146 // CSS requires text-decoration to be reset at each DOM element for tables, 
1147 // inline blocks, inline tables, run-ins, shadow DOM crossings, floating elements,
1148 // and absolute or relatively positioned elements.
1149 static bool doesNotInheritTextDecoration(RenderStyle* style, Element* e)
1150 {
1151     return style->display() == TABLE || style->display() == INLINE_TABLE || style->display() == RUN_IN
1152         || style->display() == INLINE_BLOCK || style->display() == INLINE_BOX || isAtShadowBoundary(e)
1153         || style->isFloating() || style->hasOutOfFlowPosition();
1154 }
1155
1156 static bool isDisplayFlexibleBox(EDisplay display)
1157 {
1158     return display == FLEX || display == INLINE_FLEX;
1159 }
1160
1161 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
1162 static bool isScrollableOverflow(EOverflow overflow)
1163 {
1164     return overflow == OSCROLL || overflow == OAUTO || overflow == OOVERLAY;
1165 }
1166 #endif
1167
1168 void StyleResolver::adjustRenderStyle(RenderStyle* style, RenderStyle* parentStyle, Element *e)
1169 {
1170     ASSERT(parentStyle);
1171
1172     // Cache our original display.
1173     style->setOriginalDisplay(style->display());
1174
1175     if (style->display() != NONE) {
1176         // If we have a <td> that specifies a float property, in quirks mode we just drop the float
1177         // property.
1178         // Sites also commonly use display:inline/block on <td>s and <table>s. In quirks mode we force
1179         // these tags to retain their display types.
1180         if (document().inQuirksMode() && e) {
1181             if (e->hasTagName(tdTag)) {
1182                 style->setDisplay(TABLE_CELL);
1183                 style->setFloating(NoFloat);
1184             } else if (isHTMLTableElement(e))
1185                 style->setDisplay(style->isDisplayInlineType() ? INLINE_TABLE : TABLE);
1186         }
1187
1188         if (e && (e->hasTagName(tdTag) || e->hasTagName(thTag))) {
1189             if (style->whiteSpace() == KHTML_NOWRAP) {
1190                 // Figure out if we are really nowrapping or if we should just
1191                 // use normal instead. If the width of the cell is fixed, then
1192                 // we don't actually use NOWRAP.
1193                 if (style->width().isFixed())
1194                     style->setWhiteSpace(NORMAL);
1195                 else
1196                     style->setWhiteSpace(NOWRAP);
1197             }
1198         }
1199
1200         // Tables never support the -webkit-* values for text-align and will reset back to the default.
1201         if (e && isHTMLTableElement(e) && (style->textAlign() == WEBKIT_LEFT || style->textAlign() == WEBKIT_CENTER || style->textAlign() == WEBKIT_RIGHT))
1202             style->setTextAlign(TASTART);
1203
1204         // Frames and framesets never honor position:relative or position:absolute. This is necessary to
1205         // fix a crash where a site tries to position these objects. They also never honor display.
1206         if (e && (e->hasTagName(frameTag) || e->hasTagName(framesetTag))) {
1207             style->setPosition(StaticPosition);
1208             style->setDisplay(BLOCK);
1209         }
1210
1211         // Ruby text does not support float or position. This might change with evolution of the specification.
1212         if (e && e->hasTagName(rtTag)) {
1213             style->setPosition(StaticPosition);
1214             style->setFloating(NoFloat);
1215         }
1216
1217         // FIXME: We shouldn't be overriding start/-webkit-auto like this. Do it in html.css instead.
1218         // Table headers with a text-align of -webkit-auto will change the text-align to center.
1219         if (e && e->hasTagName(thTag) && style->textAlign() == TASTART)
1220             style->setTextAlign(CENTER);
1221
1222         if (e && e->hasTagName(legendTag))
1223             style->setDisplay(BLOCK);
1224
1225         // Absolute/fixed positioned elements, floating elements and the document element need block-like outside display.
1226         if (style->hasOutOfFlowPosition() || style->isFloating() || (e && e->document().documentElement() == e))
1227             style->setDisplay(equivalentBlockDisplay(style->display(), style->isFloating(), !document().inQuirksMode()));
1228
1229         // FIXME: Don't support this mutation for pseudo styles like first-letter or first-line, since it's not completely
1230         // clear how that should work.
1231         if (style->display() == INLINE && style->styleType() == NOPSEUDO && style->writingMode() != parentStyle->writingMode())
1232             style->setDisplay(INLINE_BLOCK);
1233
1234         // After performing the display mutation, check table rows. We do not honor position:relative or position:sticky on
1235         // table rows or cells. This has been established for position:relative in CSS2.1 (and caused a crash in containingBlock()
1236         // on some sites).
1237         if ((style->display() == TABLE_HEADER_GROUP || style->display() == TABLE_ROW_GROUP
1238             || style->display() == TABLE_FOOTER_GROUP || style->display() == TABLE_ROW)
1239             && style->hasInFlowPosition())
1240             style->setPosition(StaticPosition);
1241
1242         // writing-mode does not apply to table row groups, table column groups, table rows, and table columns.
1243         // FIXME: Table cells should be allowed to be perpendicular or flipped with respect to the table, though.
1244         if (style->display() == TABLE_COLUMN || style->display() == TABLE_COLUMN_GROUP || style->display() == TABLE_FOOTER_GROUP
1245             || style->display() == TABLE_HEADER_GROUP || style->display() == TABLE_ROW || style->display() == TABLE_ROW_GROUP
1246             || style->display() == TABLE_CELL)
1247             style->setWritingMode(parentStyle->writingMode());
1248
1249         // FIXME: Since we don't support block-flow on flexible boxes yet, disallow setting
1250         // of block-flow to anything other than TopToBottomWritingMode.
1251         // https://bugs.webkit.org/show_bug.cgi?id=46418 - Flexible box support.
1252         if (style->writingMode() != TopToBottomWritingMode && (style->display() == BOX || style->display() == INLINE_BOX))
1253             style->setWritingMode(TopToBottomWritingMode);
1254
1255         if (isDisplayFlexibleBox(parentStyle->display())) {
1256             style->setFloating(NoFloat);
1257             style->setDisplay(equivalentBlockDisplay(style->display(), style->isFloating(), !document().inQuirksMode()));
1258         }
1259     }
1260
1261     // Make sure our z-index value is only applied if the object is positioned.
1262     if (style->position() == StaticPosition && !isDisplayFlexibleBox(parentStyle->display()))
1263         style->setHasAutoZIndex();
1264
1265     // Auto z-index becomes 0 for the root element and transparent objects. This prevents
1266     // cases where objects that should be blended as a single unit end up with a non-transparent
1267     // object wedged in between them. Auto z-index also becomes 0 for objects that specify transforms/masks/reflections.
1268     if (style->hasAutoZIndex() && ((e && e->document().documentElement() == e)
1269         || style->opacity() < 1.0f
1270         || style->hasTransformRelatedProperty()
1271         || style->hasMask()
1272         || style->clipPath()
1273         || style->boxReflect()
1274         || style->hasFilter()
1275         || style->hasBlendMode()
1276         || style->position() == StickyPosition
1277         || (style->position() == FixedPosition && e && e->document().page() && e->document().page()->settings().fixedPositionCreatesStackingContext())
1278         ))
1279         style->setZIndex(0);
1280
1281     // Textarea considers overflow visible as auto.
1282     if (e && isHTMLTextAreaElement(e)) {
1283         style->setOverflowX(style->overflowX() == OVISIBLE ? OAUTO : style->overflowX());
1284         style->setOverflowY(style->overflowY() == OVISIBLE ? OAUTO : style->overflowY());
1285     }
1286
1287     if (doesNotInheritTextDecoration(style, e))
1288         style->setTextDecorationsInEffect(style->textDecoration());
1289     else
1290         style->addToTextDecorationsInEffect(style->textDecoration());
1291
1292     // If either overflow value is not visible, change to auto.
1293     if (style->overflowX() == OMARQUEE && style->overflowY() != OMARQUEE)
1294         style->setOverflowY(OMARQUEE);
1295     else if (style->overflowY() == OMARQUEE && style->overflowX() != OMARQUEE)
1296         style->setOverflowX(OMARQUEE);
1297     else if (style->overflowX() == OVISIBLE && style->overflowY() != OVISIBLE) {
1298         // FIXME: Once we implement pagination controls, overflow-x should default to hidden
1299         // if overflow-y is set to -webkit-paged-x or -webkit-page-y. For now, we'll let it
1300         // default to auto so we can at least scroll through the pages.
1301         style->setOverflowX(OAUTO);
1302     } else if (style->overflowY() == OVISIBLE && style->overflowX() != OVISIBLE)
1303         style->setOverflowY(OAUTO);
1304
1305     // Call setStylesForPaginationMode() if a pagination mode is set for any non-root elements. If these
1306     // styles are specified on a root element, then they will be incorporated in
1307     // Style::createForDocument().
1308     if ((style->overflowY() == OPAGEDX || style->overflowY() == OPAGEDY) && !(e && (e->hasTagName(htmlTag) || e->hasTagName(bodyTag))))
1309         style->setColumnStylesFromPaginationMode(WebCore::paginationModeForRenderStyle(style));
1310
1311     // Table rows, sections and the table itself will support overflow:hidden and will ignore scroll/auto.
1312     // FIXME: Eventually table sections will support auto and scroll.
1313     if (style->display() == TABLE || style->display() == INLINE_TABLE
1314         || style->display() == TABLE_ROW_GROUP || style->display() == TABLE_ROW) {
1315         if (style->overflowX() != OVISIBLE && style->overflowX() != OHIDDEN)
1316             style->setOverflowX(OVISIBLE);
1317         if (style->overflowY() != OVISIBLE && style->overflowY() != OHIDDEN)
1318             style->setOverflowY(OVISIBLE);
1319     }
1320
1321     // Menulists should have visible overflow
1322     if (style->appearance() == MenulistPart) {
1323         style->setOverflowX(OVISIBLE);
1324         style->setOverflowY(OVISIBLE);
1325     }
1326
1327 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
1328     // Touch overflow scrolling creates a stacking context.
1329     if (style->hasAutoZIndex() && style->useTouchOverflowScrolling() && (isScrollableOverflow(style->overflowX()) || isScrollableOverflow(style->overflowY())))
1330         style->setZIndex(0);
1331 #endif
1332
1333     // Cull out any useless layers and also repeat patterns into additional layers.
1334     style->adjustBackgroundLayers();
1335     style->adjustMaskLayers();
1336
1337     // Do the same for animations and transitions.
1338     style->adjustAnimations();
1339     style->adjustTransitions();
1340
1341     // Important: Intrinsic margins get added to controls before the theme has adjusted the style, since the theme will
1342     // alter fonts and heights/widths.
1343     if (e && e->isFormControlElement() && style->fontSize() >= 11) {
1344         // Don't apply intrinsic margins to image buttons. The designer knows how big the images are,
1345         // so we have to treat all image buttons as though they were explicitly sized.
1346         if (!isHTMLInputElement(e) || !toHTMLInputElement(e)->isImageButton())
1347             addIntrinsicMargins(style);
1348     }
1349
1350     // Let the theme also have a crack at adjusting the style.
1351     if (style->hasAppearance())
1352         RenderTheme::defaultTheme()->adjustStyle(this, style, e, m_state.hasUAAppearance(), m_state.borderData(), m_state.backgroundData(), m_state.backgroundColor());
1353
1354     // If we have first-letter pseudo style, do not share this style.
1355     if (style->hasPseudoStyle(FIRST_LETTER))
1356         style->setUnique();
1357
1358     // FIXME: when dropping the -webkit prefix on transform-style, we should also have opacity < 1 cause flattening.
1359     if (style->preserves3D() && (style->overflowX() != OVISIBLE
1360         || style->overflowY() != OVISIBLE
1361         || style->hasFilter()))
1362         style->setTransformStyle3D(TransformStyle3DFlat);
1363
1364     // Seamless iframes behave like blocks. Map their display to inline-block when marked inline.
1365     if (e && e->hasTagName(iframeTag) && style->display() == INLINE && toHTMLIFrameElement(e)->shouldDisplaySeamlessly())
1366         style->setDisplay(INLINE_BLOCK);
1367
1368     adjustGridItemPosition(style);
1369
1370 #if ENABLE(SVG)
1371     if (e && e->isSVGElement()) {
1372         // Spec: http://www.w3.org/TR/SVG/masking.html#OverflowProperty
1373         if (style->overflowY() == OSCROLL)
1374             style->setOverflowY(OHIDDEN);
1375         else if (style->overflowY() == OAUTO)
1376             style->setOverflowY(OVISIBLE);
1377
1378         if (style->overflowX() == OSCROLL)
1379             style->setOverflowX(OHIDDEN);
1380         else if (style->overflowX() == OAUTO)
1381             style->setOverflowX(OVISIBLE);
1382
1383         // Only the root <svg> element in an SVG document fragment tree honors css position
1384         if (!(e->hasTagName(SVGNames::svgTag) && e->parentNode() && !e->parentNode()->isSVGElement()))
1385             style->setPosition(RenderStyle::initialPosition());
1386
1387         // RenderSVGRoot handles zooming for the whole SVG subtree, so foreignObject content should
1388         // not be scaled again.
1389         if (e->hasTagName(SVGNames::foreignObjectTag))
1390             style->setEffectiveZoom(RenderStyle::initialZoom());
1391     }
1392 #endif
1393 }
1394
1395 void StyleResolver::adjustGridItemPosition(RenderStyle* style) const
1396 {
1397     // If opposing grid-placement properties both specify a grid span, they both compute to ‘auto’.
1398     if (style->gridItemColumnStart().isSpan() && style->gridItemColumnEnd().isSpan()) {
1399         style->setGridItemColumnStart(GridPosition());
1400         style->setGridItemColumnEnd(GridPosition());
1401     }
1402
1403     if (style->gridItemRowStart().isSpan() && style->gridItemRowEnd().isSpan()) {
1404         style->setGridItemRowStart(GridPosition());
1405         style->setGridItemRowEnd(GridPosition());
1406     }
1407 }
1408
1409 bool StyleResolver::checkRegionStyle(Element* regionElement)
1410 {
1411     // FIXME (BUG 72472): We don't add @-webkit-region rules of scoped style sheets for the moment,
1412     // so all region rules are global by default. Verify whether that can stand or needs changing.
1413
1414     unsigned rulesSize = m_ruleSets.authorStyle()->regionSelectorsAndRuleSets().size();
1415     for (unsigned i = 0; i < rulesSize; ++i) {
1416         ASSERT(m_ruleSets.authorStyle()->regionSelectorsAndRuleSets().at(i).ruleSet.get());
1417         if (checkRegionSelector(m_ruleSets.authorStyle()->regionSelectorsAndRuleSets().at(i).selector, regionElement))
1418             return true;
1419     }
1420
1421     if (m_ruleSets.userStyle()) {
1422         rulesSize = m_ruleSets.userStyle()->regionSelectorsAndRuleSets().size();
1423         for (unsigned i = 0; i < rulesSize; ++i) {
1424             ASSERT(m_ruleSets.userStyle()->regionSelectorsAndRuleSets().at(i).ruleSet.get());
1425             if (checkRegionSelector(m_ruleSets.userStyle()->regionSelectorsAndRuleSets().at(i).selector, regionElement))
1426                 return true;
1427         }
1428     }
1429
1430     return false;
1431 }
1432
1433 static void checkForOrientationChange(RenderStyle* style)
1434 {
1435     FontOrientation fontOrientation;
1436     NonCJKGlyphOrientation glyphOrientation;
1437     style->getFontAndGlyphOrientation(fontOrientation, glyphOrientation);
1438
1439     const FontDescription& fontDescription = style->fontDescription();
1440     if (fontDescription.orientation() == fontOrientation && fontDescription.nonCJKGlyphOrientation() == glyphOrientation)
1441         return;
1442
1443     FontDescription newFontDescription(fontDescription);
1444     newFontDescription.setNonCJKGlyphOrientation(glyphOrientation);
1445     newFontDescription.setOrientation(fontOrientation);
1446     style->setFontDescription(newFontDescription);
1447 }
1448
1449 void StyleResolver::updateFont()
1450 {
1451     if (!m_state.fontDirty())
1452         return;
1453
1454     RenderStyle* style = m_state.style();
1455     checkForGenericFamilyChange(style, m_state.parentStyle());
1456     checkForZoomChange(style, m_state.parentStyle());
1457     checkForOrientationChange(style);
1458     style->font().update(m_fontSelector);
1459     m_state.setFontDirty(false);
1460 }
1461
1462 Vector<RefPtr<StyleRuleBase> > StyleResolver::styleRulesForElement(Element* e, unsigned rulesToInclude)
1463 {
1464     return pseudoStyleRulesForElement(e, NOPSEUDO, rulesToInclude);
1465 }
1466
1467 Vector<RefPtr<StyleRuleBase> > StyleResolver::pseudoStyleRulesForElement(Element* e, PseudoId pseudoId, unsigned rulesToInclude)
1468 {
1469     if (!e || !e->document().haveStylesheetsLoaded())
1470         return Vector<RefPtr<StyleRuleBase> >();
1471
1472     initElement(e);
1473     m_state.initForStyleResolve(document(), e, 0);
1474
1475     ElementRuleCollector collector(this, m_state);
1476     collector.setMode(SelectorChecker::CollectingRules);
1477     collector.setPseudoStyleRequest(PseudoStyleRequest(pseudoId));
1478     collector.setMedium(m_medium.get());
1479
1480     if (rulesToInclude & UAAndUserCSSRules) {
1481         // First we match rules from the user agent sheet.
1482         collector.matchUARules();
1483         
1484         // Now we check user sheet rules.
1485         if (m_matchAuthorAndUserStyles)
1486             collector.matchUserRules(rulesToInclude & EmptyCSSRules);
1487     }
1488
1489     if (m_matchAuthorAndUserStyles && (rulesToInclude & AuthorCSSRules)) {
1490         collector.setSameOriginOnly(!(rulesToInclude & CrossOriginCSSRules));
1491
1492         // Check the rules in author sheets.
1493         collector.matchAuthorRules(rulesToInclude & EmptyCSSRules);
1494     }
1495
1496     return collector.matchedRuleList();
1497 }
1498
1499 // -------------------------------------------------------------------------------------
1500 // this is mostly boring stuff on how to apply a certain rule to the renderstyle...
1501
1502 Length StyleResolver::convertToIntLength(const CSSPrimitiveValue* primitiveValue, const RenderStyle* style, const RenderStyle* rootStyle, double multiplier)
1503 {
1504     return primitiveValue ? primitiveValue->convertToLength<FixedIntegerConversion | PercentConversion | CalculatedConversion | FractionConversion | ViewportPercentageConversion>(style, rootStyle, multiplier) : Length(Undefined);
1505 }
1506
1507 Length StyleResolver::convertToFloatLength(const CSSPrimitiveValue* primitiveValue, const RenderStyle* style, const RenderStyle* rootStyle, double multiplier)
1508 {
1509     return primitiveValue ? primitiveValue->convertToLength<FixedFloatConversion | PercentConversion | CalculatedConversion | FractionConversion | ViewportPercentageConversion>(style, rootStyle, multiplier) : Length(Undefined);
1510 }
1511
1512 template <StyleResolver::StyleApplicationPass pass>
1513 void StyleResolver::applyProperties(const StylePropertySet* properties, StyleRule* rule, bool isImportant, bool inheritedOnly, PropertyWhitelistType propertyWhitelistType)
1514 {
1515     ASSERT((propertyWhitelistType != PropertyWhitelistRegion) || m_state.regionForStyling());
1516     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willProcessRule(&document(), rule, *this);
1517
1518     unsigned propertyCount = properties->propertyCount();
1519     for (unsigned i = 0; i < propertyCount; ++i) {
1520         StylePropertySet::PropertyReference current = properties->propertyAt(i);
1521         if (isImportant != current.isImportant())
1522             continue;
1523         if (inheritedOnly && !current.isInherited()) {
1524             // If the property value is explicitly inherited, we need to apply further non-inherited properties
1525             // as they might override the value inherited here. For this reason we don't allow declarations with
1526             // explicitly inherited properties to be cached.
1527             ASSERT(!current.value()->isInheritedValue());
1528             continue;
1529         }
1530         CSSPropertyID property = current.id();
1531
1532         if (propertyWhitelistType == PropertyWhitelistRegion && !StyleResolver::isValidRegionStyleProperty(property))
1533             continue;
1534 #if ENABLE(VIDEO_TRACK)
1535         if (propertyWhitelistType == PropertyWhitelistCue && !StyleResolver::isValidCueStyleProperty(property))
1536             continue;
1537 #endif
1538         switch (pass) {
1539 #if ENABLE(CSS_VARIABLES)
1540         case VariableDefinitions:
1541             COMPILE_ASSERT(CSSPropertyVariable < firstCSSProperty, CSS_variable_is_before_first_property);
1542             if (property == CSSPropertyVariable)
1543                 applyProperty(current.id(), current.value());
1544             break;
1545 #endif
1546         case HighPriorityProperties:
1547             COMPILE_ASSERT(firstCSSProperty == CSSPropertyColor, CSS_color_is_first_property);
1548             COMPILE_ASSERT(CSSPropertyZoom == CSSPropertyColor + 17, CSS_zoom_is_end_of_first_prop_range);
1549             COMPILE_ASSERT(CSSPropertyLineHeight == CSSPropertyZoom + 1, CSS_line_height_is_after_zoom);
1550 #if ENABLE(CSS_VARIABLES)
1551             if (property == CSSPropertyVariable)
1552                 continue;
1553 #endif
1554             // give special priority to font-xxx, color properties, etc
1555             if (property < CSSPropertyLineHeight)
1556                 applyProperty(current.id(), current.value());
1557             // we apply line-height later
1558             else if (property == CSSPropertyLineHeight)
1559                 m_state.setLineHeightValue(current.value());
1560             break;
1561         case LowPriorityProperties:
1562             if (property > CSSPropertyLineHeight)
1563                 applyProperty(current.id(), current.value());
1564         }
1565     }
1566     InspectorInstrumentation::didProcessRule(cookie);
1567 }
1568
1569 template <StyleResolver::StyleApplicationPass pass>
1570 void StyleResolver::applyMatchedProperties(const MatchResult& matchResult, bool isImportant, int startIndex, int endIndex, bool inheritedOnly)
1571 {
1572     if (startIndex == -1)
1573         return;
1574
1575     State& state = m_state;
1576     if (state.style()->insideLink() != NotInsideLink) {
1577         for (int i = startIndex; i <= endIndex; ++i) {
1578             const MatchedProperties& matchedProperties = matchResult.matchedProperties[i];
1579             unsigned linkMatchType = matchedProperties.linkMatchType;
1580             // FIXME: It would be nicer to pass these as arguments but that requires changes in many places.
1581             state.setApplyPropertyToRegularStyle(linkMatchType & SelectorChecker::MatchLink);
1582             state.setApplyPropertyToVisitedLinkStyle(linkMatchType & SelectorChecker::MatchVisited);
1583
1584             applyProperties<pass>(matchedProperties.properties.get(), matchResult.matchedRules[i], isImportant, inheritedOnly, static_cast<PropertyWhitelistType>(matchedProperties.whitelistType));
1585         }
1586         state.setApplyPropertyToRegularStyle(true);
1587         state.setApplyPropertyToVisitedLinkStyle(false);
1588         return;
1589     }
1590     for (int i = startIndex; i <= endIndex; ++i) {
1591         const MatchedProperties& matchedProperties = matchResult.matchedProperties[i];
1592         applyProperties<pass>(matchedProperties.properties.get(), matchResult.matchedRules[i], isImportant, inheritedOnly, static_cast<PropertyWhitelistType>(matchedProperties.whitelistType));
1593     }
1594 }
1595
1596 unsigned StyleResolver::computeMatchedPropertiesHash(const MatchedProperties* properties, unsigned size)
1597 {
1598     
1599     return StringHasher::hashMemory(properties, sizeof(MatchedProperties) * size);
1600 }
1601
1602 bool operator==(const StyleResolver::MatchRanges& a, const StyleResolver::MatchRanges& b)
1603 {
1604     return a.firstUARule == b.firstUARule
1605         && a.lastUARule == b.lastUARule
1606         && a.firstAuthorRule == b.firstAuthorRule
1607         && a.lastAuthorRule == b.lastAuthorRule
1608         && a.firstUserRule == b.firstUserRule
1609         && a.lastUserRule == b.lastUserRule;
1610 }
1611
1612 bool operator!=(const StyleResolver::MatchRanges& a, const StyleResolver::MatchRanges& b)
1613 {
1614     return !(a == b);
1615 }
1616
1617 bool operator==(const StyleResolver::MatchedProperties& a, const StyleResolver::MatchedProperties& b)
1618 {
1619     return a.properties == b.properties && a.linkMatchType == b.linkMatchType;
1620 }
1621
1622 bool operator!=(const StyleResolver::MatchedProperties& a, const StyleResolver::MatchedProperties& b)
1623 {
1624     return !(a == b);
1625 }
1626
1627 const StyleResolver::MatchedPropertiesCacheItem* StyleResolver::findFromMatchedPropertiesCache(unsigned hash, const MatchResult& matchResult)
1628 {
1629     ASSERT(hash);
1630
1631     MatchedPropertiesCache::iterator it = m_matchedPropertiesCache.find(hash);
1632     if (it == m_matchedPropertiesCache.end())
1633         return 0;
1634     MatchedPropertiesCacheItem& cacheItem = it->value;
1635
1636     size_t size = matchResult.matchedProperties.size();
1637     if (size != cacheItem.matchedProperties.size())
1638         return 0;
1639     for (size_t i = 0; i < size; ++i) {
1640         if (matchResult.matchedProperties[i] != cacheItem.matchedProperties[i])
1641             return 0;
1642     }
1643     if (cacheItem.ranges != matchResult.ranges)
1644         return 0;
1645     return &cacheItem;
1646 }
1647
1648 void StyleResolver::addToMatchedPropertiesCache(const RenderStyle* style, const RenderStyle* parentStyle, unsigned hash, const MatchResult& matchResult)
1649 {
1650     static const unsigned matchedDeclarationCacheAdditionsBetweenSweeps = 100;
1651     if (++m_matchedPropertiesCacheAdditionsSinceLastSweep >= matchedDeclarationCacheAdditionsBetweenSweeps
1652         && !m_matchedPropertiesCacheSweepTimer.isActive()) {
1653         static const unsigned matchedDeclarationCacheSweepTimeInSeconds = 60;
1654         m_matchedPropertiesCacheSweepTimer.startOneShot(matchedDeclarationCacheSweepTimeInSeconds);
1655     }
1656
1657     ASSERT(hash);
1658     MatchedPropertiesCacheItem cacheItem;
1659     cacheItem.matchedProperties.appendVector(matchResult.matchedProperties);
1660     cacheItem.ranges = matchResult.ranges;
1661     // Note that we don't cache the original RenderStyle instance. It may be further modified.
1662     // The RenderStyle in the cache is really just a holder for the substructures and never used as-is.
1663     cacheItem.renderStyle = RenderStyle::clone(style);
1664     cacheItem.parentRenderStyle = RenderStyle::clone(parentStyle);
1665     m_matchedPropertiesCache.add(hash, cacheItem);
1666 }
1667
1668 void StyleResolver::invalidateMatchedPropertiesCache()
1669 {
1670     m_matchedPropertiesCache.clear();
1671 }
1672
1673 static bool isCacheableInMatchedPropertiesCache(const Element* element, const RenderStyle* style, const RenderStyle* parentStyle)
1674 {
1675     // FIXME: CSSPropertyWebkitWritingMode modifies state when applying to document element. We can't skip the applying by caching.
1676     if (element == element->document().documentElement() && element->document().writingModeSetOnDocumentElement())
1677         return false;
1678     if (style->unique() || (style->styleType() != NOPSEUDO && parentStyle->unique()))
1679         return false;
1680     if (style->hasAppearance())
1681         return false;
1682     if (style->zoom() != RenderStyle::initialZoom())
1683         return false;
1684     if (style->writingMode() != RenderStyle::initialWritingMode())
1685         return false;
1686     // The cache assumes static knowledge about which properties are inherited.
1687     if (parentStyle->hasExplicitlyInheritedProperties())
1688         return false;
1689     return true;
1690 }
1691
1692 void StyleResolver::applyMatchedProperties(const MatchResult& matchResult, const Element* element)
1693 {
1694     ASSERT(element);
1695     State& state = m_state;
1696     unsigned cacheHash = matchResult.isCacheable ? computeMatchedPropertiesHash(matchResult.matchedProperties.data(), matchResult.matchedProperties.size()) : 0;
1697     bool applyInheritedOnly = false;
1698     const MatchedPropertiesCacheItem* cacheItem = 0;
1699     if (cacheHash && (cacheItem = findFromMatchedPropertiesCache(cacheHash, matchResult))) {
1700         // We can build up the style by copying non-inherited properties from an earlier style object built using the same exact
1701         // style declarations. We then only need to apply the inherited properties, if any, as their values can depend on the 
1702         // element context. This is fast and saves memory by reusing the style data structures.
1703         state.style()->copyNonInheritedFrom(cacheItem->renderStyle.get());
1704         if (state.parentStyle()->inheritedDataShared(cacheItem->parentRenderStyle.get()) && !isAtShadowBoundary(element)) {
1705             EInsideLink linkStatus = state.style()->insideLink();
1706             // If the cache item parent style has identical inherited properties to the current parent style then the
1707             // resulting style will be identical too. We copy the inherited properties over from the cache and are done.
1708             state.style()->inheritFrom(cacheItem->renderStyle.get());
1709
1710             // Unfortunately the link status is treated like an inherited property. We need to explicitly restore it.
1711             state.style()->setInsideLink(linkStatus);
1712             return;
1713         }
1714         applyInheritedOnly = true; 
1715     }
1716
1717 #if ENABLE(CSS_VARIABLES)
1718     // First apply all variable definitions, as they may be used during application of later properties.
1719     applyMatchedProperties<VariableDefinitions>(matchResult, false, 0, matchResult.matchedProperties.size() - 1, applyInheritedOnly);
1720     applyMatchedProperties<VariableDefinitions>(matchResult, true, matchResult.ranges.firstAuthorRule, matchResult.ranges.lastAuthorRule, applyInheritedOnly);
1721     applyMatchedProperties<VariableDefinitions>(matchResult, true, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, applyInheritedOnly);
1722     applyMatchedProperties<VariableDefinitions>(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
1723 #endif
1724
1725     // Now we have all of the matched rules in the appropriate order. Walk the rules and apply
1726     // high-priority properties first, i.e., those properties that other properties depend on.
1727     // The order is (1) high-priority not important, (2) high-priority important, (3) normal not important
1728     // and (4) normal important.
1729     state.setLineHeightValue(0);
1730     applyMatchedProperties<HighPriorityProperties>(matchResult, false, 0, matchResult.matchedProperties.size() - 1, applyInheritedOnly);
1731     applyMatchedProperties<HighPriorityProperties>(matchResult, true, matchResult.ranges.firstAuthorRule, matchResult.ranges.lastAuthorRule, applyInheritedOnly);
1732     applyMatchedProperties<HighPriorityProperties>(matchResult, true, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, applyInheritedOnly);
1733     applyMatchedProperties<HighPriorityProperties>(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
1734
1735     if (cacheItem && cacheItem->renderStyle->effectiveZoom() != state.style()->effectiveZoom()) {
1736         state.setFontDirty(true);
1737         applyInheritedOnly = false;
1738     }
1739
1740     // If our font got dirtied, go ahead and update it now.
1741     updateFont();
1742
1743     // Line-height is set when we are sure we decided on the font-size.
1744     if (state.lineHeightValue())
1745         applyProperty(CSSPropertyLineHeight, state.lineHeightValue());
1746
1747     // Many properties depend on the font. If it changes we just apply all properties.
1748     if (cacheItem && cacheItem->renderStyle->fontDescription() != state.style()->fontDescription())
1749         applyInheritedOnly = false;
1750
1751     // Now do the normal priority UA properties.
1752     applyMatchedProperties<LowPriorityProperties>(matchResult, false, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
1753     
1754     // Cache our border and background so that we can examine them later.
1755     state.cacheBorderAndBackground();
1756     
1757     // Now do the author and user normal priority properties and all the !important properties.
1758     applyMatchedProperties<LowPriorityProperties>(matchResult, false, matchResult.ranges.lastUARule + 1, matchResult.matchedProperties.size() - 1, applyInheritedOnly);
1759     applyMatchedProperties<LowPriorityProperties>(matchResult, true, matchResult.ranges.firstAuthorRule, matchResult.ranges.lastAuthorRule, applyInheritedOnly);
1760     applyMatchedProperties<LowPriorityProperties>(matchResult, true, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, applyInheritedOnly);
1761     applyMatchedProperties<LowPriorityProperties>(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
1762    
1763     // Start loading resources referenced by this style.
1764     loadPendingResources();
1765     
1766     ASSERT(!state.fontDirty());
1767     
1768     if (cacheItem || !cacheHash)
1769         return;
1770     if (!isCacheableInMatchedPropertiesCache(state.element(), state.style(), state.parentStyle()))
1771         return;
1772     addToMatchedPropertiesCache(state.style(), state.parentStyle(), cacheHash, matchResult);
1773 }
1774
1775 void StyleResolver::applyPropertyToStyle(CSSPropertyID id, CSSValue* value, RenderStyle* style)
1776 {
1777     initElement(0);
1778     m_state.initForStyleResolve(document(), 0, style);
1779     m_state.setStyle(style);
1780     applyPropertyToCurrentStyle(id, value);
1781 }
1782
1783 void StyleResolver::applyPropertyToCurrentStyle(CSSPropertyID id, CSSValue* value)
1784 {
1785     if (value)
1786         applyProperty(id, value);
1787 }
1788
1789 inline bool isValidVisitedLinkProperty(CSSPropertyID id)
1790 {
1791     switch (id) {
1792     case CSSPropertyBackgroundColor:
1793     case CSSPropertyBorderLeftColor:
1794     case CSSPropertyBorderRightColor:
1795     case CSSPropertyBorderTopColor:
1796     case CSSPropertyBorderBottomColor:
1797     case CSSPropertyColor:
1798     case CSSPropertyOutlineColor:
1799     case CSSPropertyWebkitColumnRuleColor:
1800 #if ENABLE(CSS3_TEXT)
1801     case CSSPropertyWebkitTextDecorationColor:
1802 #endif // CSS3_TEXT
1803     case CSSPropertyWebkitTextEmphasisColor:
1804     case CSSPropertyWebkitTextFillColor:
1805     case CSSPropertyWebkitTextStrokeColor:
1806 #if ENABLE(SVG)
1807     case CSSPropertyFill:
1808     case CSSPropertyStroke:
1809 #endif
1810         return true;
1811     default:
1812         break;
1813     }
1814
1815     return false;
1816 }
1817
1818 // http://dev.w3.org/csswg/css3-regions/#the-at-region-style-rule
1819 // FIXME: add incremental support for other region styling properties.
1820 inline bool StyleResolver::isValidRegionStyleProperty(CSSPropertyID id)
1821 {
1822     switch (id) {
1823     case CSSPropertyBackgroundColor:
1824     case CSSPropertyColor:
1825         return true;
1826     default:
1827         break;
1828     }
1829
1830     return false;
1831 }
1832
1833 #if ENABLE(VIDEO_TRACK)
1834 inline bool StyleResolver::isValidCueStyleProperty(CSSPropertyID id)
1835 {
1836     switch (id) {
1837     case CSSPropertyBackground:
1838     case CSSPropertyBackgroundAttachment:
1839     case CSSPropertyBackgroundClip:
1840     case CSSPropertyBackgroundColor:
1841     case CSSPropertyBackgroundImage:
1842     case CSSPropertyBackgroundOrigin:
1843     case CSSPropertyBackgroundPosition:
1844     case CSSPropertyBackgroundPositionX:
1845     case CSSPropertyBackgroundPositionY:
1846     case CSSPropertyBackgroundRepeat:
1847     case CSSPropertyBackgroundRepeatX:
1848     case CSSPropertyBackgroundRepeatY:
1849     case CSSPropertyBackgroundSize:
1850     case CSSPropertyColor:
1851     case CSSPropertyFont:
1852     case CSSPropertyFontFamily:
1853     case CSSPropertyFontSize:
1854     case CSSPropertyFontStyle:
1855     case CSSPropertyFontVariant:
1856     case CSSPropertyFontWeight:
1857     case CSSPropertyLineHeight:
1858     case CSSPropertyOpacity:
1859     case CSSPropertyOutline:
1860     case CSSPropertyOutlineColor:
1861     case CSSPropertyOutlineOffset:
1862     case CSSPropertyOutlineStyle:
1863     case CSSPropertyOutlineWidth:
1864     case CSSPropertyVisibility:
1865     case CSSPropertyWhiteSpace:
1866     case CSSPropertyTextDecoration:
1867     case CSSPropertyTextShadow:
1868     case CSSPropertyBorderStyle:
1869         return true;
1870     default:
1871         break;
1872     }
1873     return false;
1874 }
1875 #endif
1876 // SVG handles zooming in a different way compared to CSS. The whole document is scaled instead
1877 // of each individual length value in the render style / tree. CSSPrimitiveValue::computeLength*()
1878 // multiplies each resolved length with the zoom multiplier - so for SVG we need to disable that.
1879 // Though all CSS values that can be applied to outermost <svg> elements (width/height/border/padding...)
1880 // need to respect the scaling. RenderBox (the parent class of RenderSVGRoot) grabs values like
1881 // width/height/border/padding/... from the RenderStyle -> for SVG these values would never scale,
1882 // if we'd pass a 1.0 zoom factor everyhwere. So we only pass a zoom factor of 1.0 for specific
1883 // properties that are NOT allowed to scale within a zoomed SVG document (letter/word-spacing/font-size).
1884 bool StyleResolver::useSVGZoomRules()
1885 {
1886     return m_state.element() && m_state.element()->isSVGElement();
1887 }
1888
1889 static bool createGridTrackBreadth(CSSPrimitiveValue* primitiveValue, const StyleResolver::State& state, Length& workingLength)
1890 {
1891     if (primitiveValue->getValueID() == CSSValueWebkitMinContent) {
1892         workingLength = Length(MinContent);
1893         return true;
1894     }
1895
1896     if (primitiveValue->getValueID() == CSSValueWebkitMaxContent) {
1897         workingLength = Length(MaxContent);
1898         return true;
1899     }
1900
1901     workingLength = primitiveValue->convertToLength<FixedIntegerConversion | PercentConversion | ViewportPercentageConversion | AutoConversion>(state.style(), state.rootElementStyle(), state.style()->effectiveZoom());
1902     if (workingLength.isUndefined())
1903         return false;
1904
1905     if (primitiveValue->isLength())
1906         workingLength.setQuirk(primitiveValue->isQuirkValue());
1907
1908     return true;
1909 }
1910
1911 static bool createGridTrackSize(CSSValue* value, GridTrackSize& trackSize, const StyleResolver::State& state)
1912 {
1913     if (!value->isPrimitiveValue())
1914         return false;
1915
1916     CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(value);
1917     Pair* minMaxTrackBreadth = primitiveValue->getPairValue();
1918     if (!minMaxTrackBreadth) {
1919         Length workingLength;
1920         if (!createGridTrackBreadth(primitiveValue, state, workingLength))
1921             return false;
1922
1923         trackSize.setLength(workingLength);
1924         return true;
1925     }
1926
1927     Length minTrackBreadth;
1928     Length maxTrackBreadth;
1929     if (!createGridTrackBreadth(minMaxTrackBreadth->first(), state, minTrackBreadth) || !createGridTrackBreadth(minMaxTrackBreadth->second(), state, maxTrackBreadth))
1930         return false;
1931
1932     trackSize.setMinMax(minTrackBreadth, maxTrackBreadth);
1933     return true;
1934 }
1935
1936 static bool createGridTrackList(CSSValue* value, Vector<GridTrackSize>& trackSizes, NamedGridLinesMap& namedGridLines, const StyleResolver::State& state)
1937 {
1938     // Handle 'none'.
1939     if (value->isPrimitiveValue()) {
1940         CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(value);
1941         return primitiveValue->getValueID() == CSSValueNone;
1942     }
1943
1944     if (!value->isValueList())
1945         return false;
1946
1947     size_t currentNamedGridLine = 0;
1948     for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
1949         CSSValue* currValue = i.value();
1950         if (currValue->isPrimitiveValue()) {
1951             CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(currValue);
1952             if (primitiveValue->isString()) {
1953                 NamedGridLinesMap::AddResult result = namedGridLines.add(primitiveValue->getStringValue(), Vector<size_t>());
1954                 result.iterator->value.append(currentNamedGridLine);
1955                 continue;
1956             }
1957         }
1958
1959         ++currentNamedGridLine;
1960         GridTrackSize trackSize;
1961         if (!createGridTrackSize(currValue, trackSize, state))
1962             return false;
1963
1964         trackSizes.append(trackSize);
1965     }
1966
1967     if (trackSizes.isEmpty())
1968         return false;
1969
1970     return true;
1971 }
1972
1973
1974 static bool createGridPosition(CSSValue* value, GridPosition& position)
1975 {
1976     // For now, we only accept: 'auto' | <integer> | span && <integer>?
1977     if (value->isPrimitiveValue()) {
1978         CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(value);
1979         if (primitiveValue->getValueID() == CSSValueAuto)
1980             return true;
1981
1982         if (primitiveValue->getValueID() == CSSValueSpan) {
1983             // If the <integer> is omitted, it defaults to '1'.
1984             position.setSpanPosition(1);
1985             return true;
1986         }
1987
1988         ASSERT(primitiveValue->isNumber());
1989         position.setIntegerPosition(primitiveValue->getIntValue());
1990         return true;
1991     }
1992
1993     ASSERT_WITH_SECURITY_IMPLICATION(value->isValueList());
1994     CSSValueList* values = static_cast<CSSValueList*>(value);
1995     ASSERT(values->length() == 2);
1996     ASSERT_WITH_SECURITY_IMPLICATION(values->itemWithoutBoundsCheck(1)->isPrimitiveValue());
1997     CSSPrimitiveValue* numericValue = static_cast<CSSPrimitiveValue*>(values->itemWithoutBoundsCheck(1));
1998     ASSERT(numericValue->isNumber());
1999     position.setSpanPosition(numericValue->getIntValue());
2000     return true;
2001 }
2002
2003 #if ENABLE(CSS_VARIABLES)
2004 static bool hasVariableReference(CSSValue* value)
2005 {
2006     if (value->isPrimitiveValue()) {
2007         CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(value);
2008         return primitiveValue->hasVariableReference();
2009     }
2010
2011     if (value->isCalculationValue())
2012         return static_cast<CSSCalcValue*>(value)->hasVariableReference();
2013
2014     if (value->isReflectValue()) {
2015         CSSReflectValue* reflectValue = static_cast<CSSReflectValue*>(value);
2016         CSSPrimitiveValue* direction = reflectValue->direction();
2017         CSSPrimitiveValue* offset = reflectValue->offset();
2018         CSSValue* mask = reflectValue->mask();
2019         return (direction && hasVariableReference(direction)) || (offset && hasVariableReference(offset)) || (mask && hasVariableReference(mask));
2020     }
2021
2022     for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
2023         if (hasVariableReference(i.value()))
2024             return true;
2025     }
2026
2027     return false;
2028 }
2029
2030 void StyleResolver::resolveVariables(CSSPropertyID id, CSSValue* value, Vector<std::pair<CSSPropertyID, String> >& knownExpressions)
2031 {
2032     std::pair<CSSPropertyID, String> expression(id, value->serializeResolvingVariables(*m_state.style()->variables()));
2033
2034     if (knownExpressions.contains(expression))
2035         return; // cycle detected.
2036
2037     knownExpressions.append(expression);
2038
2039     // FIXME: It would be faster not to re-parse from strings, but for now CSS property validation lives inside the parser so we do it there.
2040     RefPtr<MutableStylePropertySet> resultSet = MutableStylePropertySet::create();
2041     if (!CSSParser::parseValue(resultSet.get(), id, expression.second, false, &document()))
2042         return; // expression failed to parse.
2043
2044     for (unsigned i = 0; i < resultSet->propertyCount(); i++) {
2045         StylePropertySet::PropertyReference property = resultSet->propertyAt(i);
2046         if (property.id() != CSSPropertyVariable && hasVariableReference(property.value()))
2047             resolveVariables(property.id(), property.value(), knownExpressions);
2048         else
2049             applyProperty(property.id(), property.value());
2050     }
2051 }
2052 #endif
2053
2054 void StyleResolver::applyProperty(CSSPropertyID id, CSSValue* value)
2055 {
2056 #if ENABLE(CSS_VARIABLES)
2057     if (id != CSSPropertyVariable && hasVariableReference(value)) {
2058         Vector<std::pair<CSSPropertyID, String> > knownExpressions;
2059         resolveVariables(id, value, knownExpressions);
2060         return;
2061     }
2062 #endif
2063
2064     // CSS variables don't resolve shorthands at parsing time, so this should be *after* handling variables.
2065     ASSERT_WITH_MESSAGE(!isExpandedShorthand(id), "Shorthand property id = %d wasn't expanded at parsing time", id);
2066
2067     State& state = m_state;
2068     bool isInherit = state.parentNode() && value->isInheritedValue();
2069     bool isInitial = value->isInitialValue() || (!state.parentNode() && value->isInheritedValue());
2070
2071     ASSERT(!isInherit || !isInitial); // isInherit -> !isInitial && isInitial -> !isInherit
2072     ASSERT(!isInherit || (state.parentNode() && state.parentStyle())); // isInherit -> (state.parentNode() && state.parentStyle())
2073
2074     if (!state.applyPropertyToRegularStyle() && (!state.applyPropertyToVisitedLinkStyle() || !isValidVisitedLinkProperty(id))) {
2075         // Limit the properties that can be applied to only the ones honored by :visited.
2076         return;
2077     }
2078
2079     if (isInherit && !state.parentStyle()->hasExplicitlyInheritedProperties() && !CSSProperty::isInheritedProperty(id))
2080         state.parentStyle()->setHasExplicitlyInheritedProperties();
2081
2082 #if ENABLE(CSS_VARIABLES)
2083     if (id == CSSPropertyVariable) {
2084         ASSERT_WITH_SECURITY_IMPLICATION(value->isVariableValue());
2085         CSSVariableValue* variable = static_cast<CSSVariableValue*>(value);
2086         ASSERT(!variable->name().isEmpty());
2087         ASSERT(!variable->value().isEmpty());
2088         state.style()->setVariable(variable->name(), variable->value());
2089         return;
2090     }
2091 #endif
2092
2093     // Check lookup table for implementations and use when available.
2094     const PropertyHandler& handler = m_deprecatedStyleBuilder.propertyHandler(id);
2095     if (handler.isValid()) {
2096         if (isInherit)
2097             handler.applyInheritValue(id, this);
2098         else if (isInitial)
2099             handler.applyInitialValue(id, this);
2100         else
2101             handler.applyValue(id, this, value);
2102         return;
2103     }
2104
2105     CSSPrimitiveValue* primitiveValue = value->isPrimitiveValue() ? static_cast<CSSPrimitiveValue*>(value) : 0;
2106
2107     float zoomFactor = state.style()->effectiveZoom();
2108
2109     // What follows is a list that maps the CSS properties into their corresponding front-end
2110     // RenderStyle values.
2111     switch (id) {
2112     // lists
2113     case CSSPropertyContent:
2114         // list of string, uri, counter, attr, i
2115         {
2116             // FIXME: In CSS3, it will be possible to inherit content. In CSS2 it is not. This
2117             // note is a reminder that eventually "inherit" needs to be supported.
2118
2119             if (isInitial) {
2120                 state.style()->clearContent();
2121                 return;
2122             }
2123
2124             if (!value->isValueList())
2125                 return;
2126
2127             bool didSet = false;
2128             for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
2129                 CSSValue* item = i.value();
2130                 if (item->isImageGeneratorValue()) {
2131                     if (item->isGradientValue())
2132                         state.style()->setContent(StyleGeneratedImage::create(static_cast<CSSGradientValue*>(item)->gradientWithStylesResolved(this).get()), didSet);
2133                     else
2134                         state.style()->setContent(StyleGeneratedImage::create(static_cast<CSSImageGeneratorValue*>(item)), didSet);
2135                     didSet = true;
2136 #if ENABLE(CSS_IMAGE_SET)
2137                 } else if (item->isImageSetValue()) {
2138                     state.style()->setContent(setOrPendingFromValue(CSSPropertyContent, static_cast<CSSImageSetValue*>(item)), didSet);
2139                     didSet = true;
2140 #endif
2141                 }
2142
2143                 if (item->isImageValue()) {
2144                     state.style()->setContent(cachedOrPendingFromValue(CSSPropertyContent, static_cast<CSSImageValue*>(item)), didSet);
2145                     didSet = true;
2146                     continue;
2147                 }
2148
2149                 if (!item->isPrimitiveValue())
2150                     continue;
2151
2152                 CSSPrimitiveValue* contentValue = static_cast<CSSPrimitiveValue*>(item);
2153
2154                 if (contentValue->isString()) {
2155                     state.style()->setContent(contentValue->getStringValue().impl(), didSet);
2156                     didSet = true;
2157                 } else if (contentValue->isAttr()) {
2158                     // FIXME: Can a namespace be specified for an attr(foo)?
2159                     if (state.style()->styleType() == NOPSEUDO)
2160                         state.style()->setUnique();
2161                     else
2162                         state.parentStyle()->setUnique();
2163                     QualifiedName attr(nullAtom, contentValue->getStringValue().impl(), nullAtom);
2164                     const AtomicString& value = state.element()->getAttribute(attr);
2165                     state.style()->setContent(value.isNull() ? emptyAtom : value.impl(), didSet);
2166                     didSet = true;
2167                     // register the fact that the attribute value affects the style
2168                     m_ruleSets.features().attrsInRules.add(attr.localName().impl());
2169                 } else if (contentValue->isCounter()) {
2170                     Counter* counterValue = contentValue->getCounterValue();
2171                     EListStyleType listStyleType = NoneListStyle;
2172                     CSSValueID listStyleIdent = counterValue->listStyleIdent();
2173                     if (listStyleIdent != CSSValueNone)
2174                         listStyleType = static_cast<EListStyleType>(listStyleIdent - CSSValueDisc);
2175                     OwnPtr<CounterContent> counter = adoptPtr(new CounterContent(counterValue->identifier(), listStyleType, counterValue->separator()));
2176                     state.style()->setContent(counter.release(), didSet);
2177                     didSet = true;
2178                 } else {
2179                     switch (contentValue->getValueID()) {
2180                     case CSSValueOpenQuote:
2181                         state.style()->setContent(OPEN_QUOTE, didSet);
2182                         didSet = true;
2183                         break;
2184                     case CSSValueCloseQuote:
2185                         state.style()->setContent(CLOSE_QUOTE, didSet);
2186                         didSet = true;
2187                         break;
2188                     case CSSValueNoOpenQuote:
2189                         state.style()->setContent(NO_OPEN_QUOTE, didSet);
2190                         didSet = true;
2191                         break;
2192                     case CSSValueNoCloseQuote:
2193                         state.style()->setContent(NO_CLOSE_QUOTE, didSet);
2194                         didSet = true;
2195                         break;
2196                     default:
2197                         // normal and none do not have any effect.
2198                         { }
2199                     }
2200                 }
2201             }
2202             if (!didSet)
2203                 state.style()->clearContent();
2204             return;
2205         }
2206     case CSSPropertyQuotes:
2207         if (isInherit) {
2208             state.style()->setQuotes(state.parentStyle()->quotes());
2209             return;
2210         }
2211         if (isInitial) {
2212             state.style()->setQuotes(0);
2213             return;
2214         }
2215         if (value->isValueList()) {
2216             CSSValueList* list = static_cast<CSSValueList*>(value);
2217             Vector<std::pair<String, String> > quotes;
2218             for (size_t i = 0; i < list->length(); i += 2) {
2219                 CSSValue* first = list->itemWithoutBoundsCheck(i);
2220                 // item() returns null if out of bounds so this is safe.
2221                 CSSValue* second = list->item(i + 1);
2222                 if (!second)
2223                     continue;
2224                 ASSERT_WITH_SECURITY_IMPLICATION(first->isPrimitiveValue());
2225                 ASSERT_WITH_SECURITY_IMPLICATION(second->isPrimitiveValue());
2226                 String startQuote = static_cast<CSSPrimitiveValue*>(first)->getStringValue();
2227                 String endQuote = static_cast<CSSPrimitiveValue*>(second)->getStringValue();
2228                 quotes.append(std::make_pair(startQuote, endQuote));
2229             }
2230             state.style()->setQuotes(QuotesData::create(quotes));
2231             return;
2232         }
2233         if (primitiveValue) {
2234             if (primitiveValue->getValueID() == CSSValueNone)
2235                 state.style()->setQuotes(QuotesData::create(Vector<std::pair<String, String> >()));
2236         }
2237         return;
2238     // Shorthand properties.
2239     case CSSPropertyFont:
2240         if (isInherit) {
2241             FontDescription fontDescription = state.parentStyle()->fontDescription();
2242             state.style()->setLineHeight(state.parentStyle()->specifiedLineHeight());
2243             state.setLineHeightValue(0);
2244             setFontDescription(fontDescription);
2245         } else if (isInitial) {
2246             Settings* settings = documentSettings();
2247             ASSERT(settings); // If we're doing style resolution, this document should always be in a frame and thus have settings
2248             if (!settings)
2249                 return;
2250             initializeFontStyle(settings);
2251         } else if (primitiveValue) {
2252             state.style()->setLineHeight(RenderStyle::initialLineHeight());
2253             state.setLineHeightValue(0);
2254
2255             FontDescription fontDescription;
2256             RenderTheme::defaultTheme()->systemFont(primitiveValue->getValueID(), fontDescription);
2257
2258             // Double-check and see if the theme did anything. If not, don't bother updating the font.
2259             if (fontDescription.isAbsoluteSize()) {
2260                 // Make sure the rendering mode and printer font settings are updated.
2261                 Settings* settings = documentSettings();
2262                 ASSERT(settings); // If we're doing style resolution, this document should always be in a frame and thus have settings
2263                 if (!settings)
2264                     return;
2265                 fontDescription.setRenderingMode(settings->fontRenderingMode());
2266                 fontDescription.setUsePrinterFont(document().printing() || !settings->screenFontSubstitutionEnabled());
2267
2268                 // Handle the zoom factor.
2269                 fontDescription.setComputedSize(Style::computedFontSizeFromSpecifiedSize(fontDescription.specifiedSize(), fontDescription.isAbsoluteSize(), useSVGZoomRules(), state.style(), document()));
2270                 setFontDescription(fontDescription);
2271             }
2272         } else if (value->isFontValue()) {
2273             FontValue* font = static_cast<FontValue*>(value);
2274             if (!font->style || !font->variant || !font->weight
2275                 || !font->size || !font->lineHeight || !font->family)
2276                 return;
2277             applyProperty(CSSPropertyFontStyle, font->style.get());
2278             applyProperty(CSSPropertyFontVariant, font->variant.get());
2279             applyProperty(CSSPropertyFontWeight, font->weight.get());
2280             // The previous properties can dirty our font but they don't try to read the font's
2281             // properties back, which is safe. However if font-size is using the 'ex' unit, it will
2282             // need query the dirtied font's x-height to get the computed size. To be safe in this
2283             // case, let's just update the font now.
2284             updateFont();
2285             applyProperty(CSSPropertyFontSize, font->size.get());
2286
2287             state.setLineHeightValue(font->lineHeight.get());
2288
2289             applyProperty(CSSPropertyFontFamily, font->family.get());
2290         }
2291         return;
2292
2293     case CSSPropertyBackground:
2294     case CSSPropertyBackgroundPosition:
2295     case CSSPropertyBackgroundRepeat:
2296     case CSSPropertyBorder:
2297     case CSSPropertyBorderBottom:
2298     case CSSPropertyBorderColor:
2299     case CSSPropertyBorderImage:
2300     case CSSPropertyBorderLeft:
2301     case CSSPropertyBorderRadius:
2302     case CSSPropertyBorderRight:
2303     case CSSPropertyBorderSpacing:
2304     case CSSPropertyBorderStyle:
2305     case CSSPropertyBorderTop:
2306     case CSSPropertyBorderWidth:
2307     case CSSPropertyListStyle:
2308     case CSSPropertyMargin:
2309     case CSSPropertyOutline:
2310     case CSSPropertyOverflow:
2311     case CSSPropertyPadding:
2312     case CSSPropertyTransition:
2313     case CSSPropertyWebkitAnimation:
2314     case CSSPropertyWebkitBorderAfter:
2315     case CSSPropertyWebkitBorderBefore:
2316     case CSSPropertyWebkitBorderEnd:
2317     case CSSPropertyWebkitBorderStart:
2318     case CSSPropertyWebkitBorderRadius:
2319     case CSSPropertyWebkitColumns:
2320     case CSSPropertyWebkitColumnRule:
2321     case CSSPropertyWebkitFlex:
2322     case CSSPropertyWebkitFlexFlow:
2323     case CSSPropertyWebkitGridColumn:
2324     case CSSPropertyWebkitGridRow:
2325     case CSSPropertyWebkitMarginCollapse:
2326     case CSSPropertyWebkitMarquee:
2327     case CSSPropertyWebkitMask:
2328     case CSSPropertyWebkitMaskPosition:
2329     case CSSPropertyWebkitMaskRepeat:
2330     case CSSPropertyWebkitTextEmphasis:
2331     case CSSPropertyWebkitTextStroke:
2332     case CSSPropertyWebkitTransition:
2333     case CSSPropertyWebkitTransformOrigin:
2334         ASSERT(isExpandedShorthand(id));
2335         ASSERT_NOT_REACHED();
2336         break;
2337
2338     // CSS3 Properties
2339     case CSSPropertyTextShadow:
2340     case CSSPropertyBoxShadow:
2341     case CSSPropertyWebkitBoxShadow: {
2342         if (isInherit) {
2343             if (id == CSSPropertyTextShadow)
2344                 return state.style()->setTextShadow(state.parentStyle()->textShadow() ? adoptPtr(new ShadowData(*state.parentStyle()->textShadow())) : nullptr);
2345             return state.style()->setBoxShadow(state.parentStyle()->boxShadow() ? adoptPtr(new ShadowData(*state.parentStyle()->boxShadow())) : nullptr);
2346         }
2347         if (isInitial || primitiveValue) // initial | none
2348             return id == CSSPropertyTextShadow ? state.style()->setTextShadow(nullptr) : state.style()->setBoxShadow(nullptr);
2349
2350         if (!value->isValueList())
2351             return;
2352
2353         for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
2354             CSSValue* currValue = i.value();
2355             if (!currValue->isShadowValue())
2356                 continue;
2357             ShadowValue* item = static_cast<ShadowValue*>(currValue);
2358             int x = item->x->computeLength<int>(state.style(), state.rootElementStyle(), zoomFactor);
2359             int y = item->y->computeLength<int>(state.style(), state.rootElementStyle(), zoomFactor);
2360             int blur = item->blur ? item->blur->computeLength<int>(state.style(), state.rootElementStyle(), zoomFactor) : 0;
2361             int spread = item->spread ? item->spread->computeLength<int>(state.style(), state.rootElementStyle(), zoomFactor) : 0;
2362             ShadowStyle shadowStyle = item->style && item->style->getValueID() == CSSValueInset ? Inset : Normal;
2363             Color color;
2364             if (item->color)
2365                 color = colorFromPrimitiveValue(item->color.get());
2366             else if (state.style())
2367                 color = state.style()->color();
2368
2369             OwnPtr<ShadowData> shadowData = adoptPtr(new ShadowData(IntPoint(x, y), blur, spread, shadowStyle, id == CSSPropertyWebkitBoxShadow, color.isValid() ? color : Color::transparent));
2370             if (id == CSSPropertyTextShadow)
2371                 state.style()->setTextShadow(shadowData.release(), i.index()); // add to the list if this is not the first entry
2372             else
2373                 state.style()->setBoxShadow(shadowData.release(), i.index()); // add to the list if this is not the first entry
2374         }
2375         return;
2376     }
2377     case CSSPropertyWebkitBoxReflect: {
2378         HANDLE_INHERIT_AND_INITIAL(boxReflect, BoxReflect)
2379         if (primitiveValue) {
2380             state.style()->setBoxReflect(RenderStyle::initialBoxReflect());
2381             return;
2382         }
2383
2384         if (!value->isReflectValue())
2385             return;
2386
2387         CSSReflectValue* reflectValue = static_cast<CSSReflectValue*>(value);
2388         RefPtr<StyleReflection> reflection = StyleReflection::create();
2389         reflection->setDirection(*reflectValue->direction());
2390         if (reflectValue->offset())
2391             reflection->setOffset(reflectValue->offset()->convertToLength<FixedIntegerConversion | PercentConversion | CalculatedConversion>(state.style(), state.rootElementStyle(), zoomFactor));
2392         NinePieceImage mask;
2393         mask.setMaskDefaults();
2394         m_styleMap.mapNinePieceImage(id, reflectValue->mask(), mask);
2395         reflection->setMask(mask);
2396
2397         state.style()->setBoxReflect(reflection.release());
2398         return;
2399     }
2400     case CSSPropertySrc: // Only used in @font-face rules.
2401         return;
2402     case CSSPropertyUnicodeRange: // Only used in @font-face rules.
2403         return;
2404     case CSSPropertyWebkitLocale: {
2405         HANDLE_INHERIT_AND_INITIAL(locale, Locale);
2406         if (!primitiveValue)
2407             return;
2408         if (primitiveValue->getValueID() == CSSValueAuto)
2409             state.style()->setLocale(nullAtom);
2410         else
2411             state.style()->setLocale(primitiveValue->getStringValue());
2412         FontDescription fontDescription = state.style()->fontDescription();
2413         fontDescription.setScript(localeToScriptCodeForFontSelection(state.style()->locale()));
2414         setFontDescription(fontDescription);
2415         return;
2416     }
2417 #if ENABLE(DASHBOARD_SUPPORT)
2418     case CSSPropertyWebkitDashboardRegion:
2419     {
2420         HANDLE_INHERIT_AND_INITIAL(dashboardRegions, DashboardRegions)
2421         if (!primitiveValue)
2422             return;
2423
2424         if (primitiveValue->getValueID() == CSSValueNone) {
2425             state.style()->setDashboardRegions(RenderStyle::noneDashboardRegions());
2426             return;
2427         }
2428
2429         DashboardRegion* region = primitiveValue->getDashboardRegionValue();
2430         if (!region)
2431             return;
2432
2433         DashboardRegion* first = region;
2434         while (region) {
2435             Length top = convertToIntLength(region->top(), state.style(), state.rootElementStyle());
2436             Length right = convertToIntLength(region->right(), state.style(), state.rootElementStyle());
2437             Length bottom = convertToIntLength(region->bottom(), state.style(), state.rootElementStyle());
2438             Length left = convertToIntLength(region->left(), state.style(), state.rootElementStyle());
2439
2440             if (top.isUndefined())
2441                 top = Length();
2442             if (right.isUndefined())
2443                 right = Length();
2444             if (bottom.isUndefined())
2445                 bottom = Length();
2446             if (left.isUndefined())
2447                 left = Length();
2448
2449             if (region->m_isCircle)
2450                 state.style()->setDashboardRegion(StyleDashboardRegion::Circle, region->m_label, top, right, bottom, left, region == first ? false : true);
2451             else if (region->m_isRectangle)
2452                 state.style()->setDashboardRegion(StyleDashboardRegion::Rectangle, region->m_label, top, right, bottom, left, region == first ? false : true);
2453             region = region->m_next.get();
2454         }
2455
2456         state.document().setHasAnnotatedRegions(true);
2457
2458         return;
2459     }
2460 #endif
2461 #if ENABLE(DRAGGABLE_REGION)
2462     case CSSPropertyWebkitAppRegion: {
2463         if (!primitiveValue || !primitiveValue->getValueID())
2464             return;
2465         state.style()->setDraggableRegionMode(primitiveValue->getValueID() == CSSValueDrag ? DraggableRegionDrag : DraggableRegionNoDrag);
2466         state.document().setHasAnnotatedRegions(true);
2467         return;
2468     }
2469 #endif
2470     case CSSPropertyWebkitTextStrokeWidth: {
2471         HANDLE_INHERIT_AND_INITIAL(textStrokeWidth, TextStrokeWidth)
2472         float width = 0;
2473         switch (primitiveValue->getValueID()) {
2474         case CSSValueThin:
2475         case CSSValueMedium:
2476         case CSSValueThick: {
2477             double result = 1.0 / 48;
2478             if (primitiveValue->getValueID() == CSSValueMedium)
2479                 result *= 3;
2480             else if (primitiveValue->getValueID() == CSSValueThick)
2481                 result *= 5;
2482             width = CSSPrimitiveValue::create(result, CSSPrimitiveValue::CSS_EMS)->computeLength<float>(state.style(), state.rootElementStyle(), zoomFactor);
2483             break;
2484         }
2485         default:
2486             width = primitiveValue->computeLength<float>(state.style(), state.rootElementStyle(), zoomFactor);
2487             break;
2488         }
2489         state.style()->setTextStrokeWidth(width);
2490         return;
2491     }
2492     case CSSPropertyWebkitTransform: {
2493         HANDLE_INHERIT_AND_INITIAL(transform, Transform);
2494         TransformOperations operations;
2495         transformsForValue(state.style(), state.rootElementStyle(), value, operations);
2496         state.style()->setTransform(operations);
2497         return;
2498     }
2499     case CSSPropertyWebkitPerspective: {
2500         HANDLE_INHERIT_AND_INITIAL(perspective, Perspective)
2501
2502         if (!primitiveValue)
2503             return;
2504
2505         if (primitiveValue->getValueID() == CSSValueNone) {
2506             state.style()->setPerspective(0);
2507             return;
2508         }
2509
2510         float perspectiveValue;
2511         if (primitiveValue->isLength())
2512             perspectiveValue = primitiveValue->computeLength<float>(state.style(), state.rootElementStyle(), zoomFactor);
2513         else if (primitiveValue->isNumber()) {
2514             // For backward compatibility, treat valueless numbers as px.
2515             perspectiveValue = CSSPrimitiveValue::create(primitiveValue->getDoubleValue(), CSSPrimitiveValue::CSS_PX)->computeLength<float>(state.style(), state.rootElementStyle(), zoomFactor);
2516         } else
2517             return;
2518
2519         if (perspectiveValue >= 0.0f)
2520             state.style()->setPerspective(perspectiveValue);
2521         return;
2522     }
2523 #if ENABLE(TOUCH_EVENTS)
2524     case CSSPropertyWebkitTapHighlightColor: {
2525         HANDLE_INHERIT_AND_INITIAL(tapHighlightColor, TapHighlightColor);
2526         if (!primitiveValue)
2527             break;
2528
2529         Color col = colorFromPrimitiveValue(primitiveValue);
2530         state.style()->setTapHighlightColor(col);
2531         return;
2532     }
2533 #endif
2534 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
2535     case CSSPropertyWebkitOverflowScrolling: {
2536         HANDLE_INHERIT_AND_INITIAL(useTouchOverflowScrolling, UseTouchOverflowScrolling);
2537         if (!primitiveValue)
2538             break;
2539         state.style()->setUseTouchOverflowScrolling(primitiveValue->getValueID() == CSSValueTouch);
2540         return;
2541     }
2542 #endif
2543     case CSSPropertyInvalid:
2544         return;
2545     // Directional properties are resolved by resolveDirectionAwareProperty() before the switch.
2546     case CSSPropertyWebkitBorderEndColor:
2547     case CSSPropertyWebkitBorderEndStyle:
2548     case CSSPropertyWebkitBorderEndWidth:
2549     case CSSPropertyWebkitBorderStartColor:
2550     case CSSPropertyWebkitBorderStartStyle:
2551     case CSSPropertyWebkitBorderStartWidth:
2552     case CSSPropertyWebkitBorderBeforeColor:
2553     case CSSPropertyWebkitBorderBeforeStyle:
2554     case CSSPropertyWebkitBorderBeforeWidth:
2555     case CSSPropertyWebkitBorderAfterColor:
2556     case CSSPropertyWebkitBorderAfterStyle:
2557     case CSSPropertyWebkitBorderAfterWidth:
2558     case CSSPropertyWebkitMarginEnd:
2559     case CSSPropertyWebkitMarginStart:
2560     case CSSPropertyWebkitMarginBefore:
2561     case CSSPropertyWebkitMarginAfter:
2562     case CSSPropertyWebkitMarginBeforeCollapse:
2563     case CSSPropertyWebkitMarginTopCollapse:
2564     case CSSPropertyWebkitMarginAfterCollapse:
2565     case CSSPropertyWebkitMarginBottomCollapse:
2566     case CSSPropertyWebkitPaddingEnd:
2567     case CSSPropertyWebkitPaddingStart:
2568     case CSSPropertyWebkitPaddingBefore:
2569     case CSSPropertyWebkitPaddingAfter:
2570     case CSSPropertyWebkitLogicalWidth:
2571     case CSSPropertyWebkitLogicalHeight:
2572     case CSSPropertyWebkitMinLogicalWidth:
2573     case CSSPropertyWebkitMinLogicalHeight:
2574     case CSSPropertyWebkitMaxLogicalWidth:
2575     case CSSPropertyWebkitMaxLogicalHeight:
2576     {
2577         CSSPropertyID newId = CSSProperty::resolveDirectionAwareProperty(id, state.style()->direction(), state.style()->writingMode());
2578         ASSERT(newId != id);
2579         return applyProperty(newId, value);
2580     }
2581     case CSSPropertyFontStretch:
2582     case CSSPropertyPage:
2583     case CSSPropertyTextLineThrough:
2584     case CSSPropertyTextLineThroughColor:
2585     case CSSPropertyTextLineThroughMode:
2586     case CSSPropertyTextLineThroughStyle:
2587     case CSSPropertyTextLineThroughWidth:
2588     case CSSPropertyTextOverline:
2589     case CSSPropertyTextOverlineColor:
2590     case CSSPropertyTextOverlineMode:
2591     case CSSPropertyTextOverlineStyle:
2592     case CSSPropertyTextOverlineWidth:
2593     case CSSPropertyTextUnderline:
2594     case CSSPropertyTextUnderlineColor:
2595     case CSSPropertyTextUnderlineMode:
2596     case CSSPropertyTextUnderlineStyle:
2597     case CSSPropertyTextUnderlineWidth:
2598     case CSSPropertyWebkitFontSizeDelta:
2599     case CSSPropertyWebkitTextDecorationsInEffect:
2600         return;
2601
2602     // CSS Text Layout Module Level 3: Vertical writing support
2603     case CSSPropertyWebkitWritingMode: {
2604         HANDLE_INHERIT_AND_INITIAL(writingMode, WritingMode);
2605         
2606         if (primitiveValue)
2607             setWritingMode(*primitiveValue);
2608
2609         // FIXME: It is not ok to modify document state while applying style.
2610         if (state.element() && state.element() == state.document().documentElement())
2611             state.document().setWritingModeSetOnDocumentElement(true);
2612         return;
2613     }
2614
2615     case CSSPropertyWebkitTextOrientation: {
2616         HANDLE_INHERIT_AND_INITIAL(textOrientation, TextOrientation);
2617
2618         if (primitiveValue)
2619             setTextOrientation(*primitiveValue);
2620
2621         return;
2622     }
2623
2624     case CSSPropertyWebkitLineBoxContain: {
2625         HANDLE_INHERIT_AND_INITIAL(lineBoxContain, LineBoxContain)
2626         if (primitiveValue && primitiveValue->getValueID() == CSSValueNone) {
2627             state.style()->setLineBoxContain(LineBoxContainNone);
2628             return;
2629         }
2630
2631         if (!value->isCSSLineBoxContainValue())
2632             return;
2633
2634         CSSLineBoxContainValue* lineBoxContainValue = static_cast<CSSLineBoxContainValue*>(value);
2635         state.style()->setLineBoxContain(lineBoxContainValue->value());
2636         return;
2637     }
2638
2639     // CSS Fonts Module Level 3
2640     case CSSPropertyWebkitFontFeatureSettings: {
2641         if (primitiveValue && primitiveValue->getValueID() == CSSValueNormal) {
2642             setFontDescription(state.style()->fontDescription().makeNormalFeatureSettings());
2643             return;
2644         }
2645
2646         if (!value->isValueList())
2647             return;
2648
2649         FontDescription fontDescription = state.style()->fontDescription();
2650         CSSValueList* list = static_cast<CSSValueList*>(value);
2651         RefPtr<FontFeatureSettings> settings = FontFeatureSettings::create();
2652         int len = list->length();
2653         for (int i = 0; i < len; ++i) {
2654             CSSValue* item = list->itemWithoutBoundsCheck(i);
2655             if (!item->isFontFeatureValue())
2656                 continue;
2657             FontFeatureValue* feature = static_cast<FontFeatureValue*>(item);
2658             settings->append(FontFeature(feature->tag(), feature->value()));
2659         }
2660         fontDescription.setFeatureSettings(settings.release());
2661         setFontDescription(fontDescription);
2662         return;
2663     }
2664
2665 #if ENABLE(CSS_FILTERS)
2666     case CSSPropertyWebkitFilter: {
2667         HANDLE_INHERIT_AND_INITIAL(filter, Filter);
2668         FilterOperations operations;
2669         if (createFilterOperations(value, operations))
2670             state.style()->setFilter(operations);
2671         return;
2672     }
2673 #endif
2674     case CSSPropertyWebkitGridAutoColumns: {
2675         GridTrackSize trackSize;
2676         if (!createGridTrackSize(value, trackSize, state))
2677             return;
2678         state.style()->setGridAutoColumns(trackSize);
2679         return;
2680     }
2681     case CSSPropertyWebkitGridAutoRows: {
2682         GridTrackSize trackSize;
2683         if (!createGridTrackSize(value, trackSize, state))
2684             return;
2685         state.style()->setGridAutoRows(trackSize);
2686         return;
2687     }
2688     case CSSPropertyWebkitGridDefinitionColumns: {
2689         Vector<GridTrackSize> trackSizes;
2690         NamedGridLinesMap namedGridLines;
2691         if (!createGridTrackList(value, trackSizes, namedGridLines, state))
2692             return;
2693         state.style()->setGridColumns(trackSizes);
2694         state.style()->setNamedGridColumnLines(namedGridLines);
2695         return;
2696     }
2697     case CSSPropertyWebkitGridDefinitionRows: {
2698         Vector<GridTrackSize> trackSizes;
2699         NamedGridLinesMap namedGridLines;
2700         if (!createGridTrackList(value, trackSizes, namedGridLines, state))
2701             return;
2702         state.style()->setGridRows(trackSizes);
2703         state.style()->setNamedGridRowLines(namedGridLines);
2704         return;
2705     }
2706
2707     case CSSPropertyWebkitGridColumnStart: {
2708         GridPosition columnStartPosition;
2709         if (!createGridPosition(value, columnStartPosition))
2710             return;
2711         state.style()->setGridItemColumnStart(columnStartPosition);
2712         return;
2713     }
2714     case CSSPropertyWebkitGridColumnEnd: {
2715         GridPosition columnEndPosition;
2716         if (!createGridPosition(value, columnEndPosition))
2717             return;
2718         state.style()->setGridItemColumnEnd(columnEndPosition);
2719         return;
2720     }
2721
2722     case CSSPropertyWebkitGridRowStart: {
2723         GridPosition rowStartPosition;
2724         if (!createGridPosition(value, rowStartPosition))
2725             return;
2726         state.style()->setGridItemRowStart(rowStartPosition);
2727         return;
2728     }
2729     case CSSPropertyWebkitGridRowEnd: {
2730         GridPosition rowEndPosition;
2731         if (!createGridPosition(value, rowEndPosition))
2732             return;
2733         state.style()->setGridItemRowEnd(rowEndPosition);
2734         return;
2735     }
2736
2737     // These properties are aliased and DeprecatedStyleBuilder already applied the property on the prefixed version.
2738     case CSSPropertyTransitionDelay:
2739     case CSSPropertyTransitionDuration:
2740     case CSSPropertyTransitionProperty:
2741     case CSSPropertyTransitionTimingFunction:
2742         return;
2743     // These properties are implemented in the DeprecatedStyleBuilder lookup table.
2744     case CSSPropertyBackgroundAttachment:
2745     case CSSPropertyBackgroundClip:
2746     case CSSPropertyBackgroundColor:
2747     case CSSPropertyBackgroundImage:
2748     case CSSPropertyBackgroundOrigin:
2749     case CSSPropertyBackgroundPositionX:
2750     case CSSPropertyBackgroundPositionY:
2751     case CSSPropertyBackgroundRepeatX:
2752     case CSSPropertyBackgroundRepeatY:
2753     case CSSPropertyBackgroundSize:
2754     case CSSPropertyBorderBottomColor:
2755     case CSSPropertyBorderBottomLeftRadius:
2756     case CSSPropertyBorderBottomRightRadius:
2757     case CSSPropertyBorderBottomStyle:
2758     case CSSPropertyBorderBottomWidth:
2759     case CSSPropertyBorderCollapse:
2760     case CSSPropertyBorderImageOutset:
2761     case CSSPropertyBorderImageRepeat:
2762     case CSSPropertyBorderImageSlice:
2763     case CSSPropertyBorderImageSource:
2764     case CSSPropertyBorderImageWidth:
2765     case CSSPropertyBorderLeftColor:
2766     case CSSPropertyBorderLeftStyle:
2767     case CSSPropertyBorderLeftWidth:
2768     case CSSPropertyBorderRightColor:
2769     case CSSPropertyBorderRightStyle:
2770     case CSSPropertyBorderRightWidth:
2771     case CSSPropertyBorderTopColor:
2772     case CSSPropertyBorderTopLeftRadius:
2773     case CSSPropertyBorderTopRightRadius:
2774     case CSSPropertyBorderTopStyle:
2775     case CSSPropertyBorderTopWidth:
2776     case CSSPropertyBottom:
2777     case CSSPropertyBoxSizing:
2778     case CSSPropertyCaptionSide:
2779     case CSSPropertyClear:
2780     case CSSPropertyClip:
2781     case CSSPropertyColor:
2782     case CSSPropertyCounterIncrement:
2783     case CSSPropertyCounterReset:
2784     case CSSPropertyCursor:
2785     case CSSPropertyDirection:
2786     case CSSPropertyDisplay:
2787     case CSSPropertyEmptyCells:
2788     case CSSPropertyFloat:
2789     case CSSPropertyFontSize:
2790     case CSSPropertyFontStyle:
2791     case CSSPropertyFontVariant:
2792     case CSSPropertyFontWeight:
2793     case CSSPropertyHeight:
2794 #if ENABLE(CSS_IMAGE_ORIENTATION)
2795     case CSSPropertyImageOrientation:
2796 #endif
2797     case CSSPropertyImageRendering:
2798 #if ENABLE(CSS_IMAGE_RESOLUTION)
2799     case CSSPropertyImageResolution:
2800 #endif
2801     case CSSPropertyLeft:
2802     case CSSPropertyLetterSpacing:
2803     case CSSPropertyLineHeight:
2804     case CSSPropertyListStyleImage:
2805     case CSSPropertyListStylePosition:
2806     case CSSPropertyListStyleType:
2807     case CSSPropertyMarginBottom:
2808     case CSSPropertyMarginLeft:
2809     case CSSPropertyMarginRight:
2810     case CSSPropertyMarginTop:
2811     case CSSPropertyMaxHeight:
2812     case CSSPropertyMaxWidth:
2813     case CSSPropertyMinHeight:
2814     case CSSPropertyMinWidth:
2815     case CSSPropertyObjectFit:
2816     case CSSPropertyOpacity:
2817     case CSSPropertyOrphans:
2818     case CSSPropertyOutlineColor:
2819     case CSSPropertyOutlineOffset:
2820     case CSSPropertyOutlineStyle:
2821     case CSSPropertyOutlineWidth:
2822     case CSSPropertyOverflowWrap:
2823     case CSSPropertyOverflowX:
2824     case CSSPropertyOverflowY:
2825     case CSSPropertyPaddingBottom:
2826     case CSSPropertyPaddingLeft:
2827     case CSSPropertyPaddingRight:
2828     case CSSPropertyPaddingTop:
2829     case CSSPropertyPageBreakAfter:
2830     case CSSPropertyPageBreakBefore:
2831     case CSSPropertyPageBreakInside:
2832     case CSSPropertyPointerEvents:
2833     case CSSPropertyPosition:
2834     case CSSPropertyResize:
2835     case CSSPropertyRight:
2836     case CSSPropertySize:
2837     case CSSPropertySpeak:
2838     case CSSPropertyTabSize:
2839     case CSSPropertyTableLayout:
2840     case CSSPropertyTextAlign:
2841     case CSSPropertyTextDecoration:
2842     case CSSPropertyTextIndent:
2843     case CSSPropertyTextOverflow:
2844     case CSSPropertyTextRendering:
2845     case CSSPropertyTextTransform:
2846     case CSSPropertyTop:
2847     case CSSPropertyUnicodeBidi:
2848 #if ENABLE(CSS_VARIABLES)
2849     case CSSPropertyVariable:
2850 #endif
2851     case CSSPropertyVerticalAlign:
2852     case CSSPropertyVisibility:
2853     case CSSPropertyWebkitAnimationDelay:
2854     case CSSPropertyWebkitAnimationDirection:
2855     case CSSPropertyWebkitAnimationDuration:
2856     case CSSPropertyWebkitAnimationFillMode:
2857     case CSSPropertyWebkitAnimationIterationCount:
2858     case CSSPropertyWebkitAnimationName:
2859     case CSSPropertyWebkitAnimationPlayState:
2860     case CSSPropertyWebkitAnimationTimingFunction:
2861     case CSSPropertyWebkitAppearance:
2862     case CSSPropertyWebkitAspectRatio:
2863     case CSSPropertyWebkitBackfaceVisibility:
2864     case CSSPropertyWebkitBackgroundClip:
2865     case CSSPropertyWebkitBackgroundComposite:
2866     case CSSPropertyWebkitBackgroundOrigin:
2867     case CSSPropertyWebkitBackgroundSize:
2868     case CSSPropertyWebkitBorderFit:
2869     case CSSPropertyWebkitBorderHorizontalSpacing:
2870     case CSSPropertyWebkitBorderImage:
2871     case CSSPropertyWebkitBorderVerticalSpacing:
2872     case CSSPropertyWebkitBoxAlign:
2873 #if ENABLE(CSS_BOX_DECORATION_BREAK)
2874     case CSSPropertyWebkitBoxDecorationBreak:
2875 #endif
2876     case CSSPropertyWebkitBoxDirection:
2877     case CSSPropertyWebkitBoxFlex:
2878     case CSSPropertyWebkitBoxFlexGroup:
2879     case CSSPropertyWebkitBoxLines:
2880     case CSSPropertyWebkitBoxOrdinalGroup:
2881     case CSSPropertyWebkitBoxOrient:
2882     case CSSPropertyWebkitBoxPack:
2883     case CSSPropertyWebkitColorCorrection:
2884     case CSSPropertyWebkitColumnAxis:
2885     case CSSPropertyWebkitColumnBreakAfter:
2886     case CSSPropertyWebkitColumnBreakBefore:
2887     case CSSPropertyWebkitColumnBreakInside:
2888     case CSSPropertyWebkitColumnCount:
2889     case CSSPropertyWebkitColumnGap:
2890     case CSSPropertyWebkitColumnProgression:
2891     case CSSPropertyWebkitColumnRuleColor:
2892     case CSSPropertyWebkitColumnRuleStyle:
2893     case CSSPropertyWebkitColumnRuleWidth:
2894     case CSSPropertyWebkitColumnSpan:
2895     case CSSPropertyWebkitColumnWidth:
2896 #if ENABLE(CURSOR_VISIBILITY)
2897     case CSSPropertyWebkitCursorVisibility:
2898 #endif
2899     case CSSPropertyWebkitAlignContent:
2900     case CSSPropertyWebkitAlignItems:
2901     case CSSPropertyWebkitAlignSelf:
2902     case CSSPropertyWebkitFlexBasis:
2903     case CSSPropertyWebkitFlexDirection:
2904     case CSSPropertyWebkitFlexGrow:
2905     case CSSPropertyWebkitFlexShrink:
2906     case CSSPropertyWebkitFlexWrap:
2907     case CSSPropertyWebkitJustifyContent:
2908     case CSSPropertyWebkitOrder:
2909 #if ENABLE(CSS_REGIONS)
2910     case CSSPropertyWebkitFlowFrom:
2911     case CSSPropertyWebkitFlowInto:
2912 #endif
2913     case CSSPropertyWebkitFontKerning:
2914     case CSSPropertyWebkitFontSmoothing:
2915     case CSSPropertyWebkitFontVariantLigatures:
2916     case CSSPropertyWebkitHighlight:
2917     case CSSPropertyWebkitHyphenateCharacter:
2918     case CSSPropertyWebkitHyphenateLimitAfter:
2919     case CSSPropertyWebkitHyphenateLimitBefore:
2920     case CSSPropertyWebkitHyphenateLimitLines:
2921     case CSSPropertyWebkitHyphens:
2922     case CSSPropertyWebkitLineAlign:
2923     case CSSPropertyWebkitLineBreak:
2924     case CSSPropertyWebkitLineClamp:
2925     case CSSPropertyWebkitLineGrid:
2926     case CSSPropertyWebkitLineSnap:
2927     case CSSPropertyWebkitMarqueeDirection:
2928     case CSSPropertyWebkitMarqueeIncrement:
2929     case CSSPropertyWebkitMarqueeRepetition:
2930     case CSSPropertyWebkitMarqueeSpeed:
2931     case CSSPropertyWebkitMarqueeStyle:
2932     case CSSPropertyWebkitMaskBoxImage:
2933     case CSSPropertyWebkitMaskBoxImageOutset:
2934     case CSSPropertyWebkitMaskBoxImageRepeat:
2935     case CSSPropertyWebkitMaskBoxImageSlice:
2936     case CSSPropertyWebkitMaskBoxImageSource:
2937     case CSSPropertyWebkitMaskBoxImageWidth:
2938     case CSSPropertyWebkitMaskClip:
2939     case CSSPropertyWebkitMaskComposite:
2940     case CSSPropertyWebkitMaskImage:
2941     case CSSPropertyWebkitMaskOrigin:
2942     case CSSPropertyWebkitMaskPositionX:
2943     case CSSPropertyWebkitMaskPositionY:
2944     case CSSPropertyWebkitMaskRepeatX:
2945     case CSSPropertyWebkitMaskRepeatY:
2946     case CSSPropertyWebkitMaskSize:
2947     case CSSPropertyWebkitMaskSourceType:
2948     case CSSPropertyWebkitNbspMode:
2949     case CSSPropertyWebkitPerspectiveOrigin:
2950     case CSSPropertyWebkitPerspectiveOriginX:
2951     case CSSPropertyWebkitPerspectiveOriginY:
2952     case CSSPropertyWebkitPrintColorAdjust:
2953 #if ENABLE(CSS_REGIONS)
2954     case CSSPropertyWebkitRegionBreakAfter:
2955     case CSSPropertyWebkitRegionBreakBefore:
2956     case CSSPropertyWebkitRegionBreakInside:
2957     case CSSPropertyWebkitRegionFragment:
2958 #endif
2959     case CSSPropertyWebkitRtlOrdering:
2960     case CSSPropertyWebkitRubyPosition:
2961     case CSSPropertyWebkitTextCombine:
2962 #if ENABLE(CSS3_TEXT)
2963     case CSSPropertyWebkitTextDecorationLine:
2964     case CSSPropertyWebkitTextDecorationStyle:
2965     case CSSPropertyWebkitTextDecorationColor:
2966     case CSSPropertyWebkitTextAlignLast:
2967     case CSSPropertyWebkitTextJustify:
2968     case CSSPropertyWebkitTextUnderlinePosition:
2969 #endif // CSS3_TEXT
2970     case CSSPropertyWebkitTextEmphasisColor:
2971     case CSSPropertyWebkitTextEmphasisPosition:
2972     case CSSPropertyWebkitTextEmphasisStyle:
2973     case CSSPropertyWebkitTextFillColor:
2974     case CSSPropertyWebkitTextSecurity:
2975     case CSSPropertyWebkitTextStrokeColor:
2976     case CSSPropertyWebkitTransformOriginX:
2977     case CSSPropertyWebkitTransformOriginY:
2978     case CSSPropertyWebkitTransformOriginZ:
2979     case CSSPropertyWebkitTransformStyle:
2980     case CSSPropertyWebkitTransitionDelay:
2981     case CSSPropertyWebkitTransitionDuration:
2982     case CSSPropertyWebkitTransitionProperty:
2983     case CSSPropertyWebkitTransitionTimingFunction:
2984     case CSSPropertyWebkitUserDrag:
2985     case CSSPropertyWebkitUserModify:
2986     case CSSPropertyWebkitUserSelect:
2987     case CSSPropertyWebkitClipPath:
2988 #if ENABLE(CSS_SHAPES)
2989     case CSSPropertyWebkitShapeMargin:
2990     case CSSPropertyWebkitShapePadding:
2991     case CSSPropertyWebkitShapeInside:
2992     case CSSPropertyWebkitShapeOutside:
2993 #endif
2994 #if ENABLE(CSS_EXCLUSIONS)
2995     case CSSPropertyWebkitWrapFlow:
2996     case CSSPropertyWebkitWrapThrough:
2997 #endif
2998 #if ENABLE(CSS_SHADERS)
2999     case CSSPropertyMix:
3000     case CSSPropertyParameters:
3001 #endif
3002     case CSSPropertyWhiteSpace:
3003     case CSSPropertyWidows:
3004     case CSSPropertyWidth:
3005     case CSSPropertyWordBreak:
3006     case CSSPropertyWordSpacing:
3007     case CSSPropertyWordWrap:
3008     case CSSPropertyZIndex:
3009     case CSSPropertyZoom:
3010 #if ENABLE(CSS_DEVICE_ADAPTATION)
3011     case CSSPropertyMaxZoom:
3012     case CSSPropertyMinZoom:
3013     case CSSPropertyOrientation:
3014     case CSSPropertyUserZoom:
3015 #endif
3016         ASSERT_NOT_REACHED();
3017         return;
3018     default:
3019 #if ENABLE(SVG)
3020         // Try the SVG properties
3021         applySVGProperty(id, value);
3022 #endif
3023         return;
3024     }
3025 }
3026
3027 PassRefPtr<StyleImage> StyleResolver::styleImage(CSSPropertyID property, CSSValue* value)
3028 {
3029     if (value->isImageValue())
3030         return cachedOrPendingFromValue(property, static_cast<CSSImageValue*>(value));
3031
3032     if (value->isImageGeneratorValue()) {
3033         if (value->isGradientValue())
3034             return generatedOrPendingFromValue(property, static_cast<CSSGradientValue*>(value)->gradientWithStylesResolved(this).get());
3035         return generatedOrPendingFromValue(property, static_cast<CSSImageGeneratorValue*>(value));
3036     }
3037
3038 #if ENABLE(CSS_IMAGE_SET)
3039     if (value->isImageSetValue())
3040         return setOrPendingFromValue(property, static_cast<CSSImageSetValue*>(value));
3041 #endif
3042
3043     if (value->isCursorImageValue())
3044         return cursorOrPendingFromValue(property, static_cast<CSSCursorImageValue*>(value));
3045
3046     return 0;
3047 }
3048
3049 PassRefPtr<StyleImage> StyleResolver::cachedOrPendingFromValue(CSSPropertyID property, CSSImageValue* value)
3050 {
3051     RefPtr<StyleImage> image = value->cachedOrPendingImage();
3052     if (image && image->isPendingImage())
3053         m_state.pendingImageProperties().set(property, value);
3054     return image.release();
3055 }
3056
3057 PassRefPtr<StyleImage> StyleResolver::generatedOrPendingFromValue(CSSPropertyID property, CSSImageGeneratorValue* value)
3058 {
3059 #if ENABLE(CSS_FILTERS)
3060     if (value->isFilterImageValue()) {
3061         // FilterImage needs to calculate FilterOperations.
3062         static_cast<CSSFilterImageValue*>(value)->createFilterOperations(this);
3063     }
3064 #endif
3065     if (value->isPending()) {
3066         m_state.pendingImageProperties().set(property, value);
3067         return StylePendingImage::create(value);
3068     }
3069     return StyleGeneratedImage::create(value);
3070 }
3071
3072 #if ENABLE(CSS_IMAGE_SET)
3073 PassRefPtr<StyleImage> StyleResolver::setOrPendingFromValue(CSSPropertyID property, CSSImageSetValue* value)
3074 {
3075     RefPtr<StyleImage> image = value->cachedOrPendingImageSet(&document());
3076     if (image && image->isPendingImage())
3077         m_state.pendingImageProperties().set(property, value);
3078     return image.release();
3079 }
3080 #endif
3081
3082 PassRefPtr<StyleImage> StyleResolver::cursorOrPendingFromValue(CSSPropertyID property, CSSCursorImageValue* value)
3083 {
3084     RefPtr<StyleImage> image = value->cachedOrPendingImage(&document());
3085     if (image && image->isPendingImage())
3086         m_state.pendingImageProperties().set(property, value);
3087     return image.release();
3088 }
3089
3090 void StyleResolver::checkForZoomChange(RenderStyle* style, RenderStyle* parentStyle)
3091 {
3092     if (style->effectiveZoom() == parentStyle->effectiveZoom())
3093         return;
3094
3095     const FontDescription& childFont = style->fontDescription();
3096     FontDescription newFontDescription(childFont);
3097     setFontSize(newFontDescription, childFont.specifiedSize());
3098     style->setFontDescription(newFontDescription);
3099 }
3100
3101 void StyleResolver::checkForGenericFamilyChange(RenderStyle* style, RenderStyle* parentStyle)
3102 {
3103     const FontDescription& childFont = style->fontDescription();
3104
3105     if (childFont.isAbsoluteSize() || !parentStyle)
3106         return;
3107
3108     const FontDescription& parentFont = parentStyle->fontDescription();
3109     if (childFont.useFixedDefaultSize() == parentFont.useFixedDefaultSize())
3110         return;
3111
3112     // For now, lump all families but monospace together.
3113     if (childFont.genericFamily() != FontDescription::MonospaceFamily
3114         && parentFont.genericFamily() != FontDescription::MonospaceFamily)
3115         return;
3116
3117     // We know the parent is monospace or the child is monospace, and that font
3118     // size was unspecified. We want to scale our font size as appropriate.
3119     // If the font uses a keyword size, then we refetch from the table rather than
3120     // multiplying by our scale factor.
3121     float size;
3122     if (childFont.keywordSize())
3123         size = Style::fontSizeForKeyword(CSSValueXxSmall + childFont.keywordSize() - 1, childFont.useFixedDefaultSize(), document());
3124     else {
3125         Settings* settings = documentSettings();
3126         float fixedScaleFactor = (settings && settings->defaultFixedFontSize() && settings->defaultFontSize())
3127             ? static_cast<float>(settings->defaultFixedFontSize()) / settings->defaultFontSize()
3128             : 1;
3129         size = parentFont.useFixedDefaultSize() ?
3130                 childFont.specifiedSize() / fixedScaleFactor :
3131                 childFont.specifiedSize() * fixedScaleFactor;
3132     }
3133
3134     FontDescription newFontDescription(childFont);
3135     setFontSize(newFontDescription, size);
3136     style->setFontDescription(newFontDescription);
3137 }
3138
3139 void StyleResolver::initializeFontStyle(Settings* settings)
3140 {
3141     FontDescription fontDescription;
3142     fontDescription.setGenericFamily(FontDescription::StandardFamily);
3143     fontDescription.setRenderingMode(settings->fontRenderingMode());
3144     fontDescription.setUsePrinterFont(document().printing() || !settings->screenFontSubstitutionEnabled());
3145     const AtomicString& standardFontFamily = documentSettings()->standardFontFamily();
3146     if (!standardFontFamily.isEmpty())
3147         fontDescription.setOneFamily(standardFontFamily);
3148     fontDescription.setKeywordSize(CSSValueMedium - CSSValueXxSmall + 1);
3149     setFontSize(fontDescription, Style::fontSizeForKeyword(CSSValueMedium, false, document()));
3150     m_state.style()->setLineHeight(RenderStyle::initialLineHeight());
3151     m_state.setLineHeightValue(0);
3152     setFontDescription(fontDescription);
3153 }
3154
3155 void StyleResolver::setFontSize(FontDescription& fontDescription, float size)
3156 {
3157     fontDescription.setSpecifiedSize(size);
3158     fontDescription.setComputedSize(Style::computedFontSizeFromSpecifiedSize(size, fontDescription.isAbsoluteSize(), useSVGZoomRules(), m_state.style(), document()));
3159 }
3160
3161 static Color colorForCSSValue(CSSValueID cssValueId)
3162 {
3163     struct ColorValue {
3164         CSSValueID cssValueId;
3165         RGBA32 color;
3166     };
3167
3168     static const ColorValue colorValues[] = {
3169         { CSSValueAqua, 0xFF00FFFF },
3170         { CSSValueBlack, 0xFF000000 },
3171         { CSSValueBlue, 0xFF0000FF },
3172         { CSSValueFuchsia, 0xFFFF00FF },
3173         { CSSValueGray, 0xFF808080 },
3174         { CSSValueGreen, 0xFF008000  },
3175         { CSSValueGrey, 0xFF808080 },
3176         { CSSValueLime, 0xFF00FF00 },
3177         { CSSValueMaroon, 0xFF800000 },
3178         { CSSValueNavy, 0xFF000080 },
3179         { CSSValueOlive, 0xFF808000  },
3180         { CSSValueOrange, 0xFFFFA500 },
3181         { CSSValuePurple, 0xFF800080 },
3182         { CSSValueRed, 0xFFFF0000 },
3183         { CSSValueSilver, 0xFFC0C0C0 },
3184         { CSSValueTeal, 0xFF008080  },
3185         { CSSValueTransparent, 0x00000000 },
3186         { CSSValueWhite, 0xFFFFFFFF },
3187         { CSSValueYellow, 0xFFFFFF00 },
3188         { CSSValueInvalid, CSSValueInvalid }
3189     };
3190
3191     for (const ColorValue* col = colorValues; col->cssValueId; ++col) {
3192         if (col->cssValueId == cssValueId)
3193             return col->color;
3194     }
3195     return RenderTheme::defaultTheme()->systemColor(cssValueId);
3196 }
3197
3198 bool StyleResolver::colorFromPrimitiveValueIsDerivedFromElement(CSSPrimitiveValue* value)
3199 {
3200     int ident = value->getValueID();
3201     switch (ident) {
3202     case CSSValueWebkitText:
3203     case CSSValueWebkitLink:
3204     case CSSValueWebkitActivelink:
3205     case CSSValueCurrentcolor:
3206         return true;
3207     default:
3208         return false;
3209     }
3210 }
3211
3212 Color StyleResolver::colorFromPrimitiveValue(CSSPrimitiveValue* value, bool forVisitedLink) const
3213 {
3214     if (value->isRGBColor())
3215         return Color(value->getRGBA32Value());
3216
3217     const State& state = m_state;
3218     CSSValueID ident = value->getValueID();
3219     switch (ident) {
3220     case 0:
3221         return Color();
3222     case CSSValueWebkitText:
3223         return state.document().textColor();
3224     case CSSValueWebkitLink:
3225         return (state.element()->isLink() && forVisitedLink) ? state.document().visitedLinkColor() : state.document().linkColor();
3226     case CSSValueWebkitActivelink:
3227         return state.document().activeLinkColor();
3228     case CSSValueWebkitFocusRingColor:
3229         return RenderTheme::focusRingColor();
3230     case CSSValueCurrentcolor:
3231         return state.style()->color();
3232     default:
3233         return colorForCSSValue(ident);
3234     }
3235 }
3236
3237 void StyleResolver::addViewportDependentMediaQueryResult(const MediaQueryExp* expr, bool result)
3238 {
3239     m_viewportDependentMediaQueryResults.append(adoptPtr(new MediaQueryResult(*expr, result)));
3240 }
3241
3242 bool StyleResolver::affectedByViewportChange() const
3243 {
3244     unsigned s = m_viewportDependentMediaQueryResults.size();
3245     for (unsigned i = 0; i < s; i++) {
3246         if (m_medium->eval(&m_viewportDependentMediaQueryResults[i]->m_expression) != m_viewportDependentMediaQueryResults[i]->m_result)
3247             return true;
3248     }
3249     return false;
3250 }
3251
3252 #if ENABLE(CSS_FILTERS)
3253 static FilterOperation::OperationType filterOperationForType(WebKitCSSFilterValue::FilterOperationType type)
3254 {
3255     switch (type) {
3256     case WebKitCSSFilterValue::ReferenceFilterOperation:
3257         return FilterOperation::REFERENCE;
3258     case WebKitCSSFilterValue::GrayscaleFilterOperation:
3259         return FilterOperation::GRAYSCALE;
3260     case WebKitCSSFilterValue::SepiaFilterOperation:
3261         return FilterOperation::SEPIA;
3262     case WebKitCSSFilterValue::SaturateFilterOperation:
3263         return FilterOperation::SATURATE;
3264     case WebKitCSSFilterValue::HueRotateFilterOperation:
3265         return FilterOperation::HUE_ROTATE;
3266     case WebKitCSSFilterValue::InvertFilterOperation:
3267         return FilterOperation::INVERT;
3268     case WebKitCSSFilterValue::OpacityFilterOperation:
3269         return FilterOperation::OPACITY;
3270     case WebKitCSSFilterValue::BrightnessFilterOperation:
3271         return FilterOperation::BRIGHTNESS;
3272     case WebKitCSSFilterValue::ContrastFilterOperation:
3273         return FilterOperation::CONTRAST;
3274     case WebKitCSSFilterValue::BlurFilterOperation:
3275         return FilterOperation::BLUR;
3276     case WebKitCSSFilterValue::DropShadowFilterOperation:
3277         return FilterOperation::DROP_SHADOW;
3278 #if ENABLE(CSS_SHADERS)
3279     case WebKitCSSFilterValue::CustomFilterOperation:
3280         return FilterOperation::CUSTOM;
3281 #endif
3282     case WebKitCSSFilterValue::UnknownFilterOperation:
3283         return FilterOperation::NONE;
3284     }
3285     return FilterOperation::NONE;
3286 }
3287
3288 #if ENABLE(CSS_FILTERS) && ENABLE(SVG)
3289 void StyleResolver::loadPendingSVGDocuments()
3290 {
3291     State& state = m_state;
3292
3293     // Crash reports indicate that we've seen calls to this function when our
3294     // style is NULL. We don't know exactly why this happens. Our guess is
3295     // reentering styleForElement().
3296     ASSERT(state.style());
3297     if (!state.style() || !state.style()->hasFilter() || state.pendingSVGDocuments().isEmpty())
3298         return;
3299
3300     CachedResourceLoader* cachedResourceLoader = state.document().cachedResourceLoader();
3301     Vector<RefPtr<FilterOperation> >& filterOperations = state.style()->mutableFilter().operations();
3302     for (unsigned i = 0; i < filterOperations.size(); ++i) {
3303         RefPtr<FilterOperation> filterOperation = filterOperations.at(i);
3304         if (filterOperation->getOperationType() == FilterOperation::REFERENCE) {
3305             ReferenceFilterOperation* referenceFilter = static_cast<ReferenceFilterOperation*>(filterOperation.get());
3306
3307             WebKitCSSSVGDocumentValue* value = state.pendingSVGDocuments().get(referenceFilter);
3308             if (!value)
3309                 continue;
3310             CachedSVGDocument* cachedDocument = value->load(cachedResourceLoader);
3311             if (!cachedDocument)
3312                 continue;
3313
3314             // Stash the CachedSVGDocument on the reference filter.
3315             referenceFilter->setCachedSVGDocumentReference(adoptPtr(new CachedSVGDocumentReference(cachedDocument)));
3316         }
3317     }
3318     state.pendingSVGDocuments().clear();
3319 }
3320 #endif
3321
3322 #if ENABLE(CSS_SHADERS)
3323 StyleShader* StyleResolver::styleShader(CSSValue* value)
3324 {
3325     if (value->isWebKitCSSShaderValue())
3326         return cachedOrPendingStyleShaderFromValue(static_cast<WebKitCSSShaderValue*>(value));
3327     return 0;
3328 }
3329
3330 StyleShader* StyleResolver::cachedOrPendingStyleShaderFromValue(WebKitCSSShaderValue* value)
3331 {
3332     StyleShader* shader = value->cachedOrPendingShader();
3333     if (shader && shader->isPendingShader())
3334         m_state.setHasPendingShaders(true);
3335     return shader;
3336 }
3337
3338 PassRefPtr<CustomFilterProgram> StyleResolver::lookupCustomFilterProgram(WebKitCSSShaderValue* vertexShader, WebKitCSSShaderValue* fragmentShader, 
3339     CustomFilterProgramType programType, const CustomFilterProgramMixSettings& mixSettings, CustomFilterMeshType meshType)
3340 {
3341     CachedResourceLoader* cachedResourceLoader = m_state.document().cachedResourceLoader();
3342     KURL vertexShaderURL = vertexShader ? vertexShader->completeURL(cachedResourceLoader) : KURL();
3343     KURL fragmentShaderURL = fragmentShader ? fragmentShader->completeURL(cachedResourceLoader) : KURL();
3344     RefPtr<StyleCustomFilterProgram> program;
3345     if (m_customFilterProgramCache)
3346         program = m_customFilterProgramCache->lookup(CustomFilterProgramInfo(vertexShaderURL, fragmentShaderURL, programType, mixSettings, meshType));
3347     if (!program) {
3348         // Create a new StyleCustomFilterProgram that will be resolved during the loadPendingShaders and added to the cache.
3349         program = StyleCustomFilterProgram::create(vertexShaderURL, vertexShader ? styleShader(vertexShader) : 0, 
3350             fragmentShaderURL, fragmentShader ? styleShader(fragmentShader) : 0, programType, mixSettings, meshType);
3351     }
3352     return program.release();
3353 }
3354
3355 void StyleResolver::loadPendingShaders()
3356 {
3357     // FIXME: We shouldn't have to check that style is non-null. This is a speculative fix for:
3358     // https://bugs.webkit.org/show_bug.cgi?id=117665
3359     if (!m_state.hasPendingShaders() || !m_state.style() || !m_state.style()->hasFilter())
3360         return;
3361
3362     CachedResourceLoader* cachedResourceLoader = m_state.document().cachedResourceLoader();
3363
3364     Vector<RefPtr<FilterOperation> >& filterOperations = m_state.style()->mutableFilter().operations();
3365     for (unsigned i = 0; i < filterOperations.size(); ++i) {
3366         RefPtr<FilterOperation> filterOperation = filterOperations.at(i);
3367         if (filterOperation->getOperationType() == FilterOperation::CUSTOM) {
3368             CustomFilterOperation* customFilter = static_cast<CustomFilterOperation*>(filterOperation.get());
3369             ASSERT(customFilter->program());
3370             StyleCustomFilterProgram* program = static_cast<StyleCustomFilterProgram*>(customFilter->program());
3371             // Note that the StylePendingShaders could be already resolved to StyleCachedShaders. That's because the rule was matched before.
3372             // However, the StyleCustomFilterProgram that was initially created could have been removed from the cache in the meanwhile,
3373             // meaning that we get a new StyleCustomFilterProgram here that is not yet in the cache, but already has loaded StyleShaders.
3374             if (!program->hasPendingShaders() && program->inCache())
3375                 continue;
3376             if (!m_customFilterProgramCache)
3377                 m_customFilterProgramCache = adoptPtr(new StyleCustomFilterProgramCache());
3378             RefPtr<StyleCustomFilterProgram> styleProgram = m_customFilterProgramCache->lookup(program);
3379             if (styleProgram.get())
3380                 customFilter->setProgram(styleProgram.release());
3381             else {
3382                 if (program->vertexShader() && program->vertexShader()->isPendingShader()) {
3383                     WebKitCSSShaderValue* shaderValue = static_cast<StylePendingShader*>(program->vertexShader())->cssShaderValue();
3384                     program->setVertexShader(shaderValue->cachedShader(cachedResourceLoader));
3385                 }
3386                 if (program->fragmentShader() && program->fragmentShader()->isPendingShader()) {
3387                     WebKitCSSShaderValue* shaderValue = static_cast<StylePendingShader*>(program->fragmentShader())->cssShaderValue();
3388                     program->setFragmentShader(shaderValue->cachedShader(cachedResourceLoader));
3389                 }
3390                 m_customFilterProgramCache->add(program);
3391             }
3392         }
3393     }
3394     m_state.setHasPendingShaders(false);
3395 }
3396
3397 static bool sortParametersByNameComparator(const RefPtr<CustomFilterParameter>& a, const RefPtr<CustomFilterParameter>& b)
3398 {
3399     return codePointCompareLessThan(a->name(), b->name());
3400 }
3401
3402 PassRefPtr<CustomFilterParameter> StyleResolver::parseCustomFilterArrayParameter(const String& name, CSSValueList* values, bool isArray)
3403 {
3404     RefPtr<CustomFilterArrayParameter> arrayParameter = CustomFilterArrayParameter::create(name, isArray ? CustomFilterArrayParameter::ARRAY : CustomFilterArrayParameter::MATRIX);
3405     for (unsigned i = 0, length = values->length(); i < length; ++i) {
3406         CSSValue* value = values->itemWithoutBoundsCheck(i);
3407         if (!value->isPrimitiveValue())
3408             return 0;
3409         CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(value);
3410         if (primitiveValue->primitiveType() != CSSPrimitiveValue::CSS_NUMBER)
3411             return 0;
3412         arrayParameter->addValue(primitiveValue->getDoubleValue());
3413     }
3414     return arrayParameter.release();
3415 }
3416
3417 PassRefPtr<CustomFilterParameter> StyleResolver::parseCustomFilterColorParameter(const String& name, CSSValueList* values)
3418 {
3419     ASSERT(values->length());
3420     CSSPrimitiveValue* firstPrimitiveValue = static_cast<CSSPrimitiveValue*>(values->itemWithoutBoundsCheck(0));
3421     RefPtr<CustomFilterColorParameter> colorParameter = CustomFilterColorParameter::create(name);
3422     colorParameter->setColor(Color(firstPrimitiveValue->getRGBA32Value()));
3423     return colorParameter.release();
3424 }
3425
3426 PassRefPtr<CustomFilterParameter> StyleResolver::parseCustomFilterNumberParameter(const String& name, CSSValueList* values)
3427 {
3428     RefPtr<CustomFilterNumberParameter> numberParameter = CustomFilterNumberParameter::create(name);
3429     for (unsigned i = 0; i < values->length(); ++i) {
3430         CSSValue* value = values->itemWithoutBoundsCheck(i);
3431         if (!value->isPrimitiveValue())
3432             return 0;
3433         CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(value);
3434         if (primitiveValue->primitiveType() != CSSPrimitiveValue::CSS_NUMBER)
3435             return 0;
3436         numberParameter->addValue(primitiveValue->getDoubleValue());
3437     }
3438     return numberParameter.release();
3439 }
3440
3441 PassRefPtr<CustomFilterParameter> StyleResolver::parseCustomFilterTransformParameter(const String& name, CSSValueList* values)
3442 {
3443     RefPtr<CustomFilterTransformParameter> transformParameter = CustomFilterTransformParameter::create(name);
3444     TransformOperations operations;
3445     transformsForValue(m_state.style(), m_state.rootElementStyle(), values, operations);
3446     transformParameter->setOperations(operations);
3447     return transformParameter.release();
3448 }
3449
3450 PassRefPtr<CustomFilterParameter> StyleResolver::parseCustomFilterParameter(const String& name, CSSValue* parameterValue)
3451 {
3452     // FIXME: Implement other parameters types parsing.
3453     // textures: https://bugs.webkit.org/show_bug.cgi?id=71442
3454     // mat2, mat3, mat4: https://bugs.webkit.org/show_bug.cgi?id=71444
3455     // Number parameters are wrapped inside a CSSValueList and all
3456     // the other functions values inherit from CSSValueList.
3457     if (!parameterValue->isValueList())
3458         return 0;
3459
3460     CSSValueList* values = static_cast<CSSValueList*>(parameterValue);
3461     if (!values->length())
3462         return 0;
3463
3464     if (parameterValue->isWebKitCSSArrayFunctionValue())
3465         return parseCustomFilterArrayParameter(name, values, true);
3466
3467     if (parameterValue->isWebKitCSSMatFunctionValue())
3468         return parseCustomFilterArrayParameter(name, values, false);
3469
3470     // If the first value of the list is a transform function,
3471     // then we could safely assume that all the remaining items
3472     // are transforms. parseCustomFilterTransformParameter will
3473     // return 0 if that assumption is incorrect.
3474     if (values->itemWithoutBoundsCheck(0)->isWebKitCSSTransformValue())
3475         return parseCustomFilterTransformParameter(name, values);
3476
3477     // We can only have arrays of colors or numbers, so use the first value to choose between those two.
3478     // We need up to 4 values (all booleans or all numbers).
3479     if (!values->itemWithoutBoundsCheck(0)->isPrimitiveValue() || values->length() > 4)
3480         return 0;
3481     
3482     CSSPrimitiveValue* firstPrimitiveValue = static_cast<CSSPrimitiveValue*>(values->itemWithoutBoundsCheck(0));
3483     if (firstPrimitiveValue->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
3484         return parseCustomFilterNumberParameter(name, values);
3485
3486     if (firstPrimitiveValue->primitiveType() == CSSPrimitiveValue::CSS_RGBCOLOR)
3487         return parseCustomFilterColorParameter(name, values);
3488
3489     return 0;
3490 }
3491
3492 bool StyleResolver::parseCustomFilterParameterList(CSSValue* parametersValue, CustomFilterParameterList& parameterList)
3493 {
3494     HashSet<String> knownParameterNames;
3495     CSSValueListIterator parameterIterator(parametersValue);
3496     for (; parameterIterator.hasMore(); parameterIterator.advance()) {
3497         if (!parameterIterator.value()->isValueList())
3498             return false;
3499         CSSValueListIterator iterator(parameterIterator.value());
3500         if (!iterator.isPrimitiveValue())
3501             return false;
3502         CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(iterator.value());
3503         if (primitiveValue->primitiveType() != CSSPrimitiveValue::CSS_STRING)
3504             return false;
3505         
3506         String name = primitiveValue->getStringValue();
3507         // Do not allow duplicate parameter names.
3508         if (knownParameterNames.contains(name))
3509             return false;
3510         knownParameterNames.add(name);
3511         
3512         iterator.advance();
3513         
3514         if (!iterator.hasMore())
3515             return false;
3516         
3517         RefPtr<CustomFilterParameter> parameter = parseCustomFilterParameter(name, iterator.value());
3518         if (!parameter)
3519             return false;
3520         parameterList.append(parameter.release());
3521     }
3522     
3523     // Make sure we sort the parameters before passing them down to the CustomFilterOperation.
3524     std::sort(parameterList.begin(), parameterList.end(), sortParametersByNameComparator);
3525     
3526     return true;
3527 }
3528
3529 PassRefPtr<CustomFilterOperation> StyleResolver::createCustomFilterOperationWithAtRuleReferenceSyntax(WebKitCSSFilterValue* filterValue)
3530 {
3531     // FIXME: Implement style resolution for the custom filter at-rule reference syntax.
3532     UNUSED_PARAM(filterValue);
3533     return 0;
3534 }
3535
3536 PassRefPtr<CustomFilterOperation> StyleResolver::createCustomFilterOperationWithInlineSyntax(WebKitCSSFilterValue* filterValue)
3537 {
3538     CSSValue* shadersValue = filterValue->itemWithoutBoundsCheck(0);
3539     ASSERT_WITH_SECURITY_IMPLICATION(shadersValue->isValueList());
3540     CSSValueList* shadersList = static_cast<CSSValueList*>(shadersValue);
3541
3542     unsigned shadersListLength = shadersList->length();
3543     ASSERT(shadersListLength);
3544
3545     WebKitCSSShaderValue* vertexShader = toWebKitCSSShaderValue(shadersList->itemWithoutBoundsCheck(0));
3546     WebKitCSSShaderValue* fragmentShader = 0;
3547     CustomFilterProgramType programType = PROGRAM_TYPE_BLENDS_ELEMENT_TEXTURE;
3548     CustomFilterProgramMixSettings mixSettings;
3549
3550     if (shadersListLength > 1) {
3551         CSSValue* fragmentShaderOrMixFunction = shadersList->itemWithoutBoundsCheck(1);
3552         if (fragmentShaderOrMixFunction->isWebKitCSSMixFunctionValue()) {
3553             WebKitCSSMixFunctionValue* mixFunction = static_cast<WebKitCSSMixFunctionValue*>(fragmentShaderOrMixFunction);
3554             CSSValueListIterator iterator(mixFunction);
3555
3556             ASSERT(mixFunction->length());
3557             fragmentShader = toWebKitCSSShaderValue(iterator.value());
3558             iterator.advance();
3559
3560             ASSERT(mixFunction->length() <= 3);
3561             while (iterator.hasMore()) {
3562                 CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(iterator.value());
3563                 if (CSSParser::isBlendMode(primitiveValue->getValueID()))
3564                     mixSettings.blendMode = *primitiveValue;
3565                 else if (CSSParser::isCompositeOperator(primitiveValue->getValueID()))
3566                     mixSettings.compositeOperator = *primitiveValue;
3567                 else
3568                     ASSERT_NOT_REACHED();
3569                 iterator.advance();
3570             }
3571         } else {
3572             programType = PROGRAM_TYPE_NO_ELEMENT_TEXTURE;
3573             fragmentShader = toWebKitCSSShaderValue(fragmentShaderOrMixFunction);
3574         }
3575     }
3576
3577     if (!vertexShader && !fragmentShader)
3578         return 0;
3579     
3580     unsigned meshRows = 1;
3581     unsigned meshColumns = 1;
3582     CustomFilterMeshType meshType = MeshTypeAttached;
3583     
3584     CSSValue* parametersValue = 0;
3585     
3586     if (filterValue->length() > 1) {
3587         CSSValueListIterator iterator(filterValue->itemWithoutBoundsCheck(1));
3588         
3589         // The second value might be the mesh box or the list of parameters:
3590         // If it starts with a number or any of the mesh-box identifiers it is 
3591         // the mesh-box list, if not it means it is the parameters list.
3592
3593         if (iterator.hasMore() && iterator.isPrimitiveValue()) {
3594             CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(iterator.value());
3595             if (primitiveValue->isNumber()) {
3596                 // If only one integer value is specified, it will set both
3597                 // the rows and the columns.
3598                 meshColumns = meshRows = primitiveValue->getIntValue();
3599                 iterator.advance();
3600                 
3601                 // Try to match another number for the rows.
3602                 if (iterator.hasMore() && iterator.isPrimitiveValue()) {
3603                     CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(iterator.value());
3604                     if (primitiveValue->isNumber()) {
3605                         meshRows = primitiveValue->getIntValue();
3606                         iterator.advance();
3607                     }
3608                 }
3609             }
3610         }
3611         
3612         if (iterator.hasMore() && iterator.isPrimitiveValue()) {
3613             CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(iterator.value());
3614             if (primitiveValue->getValueID() == CSSValueDetached) {
3615                 meshType = MeshTypeDetached;
3616                 iterator.advance();
3617             }
3618         }
3619         
3620         if (!iterator.index()) {
3621             // If no value was consumed from the mesh value, then it is just a parameter list, meaning that we end up
3622             // having just two CSSListValues: list of shaders and list of parameters.
3623             ASSERT(filterValue->length() == 2);
3624             parametersValue = filterValue->itemWithoutBoundsCheck(1);
3625         }
3626     }
3627     
3628     if (filterValue->length() > 2 && !parametersValue)
3629         parametersValue = filterValue->itemWithoutBoundsCheck(2);
3630     
3631     CustomFilterParameterList parameterList;
3632     if (parametersValue && !parseCustomFilterParameterList(parametersValue, parameterList))
3633         return 0;
3634
3635     RefPtr<CustomFilterProgram> program = lookupCustomFilterProgram(vertexShader, fragmentShader, programType, mixSettings, meshType);
3636     return CustomFilterOperation::create(program.release(), parameterList, meshRows, meshColumns);
3637 }
3638
3639 PassRefPtr<CustomFilterOperation> StyleResolver::createCustomFilterOperation(WebKitCSSFilterValue* filterValue)
3640 {
3641     ASSERT(filterValue->length());
3642     bool isAtRuleReferenceSyntax = filterValue->itemWithoutBoundsCheck(0)->isPrimitiveValue();
3643     return isAtRuleReferenceSyntax ? createCustomFilterOperationWithAtRuleReferenceSyntax(filterValue) : createCustomFilterOperationWithInlineSyntax(filterValue);
3644 }
3645
3646 #endif
3647
3648 bool StyleResolver::createFilterOperations(CSSValue* inValue, FilterOperations& outOperations)
3649 {
3650     State& state = m_state;
3651     RenderStyle* style = state.style();
3652     RenderStyle* rootStyle = state.rootElementStyle();
3653     ASSERT(outOperations.isEmpty());
3654     
3655     if (!inValue)
3656         return false;
3657     
3658     if (inValue->isPrimitiveValue()) {
3659         CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(inValue);
3660         if (primitiveValue->getValueID() == CSSValueNone)
3661             return true;
3662     }
3663     
3664     if (!inValue->isValueList())
3665         return false;
3666
3667     float zoomFactor = style ? style->effectiveZoom() : 1;
3668     FilterOperations operations;
3669     for (CSSValueListIterator i = inValue; i.hasMore(); i.advance()) {
3670         CSSValue* currValue = i.value();
3671         if (!currValue->isWebKitCSSFilterValue())
3672             continue;
3673
3674         WebKitCSSFilterValue* filterValue = static_cast<WebKitCSSFilterValue*>(i.value());
3675         FilterOperation::OperationType operationType = filterOperationForType(filterValue->operationType());
3676
3677 #if ENABLE(CSS_SHADERS)
3678         if (operationType == FilterOperation::VALIDATED_CUSTOM) {
3679             // ValidatedCustomFilterOperation is not supposed to end up in the RenderStyle.
3680             ASSERT_NOT_REACHED();
3681             continue;
3682         }
3683         if (operationType == FilterOperation::CUSTOM) {
3684             RefPtr<CustomFilterOperation> operation = createCustomFilterOperation(filterValue);
3685             if (!operation)
3686                 return false;
3687             
3688             operations.operations().append(operation);
3689             continue;
3690         }
3691 #endif
3692         if (operationType == FilterOperation::REFERENCE) {
3693 #if ENABLE(SVG)
3694             if (filterValue->length() != 1)
3695                 continue;
3696             CSSValue* argument = filterValue->itemWithoutBoundsCheck(0);
3697
3698             if (!argument->isWebKitCSSSVGDocumentValue())
3699                 continue;
3700
3701             WebKitCSSSVGDocumentValue* svgDocumentValue = static_cast<WebKitCSSSVGDocumentValue*>(argument);
3702             KURL url = m_state.document().completeURL(svgDocumentValue->url());
3703
3704             RefPtr<ReferenceFilterOperation> operation = ReferenceFilterOperation::create(svgDocumentValue->url(), url.fragmentIdentifier(), operationType);
3705             if (SVGURIReference::isExternalURIReference(svgDocumentValue->url(), &m_state.document())) {
3706                 if (!svgDocumentValue->loadRequested())
3707                     m_state.pendingSVGDocuments().set(operation.get(), svgDocumentValue);
3708                 else if (svgDocumentValue->cachedSVGDocument())
3709                     operation->setCachedSVGDocumentReference(adoptPtr(new CachedSVGDocumentReference(svgDocumentValue->cachedSVGDocument())));
3710             }
3711             operations.operations().append(operation);
3712 #endif
3713             continue;
3714         }
3715
3716         // Check that all parameters are primitive values, with the
3717         // exception of drop shadow which has a ShadowValue parameter.
3718         if (operationType != FilterOperation::DROP_SHADOW) {
3719             bool haveNonPrimitiveValue = false;
3720             for (unsigned j = 0; j < filterValue->length(); ++j) {
3721                 if (!filterValue->itemWithoutBoundsCheck(j)->isPrimitiveValue()) {
3722                     haveNonPrimitiveValue = true;
3723                     break;
3724                 }
3725             }
3726             if (haveNonPrimitiveValue)
3727                 continue;
3728         }
3729
3730         CSSPrimitiveValue* firstValue = filterValue->length() ? static_cast<CSSPrimitiveValue*>(filterValue->itemWithoutBoundsCheck(0)) : 0;
3731         switch (filterValue->operationType()) {
3732         case WebKitCSSFilterValue::GrayscaleFilterOperation:
3733         case WebKitCSSFilterValue::SepiaFilterOperation:
3734         case WebKitCSSFilterValue::SaturateFilterOperation: {
3735             double amount = 1;
3736             if (filterValue->length() == 1) {
3737                 amount = firstValue->getDoubleValue();
3738                 if (firstValue->isPercentage())
3739                     amount /= 100;
3740             }
3741
3742             operations.operations().append(BasicColorMatrixFilterOperation::create(amount, operationType));
3743             break;
3744         }
3745         case WebKitCSSFilterValue::HueRotateFilterOperation: {
3746             double angle = 0;
3747             if (filterValue->length() == 1)
3748                 angle = firstValue->computeDegrees();
3749
3750             operations.operations().append(BasicColorMatrixFilterOperation::create(angle, operationType));
3751             break;
3752         }
3753         case WebKitCSSFilterValue::InvertFilterOperation:
3754         case WebKitCSSFilterValue::BrightnessFilterOperation:
3755         case WebKitCSSFilterValue::ContrastFilterOperation:
3756         case WebKitCSSFilterValue::OpacityFilterOperation: {
3757             double amount = (filterValue->operationType() == WebKitCSSFilterValue::BrightnessFilterOperation) ? 0 : 1;
3758             if (filterValue->length() == 1) {
3759                 amount = firstValue->getDoubleValue();
3760                 if (firstValue->isPercentage())
3761                     amount /= 100;
3762             }
3763
3764             operations.operations().append(BasicComponentTransferFilterOperation::create(amount, operationType));
3765             break;
3766         }
3767         case WebKitCSSFilterValue::BlurFilterOperation: {
3768             Length stdDeviation = Length(0, Fixed);
3769             if (filterValue->length() >= 1)
3770                 stdDeviation = convertToFloatLength(firstValue, style, rootStyle, zoomFactor);
3771             if (stdDeviation.isUndefined())
3772                 return false;
3773
3774             operations.operations().append(BlurFilterOperation::create(stdDeviation, operationType));
3775             break;
3776         }
3777         case WebKitCSSFilterValue::DropShadowFilterOperation: {
3778             if (filterValue->length() != 1)
3779                 return false;
3780
3781             CSSValue* cssValue = filterValue->itemWithoutBoundsCheck(0);
3782             if (!cssValue->isShadowValue())
3783                 continue;
3784
3785             ShadowValue* item = static_cast<ShadowValue*>(cssValue);
3786             IntPoint location(item->x->computeLength<int>(style, rootStyle, zoomFactor),
3787                               item->y->computeLength<int>(style, rootStyle, zoomFactor));
3788             int blur = item->blur ? item->blur->computeLength<int>(style, rootStyle, zoomFactor) : 0;
3789             Color color;
3790             if (item->color)
3791                 color = colorFromPrimitiveValue(item->color.get());
3792
3793             operations.operations().append(DropShadowFilterOperation::create(location, blur, color.isValid() ? color : Color::transparent, operationType));
3794             break;
3795         }
3796         case WebKitCSSFilterValue::UnknownFilterOperation:
3797         default:
3798             ASSERT_NOT_REACHED();
3799             break;
3800         }
3801     }
3802
3803     outOperations = operations;
3804     return true;
3805 }
3806
3807 #endif
3808
3809 PassRefPtr<StyleImage> StyleResolver::loadPendingImage(StylePendingImage* pendingImage)
3810 {
3811     CachedResourceLoader* cachedResourceLoader = m_state.document().cachedResourceLoader();
3812
3813     if (pendingImage->cssImageValue()) {
3814         CSSImageValue* imageValue = pendingImage->cssImageValue();
3815         return imageValue->cachedImage(cachedResourceLoader);
3816     }
3817
3818     if (pendingImage->cssImageGeneratorValue()) {
3819         CSSImageGeneratorValue* imageGeneratorValue = pendingImage->cssImageGeneratorValue();
3820         imageGeneratorValue->loadSubimages(cachedResourceLoader);
3821         return StyleGeneratedImage::create(imageGeneratorValue);
3822     }
3823
3824     if (pendingImage->cssCursorImageValue()) {
3825         CSSCursorImageValue* cursorImageValue = pendingImage->cssCursorImageValue();
3826         return cursorImageValue->cachedImage(cachedResourceLoader);
3827     }
3828
3829 #if ENABLE(CSS_IMAGE_SET)
3830     if (pendingImage->cssImageSetValue()) {
3831         CSSImageSetValue* imageSetValue = pendingImage->cssImageSetValue();
3832         return imageSetValue->cachedImageSet(cachedResourceLoader);
3833     }
3834 #endif
3835
3836     return 0;
3837 }
3838
3839
3840 #if ENABLE(CSS_SHAPES)
3841 void StyleResolver::loadPendingShapeImage(ShapeValue* shapeValue)
3842 {
3843     if (!shapeValue)
3844         return;
3845
3846     StyleImage* image = shapeValue->image();
3847     if (!image || !image->isPendingImage())
3848         return;
3849
3850     StylePendingImage* pendingImage = static_cast<StylePendingImage*>(image);
3851     CSSImageValue* cssImageValue =  pendingImage->cssImageValue();
3852     CachedResourceLoader* cachedResourceLoader = m_state.document().cachedResourceLoader();
3853
3854     ResourceLoaderOptions options = CachedResourceLoader::defaultCachedResourceOptions();
3855     options.requestOriginPolicy = RestrictToSameOrigin;
3856
3857     shapeValue->setImage(cssImageValue->cachedImage(cachedResourceLoader, options));
3858 }
3859 #endif
3860
3861 void StyleResolver::loadPendingImages()
3862 {
3863     if (m_state.pendingImageProperties().isEmpty())
3864         return;
3865
3866     PendingImagePropertyMap::const_iterator::Keys end = m_state.pendingImageProperties().end().keys();
3867     for (PendingImagePropertyMap::const_iterator::Keys it = m_state.pendingImageProperties().begin().keys(); it != end; ++it) {
3868         CSSPropertyID currentProperty = *it;
3869
3870         switch (currentProperty) {
3871         case CSSPropertyBackgroundImage: {
3872             for (FillLayer* backgroundLayer = m_state.style()->accessBackgroundLayers(); backgroundLayer; backgroundLayer = backgroundLayer->next()) {
3873                 if (backgroundLayer->image() && backgroundLayer->image()->isPendingImage())
3874                     backgroundLayer->setImage(loadPendingImage(static_cast<StylePendingImage*>(backgroundLayer->image())));
3875             }
3876             break;
3877         }
3878         case CSSPropertyContent: {
3879             for (ContentData* contentData = const_cast<ContentData*>(m_state.style()->contentData()); contentData; contentData = contentData->next()) {
3880                 if (contentData->isImage()) {
3881                     StyleImage* image = static_cast<ImageContentData*>(contentData)->image();
3882                     if (image->isPendingImage()) {
3883                         RefPtr<StyleImage> loadedImage = loadPendingImage(static_cast<StylePendingImage*>(image));
3884                         if (loadedImage)
3885                             static_cast<ImageContentData*>(contentData)->setImage(loadedImage.release());
3886                     }
3887                 }
3888             }
3889             break;
3890         }
3891         case CSSPropertyCursor: {
3892             if (CursorList* cursorList = m_state.style()->cursors()) {
3893                 for (size_t i = 0; i < cursorList->size(); ++i) {
3894                     CursorData& currentCursor = cursorList->at(i);
3895                     if (StyleImage* image = currentCursor.image()) {
3896                         if (image->isPendingImage())
3897                             currentCursor.setImage(loadPendingImage(static_cast<StylePendingImage*>(image)));
3898                     }
3899                 }
3900             }
3901             break;
3902         }
3903         case CSSPropertyListStyleImage: {
3904             if (m_state.style()->listStyleImage() && m_state.style()->listStyleImage()->isPendingImage())
3905                 m_state.style()->setListStyleImage(loadPendingImage(static_cast<StylePendingImage*>(m_state.style()->listStyleImage())));
3906             break;
3907         }
3908         case CSSPropertyBorderImageSource: {
3909             if (m_state.style()->borderImageSource() && m_state.style()->borderImageSource()->isPendingImage())
3910                 m_state.style()->setBorderImageSource(loadPendingImage(static_cast<StylePendingImage*>(m_state.style()->borderImageSource())));
3911             break;
3912         }
3913         case CSSPropertyWebkitBoxReflect: {
3914             if (StyleReflection* reflection = m_state.style()->boxReflect()) {
3915                 const NinePieceImage& maskImage = reflection->mask();
3916                 if (maskImage.image() && maskImage.image()->isPendingImage()) {
3917                     RefPtr<StyleImage> loadedImage = loadPendingImage(static_cast<StylePendingImage*>(maskImage.image()));
3918                     reflection->setMask(NinePieceImage(loadedImage.release(), maskImage.imageSlices(), maskImage.fill(), maskImage.borderSlices(), maskImage.outset(), maskImage.horizontalRule(), maskImage.verticalRule()));
3919                 }
3920             }
3921             break;
3922         }
3923         case CSSPropertyWebkitMaskBoxImageSource: {
3924             if (m_state.style()->maskBoxImageSource() && m_state.style()->maskBoxImageSource()->isPendingImage())
3925                 m_state.style()->setMaskBoxImageSource(loadPendingImage(static_cast<StylePendingImage*>(m_state.style()->maskBoxImageSource())));
3926             break;
3927         }
3928         case CSSPropertyWebkitMaskImage: {
3929             for (FillLayer* maskLayer = m_state.style()->accessMaskLayers(); maskLayer; maskLayer = maskLayer->next()) {
3930                 if (maskLayer->image() && maskLayer->image()->isPendingImage())
3931                     maskLayer->setImage(loadPendingImage(static_cast<StylePendingImage*>(maskLayer->image())));
3932             }
3933             break;
3934         }
3935 #if ENABLE(CSS_SHAPES)
3936         case CSSPropertyWebkitShapeInside:
3937             loadPendingShapeImage(m_state.style()->shapeInside());
3938             break;
3939         case CSSPropertyWebkitShapeOutside:
3940             loadPendingShapeImage(m_state.style()->shapeOutside());
3941             break;
3942 #endif
3943         default:
3944             ASSERT_NOT_REACHED();
3945         }
3946     }
3947
3948     m_state.pendingImageProperties().clear();
3949 }
3950
3951 #ifndef NDEBUG
3952 static bool inLoadPendingResources = false;
3953 #endif
3954
3955 void StyleResolver::loadPendingResources()
3956 {
3957     // We've seen crashes in all three of the functions below. Some of them
3958     // indicate that style() is NULL. This NULL check will cut down on total
3959     // crashes, while the ASSERT will help us find the cause in debug builds.
3960     ASSERT(style());
3961     if (!style())
3962         return;
3963
3964 #ifndef NDEBUG
3965     // Re-entering this function will probably mean trouble. Catch it in debug builds.
3966     ASSERT(!inLoadPendingResources);
3967     inLoadPendingResources = true;