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