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