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