f9d1bbcd281812799387156bcab9edc41a9ed7d5
[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, 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                 NamedGridLinesMap::AddResult result = namedGridLines.add(primitiveValue->getStringValue(), Vector<size_t>());
2044                 result.iterator->value.append(currentNamedGridLine);
2045                 continue;
2046             }
2047         }
2048
2049         ++currentNamedGridLine;
2050         GridTrackSize trackSize;
2051         if (!createGridTrackSize(currValue, trackSize, state))
2052             return false;
2053
2054         trackSizes.append(trackSize);
2055     }
2056
2057     // The parser should have rejected any <track-list> without any <track-size> as
2058     // this is not conformant to the syntax.
2059     ASSERT(!trackSizes.isEmpty());
2060     return true;
2061 }
2062
2063
2064 static bool createGridPosition(CSSValue* value, GridPosition& position)
2065 {
2066     // We accept the specification's grammar:
2067     // 'auto' | [ <integer> || <string> ] | [ span && [ <integer> || string ] ] | <ident>
2068     if (value->isPrimitiveValue()) {
2069         CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
2070         // We translate <ident> to <string> during parsing as it makes handling it simpler.
2071         if (primitiveValue->isString()) {
2072             position.setNamedGridArea(primitiveValue->getStringValue());
2073             return true;
2074         }
2075
2076         ASSERT(primitiveValue->getValueID() == CSSValueAuto);
2077         return true;
2078     }
2079
2080     CSSValueList* values = toCSSValueList(value);
2081     ASSERT(values->length());
2082
2083     bool isSpanPosition = false;
2084     // The specification makes the <integer> optional, in which case it default to '1'.
2085     int gridLineNumber = 1;
2086     String gridLineName;
2087
2088     CSSValueListIterator it = values;
2089     CSSPrimitiveValue* currentValue = toCSSPrimitiveValue(it.value());
2090     if (currentValue->getValueID() == CSSValueSpan) {
2091         isSpanPosition = true;
2092         it.advance();
2093         currentValue = it.hasMore() ? toCSSPrimitiveValue(it.value()) : 0;
2094     }
2095
2096     if (currentValue && currentValue->isNumber()) {
2097         gridLineNumber = currentValue->getIntValue();
2098         it.advance();
2099         currentValue = it.hasMore() ? toCSSPrimitiveValue(it.value()) : 0;
2100     }
2101
2102     if (currentValue && currentValue->isString()) {
2103         gridLineName = currentValue->getStringValue();
2104         it.advance();
2105     }
2106
2107     ASSERT(!it.hasMore());
2108     if (isSpanPosition)
2109         position.setSpanPosition(gridLineNumber, gridLineName);
2110     else
2111         position.setExplicitPosition(gridLineNumber, gridLineName);
2112
2113     return true;
2114 }
2115 #endif /* ENABLE(CSS_GRID_LAYOUT) */
2116
2117 void StyleResolver::applyProperty(CSSPropertyID id, CSSValue* value)
2118 {
2119     ASSERT_WITH_MESSAGE(!isExpandedShorthand(id), "Shorthand property id = %d wasn't expanded at parsing time", id);
2120
2121     State& state = m_state;
2122
2123     if (CSSProperty::isDirectionAwareProperty(id)) {
2124         CSSPropertyID newId = CSSProperty::resolveDirectionAwareProperty(id, state.style()->direction(), state.style()->writingMode());
2125         ASSERT(newId != id);
2126         return applyProperty(newId, value);
2127     }
2128
2129     bool isInherit = state.parentStyle() && value->isInheritedValue();
2130     bool isInitial = value->isInitialValue() || (!state.parentStyle() && value->isInheritedValue());
2131
2132     ASSERT(!isInherit || !isInitial); // isInherit -> !isInitial && isInitial -> !isInherit
2133
2134     if (!state.applyPropertyToRegularStyle() && (!state.applyPropertyToVisitedLinkStyle() || !isValidVisitedLinkProperty(id))) {
2135         // Limit the properties that can be applied to only the ones honored by :visited.
2136         return;
2137     }
2138
2139     if (isInherit && !state.parentStyle()->hasExplicitlyInheritedProperties() && !CSSProperty::isInheritedProperty(id))
2140         state.parentStyle()->setHasExplicitlyInheritedProperties();
2141
2142     // Check lookup table for implementations and use when available.
2143     const PropertyHandler& handler = m_deprecatedStyleBuilder.propertyHandler(id);
2144     if (handler.isValid()) {
2145         if (isInherit)
2146             handler.applyInheritValue(id, this);
2147         else if (isInitial)
2148             handler.applyInitialValue(id, this);
2149         else
2150             handler.applyValue(id, this, value);
2151         return;
2152     }
2153
2154     CSSPrimitiveValue* primitiveValue = value->isPrimitiveValue() ? toCSSPrimitiveValue(value) : 0;
2155
2156     float zoomFactor = state.style()->effectiveZoom();
2157
2158     // What follows is a list that maps the CSS properties into their corresponding front-end
2159     // RenderStyle values.
2160     switch (id) {
2161     // lists
2162     case CSSPropertyContent:
2163         // list of string, uri, counter, attr, i
2164         {
2165             // FIXME: In CSS3, it will be possible to inherit content. In CSS2 it is not. This
2166             // note is a reminder that eventually "inherit" needs to be supported.
2167
2168             if (isInitial) {
2169                 state.style()->clearContent();
2170                 return;
2171             }
2172
2173             if (!value->isValueList())
2174                 return;
2175
2176             bool didSet = false;
2177             for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
2178                 CSSValue* item = i.value();
2179                 if (item->isImageGeneratorValue()) {
2180                     if (item->isGradientValue())
2181                         state.style()->setContent(StyleGeneratedImage::create(*toCSSGradientValue(item)->gradientWithStylesResolved(this)), didSet);
2182                     else
2183                         state.style()->setContent(StyleGeneratedImage::create(*toCSSImageGeneratorValue(item)), didSet);
2184                     didSet = true;
2185 #if ENABLE(CSS_IMAGE_SET)
2186                 } else if (item->isImageSetValue()) {
2187                     state.style()->setContent(setOrPendingFromValue(CSSPropertyContent, toCSSImageSetValue(item)), didSet);
2188                     didSet = true;
2189 #endif
2190                 }
2191
2192                 if (item->isImageValue()) {
2193                     state.style()->setContent(cachedOrPendingFromValue(CSSPropertyContent, toCSSImageValue(item)), didSet);
2194                     didSet = true;
2195                     continue;
2196                 }
2197
2198                 if (!item->isPrimitiveValue())
2199                     continue;
2200
2201                 CSSPrimitiveValue* contentValue = toCSSPrimitiveValue(item);
2202
2203                 if (contentValue->isString()) {
2204                     state.style()->setContent(contentValue->getStringValue().impl(), didSet);
2205                     didSet = true;
2206                 } else if (contentValue->isAttr()) {
2207                     // FIXME: Can a namespace be specified for an attr(foo)?
2208                     if (state.style()->styleType() == NOPSEUDO)
2209                         state.style()->setUnique();
2210                     else
2211                         state.parentStyle()->setUnique();
2212                     QualifiedName attr(nullAtom, contentValue->getStringValue().impl(), nullAtom);
2213                     const AtomicString& value = state.element()->getAttribute(attr);
2214                     state.style()->setContent(value.isNull() ? emptyAtom : value.impl(), didSet);
2215                     didSet = true;
2216                     // Register the fact that the attribute value affects the style.
2217                     m_ruleSets.features().attrsInRules.add(attr.localName().impl());
2218                 } else if (contentValue->isCounter()) {
2219                     Counter* counterValue = contentValue->getCounterValue();
2220                     EListStyleType listStyleType = NoneListStyle;
2221                     CSSValueID listStyleIdent = counterValue->listStyleIdent();
2222                     if (listStyleIdent != CSSValueNone)
2223                         listStyleType = static_cast<EListStyleType>(listStyleIdent - CSSValueDisc);
2224                     auto counter = std::make_unique<CounterContent>(counterValue->identifier(), listStyleType, counterValue->separator());
2225                     state.style()->setContent(std::move(counter), didSet);
2226                     didSet = true;
2227                 } else {
2228                     switch (contentValue->getValueID()) {
2229                     case CSSValueOpenQuote:
2230                         state.style()->setContent(OPEN_QUOTE, didSet);
2231                         didSet = true;
2232                         break;
2233                     case CSSValueCloseQuote:
2234                         state.style()->setContent(CLOSE_QUOTE, didSet);
2235                         didSet = true;
2236                         break;
2237                     case CSSValueNoOpenQuote:
2238                         state.style()->setContent(NO_OPEN_QUOTE, didSet);
2239                         didSet = true;
2240                         break;
2241                     case CSSValueNoCloseQuote:
2242                         state.style()->setContent(NO_CLOSE_QUOTE, didSet);
2243                         didSet = true;
2244                         break;
2245                     default:
2246                         // normal and none do not have any effect.
2247                         { }
2248                     }
2249                 }
2250             }
2251             if (!didSet)
2252                 state.style()->clearContent();
2253             return;
2254         }
2255     case CSSPropertyWebkitAlt:
2256         {
2257             bool didSet = false;
2258             if (primitiveValue->isString()) {
2259                 state.style()->setContentAltText(primitiveValue->getStringValue().impl());
2260                 didSet = true;
2261             } else if (primitiveValue->isAttr()) {
2262                 // FIXME: Can a namespace be specified for an attr(foo)?
2263                 if (state.style()->styleType() == NOPSEUDO)
2264                     state.style()->setUnique();
2265                 else
2266                     state.parentStyle()->setUnique();
2267                 QualifiedName attr(nullAtom, primitiveValue->getStringValue().impl(), nullAtom);
2268                 const AtomicString& value = state.element()->getAttribute(attr);
2269                 state.style()->setContentAltText(value.isNull() ? emptyAtom : value.impl());
2270                 didSet = true;
2271                 // Register the fact that the attribute value affects the style.
2272                 m_ruleSets.features().attrsInRules.add(attr.localName().impl());
2273             }
2274             if (!didSet)
2275                 state.style()->setContentAltText(emptyAtom);
2276             return;
2277         }
2278         
2279     case CSSPropertyQuotes:
2280         if (isInherit) {
2281             state.style()->setQuotes(state.parentStyle()->quotes());
2282             return;
2283         }
2284         if (isInitial) {
2285             state.style()->setQuotes(0);
2286             return;
2287         }
2288         if (value->isValueList()) {
2289             CSSValueList* list = toCSSValueList(value);
2290             Vector<std::pair<String, String>> quotes;
2291             for (size_t i = 0; i < list->length(); i += 2) {
2292                 CSSValue* first = list->itemWithoutBoundsCheck(i);
2293                 // item() returns null if out of bounds so this is safe.
2294                 CSSValue* second = list->item(i + 1);
2295                 if (!second)
2296                     continue;
2297                 ASSERT_WITH_SECURITY_IMPLICATION(first->isPrimitiveValue());
2298                 ASSERT_WITH_SECURITY_IMPLICATION(second->isPrimitiveValue());
2299                 String startQuote = toCSSPrimitiveValue(first)->getStringValue();
2300                 String endQuote = toCSSPrimitiveValue(second)->getStringValue();
2301                 quotes.append(std::make_pair(startQuote, endQuote));
2302             }
2303             state.style()->setQuotes(QuotesData::create(quotes));
2304             return;
2305         }
2306         if (primitiveValue) {
2307             if (primitiveValue->getValueID() == CSSValueNone)
2308                 state.style()->setQuotes(QuotesData::create(Vector<std::pair<String, String>>()));
2309         }
2310         return;
2311     // Shorthand properties.
2312     case CSSPropertyFont:
2313         if (isInherit) {
2314             FontDescription fontDescription = state.parentStyle()->fontDescription();
2315             state.style()->setLineHeight(state.parentStyle()->specifiedLineHeight());
2316             state.setLineHeightValue(0);
2317             setFontDescription(fontDescription);
2318         } else if (isInitial) {
2319             Settings* settings = documentSettings();
2320             ASSERT(settings); // If we're doing style resolution, this document should always be in a frame and thus have settings
2321             if (!settings)
2322                 return;
2323             initializeFontStyle(settings);
2324         } else if (primitiveValue) {
2325             state.style()->setLineHeight(RenderStyle::initialLineHeight());
2326             state.setLineHeightValue(0);
2327
2328             FontDescription fontDescription;
2329             RenderTheme::defaultTheme()->systemFont(primitiveValue->getValueID(), fontDescription);
2330
2331             // Double-check and see if the theme did anything. If not, don't bother updating the font.
2332             if (fontDescription.isAbsoluteSize()) {
2333                 // Make sure the rendering mode and printer font settings are updated.
2334                 Settings* settings = documentSettings();
2335                 ASSERT(settings); // If we're doing style resolution, this document should always be in a frame and thus have settings
2336                 if (!settings)
2337                     return;
2338                 fontDescription.setRenderingMode(settings->fontRenderingMode());
2339                 fontDescription.setUsePrinterFont(document().printing() || !settings->screenFontSubstitutionEnabled());
2340
2341                 // Handle the zoom factor.
2342                 fontDescription.setComputedSize(Style::computedFontSizeFromSpecifiedSize(fontDescription.specifiedSize(), fontDescription.isAbsoluteSize(), useSVGZoomRules(), state.style(), document()));
2343                 setFontDescription(fontDescription);
2344             }
2345         } else if (value->isFontValue()) {
2346             CSSFontValue* font = toCSSFontValue(value);
2347             if (!font->style || !font->variant || !font->weight
2348                 || !font->size || !font->lineHeight || !font->family)
2349                 return;
2350             applyProperty(CSSPropertyFontStyle, font->style.get());
2351             applyProperty(CSSPropertyFontVariant, font->variant.get());
2352             applyProperty(CSSPropertyFontWeight, font->weight.get());
2353             // The previous properties can dirty our font but they don't try to read the font's
2354             // properties back, which is safe. However if font-size is using the 'ex' unit, it will
2355             // need query the dirtied font's x-height to get the computed size. To be safe in this
2356             // case, let's just update the font now.
2357             updateFont();
2358             applyProperty(CSSPropertyFontSize, font->size.get());
2359
2360             state.setLineHeightValue(font->lineHeight.get());
2361
2362             applyProperty(CSSPropertyFontFamily, font->family.get());
2363         }
2364         return;
2365
2366     case CSSPropertyBackground:
2367     case CSSPropertyBackgroundPosition:
2368     case CSSPropertyBackgroundRepeat:
2369     case CSSPropertyBorder:
2370     case CSSPropertyBorderBottom:
2371     case CSSPropertyBorderColor:
2372     case CSSPropertyBorderImage:
2373     case CSSPropertyBorderLeft:
2374     case CSSPropertyBorderRadius:
2375     case CSSPropertyBorderRight:
2376     case CSSPropertyBorderSpacing:
2377     case CSSPropertyBorderStyle:
2378     case CSSPropertyBorderTop:
2379     case CSSPropertyBorderWidth:
2380     case CSSPropertyListStyle:
2381     case CSSPropertyMargin:
2382     case CSSPropertyOutline:
2383     case CSSPropertyOverflow:
2384     case CSSPropertyPadding:
2385     case CSSPropertyTransition:
2386     case CSSPropertyWebkitAnimation:
2387     case CSSPropertyWebkitBorderAfter:
2388     case CSSPropertyWebkitBorderBefore:
2389     case CSSPropertyWebkitBorderEnd:
2390     case CSSPropertyWebkitBorderStart:
2391     case CSSPropertyWebkitBorderRadius:
2392     case CSSPropertyWebkitColumns:
2393     case CSSPropertyWebkitColumnRule:
2394     case CSSPropertyWebkitFlex:
2395     case CSSPropertyWebkitFlexFlow:
2396 #if ENABLE(CSS_GRID_LAYOUT)
2397     case CSSPropertyWebkitGridArea:
2398     case CSSPropertyWebkitGridColumn:
2399     case CSSPropertyWebkitGridRow:
2400 #endif
2401     case CSSPropertyWebkitMarginCollapse:
2402     case CSSPropertyWebkitMarquee:
2403     case CSSPropertyWebkitMask:
2404     case CSSPropertyWebkitMaskPosition:
2405     case CSSPropertyWebkitMaskRepeat:
2406     case CSSPropertyWebkitTextEmphasis:
2407     case CSSPropertyWebkitTextStroke:
2408     case CSSPropertyWebkitTransition:
2409     case CSSPropertyWebkitTransformOrigin:
2410         ASSERT(isExpandedShorthand(id));
2411         ASSERT_NOT_REACHED();
2412         break;
2413
2414     // CSS3 Properties
2415     case CSSPropertyTextShadow:
2416     case CSSPropertyBoxShadow:
2417     case CSSPropertyWebkitBoxShadow: {
2418         if (isInherit) {
2419             if (id == CSSPropertyTextShadow)
2420                 return state.style()->setTextShadow(state.parentStyle()->textShadow() ? std::make_unique<ShadowData>(*state.parentStyle()->textShadow()) : nullptr);
2421             return state.style()->setBoxShadow(state.parentStyle()->boxShadow() ? std::make_unique<ShadowData>(*state.parentStyle()->boxShadow()) : nullptr);
2422         }
2423         if (isInitial || primitiveValue) // initial | none
2424             return id == CSSPropertyTextShadow ? state.style()->setTextShadow(nullptr) : state.style()->setBoxShadow(nullptr);
2425
2426         if (!value->isValueList())
2427             return;
2428
2429         for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
2430             CSSValue* currValue = i.value();
2431             if (!currValue->isShadowValue())
2432                 continue;
2433             CSSShadowValue* item = toCSSShadowValue(currValue);
2434             int x = item->x->computeLength<int>(state.style(), state.rootElementStyle(), zoomFactor);
2435             if (item->x->isViewportPercentageLength())
2436                 x = viewportPercentageValue(*item->x, x);
2437             int y = item->y->computeLength<int>(state.style(), state.rootElementStyle(), zoomFactor);
2438             if (item->y->isViewportPercentageLength())
2439                 y = viewportPercentageValue(*item->y, y);
2440             int blur = item->blur ? item->blur->computeLength<int>(state.style(), state.rootElementStyle(), zoomFactor) : 0;
2441             if (item->blur && item->blur->isViewportPercentageLength())
2442                 blur = viewportPercentageValue(*item->blur, blur);
2443             int spread = item->spread ? item->spread->computeLength<int>(state.style(), state.rootElementStyle(), zoomFactor) : 0;
2444             if (item->spread && item->spread->isViewportPercentageLength())
2445                 spread = viewportPercentageValue(*item->spread, spread);
2446             ShadowStyle shadowStyle = item->style && item->style->getValueID() == CSSValueInset ? Inset : Normal;
2447             Color color;
2448             if (item->color)
2449                 color = colorFromPrimitiveValue(item->color.get());
2450             else if (state.style())
2451                 color = state.style()->color();
2452
2453             auto shadowData = std::make_unique<ShadowData>(IntPoint(x, y), blur, spread, shadowStyle, id == CSSPropertyWebkitBoxShadow, color.isValid() ? color : Color::transparent);
2454             if (id == CSSPropertyTextShadow)
2455                 state.style()->setTextShadow(std::move(shadowData), i.index()); // add to the list if this is not the first entry
2456             else
2457                 state.style()->setBoxShadow(std::move(shadowData), i.index()); // add to the list if this is not the first entry
2458         }
2459         return;
2460     }
2461     case CSSPropertyWebkitBoxReflect: {
2462         HANDLE_INHERIT_AND_INITIAL(boxReflect, BoxReflect)
2463         if (primitiveValue) {
2464             state.style()->setBoxReflect(RenderStyle::initialBoxReflect());
2465             return;
2466         }
2467
2468         if (!value->isReflectValue())
2469             return;
2470
2471         CSSReflectValue* reflectValue = toCSSReflectValue(value);
2472         RefPtr<StyleReflection> reflection = StyleReflection::create();
2473         reflection->setDirection(*reflectValue->direction());
2474         if (reflectValue->offset())
2475             reflection->setOffset(reflectValue->offset()->convertToLength<FixedIntegerConversion | PercentConversion | CalculatedConversion>(state.style(), state.rootElementStyle(), zoomFactor));
2476         NinePieceImage mask;
2477         mask.setMaskDefaults();
2478         m_styleMap.mapNinePieceImage(id, reflectValue->mask(), mask);
2479         reflection->setMask(mask);
2480
2481         state.style()->setBoxReflect(reflection.release());
2482         return;
2483     }
2484     case CSSPropertySrc: // Only used in @font-face rules.
2485         return;
2486     case CSSPropertyUnicodeRange: // Only used in @font-face rules.
2487         return;
2488     case CSSPropertyWebkitLocale: {
2489         HANDLE_INHERIT_AND_INITIAL(locale, Locale);
2490         if (!primitiveValue)
2491             return;
2492         if (primitiveValue->getValueID() == CSSValueAuto)
2493             state.style()->setLocale(nullAtom);
2494         else
2495             state.style()->setLocale(primitiveValue->getStringValue());
2496         FontDescription fontDescription = state.style()->fontDescription();
2497         fontDescription.setScript(localeToScriptCodeForFontSelection(state.style()->locale()));
2498         setFontDescription(fontDescription);
2499         return;
2500     }
2501 #if ENABLE(IOS_TEXT_AUTOSIZING)
2502     case CSSPropertyWebkitTextSizeAdjust: {
2503         HANDLE_INHERIT_AND_INITIAL(textSizeAdjust, TextSizeAdjust)
2504         if (!primitiveValue)
2505             return;
2506
2507         if (primitiveValue->getValueID() == CSSValueAuto)
2508             state.style()->setTextSizeAdjust(TextSizeAdjustment(AutoTextSizeAdjustment));
2509         else if (primitiveValue->getValueID() == CSSValueNone)
2510             state.style()->setTextSizeAdjust(TextSizeAdjustment(NoTextSizeAdjustment));
2511         else
2512             state.style()->setTextSizeAdjust(TextSizeAdjustment(primitiveValue->getFloatValue()));
2513
2514         state.setFontDirty(true);
2515         return;
2516     }
2517 #endif
2518 #if ENABLE(DASHBOARD_SUPPORT)
2519     case CSSPropertyWebkitDashboardRegion:
2520     {
2521         HANDLE_INHERIT_AND_INITIAL(dashboardRegions, DashboardRegions)
2522         if (!primitiveValue)
2523             return;
2524
2525         if (primitiveValue->getValueID() == CSSValueNone) {
2526             state.style()->setDashboardRegions(RenderStyle::noneDashboardRegions());
2527             return;
2528         }
2529
2530         DashboardRegion* region = primitiveValue->getDashboardRegionValue();
2531         if (!region)
2532             return;
2533
2534         DashboardRegion* first = region;
2535         while (region) {
2536             Length top = convertToIntLength(region->top(), state.style(), state.rootElementStyle());
2537             Length right = convertToIntLength(region->right(), state.style(), state.rootElementStyle());
2538             Length bottom = convertToIntLength(region->bottom(), state.style(), state.rootElementStyle());
2539             Length left = convertToIntLength(region->left(), state.style(), state.rootElementStyle());
2540
2541             if (top.isUndefined())
2542                 top = Length();
2543             if (right.isUndefined())
2544                 right = Length();
2545             if (bottom.isUndefined())
2546                 bottom = Length();
2547             if (left.isUndefined())
2548                 left = Length();
2549
2550             if (region->m_isCircle)
2551                 state.style()->setDashboardRegion(StyleDashboardRegion::Circle, region->m_label, top, right, bottom, left, region == first ? false : true);
2552             else if (region->m_isRectangle)
2553                 state.style()->setDashboardRegion(StyleDashboardRegion::Rectangle, region->m_label, top, right, bottom, left, region == first ? false : true);
2554             region = region->m_next.get();
2555         }
2556
2557         state.document().setHasAnnotatedRegions(true);
2558
2559         return;
2560     }
2561 #endif
2562     case CSSPropertyWebkitTextStrokeWidth: {
2563         HANDLE_INHERIT_AND_INITIAL(textStrokeWidth, TextStrokeWidth)
2564         float width = 0;
2565         switch (primitiveValue->getValueID()) {
2566         case CSSValueThin:
2567         case CSSValueMedium:
2568         case CSSValueThick: {
2569             double result = 1.0 / 48;
2570             if (primitiveValue->getValueID() == CSSValueMedium)
2571                 result *= 3;
2572             else if (primitiveValue->getValueID() == CSSValueThick)
2573                 result *= 5;
2574             Ref<CSSPrimitiveValue> value(CSSPrimitiveValue::create(result, CSSPrimitiveValue::CSS_EMS));
2575             width = value.get().computeLength<float>(state.style(), state.rootElementStyle(), zoomFactor);
2576             break;
2577         }
2578         default:
2579             width = primitiveValue->computeLength<float>(state.style(), state.rootElementStyle(), zoomFactor);
2580             break;
2581         }
2582         state.style()->setTextStrokeWidth(width);
2583         return;
2584     }
2585     case CSSPropertyWebkitTransform: {
2586         HANDLE_INHERIT_AND_INITIAL(transform, Transform);
2587         TransformOperations operations;
2588         transformsForValue(state.style(), state.rootElementStyle(), value, operations);
2589         state.style()->setTransform(operations);
2590         return;
2591     }
2592     case CSSPropertyWebkitPerspective: {
2593         HANDLE_INHERIT_AND_INITIAL(perspective, Perspective)
2594
2595         if (!primitiveValue)
2596             return;
2597
2598         if (primitiveValue->getValueID() == CSSValueNone) {
2599             state.style()->setPerspective(0);
2600             return;
2601         }
2602
2603         float perspectiveValue;
2604         if (primitiveValue->isLength())
2605             perspectiveValue = primitiveValue->computeLength<float>(state.style(), state.rootElementStyle(), zoomFactor);
2606         else if (primitiveValue->isNumber()) {
2607             // For backward compatibility, treat valueless numbers as px.
2608             Ref<CSSPrimitiveValue> value(CSSPrimitiveValue::create(primitiveValue->getDoubleValue(), CSSPrimitiveValue::CSS_PX));
2609             perspectiveValue = value.get().computeLength<float>(state.style(), state.rootElementStyle(), zoomFactor);
2610         } else
2611             return;
2612
2613         if (perspectiveValue >= 0.0f)
2614             state.style()->setPerspective(perspectiveValue);
2615         return;
2616     }
2617 #if PLATFORM(IOS)
2618     case CSSPropertyWebkitTouchCallout: {
2619         HANDLE_INHERIT_AND_INITIAL(touchCalloutEnabled, TouchCalloutEnabled);
2620         if (!primitiveValue)
2621             break;
2622
2623         state.style()->setTouchCalloutEnabled(primitiveValue->getStringValue().lower() != "none");
2624         return;
2625     }
2626
2627     // FIXME: CSSPropertyWebkitCompositionFillColor shouldn't be iOS-specific. Once we fix up its usage in
2628     // InlineTextBox::paintCompositionBackground() we should move it outside the PLATFORM(IOS)-guard.
2629     // See <https://bugs.webkit.org/show_bug.cgi?id=126296>.
2630     case CSSPropertyWebkitCompositionFillColor: {
2631         HANDLE_INHERIT_AND_INITIAL(compositionFillColor, CompositionFillColor);
2632         if (!primitiveValue)
2633             break;
2634         state.style()->setCompositionFillColor(colorFromPrimitiveValue(primitiveValue));
2635         return;
2636     }
2637 #endif
2638 #if ENABLE(TOUCH_EVENTS)
2639     case CSSPropertyWebkitTapHighlightColor: {
2640         HANDLE_INHERIT_AND_INITIAL(tapHighlightColor, TapHighlightColor);
2641         if (!primitiveValue)
2642             break;
2643
2644         Color col = colorFromPrimitiveValue(primitiveValue);
2645         state.style()->setTapHighlightColor(col);
2646         return;
2647     }
2648 #endif
2649 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
2650     case CSSPropertyWebkitOverflowScrolling: {
2651         HANDLE_INHERIT_AND_INITIAL(useTouchOverflowScrolling, UseTouchOverflowScrolling);
2652         if (!primitiveValue)
2653             break;
2654         state.style()->setUseTouchOverflowScrolling(primitiveValue->getValueID() == CSSValueTouch);
2655         return;
2656     }
2657 #endif
2658     case CSSPropertyInvalid:
2659         return;
2660     case CSSPropertyFontStretch:
2661     case CSSPropertyPage:
2662     case CSSPropertyTextLineThrough:
2663     case CSSPropertyTextLineThroughColor:
2664     case CSSPropertyTextLineThroughMode:
2665     case CSSPropertyTextLineThroughStyle:
2666     case CSSPropertyTextLineThroughWidth:
2667     case CSSPropertyTextOverline:
2668     case CSSPropertyTextOverlineColor:
2669     case CSSPropertyTextOverlineMode:
2670     case CSSPropertyTextOverlineStyle:
2671     case CSSPropertyTextOverlineWidth:
2672     case CSSPropertyTextUnderline:
2673     case CSSPropertyTextUnderlineColor:
2674     case CSSPropertyTextUnderlineMode:
2675     case CSSPropertyTextUnderlineStyle:
2676     case CSSPropertyTextUnderlineWidth:
2677     case CSSPropertyWebkitFontSizeDelta:
2678     case CSSPropertyWebkitTextDecorationsInEffect:
2679         return;
2680
2681     // CSS Text Layout Module Level 3: Vertical writing support
2682     case CSSPropertyWebkitWritingMode: {
2683         HANDLE_INHERIT_AND_INITIAL(writingMode, WritingMode);
2684         
2685         if (primitiveValue)
2686             setWritingMode(*primitiveValue);
2687
2688         // FIXME: It is not ok to modify document state while applying style.
2689         if (state.element() && state.element() == state.document().documentElement())
2690             state.document().setWritingModeSetOnDocumentElement(true);
2691         return;
2692     }
2693
2694     case CSSPropertyWebkitTextOrientation: {
2695         HANDLE_INHERIT_AND_INITIAL(textOrientation, TextOrientation);
2696
2697         if (primitiveValue)
2698             setTextOrientation(*primitiveValue);
2699
2700         return;
2701     }
2702
2703     case CSSPropertyWebkitLineBoxContain: {
2704         HANDLE_INHERIT_AND_INITIAL(lineBoxContain, LineBoxContain)
2705         if (primitiveValue && primitiveValue->getValueID() == CSSValueNone) {
2706             state.style()->setLineBoxContain(LineBoxContainNone);
2707             return;
2708         }
2709
2710         if (!value->isLineBoxContainValue())
2711             return;
2712
2713         state.style()->setLineBoxContain(toCSSLineBoxContainValue(value)->value());
2714         return;
2715     }
2716
2717     // CSS Fonts Module Level 3
2718     case CSSPropertyWebkitFontFeatureSettings: {
2719         if (primitiveValue && primitiveValue->getValueID() == CSSValueNormal) {
2720             setFontDescription(state.style()->fontDescription().makeNormalFeatureSettings());
2721             return;
2722         }
2723
2724         if (!value->isValueList())
2725             return;
2726
2727         FontDescription fontDescription = state.style()->fontDescription();
2728         CSSValueList* list = toCSSValueList(value);
2729         RefPtr<FontFeatureSettings> settings = FontFeatureSettings::create();
2730         int len = list->length();
2731         for (int i = 0; i < len; ++i) {
2732             CSSValue* item = list->itemWithoutBoundsCheck(i);
2733             if (!item->isFontFeatureValue())
2734                 continue;
2735             CSSFontFeatureValue* feature = toCSSFontFeatureValue(item);
2736             settings->append(FontFeature(feature->tag(), feature->value()));
2737         }
2738         fontDescription.setFeatureSettings(settings.release());
2739         setFontDescription(fontDescription);
2740         return;
2741     }
2742
2743 #if ENABLE(CSS_FILTERS)
2744     case CSSPropertyWebkitFilter: {
2745         HANDLE_INHERIT_AND_INITIAL(filter, Filter);
2746         FilterOperations operations;
2747         if (createFilterOperations(value, operations))
2748             state.style()->setFilter(operations);
2749         return;
2750     }
2751 #endif
2752 #if ENABLE(CSS_GRID_LAYOUT)
2753     case CSSPropertyWebkitGridAutoColumns: {
2754         HANDLE_INHERIT_AND_INITIAL(gridAutoColumns, GridAutoColumns);
2755         GridTrackSize trackSize;
2756         if (!createGridTrackSize(value, trackSize, state))
2757             return;
2758         state.style()->setGridAutoColumns(trackSize);
2759         return;
2760     }
2761     case CSSPropertyWebkitGridAutoRows: {
2762         HANDLE_INHERIT_AND_INITIAL(gridAutoRows, GridAutoRows);
2763         GridTrackSize trackSize;
2764         if (!createGridTrackSize(value, trackSize, state))
2765             return;
2766         state.style()->setGridAutoRows(trackSize);
2767         return;
2768     }
2769     case CSSPropertyWebkitGridTemplateColumns: {
2770         if (isInherit) {
2771             m_state.style()->setGridColumns(m_state.parentStyle()->gridColumns());
2772             m_state.style()->setNamedGridColumnLines(m_state.parentStyle()->namedGridColumnLines());
2773             return;
2774         }
2775         if (isInitial) {
2776             m_state.style()->setGridColumns(RenderStyle::initialGridColumns());
2777             m_state.style()->setNamedGridColumnLines(RenderStyle::initialNamedGridColumnLines());
2778             return;
2779         }
2780         Vector<GridTrackSize> trackSizes;
2781         NamedGridLinesMap namedGridLines;
2782         if (!createGridTrackList(value, trackSizes, namedGridLines, state))
2783             return;
2784         state.style()->setGridColumns(trackSizes);
2785         state.style()->setNamedGridColumnLines(namedGridLines);
2786         return;
2787     }
2788     case CSSPropertyWebkitGridTemplateRows: {
2789         if (isInherit) {
2790             m_state.style()->setGridRows(m_state.parentStyle()->gridRows());
2791             m_state.style()->setNamedGridRowLines(m_state.parentStyle()->namedGridRowLines());
2792             return;
2793         }
2794         if (isInitial) {
2795             m_state.style()->setGridRows(RenderStyle::initialGridRows());
2796             m_state.style()->setNamedGridRowLines(RenderStyle::initialNamedGridRowLines());
2797             return;
2798         }
2799         Vector<GridTrackSize> trackSizes;
2800         NamedGridLinesMap namedGridLines;
2801         if (!createGridTrackList(value, trackSizes, namedGridLines, state))
2802             return;
2803         state.style()->setGridRows(trackSizes);
2804         state.style()->setNamedGridRowLines(namedGridLines);
2805         return;
2806     }
2807
2808     case CSSPropertyWebkitGridColumnStart: {
2809         HANDLE_INHERIT_AND_INITIAL(gridItemColumnStart, GridItemColumnStart);
2810         GridPosition columnStartPosition;
2811         if (!createGridPosition(value, columnStartPosition))
2812             return;
2813         state.style()->setGridItemColumnStart(columnStartPosition);
2814         return;
2815     }
2816     case CSSPropertyWebkitGridColumnEnd: {
2817         HANDLE_INHERIT_AND_INITIAL(gridItemColumnEnd, GridItemColumnEnd);
2818         GridPosition columnEndPosition;
2819         if (!createGridPosition(value, columnEndPosition))
2820             return;
2821         state.style()->setGridItemColumnEnd(columnEndPosition);
2822         return;
2823     }
2824
2825     case CSSPropertyWebkitGridRowStart: {
2826         HANDLE_INHERIT_AND_INITIAL(gridItemRowStart, GridItemRowStart);
2827         GridPosition rowStartPosition;
2828         if (!createGridPosition(value, rowStartPosition))
2829             return;
2830         state.style()->setGridItemRowStart(rowStartPosition);
2831         return;
2832     }
2833     case CSSPropertyWebkitGridRowEnd: {
2834         HANDLE_INHERIT_AND_INITIAL(gridItemRowEnd, GridItemRowEnd);
2835         GridPosition rowEndPosition;
2836         if (!createGridPosition(value, rowEndPosition))
2837             return;
2838         state.style()->setGridItemRowEnd(rowEndPosition);
2839         return;
2840     }
2841     case CSSPropertyWebkitGridTemplateAreas: {
2842         if (isInherit) {
2843             state.style()->setNamedGridArea(state.parentStyle()->namedGridArea());
2844             state.style()->setNamedGridAreaRowCount(state.parentStyle()->namedGridAreaRowCount());
2845             state.style()->setNamedGridAreaColumnCount(state.parentStyle()->namedGridAreaColumnCount());
2846             return;
2847         }
2848         if (isInitial) {
2849             state.style()->setNamedGridArea(RenderStyle::initialNamedGridArea());
2850             state.style()->setNamedGridAreaRowCount(RenderStyle::initialNamedGridAreaCount());
2851             state.style()->setNamedGridAreaColumnCount(RenderStyle::initialNamedGridAreaCount());
2852             return;
2853         }
2854
2855         if (value->isPrimitiveValue()) {
2856             ASSERT(toCSSPrimitiveValue(value)->getValueID() == CSSValueNone);
2857             return;
2858         }
2859
2860         CSSGridTemplateAreasValue* gridTemplateValue = toCSSGridTemplateAreasValue(value);
2861         state.style()->setNamedGridArea(gridTemplateValue->gridAreaMap());
2862         state.style()->setNamedGridAreaRowCount(gridTemplateValue->rowCount());
2863         state.style()->setNamedGridAreaColumnCount(gridTemplateValue->columnCount());
2864         return;
2865     }
2866 #endif /* ENABLE(CSS_GRID_LAYOUT) */
2867     // These properties are aliased and DeprecatedStyleBuilder already applied the property on the prefixed version.
2868     case CSSPropertyTransitionDelay:
2869     case CSSPropertyTransitionDuration:
2870     case CSSPropertyTransitionProperty:
2871     case CSSPropertyTransitionTimingFunction:
2872         return;
2873     // These properties are implemented in the DeprecatedStyleBuilder lookup table.
2874     case CSSPropertyBackgroundAttachment:
2875     case CSSPropertyBackgroundClip:
2876     case CSSPropertyBackgroundColor:
2877     case CSSPropertyBackgroundImage:
2878     case CSSPropertyBackgroundOrigin:
2879     case CSSPropertyBackgroundPositionX:
2880     case CSSPropertyBackgroundPositionY:
2881     case CSSPropertyBackgroundRepeatX:
2882     case CSSPropertyBackgroundRepeatY:
2883     case CSSPropertyBackgroundSize:
2884     case CSSPropertyBorderBottomColor:
2885     case CSSPropertyBorderBottomLeftRadius:
2886     case CSSPropertyBorderBottomRightRadius:
2887     case CSSPropertyBorderBottomStyle:
2888     case CSSPropertyBorderBottomWidth:
2889     case CSSPropertyBorderCollapse:
2890     case CSSPropertyBorderImageOutset:
2891     case CSSPropertyBorderImageRepeat:
2892     case CSSPropertyBorderImageSlice:
2893     case CSSPropertyBorderImageSource:
2894     case CSSPropertyBorderImageWidth:
2895     case CSSPropertyBorderLeftColor:
2896     case CSSPropertyBorderLeftStyle:
2897     case CSSPropertyBorderLeftWidth:
2898     case CSSPropertyBorderRightColor:
2899     case CSSPropertyBorderRightStyle:
2900     case CSSPropertyBorderRightWidth:
2901     case CSSPropertyBorderTopColor:
2902     case CSSPropertyBorderTopLeftRadius:
2903     case CSSPropertyBorderTopRightRadius:
2904     case CSSPropertyBorderTopStyle:
2905     case CSSPropertyBorderTopWidth:
2906     case CSSPropertyBottom:
2907     case CSSPropertyBoxSizing:
2908     case CSSPropertyCaptionSide:
2909     case CSSPropertyClear:
2910     case CSSPropertyClip:
2911     case CSSPropertyColor:
2912     case CSSPropertyCounterIncrement:
2913     case CSSPropertyCounterReset:
2914     case CSSPropertyCursor:
2915     case CSSPropertyDirection:
2916     case CSSPropertyDisplay:
2917     case CSSPropertyEmptyCells:
2918     case CSSPropertyFloat:
2919     case CSSPropertyFontSize:
2920     case CSSPropertyFontStyle:
2921     case CSSPropertyFontVariant:
2922     case CSSPropertyFontWeight:
2923     case CSSPropertyHeight:
2924 #if ENABLE(CSS_IMAGE_ORIENTATION)
2925     case CSSPropertyImageOrientation:
2926 #endif
2927     case CSSPropertyImageRendering:
2928 #if ENABLE(CSS_IMAGE_RESOLUTION)
2929     case CSSPropertyImageResolution:
2930 #endif
2931     case CSSPropertyLeft:
2932     case CSSPropertyLetterSpacing:
2933     case CSSPropertyLineHeight:
2934     case CSSPropertyListStyleImage:
2935     case CSSPropertyListStylePosition:
2936     case CSSPropertyListStyleType:
2937     case CSSPropertyMarginBottom:
2938     case CSSPropertyMarginLeft:
2939     case CSSPropertyMarginRight:
2940     case CSSPropertyMarginTop:
2941     case CSSPropertyMaxHeight:
2942     case CSSPropertyMaxWidth:
2943     case CSSPropertyMinHeight:
2944     case CSSPropertyMinWidth:
2945     case CSSPropertyObjectFit:
2946     case CSSPropertyOpacity:
2947     case CSSPropertyOrphans:
2948     case CSSPropertyOutlineColor:
2949     case CSSPropertyOutlineOffset:
2950     case CSSPropertyOutlineStyle:
2951     case CSSPropertyOutlineWidth:
2952     case CSSPropertyOverflowWrap:
2953     case CSSPropertyOverflowX:
2954     case CSSPropertyOverflowY:
2955     case CSSPropertyPaddingBottom:
2956     case CSSPropertyPaddingLeft:
2957     case CSSPropertyPaddingRight:
2958     case CSSPropertyPaddingTop:
2959     case CSSPropertyPageBreakAfter:
2960     case CSSPropertyPageBreakBefore:
2961     case CSSPropertyPageBreakInside:
2962     case CSSPropertyPointerEvents:
2963     case CSSPropertyPosition:
2964     case CSSPropertyResize:
2965     case CSSPropertyRight:
2966     case CSSPropertySize:
2967     case CSSPropertySpeak:
2968     case CSSPropertyTabSize:
2969     case CSSPropertyTableLayout:
2970     case CSSPropertyTextAlign:
2971     case CSSPropertyTextDecoration:
2972     case CSSPropertyTextIndent:
2973     case CSSPropertyTextOverflow:
2974     case CSSPropertyTextRendering:
2975     case CSSPropertyTextTransform:
2976     case CSSPropertyTop:
2977     case CSSPropertyUnicodeBidi:
2978     case CSSPropertyVerticalAlign:
2979     case CSSPropertyVisibility:
2980     case CSSPropertyWebkitAnimationDelay:
2981     case CSSPropertyWebkitAnimationDirection:
2982     case CSSPropertyWebkitAnimationDuration:
2983     case CSSPropertyWebkitAnimationFillMode:
2984     case CSSPropertyWebkitAnimationIterationCount:
2985     case CSSPropertyWebkitAnimationName:
2986     case CSSPropertyWebkitAnimationPlayState:
2987     case CSSPropertyWebkitAnimationTimingFunction:
2988     case CSSPropertyWebkitAppearance:
2989     case CSSPropertyWebkitAspectRatio:
2990     case CSSPropertyWebkitBackfaceVisibility:
2991     case CSSPropertyWebkitBackgroundClip:
2992     case CSSPropertyWebkitBackgroundComposite:
2993     case CSSPropertyWebkitBackgroundOrigin:
2994     case CSSPropertyWebkitBackgroundSize:
2995     case CSSPropertyWebkitBorderFit:
2996     case CSSPropertyWebkitBorderHorizontalSpacing:
2997     case CSSPropertyWebkitBorderImage:
2998     case CSSPropertyWebkitBorderVerticalSpacing:
2999     case CSSPropertyWebkitBoxAlign:
3000 #if ENABLE(CSS_BOX_DECORATION_BREAK)
3001     case CSSPropertyWebkitBoxDecorationBreak:
3002 #endif
3003     case CSSPropertyWebkitBoxDirection:
3004     case CSSPropertyWebkitBoxFlex:
3005     case CSSPropertyWebkitBoxFlexGroup:
3006     case CSSPropertyWebkitBoxLines:
3007     case CSSPropertyWebkitBoxOrdinalGroup:
3008     case CSSPropertyWebkitBoxOrient:
3009     case CSSPropertyWebkitBoxPack:
3010     case CSSPropertyWebkitColorCorrection:
3011     case CSSPropertyWebkitColumnAxis:
3012     case CSSPropertyWebkitColumnBreakAfter:
3013     case CSSPropertyWebkitColumnBreakBefore:
3014     case CSSPropertyWebkitColumnBreakInside:
3015     case CSSPropertyWebkitColumnCount:
3016     case CSSPropertyWebkitColumnGap:
3017     case CSSPropertyWebkitColumnProgression:
3018     case CSSPropertyWebkitColumnRuleColor:
3019     case CSSPropertyWebkitColumnRuleStyle:
3020     case CSSPropertyWebkitColumnRuleWidth:
3021     case CSSPropertyWebkitColumnSpan:
3022     case CSSPropertyWebkitColumnWidth:
3023 #if ENABLE(CURSOR_VISIBILITY)
3024     case CSSPropertyWebkitCursorVisibility:
3025 #endif
3026     case CSSPropertyWebkitAlignContent:
3027     case CSSPropertyWebkitAlignItems:
3028     case CSSPropertyWebkitAlignSelf:
3029     case CSSPropertyWebkitFlexBasis:
3030     case CSSPropertyWebkitFlexDirection:
3031     case CSSPropertyWebkitFlexGrow:
3032     case CSSPropertyWebkitFlexShrink:
3033     case CSSPropertyWebkitFlexWrap:
3034     case CSSPropertyWebkitJustifyContent:
3035     case CSSPropertyWebkitOrder:
3036 #if ENABLE(CSS_REGIONS)
3037     case CSSPropertyWebkitFlowFrom:
3038     case CSSPropertyWebkitFlowInto:
3039 #endif
3040     case CSSPropertyWebkitFontKerning:
3041     case CSSPropertyWebkitFontSmoothing:
3042     case CSSPropertyWebkitFontVariantLigatures:
3043     case CSSPropertyWebkitHyphenateCharacter:
3044     case CSSPropertyWebkitHyphenateLimitAfter:
3045     case CSSPropertyWebkitHyphenateLimitBefore:
3046     case CSSPropertyWebkitHyphenateLimitLines:
3047     case CSSPropertyWebkitHyphens:
3048     case CSSPropertyWebkitLineAlign:
3049     case CSSPropertyWebkitLineBreak:
3050     case CSSPropertyWebkitLineClamp:
3051     case CSSPropertyWebkitLineGrid:
3052     case CSSPropertyWebkitLineSnap:
3053     case CSSPropertyWebkitMarqueeDirection:
3054     case CSSPropertyWebkitMarqueeIncrement:
3055     case CSSPropertyWebkitMarqueeRepetition:
3056     case CSSPropertyWebkitMarqueeSpeed:
3057     case CSSPropertyWebkitMarqueeStyle:
3058     case CSSPropertyWebkitMaskBoxImage:
3059     case CSSPropertyWebkitMaskBoxImageOutset:
3060     case CSSPropertyWebkitMaskBoxImageRepeat:
3061     case CSSPropertyWebkitMaskBoxImageSlice:
3062     case CSSPropertyWebkitMaskBoxImageSource:
3063     case CSSPropertyWebkitMaskBoxImageWidth:
3064     case CSSPropertyWebkitMaskClip:
3065     case CSSPropertyWebkitMaskComposite:
3066     case CSSPropertyWebkitMaskImage:
3067     case CSSPropertyWebkitMaskOrigin:
3068     case CSSPropertyWebkitMaskPositionX:
3069     case CSSPropertyWebkitMaskPositionY:
3070     case CSSPropertyWebkitMaskRepeatX:
3071     case CSSPropertyWebkitMaskRepeatY:
3072     case CSSPropertyWebkitMaskSize:
3073     case CSSPropertyWebkitMaskSourceType:
3074     case CSSPropertyWebkitNbspMode:
3075     case CSSPropertyWebkitPerspectiveOrigin:
3076     case CSSPropertyWebkitPerspectiveOriginX:
3077     case CSSPropertyWebkitPerspectiveOriginY:
3078     case CSSPropertyWebkitPrintColorAdjust:
3079 #if ENABLE(CSS_REGIONS)
3080     case CSSPropertyWebkitRegionBreakAfter:
3081     case CSSPropertyWebkitRegionBreakBefore:
3082     case CSSPropertyWebkitRegionBreakInside:
3083     case CSSPropertyWebkitRegionFragment:
3084 #endif
3085     case CSSPropertyWebkitRtlOrdering:
3086     case CSSPropertyWebkitRubyPosition:
3087     case CSSPropertyWebkitTextCombine:
3088 #if ENABLE(CSS3_TEXT)
3089     case CSSPropertyWebkitTextAlignLast:
3090     case CSSPropertyWebkitTextJustify:
3091 #endif // CSS3_TEXT
3092     case CSSPropertyWebkitTextDecorationLine:
3093     case CSSPropertyWebkitTextDecorationStyle:
3094     case CSSPropertyWebkitTextDecorationColor:
3095     case CSSPropertyWebkitTextDecorationSkip:
3096     case CSSPropertyWebkitTextUnderlinePosition:
3097     case CSSPropertyWebkitTextEmphasisColor:
3098     case CSSPropertyWebkitTextEmphasisPosition:
3099     case CSSPropertyWebkitTextEmphasisStyle:
3100     case CSSPropertyWebkitTextFillColor:
3101     case CSSPropertyWebkitTextSecurity:
3102     case CSSPropertyWebkitTextStrokeColor:
3103     case CSSPropertyWebkitTransformOriginX:
3104     case CSSPropertyWebkitTransformOriginY:
3105     case CSSPropertyWebkitTransformOriginZ:
3106     case CSSPropertyWebkitTransformStyle:
3107     case CSSPropertyWebkitTransitionDelay:
3108     case CSSPropertyWebkitTransitionDuration:
3109     case CSSPropertyWebkitTransitionProperty:
3110     case CSSPropertyWebkitTransitionTimingFunction:
3111     case CSSPropertyWebkitUserDrag:
3112     case CSSPropertyWebkitUserModify:
3113     case CSSPropertyWebkitUserSelect:
3114     case CSSPropertyWebkitClipPath:
3115 #if ENABLE(CSS_SHAPES) && ENABLE(CSS_SHAPE_INSIDE)
3116     case CSSPropertyWebkitShapeInside:
3117     case CSSPropertyWebkitShapePadding:
3118 #endif
3119 #if ENABLE(CSS_SHAPES)
3120     case CSSPropertyWebkitShapeMargin:
3121     case CSSPropertyWebkitShapeImageThreshold:
3122     case CSSPropertyWebkitShapeOutside:
3123 #endif
3124 #if ENABLE(CSS_EXCLUSIONS)
3125     case CSSPropertyWebkitWrapFlow:
3126     case CSSPropertyWebkitWrapThrough:
3127 #endif
3128     case CSSPropertyWhiteSpace:
3129     case CSSPropertyWidows:
3130     case CSSPropertyWidth:
3131     case CSSPropertyWordBreak:
3132     case CSSPropertyWordSpacing:
3133     case CSSPropertyWordWrap:
3134     case CSSPropertyZIndex:
3135     case CSSPropertyZoom:
3136 #if ENABLE(CSS_DEVICE_ADAPTATION)
3137     case CSSPropertyMaxZoom:
3138     case CSSPropertyMinZoom:
3139     case CSSPropertyOrientation:
3140     case CSSPropertyUserZoom:
3141 #endif
3142         ASSERT_NOT_REACHED();
3143         return;
3144     default:
3145         // Try the SVG properties
3146         applySVGProperty(id, value);
3147         return;
3148     }
3149 }
3150
3151 PassRefPtr<StyleImage> StyleResolver::styleImage(CSSPropertyID property, CSSValue* value)
3152 {
3153     if (value->isImageValue())
3154         return cachedOrPendingFromValue(property, toCSSImageValue(value));
3155
3156     if (value->isImageGeneratorValue()) {
3157         if (value->isGradientValue())
3158             return generatedOrPendingFromValue(property, *toCSSGradientValue(value)->gradientWithStylesResolved(this));
3159         return generatedOrPendingFromValue(property, toCSSImageGeneratorValue(*value));
3160     }
3161
3162 #if ENABLE(CSS_IMAGE_SET)
3163     if (value->isImageSetValue())
3164         return setOrPendingFromValue(property, toCSSImageSetValue(value));
3165 #endif
3166
3167     if (value->isCursorImageValue())
3168         return cursorOrPendingFromValue(property, toCSSCursorImageValue(value));
3169
3170     return 0;
3171 }
3172
3173 PassRefPtr<StyleImage> StyleResolver::cachedOrPendingFromValue(CSSPropertyID property, CSSImageValue* value)
3174 {
3175     RefPtr<StyleImage> image = value->cachedOrPendingImage();
3176     if (image && image->isPendingImage())
3177         m_state.pendingImageProperties().set(property, value);
3178     return image.release();
3179 }
3180
3181 PassRefPtr<StyleImage> StyleResolver::generatedOrPendingFromValue(CSSPropertyID property, CSSImageGeneratorValue& value)
3182 {
3183 #if ENABLE(CSS_FILTERS)
3184     if (value.isFilterImageValue()) {
3185         // FilterImage needs to calculate FilterOperations.
3186         toCSSFilterImageValue(value).createFilterOperations(this);
3187     }
3188 #endif
3189     if (value.isPending()) {
3190         m_state.pendingImageProperties().set(property, &value);
3191         return StylePendingImage::create(&value);
3192     }
3193     return StyleGeneratedImage::create(value);
3194 }
3195
3196 #if ENABLE(CSS_IMAGE_SET)
3197 PassRefPtr<StyleImage> StyleResolver::setOrPendingFromValue(CSSPropertyID property, CSSImageSetValue* value)
3198 {
3199     RefPtr<StyleImage> image = value->cachedOrPendingImageSet(document());
3200     if (image && image->isPendingImage())
3201         m_state.pendingImageProperties().set(property, value);
3202     return image.release();
3203 }
3204 #endif
3205
3206 PassRefPtr<StyleImage> StyleResolver::cursorOrPendingFromValue(CSSPropertyID property, CSSCursorImageValue* value)
3207 {
3208     RefPtr<StyleImage> image = value->cachedOrPendingImage(document());
3209     if (image && image->isPendingImage())
3210         m_state.pendingImageProperties().set(property, value);
3211     return image.release();
3212 }
3213
3214 #if ENABLE(IOS_TEXT_AUTOSIZING)
3215 void StyleResolver::checkForTextSizeAdjust(RenderStyle* style)
3216 {
3217     if (style->textSizeAdjust().isAuto())
3218         return;
3219
3220     FontDescription newFontDescription(style->fontDescription());
3221     if (!style->textSizeAdjust().isNone())
3222         newFontDescription.setComputedSize(newFontDescription.specifiedSize() * style->textSizeAdjust().multiplier());
3223     else
3224         newFontDescription.setComputedSize(newFontDescription.specifiedSize());
3225     style->setFontDescription(newFontDescription);
3226 }
3227 #endif
3228
3229 void StyleResolver::checkForZoomChange(RenderStyle* style, RenderStyle* parentStyle)
3230 {
3231     if (!parentStyle)
3232         return;
3233     
3234     if (style->effectiveZoom() == parentStyle->effectiveZoom())
3235         return;
3236
3237     const FontDescription& childFont = style->fontDescription();
3238     FontDescription newFontDescription(childFont);
3239     setFontSize(newFontDescription, childFont.specifiedSize());
3240     style->setFontDescription(newFontDescription);
3241 }
3242
3243 void StyleResolver::checkForGenericFamilyChange(RenderStyle* style, RenderStyle* parentStyle)
3244 {
3245     const FontDescription& childFont = style->fontDescription();
3246
3247     if (childFont.isAbsoluteSize() || !parentStyle)
3248         return;
3249
3250     const FontDescription& parentFont = parentStyle->fontDescription();
3251     if (childFont.useFixedDefaultSize() == parentFont.useFixedDefaultSize())
3252         return;
3253
3254     // For now, lump all families but monospace together.
3255     if (childFont.genericFamily() != FontDescription::MonospaceFamily
3256         && parentFont.genericFamily() != FontDescription::MonospaceFamily)
3257         return;
3258
3259     // We know the parent is monospace or the child is monospace, and that font
3260     // size was unspecified. We want to scale our font size as appropriate.
3261     // If the font uses a keyword size, then we refetch from the table rather than
3262     // multiplying by our scale factor.
3263     float size;
3264     if (childFont.keywordSize())
3265         size = Style::fontSizeForKeyword(CSSValueXxSmall + childFont.keywordSize() - 1, childFont.useFixedDefaultSize(), document());
3266     else {
3267         Settings* settings = documentSettings();
3268         float fixedScaleFactor = (settings && settings->defaultFixedFontSize() && settings->defaultFontSize())
3269             ? static_cast<float>(settings->defaultFixedFontSize()) / settings->defaultFontSize()
3270             : 1;
3271         size = parentFont.useFixedDefaultSize() ?
3272                 childFont.specifiedSize() / fixedScaleFactor :
3273                 childFont.specifiedSize() * fixedScaleFactor;
3274     }
3275
3276     FontDescription newFontDescription(childFont);
3277     setFontSize(newFontDescription, size);
3278     style->setFontDescription(newFontDescription);
3279 }
3280
3281 void StyleResolver::initializeFontStyle(Settings* settings)
3282 {
3283     FontDescription fontDescription;
3284     fontDescription.setGenericFamily(FontDescription::StandardFamily);
3285     fontDescription.setRenderingMode(settings->fontRenderingMode());
3286     fontDescription.setUsePrinterFont(document().printing() || !settings->screenFontSubstitutionEnabled());
3287     const AtomicString& standardFontFamily = documentSettings()->standardFontFamily();
3288     if (!standardFontFamily.isEmpty())
3289         fontDescription.setOneFamily(standardFontFamily);
3290     fontDescription.setKeywordSize(CSSValueMedium - CSSValueXxSmall + 1);
3291     setFontSize(fontDescription, Style::fontSizeForKeyword(CSSValueMedium, false, document()));
3292     m_state.style()->setLineHeight(RenderStyle::initialLineHeight());
3293     m_state.setLineHeightValue(0);
3294     setFontDescription(fontDescription);
3295 }
3296
3297 void StyleResolver::setFontSize(FontDescription& fontDescription, float size)
3298 {
3299     fontDescription.setSpecifiedSize(size);
3300     fontDescription.setComputedSize(Style::computedFontSizeFromSpecifiedSize(size, fontDescription.isAbsoluteSize(), useSVGZoomRules(), m_state.style(), document()));
3301 }
3302
3303 static Color colorForCSSValue(CSSValueID cssValueId)
3304 {
3305     struct ColorValue {
3306         CSSValueID cssValueId;
3307         RGBA32 color;
3308     };
3309
3310     static const ColorValue colorValues[] = {
3311         { CSSValueAqua, 0xFF00FFFF },
3312         { CSSValueBlack, 0xFF000000 },
3313         { CSSValueBlue, 0xFF0000FF },
3314         { CSSValueFuchsia, 0xFFFF00FF },
3315         { CSSValueGray, 0xFF808080 },
3316         { CSSValueGreen, 0xFF008000  },
3317         { CSSValueGrey, 0xFF808080 },
3318         { CSSValueLime, 0xFF00FF00 },
3319         { CSSValueMaroon, 0xFF800000 },
3320         { CSSValueNavy, 0xFF000080 },
3321         { CSSValueOlive, 0xFF808000  },
3322         { CSSValueOrange, 0xFFFFA500 },
3323         { CSSValuePurple, 0xFF800080 },
3324         { CSSValueRed, 0xFFFF0000 },
3325         { CSSValueSilver, 0xFFC0C0C0 },
3326         { CSSValueTeal, 0xFF008080  },
3327         { CSSValueTransparent, 0x00000000 },
3328         { CSSValueWhite, 0xFFFFFFFF },
3329         { CSSValueYellow, 0xFFFFFF00 },
3330         { CSSValueInvalid, CSSValueInvalid }
3331     };
3332
3333     for (const ColorValue* col = colorValues; col->cssValueId; ++col) {
3334         if (col->cssValueId == cssValueId)
3335             return col->color;
3336     }
3337     return RenderTheme::defaultTheme()->systemColor(cssValueId);
3338 }
3339
3340 bool StyleResolver::colorFromPrimitiveValueIsDerivedFromElement(CSSPrimitiveValue* value)
3341 {
3342     int ident = value->getValueID();
3343     switch (ident) {
3344     case CSSValueWebkitText:
3345     case CSSValueWebkitLink:
3346     case CSSValueWebkitActivelink:
3347     case CSSValueCurrentcolor:
3348         return true;
3349     default:
3350         return false;
3351     }
3352 }
3353
3354 Color StyleResolver::colorFromPrimitiveValue(CSSPrimitiveValue* value, bool forVisitedLink) const
3355 {
3356     if (value->isRGBColor())
3357         return Color(value->getRGBA32Value());
3358
3359     const State& state = m_state;
3360     CSSValueID ident = value->getValueID();
3361     switch (ident) {
3362     case 0:
3363         return Color();
3364     case CSSValueWebkitText:
3365         return state.document().textColor();
3366     case CSSValueWebkitLink:
3367         return (state.element()->isLink() && forVisitedLink) ? state.document().visitedLinkColor() : state.document().linkColor();
3368     case CSSValueWebkitActivelink:
3369         return state.document().activeLinkColor();
3370     case CSSValueWebkitFocusRingColor:
3371         return RenderTheme::focusRingColor();
3372     case CSSValueCurrentcolor:
3373         return state.style()->color();
3374     default:
3375         return colorForCSSValue(ident);
3376     }
3377 }
3378
3379 void StyleResolver::addViewportDependentMediaQueryResult(const MediaQueryExp* expr, bool result)
3380 {
3381     m_viewportDependentMediaQueryResults.append(std::make_unique<MediaQueryResult>(*expr, result));
3382 }
3383
3384 bool StyleResolver::affectedByViewportChange() const
3385 {
3386     unsigned s = m_viewportDependentMediaQueryResults.size();
3387     for (unsigned i = 0; i < s; i++) {
3388         if (m_medium->eval(&m_viewportDependentMediaQueryResults[i]->m_expression) != m_viewportDependentMediaQueryResults[i]->m_result)
3389             return true;
3390     }
3391     return false;
3392 }
3393
3394 #if ENABLE(CSS_FILTERS)
3395 static FilterOperation::OperationType filterOperationForType(WebKitCSSFilterValue::FilterOperationType type)
3396 {
3397     switch (type) {
3398     case WebKitCSSFilterValue::ReferenceFilterOperation:
3399         return FilterOperation::REFERENCE;
3400     case WebKitCSSFilterValue::GrayscaleFilterOperation:
3401         return FilterOperation::GRAYSCALE;
3402     case WebKitCSSFilterValue::SepiaFilterOperation:
3403         return FilterOperation::SEPIA;
3404     case WebKitCSSFilterValue::SaturateFilterOperation:
3405         return FilterOperation::SATURATE;
3406     case WebKitCSSFilterValue::HueRotateFilterOperation:
3407         return FilterOperation::HUE_ROTATE;
3408     case WebKitCSSFilterValue::InvertFilterOperation:
3409         return FilterOperation::INVERT;
3410     case WebKitCSSFilterValue::OpacityFilterOperation:
3411         return FilterOperation::OPACITY;
3412     case WebKitCSSFilterValue::BrightnessFilterOperation:
3413         return FilterOperation::BRIGHTNESS;
3414     case WebKitCSSFilterValue::ContrastFilterOperation:
3415         return FilterOperation::CONTRAST;
3416     case WebKitCSSFilterValue::BlurFilterOperation:
3417         return FilterOperation::BLUR;
3418     case WebKitCSSFilterValue::DropShadowFilterOperation:
3419         return FilterOperation::DROP_SHADOW;
3420     case WebKitCSSFilterValue::UnknownFilterOperation:
3421         return FilterOperation::NONE;
3422     }
3423     return FilterOperation::NONE;
3424 }
3425
3426 void StyleResolver::loadPendingSVGDocuments()
3427 {
3428     State& state = m_state;
3429
3430     // Crash reports indicate that we've seen calls to this function when our
3431     // style is NULL. We don't know exactly why this happens. Our guess is
3432     // reentering styleForElement().
3433     ASSERT(state.style());
3434     if (!state.style() || !state.style()->hasFilter() || state.filtersWithPendingSVGDocuments().isEmpty())
3435         return;
3436
3437     CachedResourceLoader* cachedResourceLoader = state.document().cachedResourceLoader();
3438     for (auto& filterOperation : state.filtersWithPendingSVGDocuments())
3439         filterOperation->getOrCreateCachedSVGDocumentReference()->load(cachedResourceLoader);
3440
3441     state.filtersWithPendingSVGDocuments().clear();
3442 }
3443
3444 bool StyleResolver::createFilterOperations(CSSValue* inValue, FilterOperations& outOperations)
3445 {
3446     State& state = m_state;
3447     RenderStyle* style = state.style();
3448     RenderStyle* rootStyle = state.rootElementStyle();
3449     ASSERT(outOperations.isEmpty());
3450     
3451     if (!inValue)
3452         return false;
3453     
3454     if (inValue->isPrimitiveValue()) {
3455         CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(inValue);
3456         if (primitiveValue->getValueID() == CSSValueNone)
3457             return true;
3458     }
3459     
3460     if (!inValue->isValueList())
3461         return false;
3462
3463     float zoomFactor = style ? style->effectiveZoom() : 1;
3464     FilterOperations operations;
3465     for (CSSValueListIterator i = inValue; i.hasMore(); i.advance()) {
3466         CSSValue* currValue = i.value();
3467         if (!currValue->isWebKitCSSFilterValue())
3468             continue;
3469
3470         WebKitCSSFilterValue* filterValue = toWebKitCSSFilterValue(i.value());
3471         FilterOperation::OperationType operationType = filterOperationForType(filterValue->operationType());
3472
3473         if (operationType == FilterOperation::REFERENCE) {
3474             if (filterValue->length() != 1)
3475                 continue;
3476             CSSValue* argument = filterValue->itemWithoutBoundsCheck(0);
3477
3478             if (!argument->isPrimitiveValue())
3479                 continue;
3480
3481             CSSPrimitiveValue& primitiveValue = toCSSPrimitiveValue(*argument);
3482             String cssUrl = primitiveValue.getStringValue();
3483             URL url = m_state.document().completeURL(cssUrl);
3484
3485             RefPtr<ReferenceFilterOperation> operation = ReferenceFilterOperation::create(cssUrl, url.fragmentIdentifier(), operationType);
3486             if (SVGURIReference::isExternalURIReference(cssUrl, m_state.document()))
3487                 state.filtersWithPendingSVGDocuments().append(operation);
3488
3489             operations.operations().append(operation);
3490             continue;
3491         }
3492
3493         // Check that all parameters are primitive values, with the
3494         // exception of drop shadow which has a CSSShadowValue parameter.
3495         CSSPrimitiveValue* firstValue = nullptr;
3496         if (operationType != FilterOperation::DROP_SHADOW) {
3497             bool haveNonPrimitiveValue = false;
3498             for (unsigned j = 0; j < filterValue->length(); ++j) {
3499                 if (!filterValue->itemWithoutBoundsCheck(j)->isPrimitiveValue()) {
3500                     haveNonPrimitiveValue = true;
3501                     break;
3502                 }
3503             }
3504             if (haveNonPrimitiveValue)
3505                 continue;
3506             if (filterValue->length())
3507                 firstValue = toCSSPrimitiveValue(filterValue->itemWithoutBoundsCheck(0));
3508         }
3509
3510         switch (filterValue->operationType()) {
3511         case WebKitCSSFilterValue::GrayscaleFilterOperation:
3512         case WebKitCSSFilterValue::SepiaFilterOperation:
3513         case WebKitCSSFilterValue::SaturateFilterOperation: {
3514             double amount = 1;
3515             if (filterValue->length() == 1) {
3516                 amount = firstValue->getDoubleValue();
3517                 if (firstValue->isPercentage())
3518                     amount /= 100;
3519             }
3520
3521             operations.operations().append(BasicColorMatrixFilterOperation::create(amount, operationType));
3522             break;
3523         }
3524         case WebKitCSSFilterValue::HueRotateFilterOperation: {
3525             double angle = 0;
3526             if (filterValue->length() == 1)
3527                 angle = firstValue->computeDegrees();
3528
3529             operations.operations().append(BasicColorMatrixFilterOperation::create(angle, operationType));
3530             break;
3531         }
3532         case WebKitCSSFilterValue::InvertFilterOperation:
3533         case WebKitCSSFilterValue::BrightnessFilterOperation:
3534         case WebKitCSSFilterValue::ContrastFilterOperation:
3535         case WebKitCSSFilterValue::OpacityFilterOperation: {
3536             double amount = (filterValue->operationType() == WebKitCSSFilterValue::BrightnessFilterOperation) ? 0 : 1;
3537             if (filterValue->length() == 1) {
3538                 amount = firstValue->getDoubleValue();
3539                 if (firstValue->isPercentage())
3540                     amount /= 100;
3541             }
3542
3543             operations.operations().append(BasicComponentTransferFilterOperation::create(amount, operationType));
3544             break;
3545         }
3546         case WebKitCSSFilterValue::BlurFilterOperation: {
3547             Length stdDeviation = Length(0, Fixed);
3548             if (filterValue->length() >= 1)
3549                 stdDeviation = convertToFloatLength(firstValue, style, rootStyle, zoomFactor);
3550             if (stdDeviation.isUndefined())
3551                 return false;
3552
3553             operations.operations().append(BlurFilterOperation::create(stdDeviation, operationType));
3554             break;
3555         }
3556         case WebKitCSSFilterValue::DropShadowFilterOperation: {
3557             if (filterValue->length() != 1)
3558                 return false;
3559
3560             CSSValue* cssValue = filterValue->itemWithoutBoundsCheck(0);
3561             if (!cssValue->isShadowValue())
3562                 continue;
3563
3564             CSSShadowValue* item = toCSSShadowValue(cssValue);
3565             int x = item->x->computeLength<int>(style, rootStyle, zoomFactor);
3566             if (item->x->isViewportPercentageLength())
3567                 x = viewportPercentageValue(*item->x, x);
3568             int y = item->y->computeLength<int>(style, rootStyle, zoomFactor);
3569             if (item->y->isViewportPercentageLength())
3570                 y = viewportPercentageValue(*item->y, y);
3571             IntPoint location(x, y);
3572             int blur = item->blur ? item->blur->computeLength<int>(style, rootStyle, zoomFactor) : 0;
3573             if (item->blur && item->blur->isViewportPercentageLength())
3574                 blur = viewportPercentageValue(*item->blur, blur);
3575             Color color;
3576             if (item->color)
3577                 color = colorFromPrimitiveValue(item->color.get());
3578
3579             operations.operations().append(DropShadowFilterOperation::create(location, blur, color.isValid() ? color : Color::transparent, operationType));
3580             break;
3581         }
3582         case WebKitCSSFilterValue::UnknownFilterOperation:
3583         default:
3584             ASSERT_NOT_REACHED();
3585             break;
3586         }
3587     }
3588
3589     outOperations = operations;
3590     return true;
3591 }
3592
3593 #endif
3594
3595 PassRefPtr<StyleImage> StyleResolver::loadPendingImage(const StylePendingImage& pendingImage, const ResourceLoaderOptions& options)
3596 {
3597     if (auto imageValue = pendingImage.cssImageValue())
3598         return imageValue->cachedImage(m_state.document().cachedResourceLoader(), options);
3599
3600     if (auto imageGeneratorValue = pendingImage.cssImageGeneratorValue()) {
3601         imageGeneratorValue->loadSubimages(m_state.document().cachedResourceLoader());
3602         return StyleGeneratedImage::create(*imageGeneratorValue);
3603     }
3604
3605     if (auto cursorImageValue = pendingImage.cssCursorImageValue())
3606         return cursorImageValue->cachedImage(m_state.document().cachedResourceLoader());
3607
3608 #if ENABLE(CSS_IMAGE_SET)
3609     if (auto imageSetValue = pendingImage.cssImageSetValue())
3610         return imageSetValue->cachedImageSet(m_state.document().cachedResourceLoader(), options);
3611 #endif
3612
3613     return nullptr;
3614 }
3615
3616 PassRefPtr<StyleImage> StyleResolver::loadPendingImage(const StylePendingImage& pendingImage)
3617 {
3618     return loadPendingImage(pendingImage, CachedResourceLoader::defaultCachedResourceOptions());
3619 }
3620
3621 #if ENABLE(CSS_SHAPES)
3622 void StyleResolver::loadPendingShapeImage(ShapeValue* shapeValue)
3623 {
3624     if (!shapeValue)
3625         return;
3626
3627     StyleImage* image = shapeValue->image();
3628     if (!image || !image->isPendingImage())
3629         return;
3630
3631     auto& pendingImage = toStylePendingImage(*image);
3632
3633     ResourceLoaderOptions options = CachedResourceLoader::defaultCachedResourceOptions();
3634     options.requestOriginPolicy = PotentiallyCrossOriginEnabled;
3635     options.allowCredentials = DoNotAllowStoredCredentials;
3636
3637     shapeValue->setImage(loadPendingImage(pendingImage, options));
3638 }
3639 #endif
3640
3641 void StyleResolver::loadPendingImages()
3642 {
3643     if (m_state.pendingImageProperties().isEmpty())
3644         return;
3645
3646     auto end = m_state.pendingImageProperties().end().keys();
3647     for (auto it = m_state.pendingImageProperties().begin().keys(); it != end; ++it) {
3648         CSSPropertyID currentProperty = *it;
3649
3650         switch (currentProperty) {
3651         case CSSPropertyBackgroundImage: {
3652             for (FillLayer* backgroundLayer = m_state.style()->accessBackgroundLayers(); backgroundLayer; backgroundLayer = backgroundLayer->next()) {
3653                 auto styleImage = backgroundLayer->image();
3654                 if (styleImage && styleImage->isPendingImage())
3655                     backgroundLayer->setImage(loadPendingImage(toStylePendingImage(*styleImage)));
3656             }
3657             break;
3658         }
3659         case CSSPropertyContent: {
3660             for (ContentData* contentData = const_cast<ContentData*>(m_state.style()->contentData()); contentData; contentData = contentData->next()) {
3661                 if (contentData->isImage()) {
3662                     auto& styleImage = toImageContentData(contentData)->image();
3663                     if (styleImage.isPendingImage()) {
3664                         RefPtr<StyleImage> loadedImage = loadPendingImage(toStylePendingImage(styleImage));
3665                         if (loadedImage)
3666                             toImageContentData(contentData)->setImage(loadedImage.release());
3667                     }
3668                 }
3669             }
3670             break;
3671         }
3672         case CSSPropertyCursor: {
3673             if (CursorList* cursorList = m_state.style()->cursors()) {
3674                 for (size_t i = 0; i < cursorList->size(); ++i) {
3675                     CursorData& currentCursor = cursorList->at(i);
3676                     auto styleImage = currentCursor.image();
3677                     if (styleImage && styleImage->isPendingImage())
3678                         currentCursor.setImage(loadPendingImage(toStylePendingImage(*styleImage)));
3679                 }
3680             }
3681             break;
3682         }
3683         case CSSPropertyListStyleImage: {
3684             auto styleImage = m_state.style()->listStyleImage();
3685             if (styleImage && styleImage->isPendingImage())
3686                 m_state.style()->setListStyleImage(loadPendingImage(toStylePendingImage(*styleImage)));
3687             break;
3688         }
3689         case CSSPropertyBorderImageSource: {
3690             auto styleImage = m_state.style()->borderImageSource();
3691             if (styleImage && styleImage->isPendingImage())
3692                 m_state.style()->setBorderImageSource(loadPendingImage(toStylePendingImage(*styleImage)));
3693             break;
3694         }
3695         case CSSPropertyWebkitBoxReflect: {
3696             if (StyleReflection* reflection = m_state.style()->boxReflect()) {
3697                 const NinePieceImage& maskImage = reflection->mask();
3698                 auto styleImage = maskImage.image();
3699                 if (styleImage && styleImage->isPendingImage()) {
3700                     RefPtr<StyleImage> loadedImage = loadPendingImage(toStylePendingImage(*styleImage));
3701                     reflection->setMask(NinePieceImage(loadedImage.release(), maskImage.imageSlices(), maskImage.fill(), maskImage.borderSlices(), maskImage.outset(), maskImage.horizontalRule(), maskImage.verticalRule()));
3702                 }
3703             }
3704             break;
3705         }
3706         case CSSPropertyWebkitMaskBoxImageSource: {
3707             auto styleImage = m_state.style()->maskBoxImageSource();
3708             if (styleImage && styleImage->isPendingImage())
3709                 m_state.style()->setMaskBoxImageSource(loadPendingImage(toStylePendingImage(*styleImage)));
3710             break;
3711         }
3712         case CSSPropertyWebkitMaskImage: {
3713             for (FillLayer* maskLayer = m_state.style()->accessMaskLayers(); maskLayer; maskLayer = maskLayer->next()) {
3714                 auto styleImage = maskLayer->image();
3715                 if (styleImage && styleImage->isPendingImage())
3716                     maskLayer->setImage(loadPendingImage(toStylePendingImage(*styleImage)));
3717             }
3718             break;
3719         }
3720 #if ENABLE(CSS_SHAPES) && ENABLE(CSS_SHAPE_INSIDE)
3721         case CSSPropertyWebkitShapeInside:
3722             loadPendingShapeImage(m_state.style()->shapeInside());
3723             break;
3724 #endif
3725 #if ENABLE(CSS_SHAPES)
3726         case CSSPropertyWebkitShapeOutside:
3727             loadPendingShapeImage(m_state.style()->shapeOutside());
3728             break;
3729 #endif
3730         default:
3731             ASSERT_NOT_REACHED();
3732         }
3733     }
3734
3735     m_state.pendingImageProperties().clear();
3736 }
3737
3738 #ifndef NDEBUG
3739 static bool inLoadPendingResources = false;
3740 #endif
3741
3742 void StyleResolver::loadPendingResources()
3743 {
3744     // We've seen crashes in all three of the functions below. Some of them
3745     // indicate that style() is NULL. This NULL check will cut down on total
3746     // crashes, while the ASSERT will help us find the cause in debug builds.
3747     ASSERT(style());
3748     if (!style())
3749         return;
3750
3751 #ifndef NDEBUG
3752     // Re-entering this function will probably mean trouble. Catch it in debug builds.
3753     ASSERT(!inLoadPendingResources);
3754     inLoadPendingResources = true;
3755 #endif
3756
3757     // Start loading images referenced by this style.
3758     loadPendingImages();
3759
3760 #if ENABLE(CSS_FILTERS)
3761     // Start loading the SVG Documents referenced by this style.
3762     loadPendingSVGDocuments();
3763 #endif
3764
3765 #ifndef NDEBUG
3766     inLoadPendingResources = false;
3767 #endif
3768 }
3769
3770 inline StyleResolver::MatchedProperties::MatchedProperties()
3771     : possiblyPaddedMember(0)
3772 {
3773 }
3774
3775 StyleResolver::MatchedProperties::~MatchedProperties()
3776 {
3777 }
3778
3779 int StyleResolver::viewportPercentageValue(CSSPrimitiveValue& unit, int percentage)
3780 {
3781     int viewPortHeight = document().renderView()->viewportSize().height() * percentage / 100.0f;
3782     int viewPortWidth = document().renderView()->viewportSize().width() * percentage / 100.0f;
3783
3784     if (unit.isViewportPercentageHeight())
3785         return viewPortHeight;
3786     if (unit.isViewportPercentageWidth())
3787         return viewPortWidth;
3788     if (unit.isViewportPercentageMax())
3789         return std::max(viewPortWidth, viewPortHeight);
3790     if (unit.isViewportPercentageMin())
3791         return std::min(viewPortWidth, viewPortHeight);
3792
3793     ASSERT_NOT_REACHED();
3794     return 0;
3795 }
3796
3797 StyleResolver::CascadedProperties::CascadedProperties(TextDirection direction, WritingMode writingMode)
3798     : m_direction(direction)
3799     , m_writingMode(writingMode)
3800 {
3801 }
3802
3803 inline StyleResolver::CascadedProperties::Property& StyleResolver::CascadedProperties::property(CSSPropertyID id)
3804 {
3805     return m_properties[id];
3806 }
3807
3808 void StyleResolver::CascadedProperties::setPropertyInternal(Property& property, CSSPropertyID id, CSSValue& cssValue, unsigned linkMatchType)
3809 {
3810     ASSERT(linkMatchType <= SelectorChecker::MatchAll);
3811     property.id = id;
3812     if (linkMatchType == SelectorChecker::MatchAll) {
3813         property.cssValue[0] = &cssValue;
3814         property.cssValue[SelectorChecker::MatchLink] = &cssValue;
3815         property.cssValue[SelectorChecker::MatchVisited] = &cssValue;
3816     } else
3817         property.cssValue[linkMatchType] = &cssValue;
3818 }
3819
3820 void StyleResolver::CascadedProperties::set(CSSPropertyID id, CSSValue& cssValue, unsigned linkMatchType)
3821 {
3822     if (CSSProperty::isDirectionAwareProperty(id))
3823         id = CSSProperty::resolveDirectionAwareProperty(id, m_direction, m_writingMode);
3824
3825     ASSERT(!shouldApplyPropertyInParseOrder(id));
3826
3827     auto& property = m_properties[id];
3828     if (!m_propertyIsPresent.test(id))
3829         memset(property.cssValue, 0, sizeof(property.cssValue));
3830     m_propertyIsPresent.set(id);
3831     setPropertyInternal(property, id, cssValue, linkMatchType);
3832 }
3833
3834 void StyleResolver::CascadedProperties::setDeferred(CSSPropertyID id, CSSValue& cssValue, unsigned linkMatchType)
3835 {
3836     ASSERT(!CSSProperty::isDirectionAwareProperty(id));
3837     ASSERT(shouldApplyPropertyInParseOrder(id));
3838
3839     Property property;
3840     memset(property.cssValue, 0, sizeof(property.cssValue));
3841     setPropertyInternal(property, id, cssValue, linkMatchType);
3842     m_deferredProperties.append(property);
3843 }
3844
3845 bool StyleResolver::CascadedProperties::addStyleProperties(const StyleProperties& properties, StyleRule&, bool isImportant, bool inheritedOnly, PropertyWhitelistType propertyWhitelistType, unsigned linkMatchType)
3846 {
3847     for (unsigned i = 0, count = properties.propertyCount(); i < count; ++i) {
3848         auto current = properties.propertyAt(i);
3849         if (isImportant != current.isImportant())
3850             continue;
3851         if (inheritedOnly && !current.isInherited()) {
3852             // If the property value is explicitly inherited, we need to apply further non-inherited properties
3853             // as they might override the value inherited here. For this reason we don't allow declarations with
3854             // explicitly inherited properties to be cached.
3855             if (current.value()->isInheritedValue())
3856                 return false;
3857             continue;
3858         }
3859         CSSPropertyID propertyID = current.id();
3860
3861         if (propertyWhitelistType == PropertyWhitelistRegion && !StyleResolver::isValidRegionStyleProperty(propertyID))
3862             continue;
3863 #if ENABLE(VIDEO_TRACK)
3864         if (propertyWhitelistType == PropertyWhitelistCue && !StyleResolver::isValidCueStyleProperty(propertyID))
3865             continue;
3866 #endif
3867
3868         if (shouldApplyPropertyInParseOrder(propertyID))
3869             setDeferred(propertyID, *current.value(), linkMatchType);
3870         else
3871             set(propertyID, *current.value(), linkMatchType);
3872     }
3873     return true;
3874 }
3875
3876 bool StyleResolver::CascadedProperties::addMatches(const MatchResult& matchResult, bool important, int startIndex, int endIndex, bool inheritedOnly)
3877 {
3878     if (startIndex == -1)
3879         return true;
3880
3881     for (int i = startIndex; i <= endIndex; ++i) {
3882         const MatchedProperties& matchedProperties = matchResult.matchedProperties[i];
3883         if (!addStyleProperties(*matchedProperties.properties, *matchResult.matchedRules[i], important, inheritedOnly, static_cast<PropertyWhitelistType>(matchedProperties.whitelistType), matchedProperties.linkMatchType))
3884             return false;
3885     }
3886     return true;
3887 }
3888
3889 void StyleResolver::CascadedProperties::applyDeferredProperties(StyleResolver& resolver)
3890 {
3891     for (auto& property : m_deferredProperties)
3892         property.apply(resolver);
3893 }
3894
3895 void StyleResolver::CascadedProperties::Property::apply(StyleResolver& resolver)
3896 {
3897     State& state = resolver.state();
3898
3899     // FIXME: It would be nice if line-height were less of a special snowflake.
3900     if (id == CSSPropertyLineHeight) {
3901         if (auto value = state.style()->insideLink() == NotInsideLink ? cssValue[0] : cssValue[SelectorChecker::MatchLink])
3902             state.setLineHeightValue(value);
3903         return;
3904     }
3905
3906     if (cssValue[0]) {
3907         state.setApplyPropertyToRegularStyle(true);
3908         state.setApplyPropertyToVisitedLinkStyle(false);
3909         resolver.applyProperty(id, cssValue[0]);
3910     }
3911
3912     if (state.style()->insideLink() == NotInsideLink)
3913         return;
3914
3915     if (cssValue[SelectorChecker::MatchLink]) {
3916         state.setApplyPropertyToRegularStyle(true);
3917         state.setApplyPropertyToVisitedLinkStyle(false);
3918         resolver.applyProperty(id, cssValue[SelectorChecker::MatchLink]);
3919     }
3920
3921     if (cssValue[SelectorChecker::MatchVisited]) {
3922         state.setApplyPropertyToRegularStyle(false);
3923         state.setApplyPropertyToVisitedLinkStyle(true);
3924         resolver.applyProperty(id, cssValue[SelectorChecker::MatchVisited]);
3925     }
3926
3927     state.setApplyPropertyToRegularStyle(true);
3928     state.setApplyPropertyToVisitedLinkStyle(false);
3929 }
3930
3931 void StyleResolver::applyCascadedProperties(CascadedProperties& cascade, int firstProperty, int lastProperty)
3932 {
3933     for (int id = firstProperty; id <= lastProperty; ++id) {
3934         CSSPropertyID propertyID = static_cast<CSSPropertyID>(id);
3935         if (!cascade.hasProperty(propertyID))
3936             continue;
3937         auto& property = cascade.property(propertyID);
3938         ASSERT(!shouldApplyPropertyInParseOrder(propertyID));
3939         property.apply(*this);
3940     }
3941 }
3942
3943 } // namespace WebCore