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