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