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