font-variant-* properties in @font-face declarations should be honored
[WebKit-https.git] / Source / WebCore / css / StyleBuilderCustom.h
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  * Copyright (C) 2014 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef StyleBuilderCustom_h
28 #define StyleBuilderCustom_h
29
30 #include "CSSAspectRatioValue.h"
31 #include "CSSCursorImageValue.h"
32 #include "CSSFontFamily.h"
33 #include "CSSFontValue.h"
34 #include "CSSGradientValue.h"
35 #include "CSSShadowValue.h"
36 #include "Counter.h"
37 #include "CounterContent.h"
38 #include "CursorList.h"
39 #include "DashboardRegion.h"
40 #include "ElementAncestorIterator.h"
41 #include "FontVariantBuilder.h"
42 #include "Frame.h"
43 #include "HTMLElement.h"
44 #include "LocaleToScriptMapping.h"
45 #include "Rect.h"
46 #include "RenderTheme.h"
47 #include "SVGElement.h"
48 #include "SVGRenderStyle.h"
49 #include "StyleBuilderConverter.h"
50 #include "StyleFontSizeFunctions.h"
51 #include "StyleGeneratedImage.h"
52 #include "StyleResolver.h"
53 #include "WillChangeData.h"
54
55 namespace WebCore {
56
57 #define DECLARE_PROPERTY_CUSTOM_HANDLERS(property) \
58     static void applyInherit##property(StyleResolver&); \
59     static void applyInitial##property(StyleResolver&); \
60     static void applyValue##property(StyleResolver&, CSSValue&)
61
62 // Note that we assume the CSS parser only allows valid CSSValue types.
63 class StyleBuilderCustom {
64 public:
65     // Custom handling of inherit, initial and value setting.
66     DECLARE_PROPERTY_CUSTOM_HANDLERS(BorderImageOutset);
67     DECLARE_PROPERTY_CUSTOM_HANDLERS(BorderImageRepeat);
68     DECLARE_PROPERTY_CUSTOM_HANDLERS(BorderImageSlice);
69     DECLARE_PROPERTY_CUSTOM_HANDLERS(BorderImageWidth);
70     DECLARE_PROPERTY_CUSTOM_HANDLERS(BoxShadow);
71     DECLARE_PROPERTY_CUSTOM_HANDLERS(Clip);
72     DECLARE_PROPERTY_CUSTOM_HANDLERS(ColumnGap);
73     DECLARE_PROPERTY_CUSTOM_HANDLERS(Content);
74     DECLARE_PROPERTY_CUSTOM_HANDLERS(CounterIncrement);
75     DECLARE_PROPERTY_CUSTOM_HANDLERS(CounterReset);
76     DECLARE_PROPERTY_CUSTOM_HANDLERS(Cursor);
77     DECLARE_PROPERTY_CUSTOM_HANDLERS(Fill);
78     DECLARE_PROPERTY_CUSTOM_HANDLERS(FontFamily);
79     DECLARE_PROPERTY_CUSTOM_HANDLERS(FontSize);
80     DECLARE_PROPERTY_CUSTOM_HANDLERS(FontWeight);
81 #if ENABLE(CSS_IMAGE_RESOLUTION)
82     DECLARE_PROPERTY_CUSTOM_HANDLERS(ImageResolution);
83 #endif
84 #if ENABLE(IOS_TEXT_AUTOSIZING)
85     DECLARE_PROPERTY_CUSTOM_HANDLERS(LineHeight);
86 #endif
87     DECLARE_PROPERTY_CUSTOM_HANDLERS(OutlineStyle);
88     DECLARE_PROPERTY_CUSTOM_HANDLERS(Size);
89     DECLARE_PROPERTY_CUSTOM_HANDLERS(Stroke);
90     DECLARE_PROPERTY_CUSTOM_HANDLERS(TextIndent);
91     DECLARE_PROPERTY_CUSTOM_HANDLERS(TextShadow);
92     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitAspectRatio);
93     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitBoxShadow);
94     DECLARE_PROPERTY_CUSTOM_HANDLERS(FontVariantLigatures);
95     DECLARE_PROPERTY_CUSTOM_HANDLERS(FontVariantNumeric);
96     DECLARE_PROPERTY_CUSTOM_HANDLERS(FontVariantEastAsian);
97 #if ENABLE(CSS_GRID_LAYOUT)
98     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitGridTemplateAreas);
99     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitGridTemplateColumns);
100     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitGridTemplateRows);
101 #endif // ENABLE(CSS_GRID_LAYOUT)
102     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitMaskBoxImageOutset);
103     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitMaskBoxImageRepeat);
104     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitMaskBoxImageSlice);
105     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitMaskBoxImageWidth);
106     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitSvgShadow);
107     DECLARE_PROPERTY_CUSTOM_HANDLERS(WebkitTextEmphasisStyle);
108     DECLARE_PROPERTY_CUSTOM_HANDLERS(Zoom);
109
110     // Custom handling of initial + inherit value setting only.
111     static void applyInitialWebkitMaskImage(StyleResolver&) { }
112     static void applyInheritWebkitMaskImage(StyleResolver&) { }
113     static void applyInitialFontFeatureSettings(StyleResolver&) { }
114     static void applyInheritFontFeatureSettings(StyleResolver&) { }
115
116     // Custom handling of inherit + value setting only.
117     static void applyInheritDisplay(StyleResolver&);
118     static void applyValueDisplay(StyleResolver&, CSSValue&);
119
120     // Custom handling of value setting only.
121     static void applyValueBaselineShift(StyleResolver&, CSSValue&);
122     static void applyValueDirection(StyleResolver&, CSSValue&);
123     static void applyValueVerticalAlign(StyleResolver&, CSSValue&);
124 #if ENABLE(DASHBOARD_SUPPORT)
125     static void applyValueWebkitDashboardRegion(StyleResolver&, CSSValue&);
126 #endif
127     static void applyValueWebkitLocale(StyleResolver&, CSSValue&);
128     static void applyValueWebkitTextOrientation(StyleResolver&, CSSValue&);
129 #if ENABLE(IOS_TEXT_AUTOSIZING)
130     static void applyValueWebkitTextSizeAdjust(StyleResolver&, CSSValue&);
131 #endif
132     static void applyValueWebkitTextZoom(StyleResolver&, CSSValue&);
133     static void applyValueWebkitWritingMode(StyleResolver&, CSSValue&);
134     static void applyValueAlt(StyleResolver&, CSSValue&);
135 #if ENABLE(CSS_SCROLL_SNAP)
136     static void applyInitialWebkitScrollSnapPointsX(StyleResolver&);
137     static void applyInheritWebkitScrollSnapPointsX(StyleResolver&);
138     static void applyInitialWebkitScrollSnapPointsY(StyleResolver&);
139     static void applyInheritWebkitScrollSnapPointsY(StyleResolver&);
140 #endif
141     static void applyValueWillChange(StyleResolver&, CSSValue&);
142
143 private:
144     static void resetEffectiveZoom(StyleResolver&);
145
146     static Length mmLength(double mm);
147     static Length inchLength(double inch);
148     static bool getPageSizeFromName(CSSPrimitiveValue* pageSizeName, CSSPrimitiveValue* pageOrientation, Length& width, Length& height);
149
150     template <CSSPropertyID id>
151     static void applyTextOrBoxShadowValue(StyleResolver&, CSSValue&);
152     static bool isValidDisplayValue(StyleResolver&, EDisplay);
153
154     enum CounterBehavior {Increment = 0, Reset};
155     template <CounterBehavior counterBehavior>
156     static void applyInheritCounter(StyleResolver&);
157     template <CounterBehavior counterBehavior>
158     static void applyValueCounter(StyleResolver&, CSSValue&);
159
160     static float largerFontSize(float size);
161     static float smallerFontSize(float size);
162     static float determineRubyTextSizeMultiplier(StyleResolver&);
163 };
164
165 inline void StyleBuilderCustom::applyValueDirection(StyleResolver& styleResolver, CSSValue& value)
166 {
167     styleResolver.style()->setDirection(downcast<CSSPrimitiveValue>(value));
168
169     Element* element = styleResolver.element();
170     if (element && styleResolver.element() == element->document().documentElement())
171         element->document().setDirectionSetOnDocumentElement(true);
172 }
173
174 inline void StyleBuilderCustom::resetEffectiveZoom(StyleResolver& styleResolver)
175 {
176     // Reset the zoom in effect. This allows the setZoom method to accurately compute a new zoom in effect.
177     styleResolver.setEffectiveZoom(styleResolver.parentStyle() ? styleResolver.parentStyle()->effectiveZoom() : RenderStyle::initialZoom());
178 }
179
180 inline void StyleBuilderCustom::applyInitialZoom(StyleResolver& styleResolver)
181 {
182     resetEffectiveZoom(styleResolver);
183     styleResolver.setZoom(RenderStyle::initialZoom());
184 }
185
186 inline void StyleBuilderCustom::applyInheritZoom(StyleResolver& styleResolver)
187 {
188     resetEffectiveZoom(styleResolver);
189     styleResolver.setZoom(styleResolver.parentStyle()->zoom());
190 }
191
192 inline void StyleBuilderCustom::applyValueZoom(StyleResolver& styleResolver, CSSValue& value)
193 {
194     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
195
196     if (primitiveValue.getValueID() == CSSValueNormal) {
197         resetEffectiveZoom(styleResolver);
198         styleResolver.setZoom(RenderStyle::initialZoom());
199     } else if (primitiveValue.getValueID() == CSSValueReset) {
200         styleResolver.setEffectiveZoom(RenderStyle::initialZoom());
201         styleResolver.setZoom(RenderStyle::initialZoom());
202     } else if (primitiveValue.getValueID() == CSSValueDocument) {
203         float docZoom = styleResolver.rootElementStyle() ? styleResolver.rootElementStyle()->zoom() : RenderStyle::initialZoom();
204         styleResolver.setEffectiveZoom(docZoom);
205         styleResolver.setZoom(docZoom);
206     } else if (primitiveValue.isPercentage()) {
207         resetEffectiveZoom(styleResolver);
208         if (float percent = primitiveValue.getFloatValue())
209             styleResolver.setZoom(percent / 100.0f);
210     } else if (primitiveValue.isNumber()) {
211         resetEffectiveZoom(styleResolver);
212         if (float number = primitiveValue.getFloatValue())
213             styleResolver.setZoom(number);
214     }
215 }
216 inline Length StyleBuilderCustom::mmLength(double mm)
217 {
218     Ref<CSSPrimitiveValue> value(CSSPrimitiveValue::create(mm, CSSPrimitiveValue::CSS_MM));
219     return value.get().computeLength<Length>(CSSToLengthConversionData());
220 }
221 inline Length StyleBuilderCustom::inchLength(double inch)
222 {
223     Ref<CSSPrimitiveValue> value(CSSPrimitiveValue::create(inch, CSSPrimitiveValue::CSS_IN));
224     return value.get().computeLength<Length>(CSSToLengthConversionData());
225 }
226 bool StyleBuilderCustom::getPageSizeFromName(CSSPrimitiveValue* pageSizeName, CSSPrimitiveValue* pageOrientation, Length& width, Length& height)
227 {
228     static NeverDestroyed<Length> a5Width(mmLength(148));
229     static NeverDestroyed<Length> a5Height(mmLength(210));
230     static NeverDestroyed<Length> a4Width(mmLength(210));
231     static NeverDestroyed<Length> a4Height(mmLength(297));
232     static NeverDestroyed<Length> a3Width(mmLength(297));
233     static NeverDestroyed<Length> a3Height(mmLength(420));
234     static NeverDestroyed<Length> b5Width(mmLength(176));
235     static NeverDestroyed<Length> b5Height(mmLength(250));
236     static NeverDestroyed<Length> b4Width(mmLength(250));
237     static NeverDestroyed<Length> b4Height(mmLength(353));
238     static NeverDestroyed<Length> letterWidth(inchLength(8.5));
239     static NeverDestroyed<Length> letterHeight(inchLength(11));
240     static NeverDestroyed<Length> legalWidth(inchLength(8.5));
241     static NeverDestroyed<Length> legalHeight(inchLength(14));
242     static NeverDestroyed<Length> ledgerWidth(inchLength(11));
243     static NeverDestroyed<Length> ledgerHeight(inchLength(17));
244
245     if (!pageSizeName)
246         return false;
247
248     switch (pageSizeName->getValueID()) {
249     case CSSValueA5:
250         width = a5Width;
251         height = a5Height;
252         break;
253     case CSSValueA4:
254         width = a4Width;
255         height = a4Height;
256         break;
257     case CSSValueA3:
258         width = a3Width;
259         height = a3Height;
260         break;
261     case CSSValueB5:
262         width = b5Width;
263         height = b5Height;
264         break;
265     case CSSValueB4:
266         width = b4Width;
267         height = b4Height;
268         break;
269     case CSSValueLetter:
270         width = letterWidth;
271         height = letterHeight;
272         break;
273     case CSSValueLegal:
274         width = legalWidth;
275         height = legalHeight;
276         break;
277     case CSSValueLedger:
278         width = ledgerWidth;
279         height = ledgerHeight;
280         break;
281     default:
282         return false;
283     }
284
285     if (pageOrientation) {
286         switch (pageOrientation->getValueID()) {
287         case CSSValueLandscape:
288             std::swap(width, height);
289             break;
290         case CSSValuePortrait:
291             // Nothing to do.
292             break;
293         default:
294             return false;
295         }
296     }
297     return true;
298 }
299
300 inline void StyleBuilderCustom::applyValueVerticalAlign(StyleResolver& styleResolver, CSSValue& value)
301 {
302     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
303     if (primitiveValue.getValueID())
304         styleResolver.style()->setVerticalAlign(primitiveValue);
305     else
306         styleResolver.style()->setVerticalAlignLength(primitiveValue.convertToLength<FixedIntegerConversion | PercentConversion | CalculatedConversion>(styleResolver.state().cssToLengthConversionData()));
307 }
308
309 #if ENABLE(DASHBOARD_SUPPORT)
310 static Length convertToIntLength(const CSSPrimitiveValue* primitiveValue, const CSSToLengthConversionData& conversionData)
311 {
312     return primitiveValue ? primitiveValue->convertToLength<FixedIntegerConversion | PercentConversion | CalculatedConversion>(conversionData) : Length(Undefined);
313 }
314
315 inline void StyleBuilderCustom::applyValueWebkitDashboardRegion(StyleResolver& styleResolver, CSSValue& value)
316 {
317     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
318     if (primitiveValue.getValueID() == CSSValueNone) {
319         styleResolver.style()->setDashboardRegions(RenderStyle::noneDashboardRegions());
320         return;
321     }
322
323     DashboardRegion* region = primitiveValue.getDashboardRegionValue();
324     if (!region)
325         return;
326
327     DashboardRegion* first = region;
328     while (region) {
329         Length top = convertToIntLength(region->top(), styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f));
330         Length right = convertToIntLength(region->right(), styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f));
331         Length bottom = convertToIntLength(region->bottom(), styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f));
332         Length left = convertToIntLength(region->left(), styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f));
333
334         if (top.isUndefined())
335             top = Length();
336         if (right.isUndefined())
337             right = Length();
338         if (bottom.isUndefined())
339             bottom = Length();
340         if (left.isUndefined())
341             left = Length();
342
343         if (region->m_isCircle)
344             styleResolver.style()->setDashboardRegion(StyleDashboardRegion::Circle, region->m_label, top, right, bottom, left, region == first ? false : true);
345         else if (region->m_isRectangle)
346             styleResolver.style()->setDashboardRegion(StyleDashboardRegion::Rectangle, region->m_label, top, right, bottom, left, region == first ? false : true);
347         region = region->m_next.get();
348     }
349
350     styleResolver.document().setHasAnnotatedRegions(true);
351 }
352 #endif // ENABLE(DASHBOARD_SUPPORT)
353
354 #if ENABLE(CSS_IMAGE_RESOLUTION)
355 inline void StyleBuilderCustom::applyInheritImageResolution(StyleResolver& styleResolver)
356 {
357     styleResolver.style()->setImageResolutionSource(styleResolver.parentStyle()->imageResolutionSource());
358     styleResolver.style()->setImageResolutionSnap(styleResolver.parentStyle()->imageResolutionSnap());
359     styleResolver.style()->setImageResolution(styleResolver.parentStyle()->imageResolution());
360 }
361
362 inline void StyleBuilderCustom::applyInitialImageResolution(StyleResolver& styleResolver)
363 {
364     styleResolver.style()->setImageResolutionSource(RenderStyle::initialImageResolutionSource());
365     styleResolver.style()->setImageResolutionSnap(RenderStyle::initialImageResolutionSnap());
366     styleResolver.style()->setImageResolution(RenderStyle::initialImageResolution());
367 }
368
369 inline void StyleBuilderCustom::applyValueImageResolution(StyleResolver& styleResolver, CSSValue& value)
370 {
371     ImageResolutionSource source = RenderStyle::initialImageResolutionSource();
372     ImageResolutionSnap snap = RenderStyle::initialImageResolutionSnap();
373     double resolution = RenderStyle::initialImageResolution();
374     for (auto& item : downcast<CSSValueList>(value)) {
375         CSSPrimitiveValue& primitiveValue = downcast<CSSPrimitiveValue>(item.get());
376         if (primitiveValue.getValueID() == CSSValueFromImage)
377             source = ImageResolutionFromImage;
378         else if (primitiveValue.getValueID() == CSSValueSnap)
379             snap = ImageResolutionSnapPixels;
380         else
381             resolution = primitiveValue.getDoubleValue(CSSPrimitiveValue::CSS_DPPX);
382     }
383     styleResolver.style()->setImageResolutionSource(source);
384     styleResolver.style()->setImageResolutionSnap(snap);
385     styleResolver.style()->setImageResolution(resolution);
386 }
387 #endif // ENABLE(CSS_IMAGE_RESOLUTION)
388
389 inline void StyleBuilderCustom::applyInheritSize(StyleResolver&) { }
390 inline void StyleBuilderCustom::applyInitialSize(StyleResolver&) { }
391 inline void StyleBuilderCustom::applyValueSize(StyleResolver& styleResolver, CSSValue& value)
392 {
393     styleResolver.style()->resetPageSizeType();
394     Length width;
395     Length height;
396     PageSizeType pageSizeType = PAGE_SIZE_AUTO;
397     if (!is<CSSValueList>(value))
398         return;
399
400     auto& valueList = downcast<CSSValueList>(value);
401     switch (valueList.length()) {
402     case 2: {
403         CSSValue* firstValue = valueList.itemWithoutBoundsCheck(0);
404         CSSValue* secondValue = valueList.itemWithoutBoundsCheck(1);
405         // <length>{2} | <page-size> <orientation>
406         if (!is<CSSPrimitiveValue>(*firstValue) || !is<CSSPrimitiveValue>(*secondValue))
407             return;
408         auto& firstPrimitiveValue = downcast<CSSPrimitiveValue>(*firstValue);
409         auto& secondPrimitiveValue = downcast<CSSPrimitiveValue>(*secondValue);
410         if (firstPrimitiveValue.isLength()) {
411             // <length>{2}
412             if (!secondPrimitiveValue.isLength())
413                 return;
414             CSSToLengthConversionData conversionData = styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f);
415             width = firstPrimitiveValue.computeLength<Length>(conversionData);
416             height = secondPrimitiveValue.computeLength<Length>(conversionData);
417         } else {
418             // <page-size> <orientation>
419             // The value order is guaranteed. See CSSParser::parseSizeParameter.
420             if (!getPageSizeFromName(&firstPrimitiveValue, &secondPrimitiveValue, width, height))
421                 return;
422         }
423         pageSizeType = PAGE_SIZE_RESOLVED;
424         break;
425     }
426     case 1: {
427         CSSValue* value = valueList.itemWithoutBoundsCheck(0);
428         // <length> | auto | <page-size> | [ portrait | landscape]
429         if (!is<CSSPrimitiveValue>(*value))
430             return;
431         auto& primitiveValue = downcast<CSSPrimitiveValue>(*value);
432         if (primitiveValue.isLength()) {
433             // <length>
434             pageSizeType = PAGE_SIZE_RESOLVED;
435             width = height = primitiveValue.computeLength<Length>(styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f));
436         } else {
437             switch (primitiveValue.getValueID()) {
438             case 0:
439                 return;
440             case CSSValueAuto:
441                 pageSizeType = PAGE_SIZE_AUTO;
442                 break;
443             case CSSValuePortrait:
444                 pageSizeType = PAGE_SIZE_AUTO_PORTRAIT;
445                 break;
446             case CSSValueLandscape:
447                 pageSizeType = PAGE_SIZE_AUTO_LANDSCAPE;
448                 break;
449             default:
450                 // <page-size>
451                 pageSizeType = PAGE_SIZE_RESOLVED;
452                 if (!getPageSizeFromName(&primitiveValue, nullptr, width, height))
453                     return;
454             }
455         }
456         break;
457     }
458     default:
459         return;
460     }
461     styleResolver.style()->setPageSizeType(pageSizeType);
462     styleResolver.style()->setPageSize(LengthSize(width, height));
463 }
464
465 inline void StyleBuilderCustom::applyInheritTextIndent(StyleResolver& styleResolver)
466 {
467     styleResolver.style()->setTextIndent(styleResolver.parentStyle()->textIndent());
468 #if ENABLE(CSS3_TEXT)
469     styleResolver.style()->setTextIndentLine(styleResolver.parentStyle()->textIndentLine());
470     styleResolver.style()->setTextIndentType(styleResolver.parentStyle()->textIndentType());
471 #endif
472 }
473
474 inline void StyleBuilderCustom::applyInitialTextIndent(StyleResolver& styleResolver)
475 {
476     styleResolver.style()->setTextIndent(RenderStyle::initialTextIndent());
477 #if ENABLE(CSS3_TEXT)
478     styleResolver.style()->setTextIndentLine(RenderStyle::initialTextIndentLine());
479     styleResolver.style()->setTextIndentType(RenderStyle::initialTextIndentType());
480 #endif
481 }
482
483 inline void StyleBuilderCustom::applyValueTextIndent(StyleResolver& styleResolver, CSSValue& value)
484 {
485     Length lengthOrPercentageValue;
486 #if ENABLE(CSS3_TEXT)
487     TextIndentLine textIndentLineValue = RenderStyle::initialTextIndentLine();
488     TextIndentType textIndentTypeValue = RenderStyle::initialTextIndentType();
489 #endif
490     for (auto& item : downcast<CSSValueList>(value)) {
491         auto& primitiveValue = downcast<CSSPrimitiveValue>(item.get());
492         if (!primitiveValue.getValueID())
493             lengthOrPercentageValue = primitiveValue.convertToLength<FixedIntegerConversion | PercentConversion | CalculatedConversion>(styleResolver.state().cssToLengthConversionData());
494 #if ENABLE(CSS3_TEXT)
495         else if (primitiveValue.getValueID() == CSSValueWebkitEachLine)
496             textIndentLineValue = TextIndentEachLine;
497         else if (primitiveValue.getValueID() == CSSValueWebkitHanging)
498             textIndentTypeValue = TextIndentHanging;
499 #endif
500     }
501
502     if (lengthOrPercentageValue.isUndefined())
503         return;
504
505     styleResolver.style()->setTextIndent(lengthOrPercentageValue);
506 #if ENABLE(CSS3_TEXT)
507     styleResolver.style()->setTextIndentLine(textIndentLineValue);
508     styleResolver.style()->setTextIndentType(textIndentTypeValue);
509 #endif
510 }
511
512 enum BorderImageType { BorderImage, WebkitMaskBoxImage };
513 enum BorderImageModifierType { Outset, Repeat, Slice, Width };
514 template <BorderImageType type, BorderImageModifierType modifier>
515 class ApplyPropertyBorderImageModifier {
516 public:
517     static void applyInheritValue(StyleResolver& styleResolver)
518     {
519         NinePieceImage image(getValue(styleResolver.style()));
520         switch (modifier) {
521         case Outset:
522             image.copyOutsetFrom(getValue(styleResolver.parentStyle()));
523             break;
524         case Repeat:
525             image.copyRepeatFrom(getValue(styleResolver.parentStyle()));
526             break;
527         case Slice:
528             image.copyImageSlicesFrom(getValue(styleResolver.parentStyle()));
529             break;
530         case Width:
531             image.copyBorderSlicesFrom(getValue(styleResolver.parentStyle()));
532             break;
533         }
534         setValue(styleResolver.style(), image);
535     }
536
537     static void applyInitialValue(StyleResolver& styleResolver)
538     {
539         NinePieceImage image(getValue(styleResolver.style()));
540         switch (modifier) {
541         case Outset:
542             image.setOutset(LengthBox(0));
543             break;
544         case Repeat:
545             image.setHorizontalRule(StretchImageRule);
546             image.setVerticalRule(StretchImageRule);
547             break;
548         case Slice:
549             // Masks have a different initial value for slices. Preserve the value of 0 for backwards compatibility.
550             image.setImageSlices(type == BorderImage ? LengthBox(Length(100, Percent), Length(100, Percent), Length(100, Percent), Length(100, Percent)) : LengthBox());
551             image.setFill(false);
552             break;
553         case Width:
554             // Masks have a different initial value for widths. They use an 'auto' value rather than trying to fit to the border.
555             image.setBorderSlices(type == BorderImage ? LengthBox(Length(1, Relative), Length(1, Relative), Length(1, Relative), Length(1, Relative)) : LengthBox());
556             break;
557         }
558         setValue(styleResolver.style(), image);
559     }
560
561     static void applyValue(StyleResolver& styleResolver, CSSValue& value)
562     {
563         NinePieceImage image(getValue(styleResolver.style()));
564         switch (modifier) {
565         case Outset:
566             image.setOutset(styleResolver.styleMap()->mapNinePieceImageQuad(value));
567             break;
568         case Repeat:
569             styleResolver.styleMap()->mapNinePieceImageRepeat(value, image);
570             break;
571         case Slice:
572             styleResolver.styleMap()->mapNinePieceImageSlice(value, image);
573             break;
574         case Width:
575             image.setBorderSlices(styleResolver.styleMap()->mapNinePieceImageQuad(value));
576             break;
577         }
578         setValue(styleResolver.style(), image);
579     }
580
581 private:
582     static const NinePieceImage& getValue(RenderStyle* style)
583     {
584         return type == BorderImage ? style->borderImage() : style->maskBoxImage();
585     }
586
587     static void setValue(RenderStyle* style, const NinePieceImage& value)
588     {
589         return type == BorderImage ? style->setBorderImage(value) : style->setMaskBoxImage(value);
590     }
591 };
592
593 #define DEFINE_BORDER_IMAGE_MODIFIER_HANDLER(type, modifier) \
594 inline void StyleBuilderCustom::applyInherit##type##modifier(StyleResolver& styleResolver) \
595 { \
596     ApplyPropertyBorderImageModifier<type, modifier>::applyInheritValue(styleResolver); \
597 } \
598 inline void StyleBuilderCustom::applyInitial##type##modifier(StyleResolver& styleResolver) \
599 { \
600     ApplyPropertyBorderImageModifier<type, modifier>::applyInitialValue(styleResolver); \
601 } \
602 inline void StyleBuilderCustom::applyValue##type##modifier(StyleResolver& styleResolver, CSSValue& value) \
603 { \
604     ApplyPropertyBorderImageModifier<type, modifier>::applyValue(styleResolver, value); \
605 }
606
607 DEFINE_BORDER_IMAGE_MODIFIER_HANDLER(BorderImage, Outset)
608 DEFINE_BORDER_IMAGE_MODIFIER_HANDLER(BorderImage, Repeat)
609 DEFINE_BORDER_IMAGE_MODIFIER_HANDLER(BorderImage, Slice)
610 DEFINE_BORDER_IMAGE_MODIFIER_HANDLER(BorderImage, Width)
611 DEFINE_BORDER_IMAGE_MODIFIER_HANDLER(WebkitMaskBoxImage, Outset)
612 DEFINE_BORDER_IMAGE_MODIFIER_HANDLER(WebkitMaskBoxImage, Repeat)
613 DEFINE_BORDER_IMAGE_MODIFIER_HANDLER(WebkitMaskBoxImage, Slice)
614 DEFINE_BORDER_IMAGE_MODIFIER_HANDLER(WebkitMaskBoxImage, Width)
615
616 #if ENABLE(IOS_TEXT_AUTOSIZING)
617
618 inline void StyleBuilderCustom::applyInheritLineHeight(StyleResolver& styleResolver)
619 {
620     styleResolver.style()->setLineHeight(styleResolver.parentStyle()->lineHeight());
621     styleResolver.style()->setSpecifiedLineHeight(styleResolver.parentStyle()->specifiedLineHeight());
622 }
623
624 inline void StyleBuilderCustom::applyInitialLineHeight(StyleResolver& styleResolver)
625 {
626     styleResolver.style()->setLineHeight(RenderStyle::initialLineHeight());
627     styleResolver.style()->setSpecifiedLineHeight(RenderStyle::initialSpecifiedLineHeight());
628 }
629
630 inline void StyleBuilderCustom::applyValueLineHeight(StyleResolver& styleResolver, CSSValue& value)
631 {
632     float multiplier = styleResolver.style()->textSizeAdjust().isPercentage() ? styleResolver.style()->textSizeAdjust().multiplier() : 1.f;
633     Optional<Length> lineHeight = StyleBuilderConverter::convertLineHeight(styleResolver, value, multiplier);
634     if (!lineHeight)
635         return;
636
637     styleResolver.style()->setLineHeight(lineHeight.value());
638     styleResolver.style()->setSpecifiedLineHeight(lineHeight.value());
639 }
640
641 #endif
642
643 inline void StyleBuilderCustom::applyInheritOutlineStyle(StyleResolver& styleResolver)
644 {
645     styleResolver.style()->setOutlineStyleIsAuto(styleResolver.parentStyle()->outlineStyleIsAuto());
646     styleResolver.style()->setOutlineStyle(styleResolver.parentStyle()->outlineStyle());
647 }
648
649 inline void StyleBuilderCustom::applyInitialOutlineStyle(StyleResolver& styleResolver)
650 {
651     styleResolver.style()->setOutlineStyleIsAuto(RenderStyle::initialOutlineStyleIsAuto());
652     styleResolver.style()->setOutlineStyle(RenderStyle::initialBorderStyle());
653 }
654
655 inline void StyleBuilderCustom::applyValueOutlineStyle(StyleResolver& styleResolver, CSSValue& value)
656 {
657     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
658
659     styleResolver.style()->setOutlineStyleIsAuto(primitiveValue);
660     styleResolver.style()->setOutlineStyle(primitiveValue);
661 }
662
663 inline void StyleBuilderCustom::applyInitialClip(StyleResolver& styleResolver)
664 {
665     styleResolver.style()->setClip(Length(), Length(), Length(), Length());
666     styleResolver.style()->setHasClip(false);
667 }
668
669 inline void StyleBuilderCustom::applyInheritClip(StyleResolver& styleResolver)
670 {
671     RenderStyle* parentStyle = styleResolver.parentStyle();
672     if (!parentStyle->hasClip())
673         return applyInitialClip(styleResolver);
674     styleResolver.style()->setClip(parentStyle->clipTop(), parentStyle->clipRight(), parentStyle->clipBottom(), parentStyle->clipLeft());
675     styleResolver.style()->setHasClip(true);
676 }
677
678 inline void StyleBuilderCustom::applyValueClip(StyleResolver& styleResolver, CSSValue& value)
679 {
680     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
681
682     if (Rect* rect = primitiveValue.getRectValue()) {
683         auto conversionData = styleResolver.state().cssToLengthConversionData();
684         Length top = rect->top()->convertToLength<FixedIntegerConversion | PercentConversion | AutoConversion>(conversionData);
685         Length right = rect->right()->convertToLength<FixedIntegerConversion | PercentConversion | AutoConversion>(conversionData);
686         Length bottom = rect->bottom()->convertToLength<FixedIntegerConversion | PercentConversion | AutoConversion>(conversionData);
687         Length left = rect->left()->convertToLength<FixedIntegerConversion | PercentConversion | AutoConversion>(conversionData);
688         styleResolver.style()->setClip(top, right, bottom, left);
689         styleResolver.style()->setHasClip(true);
690     } else {
691         ASSERT(primitiveValue.getValueID() == CSSValueAuto);
692         applyInitialClip(styleResolver);
693     }
694 }
695
696 inline void StyleBuilderCustom::applyValueWebkitLocale(StyleResolver& styleResolver, CSSValue& value)
697 {
698     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
699     if (primitiveValue.getValueID() == CSSValueAuto)
700         styleResolver.style()->setLocale(nullAtom);
701     else
702         styleResolver.style()->setLocale(primitiveValue.getStringValue());
703
704     FontCascadeDescription fontDescription = styleResolver.style()->fontDescription();
705     fontDescription.setScript(localeToScriptCodeForFontSelection(styleResolver.style()->locale()));
706     styleResolver.setFontDescription(fontDescription);
707 }
708
709 inline void StyleBuilderCustom::applyValueWebkitWritingMode(StyleResolver& styleResolver, CSSValue& value)
710 {
711     styleResolver.setWritingMode(downcast<CSSPrimitiveValue>(value));
712
713     // FIXME: It is not ok to modify document state while applying style.
714     auto& state = styleResolver.state();
715     if (state.element() && state.element() == state.document().documentElement())
716         state.document().setWritingModeSetOnDocumentElement(true);
717 }
718
719 inline void StyleBuilderCustom::applyValueWebkitTextOrientation(StyleResolver& styleResolver, CSSValue& value)
720 {
721     styleResolver.setTextOrientation(downcast<CSSPrimitiveValue>(value));
722 }
723
724 #if ENABLE(IOS_TEXT_AUTOSIZING)
725 inline void StyleBuilderCustom::applyValueWebkitTextSizeAdjust(StyleResolver& styleResolver, CSSValue& value)
726 {
727     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
728     if (primitiveValue.getValueID() == CSSValueAuto)
729         styleResolver.style()->setTextSizeAdjust(TextSizeAdjustment(AutoTextSizeAdjustment));
730     else if (primitiveValue.getValueID() == CSSValueNone)
731         styleResolver.style()->setTextSizeAdjust(TextSizeAdjustment(NoTextSizeAdjustment));
732     else
733         styleResolver.style()->setTextSizeAdjust(TextSizeAdjustment(primitiveValue.getFloatValue()));
734
735     styleResolver.state().setFontDirty(true);
736 }
737 #endif
738
739 inline void StyleBuilderCustom::applyValueWebkitTextZoom(StyleResolver& styleResolver, CSSValue& value)
740 {
741     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
742     if (primitiveValue.getValueID() == CSSValueNormal)
743         styleResolver.style()->setTextZoom(TextZoomNormal);
744     else if (primitiveValue.getValueID() == CSSValueReset)
745         styleResolver.style()->setTextZoom(TextZoomReset);
746     styleResolver.state().setFontDirty(true);
747 }
748
749 template <CSSPropertyID id>
750 inline void StyleBuilderCustom::applyTextOrBoxShadowValue(StyleResolver& styleResolver, CSSValue& value)
751 {
752     if (is<CSSPrimitiveValue>(value)) {
753         ASSERT(downcast<CSSPrimitiveValue>(value).getValueID() == CSSValueNone);
754         if (id == CSSPropertyTextShadow)
755             styleResolver.style()->setTextShadow(nullptr);
756         else
757             styleResolver.style()->setBoxShadow(nullptr);
758         return;
759     }
760
761     bool isFirstEntry = true;
762     for (auto& item : downcast<CSSValueList>(value)) {
763         auto& shadowValue = downcast<CSSShadowValue>(item.get());
764         auto conversionData = styleResolver.state().cssToLengthConversionData();
765         int x = shadowValue.x->computeLength<int>(conversionData);
766         int y = shadowValue.y->computeLength<int>(conversionData);
767         int blur = shadowValue.blur ? shadowValue.blur->computeLength<int>(conversionData) : 0;
768         int spread = shadowValue.spread ? shadowValue.spread->computeLength<int>(conversionData) : 0;
769         ShadowStyle shadowStyle = shadowValue.style && shadowValue.style->getValueID() == CSSValueInset ? Inset : Normal;
770         Color color;
771         if (shadowValue.color)
772             color = styleResolver.colorFromPrimitiveValue(*shadowValue.color);
773         else
774             color = styleResolver.style()->color();
775         auto shadowData = std::make_unique<ShadowData>(IntPoint(x, y), blur, spread, shadowStyle, id == CSSPropertyWebkitBoxShadow, color.isValid() ? color : Color::transparent);
776         if (id == CSSPropertyTextShadow)
777             styleResolver.style()->setTextShadow(WTF::move(shadowData), !isFirstEntry); // add to the list if this is not the first entry
778         else
779             styleResolver.style()->setBoxShadow(WTF::move(shadowData), !isFirstEntry); // add to the list if this is not the first entry
780         isFirstEntry = false;
781     }
782 }
783
784 inline void StyleBuilderCustom::applyInitialTextShadow(StyleResolver& styleResolver)
785 {
786     styleResolver.style()->setTextShadow(nullptr);
787 }
788
789 inline void StyleBuilderCustom::applyInheritTextShadow(StyleResolver& styleResolver)
790 {
791     styleResolver.style()->setTextShadow(styleResolver.parentStyle()->textShadow() ? std::make_unique<ShadowData>(*styleResolver.parentStyle()->textShadow()) : nullptr);
792 }
793
794 inline void StyleBuilderCustom::applyValueTextShadow(StyleResolver& styleResolver, CSSValue& value)
795 {
796     applyTextOrBoxShadowValue<CSSPropertyTextShadow>(styleResolver, value);
797 }
798
799 inline void StyleBuilderCustom::applyInitialBoxShadow(StyleResolver& styleResolver)
800 {
801     styleResolver.style()->setBoxShadow(nullptr);
802 }
803
804 inline void StyleBuilderCustom::applyInheritBoxShadow(StyleResolver& styleResolver)
805 {
806     styleResolver.style()->setBoxShadow(styleResolver.parentStyle()->boxShadow() ? std::make_unique<ShadowData>(*styleResolver.parentStyle()->boxShadow()) : nullptr);
807 }
808
809 inline void StyleBuilderCustom::applyValueBoxShadow(StyleResolver& styleResolver, CSSValue& value)
810 {
811     applyTextOrBoxShadowValue<CSSPropertyBoxShadow>(styleResolver, value);
812 }
813
814 inline void StyleBuilderCustom::applyInitialWebkitBoxShadow(StyleResolver& styleResolver)
815 {
816     applyInitialBoxShadow(styleResolver);
817 }
818
819 inline void StyleBuilderCustom::applyInheritWebkitBoxShadow(StyleResolver& styleResolver)
820 {
821     applyInheritBoxShadow(styleResolver);
822 }
823
824 inline void StyleBuilderCustom::applyValueWebkitBoxShadow(StyleResolver& styleResolver, CSSValue& value)
825 {
826     applyTextOrBoxShadowValue<CSSPropertyWebkitBoxShadow>(styleResolver, value);
827 }
828
829 inline void StyleBuilderCustom::applyInitialFontFamily(StyleResolver& styleResolver)
830 {
831     auto fontDescription = styleResolver.style()->fontDescription();
832     auto initialDesc = FontCascadeDescription();
833
834     // We need to adjust the size to account for the generic family change from monospace to non-monospace.
835     if (fontDescription.useFixedDefaultSize()) {
836         if (CSSValueID sizeIdentifier = fontDescription.keywordSizeAsIdentifier())
837             styleResolver.setFontSize(fontDescription, Style::fontSizeForKeyword(sizeIdentifier, false, styleResolver.document()));
838     }
839     if (!initialDesc.firstFamily().isEmpty())
840         fontDescription.setFamilies(initialDesc.families());
841
842     styleResolver.setFontDescription(fontDescription);
843 }
844
845 inline void StyleBuilderCustom::applyInheritFontFamily(StyleResolver& styleResolver)
846 {
847     auto fontDescription = styleResolver.style()->fontDescription();
848     auto parentFontDescription = styleResolver.parentStyle()->fontDescription();
849
850     fontDescription.setFamilies(parentFontDescription.families());
851     fontDescription.setIsSpecifiedFont(parentFontDescription.isSpecifiedFont());
852     styleResolver.setFontDescription(fontDescription);
853 }
854
855 inline void StyleBuilderCustom::applyValueFontFamily(StyleResolver& styleResolver, CSSValue& value)
856 {
857     auto& valueList = downcast<CSSValueList>(value);
858
859     auto fontDescription = styleResolver.style()->fontDescription();
860     // Before mapping in a new font-family property, we should reset the generic family.
861     bool oldFamilyUsedFixedDefaultSize = fontDescription.useFixedDefaultSize();
862
863     Vector<AtomicString> families;
864     families.reserveInitialCapacity(valueList.length());
865
866     for (auto& item : valueList) {
867         auto& contentValue = downcast<CSSPrimitiveValue>(item.get());
868         AtomicString family;
869         bool isGenericFamily = false;
870         if (contentValue.isFontFamily()) {
871             const CSSFontFamily& fontFamily = contentValue.fontFamily();
872             family = fontFamily.familyName;
873             // If the family name was resolved by the CSS parser from a system font ID, then it is generic.
874             isGenericFamily = fontFamily.fromSystemFontID;
875         } else {
876             switch (contentValue.getValueID()) {
877             case CSSValueWebkitBody:
878                 if (Settings* settings = styleResolver.document().settings())
879                     family = settings->standardFontFamily();
880                 break;
881             case CSSValueSerif:
882                 family = serifFamily;
883                 isGenericFamily = true;
884                 break;
885             case CSSValueSansSerif:
886                 family = sansSerifFamily;
887                 isGenericFamily = true;
888                 break;
889             case CSSValueCursive:
890                 family = cursiveFamily;
891                 isGenericFamily = true;
892                 break;
893             case CSSValueFantasy:
894                 family = fantasyFamily;
895                 isGenericFamily = true;
896                 break;
897             case CSSValueMonospace:
898                 family = monospaceFamily;
899                 isGenericFamily = true;
900                 break;
901             case CSSValueWebkitPictograph:
902                 family = pictographFamily;
903                 isGenericFamily = true;
904                 break;
905             default:
906                 break;
907             }
908         }
909
910         if (family.isEmpty())
911             continue;
912         if (families.isEmpty())
913             fontDescription.setIsSpecifiedFont(!isGenericFamily);
914         families.uncheckedAppend(family);
915     }
916
917     if (families.isEmpty())
918         return;
919     fontDescription.setFamilies(families);
920
921     if (fontDescription.useFixedDefaultSize() != oldFamilyUsedFixedDefaultSize) {
922         if (CSSValueID sizeIdentifier = fontDescription.keywordSizeAsIdentifier())
923             styleResolver.setFontSize(fontDescription, Style::fontSizeForKeyword(sizeIdentifier, !oldFamilyUsedFixedDefaultSize, styleResolver.document()));
924     }
925
926     styleResolver.setFontDescription(fontDescription);
927 }
928
929 inline bool StyleBuilderCustom::isValidDisplayValue(StyleResolver& styleResolver, EDisplay display)
930 {
931     if (is<SVGElement>(styleResolver.element()) && styleResolver.style()->styleType() == NOPSEUDO)
932         return display == INLINE || display == BLOCK || display == NONE;
933     return true;
934 }
935
936 inline void StyleBuilderCustom::applyInheritDisplay(StyleResolver& styleResolver)
937 {
938     EDisplay display = styleResolver.parentStyle()->display();
939     if (isValidDisplayValue(styleResolver, display))
940         styleResolver.style()->setDisplay(display);
941 }
942
943 inline void StyleBuilderCustom::applyValueDisplay(StyleResolver& styleResolver, CSSValue& value)
944 {
945     EDisplay display = downcast<CSSPrimitiveValue>(value);
946     if (isValidDisplayValue(styleResolver, display))
947         styleResolver.style()->setDisplay(display);
948 }
949
950 inline void StyleBuilderCustom::applyValueBaselineShift(StyleResolver& styleResolver, CSSValue& value)
951 {
952     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
953     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
954     if (primitiveValue.isValueID()) {
955         switch (primitiveValue.getValueID()) {
956         case CSSValueBaseline:
957             svgStyle.setBaselineShift(BS_BASELINE);
958             break;
959         case CSSValueSub:
960             svgStyle.setBaselineShift(BS_SUB);
961             break;
962         case CSSValueSuper:
963             svgStyle.setBaselineShift(BS_SUPER);
964             break;
965         default:
966             break;
967         }
968     } else {
969         svgStyle.setBaselineShift(BS_LENGTH);
970         svgStyle.setBaselineShiftValue(SVGLength::fromCSSPrimitiveValue(primitiveValue));
971     }
972 }
973
974 inline void StyleBuilderCustom::applyInitialWebkitAspectRatio(StyleResolver& styleResolver)
975 {
976     styleResolver.style()->setAspectRatioType(RenderStyle::initialAspectRatioType());
977     styleResolver.style()->setAspectRatioDenominator(RenderStyle::initialAspectRatioDenominator());
978     styleResolver.style()->setAspectRatioNumerator(RenderStyle::initialAspectRatioNumerator());
979 }
980
981 inline void StyleBuilderCustom::applyInheritWebkitAspectRatio(StyleResolver& styleResolver)
982 {
983     if (styleResolver.parentStyle()->aspectRatioType() == AspectRatioAuto)
984         return;
985     styleResolver.style()->setAspectRatioType(styleResolver.parentStyle()->aspectRatioType());
986     styleResolver.style()->setAspectRatioDenominator(styleResolver.parentStyle()->aspectRatioDenominator());
987     styleResolver.style()->setAspectRatioNumerator(styleResolver.parentStyle()->aspectRatioNumerator());
988 }
989
990 inline void StyleBuilderCustom::applyValueWebkitAspectRatio(StyleResolver& styleResolver, CSSValue& value)
991 {
992     if (is<CSSPrimitiveValue>(value)) {
993         auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
994
995         if (primitiveValue.getValueID() == CSSValueFromDimensions)
996             return styleResolver.style()->setAspectRatioType(AspectRatioFromDimensions);
997         if (primitiveValue.getValueID() == CSSValueFromIntrinsic)
998             return styleResolver.style()->setAspectRatioType(AspectRatioFromIntrinsic);
999
1000         ASSERT(primitiveValue.getValueID() == CSSValueAuto);
1001         return styleResolver.style()->setAspectRatioType(AspectRatioAuto);
1002     }
1003
1004     auto& aspectRatioValue = downcast<CSSAspectRatioValue>(value);
1005     styleResolver.style()->setAspectRatioType(AspectRatioSpecified);
1006     styleResolver.style()->setAspectRatioDenominator(aspectRatioValue.denominatorValue());
1007     styleResolver.style()->setAspectRatioNumerator(aspectRatioValue.numeratorValue());
1008 }
1009
1010 inline void StyleBuilderCustom::applyInitialWebkitTextEmphasisStyle(StyleResolver& styleResolver)
1011 {
1012     styleResolver.style()->setTextEmphasisFill(RenderStyle::initialTextEmphasisFill());
1013     styleResolver.style()->setTextEmphasisMark(RenderStyle::initialTextEmphasisMark());
1014     styleResolver.style()->setTextEmphasisCustomMark(RenderStyle::initialTextEmphasisCustomMark());
1015 }
1016
1017 inline void StyleBuilderCustom::applyInheritWebkitTextEmphasisStyle(StyleResolver& styleResolver)
1018 {
1019     styleResolver.style()->setTextEmphasisFill(styleResolver.parentStyle()->textEmphasisFill());
1020     styleResolver.style()->setTextEmphasisMark(styleResolver.parentStyle()->textEmphasisMark());
1021     styleResolver.style()->setTextEmphasisCustomMark(styleResolver.parentStyle()->textEmphasisCustomMark());
1022 }
1023
1024 inline void StyleBuilderCustom::applyValueWebkitTextEmphasisStyle(StyleResolver& styleResolver, CSSValue& value)
1025 {
1026     if (is<CSSValueList>(value)) {
1027         auto& list = downcast<CSSValueList>(value);
1028         ASSERT(list.length() == 2);
1029
1030         for (auto& item : list) {
1031             CSSPrimitiveValue& value = downcast<CSSPrimitiveValue>(item.get());
1032             if (value.getValueID() == CSSValueFilled || value.getValueID() == CSSValueOpen)
1033                 styleResolver.style()->setTextEmphasisFill(value);
1034             else
1035                 styleResolver.style()->setTextEmphasisMark(value);
1036         }
1037         styleResolver.style()->setTextEmphasisCustomMark(nullAtom);
1038         return;
1039     }
1040
1041     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
1042     if (primitiveValue.isString()) {
1043         styleResolver.style()->setTextEmphasisFill(TextEmphasisFillFilled);
1044         styleResolver.style()->setTextEmphasisMark(TextEmphasisMarkCustom);
1045         styleResolver.style()->setTextEmphasisCustomMark(primitiveValue.getStringValue());
1046         return;
1047     }
1048
1049     styleResolver.style()->setTextEmphasisCustomMark(nullAtom);
1050
1051     if (primitiveValue.getValueID() == CSSValueFilled || primitiveValue.getValueID() == CSSValueOpen) {
1052         styleResolver.style()->setTextEmphasisFill(primitiveValue);
1053         styleResolver.style()->setTextEmphasisMark(TextEmphasisMarkAuto);
1054     } else {
1055         styleResolver.style()->setTextEmphasisFill(TextEmphasisFillFilled);
1056         styleResolver.style()->setTextEmphasisMark(primitiveValue);
1057     }
1058 }
1059
1060 template <StyleBuilderCustom::CounterBehavior counterBehavior>
1061 inline void StyleBuilderCustom::applyInheritCounter(StyleResolver& styleResolver)
1062 {
1063     CounterDirectiveMap& map = styleResolver.style()->accessCounterDirectives();
1064     for (auto& keyValue : styleResolver.parentStyle()->accessCounterDirectives()) {
1065         CounterDirectives& directives = map.add(keyValue.key, CounterDirectives()).iterator->value;
1066         if (counterBehavior == Reset)
1067             directives.inheritReset(keyValue.value);
1068         else
1069             directives.inheritIncrement(keyValue.value);
1070     }
1071 }
1072
1073 template <StyleBuilderCustom::CounterBehavior counterBehavior>
1074 inline void StyleBuilderCustom::applyValueCounter(StyleResolver& styleResolver, CSSValue& value)
1075 {
1076     bool setCounterIncrementToNone = counterBehavior == Increment && is<CSSPrimitiveValue>(value) && downcast<CSSPrimitiveValue>(value).getValueID() == CSSValueNone;
1077
1078     if (!is<CSSValueList>(value) && !setCounterIncrementToNone)
1079         return;
1080
1081     CounterDirectiveMap& map = styleResolver.style()->accessCounterDirectives();
1082     for (auto& keyValue : map) {
1083         if (counterBehavior == Reset)
1084             keyValue.value.clearReset();
1085         else
1086             keyValue.value.clearIncrement();
1087     }
1088
1089     if (setCounterIncrementToNone)
1090         return;
1091
1092     for (auto& item : downcast<CSSValueList>(value)) {
1093         Pair* pair = downcast<CSSPrimitiveValue>(item.get()).getPairValue();
1094         if (!pair || !pair->first() || !pair->second())
1095             continue;
1096
1097         AtomicString identifier = pair->first()->getStringValue();
1098         int value = pair->second()->getIntValue();
1099         CounterDirectives& directives = map.add(identifier, CounterDirectives()).iterator->value;
1100         if (counterBehavior == Reset)
1101             directives.setResetValue(value);
1102         else
1103             directives.addIncrementValue(value);
1104     }
1105 }
1106
1107 inline void StyleBuilderCustom::applyInitialCounterIncrement(StyleResolver&) { }
1108
1109 inline void StyleBuilderCustom::applyInheritCounterIncrement(StyleResolver& styleResolver)
1110 {
1111     applyInheritCounter<Increment>(styleResolver);
1112 }
1113
1114 inline void StyleBuilderCustom::applyValueCounterIncrement(StyleResolver& styleResolver, CSSValue& value)
1115 {
1116     applyValueCounter<Increment>(styleResolver, value);
1117 }
1118
1119 inline void StyleBuilderCustom::applyInitialCounterReset(StyleResolver&) { }
1120
1121 inline void StyleBuilderCustom::applyInheritCounterReset(StyleResolver& styleResolver)
1122 {
1123     applyInheritCounter<Reset>(styleResolver);
1124 }
1125
1126 inline void StyleBuilderCustom::applyValueCounterReset(StyleResolver& styleResolver, CSSValue& value)
1127 {
1128     applyValueCounter<Reset>(styleResolver, value);
1129 }
1130
1131 inline void StyleBuilderCustom::applyInitialCursor(StyleResolver& styleResolver)
1132 {
1133     styleResolver.style()->clearCursorList();
1134     styleResolver.style()->setCursor(RenderStyle::initialCursor());
1135 }
1136
1137 inline void StyleBuilderCustom::applyInheritCursor(StyleResolver& styleResolver)
1138 {
1139     styleResolver.style()->setCursor(styleResolver.parentStyle()->cursor());
1140     styleResolver.style()->setCursorList(styleResolver.parentStyle()->cursors());
1141 }
1142
1143 inline void StyleBuilderCustom::applyValueCursor(StyleResolver& styleResolver, CSSValue& value)
1144 {
1145     styleResolver.style()->clearCursorList();
1146     if (is<CSSPrimitiveValue>(value)) {
1147         ECursor cursor = downcast<CSSPrimitiveValue>(value);
1148         if (styleResolver.style()->cursor() != cursor)
1149             styleResolver.style()->setCursor(cursor);
1150         return;
1151     }
1152
1153     styleResolver.style()->setCursor(CursorAuto);
1154     auto& list = downcast<CSSValueList>(value);
1155     for (auto& item : list) {
1156         if (is<CSSCursorImageValue>(item.get())) {
1157             auto& image = downcast<CSSCursorImageValue>(item.get());
1158             if (image.updateIfSVGCursorIsUsed(styleResolver.element())) // Elements with SVG cursors are not allowed to share style.
1159                 styleResolver.style()->setUnique();
1160             styleResolver.style()->addCursor(styleResolver.styleImage(CSSPropertyCursor, image), image.hotSpot());
1161             continue;
1162         }
1163
1164         styleResolver.style()->setCursor(downcast<CSSPrimitiveValue>(item.get()));
1165         ASSERT_WITH_MESSAGE(item.ptr() == list.item(list.length() - 1), "Cursor ID fallback should always be last in the list");
1166         return;
1167     }
1168 }
1169
1170 inline void StyleBuilderCustom::applyInitialFill(StyleResolver& styleResolver)
1171 {
1172     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
1173     svgStyle.setFillPaint(SVGRenderStyle::initialFillPaintType(), SVGRenderStyle::initialFillPaintColor(), SVGRenderStyle::initialFillPaintUri(), styleResolver.applyPropertyToRegularStyle(), styleResolver.applyPropertyToVisitedLinkStyle());
1174 }
1175
1176 inline void StyleBuilderCustom::applyInheritFill(StyleResolver& styleResolver)
1177 {
1178     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
1179     const SVGRenderStyle& svgParentStyle = styleResolver.parentStyle()->svgStyle();
1180     svgStyle.setFillPaint(svgParentStyle.fillPaintType(), svgParentStyle.fillPaintColor(), svgParentStyle.fillPaintUri(), styleResolver.applyPropertyToRegularStyle(), styleResolver.applyPropertyToVisitedLinkStyle());
1181
1182 }
1183
1184 inline void StyleBuilderCustom::applyValueFill(StyleResolver& styleResolver, CSSValue& value)
1185 {
1186     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
1187     SVGPaint& svgPaint = downcast<SVGPaint>(value);
1188     svgStyle.setFillPaint(svgPaint.paintType(), StyleBuilderConverter::convertSVGColor(styleResolver, svgPaint), svgPaint.uri(), styleResolver.applyPropertyToRegularStyle(), styleResolver.applyPropertyToVisitedLinkStyle());
1189 }
1190
1191 inline void StyleBuilderCustom::applyInitialStroke(StyleResolver& styleResolver)
1192 {
1193     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
1194     svgStyle.setStrokePaint(SVGRenderStyle::initialStrokePaintType(), SVGRenderStyle::initialStrokePaintColor(), SVGRenderStyle::initialStrokePaintUri(), styleResolver.applyPropertyToRegularStyle(), styleResolver.applyPropertyToVisitedLinkStyle());
1195 }
1196
1197 inline void StyleBuilderCustom::applyInheritStroke(StyleResolver& styleResolver)
1198 {
1199     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
1200     const SVGRenderStyle& svgParentStyle = styleResolver.parentStyle()->svgStyle();
1201     svgStyle.setStrokePaint(svgParentStyle.strokePaintType(), svgParentStyle.strokePaintColor(), svgParentStyle.strokePaintUri(), styleResolver.applyPropertyToRegularStyle(), styleResolver.applyPropertyToVisitedLinkStyle());
1202 }
1203
1204 inline void StyleBuilderCustom::applyValueStroke(StyleResolver& styleResolver, CSSValue& value)
1205 {
1206     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
1207     SVGPaint& svgPaint = downcast<SVGPaint>(value);
1208     svgStyle.setStrokePaint(svgPaint.paintType(), StyleBuilderConverter::convertSVGColor(styleResolver, svgPaint), svgPaint.uri(), styleResolver.applyPropertyToRegularStyle(), styleResolver.applyPropertyToVisitedLinkStyle());
1209 }
1210
1211 inline void StyleBuilderCustom::applyInitialWebkitSvgShadow(StyleResolver& styleResolver)
1212 {
1213     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
1214     svgStyle.setShadow(nullptr);
1215 }
1216
1217 inline void StyleBuilderCustom::applyInheritWebkitSvgShadow(StyleResolver& styleResolver)
1218 {
1219     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
1220     const SVGRenderStyle& svgParentStyle = styleResolver.parentStyle()->svgStyle();
1221     svgStyle.setShadow(svgParentStyle.shadow() ? std::make_unique<ShadowData>(*svgParentStyle.shadow()) : nullptr);
1222 }
1223
1224 inline void StyleBuilderCustom::applyValueWebkitSvgShadow(StyleResolver& styleResolver, CSSValue& value)
1225 {
1226     SVGRenderStyle& svgStyle = styleResolver.style()->accessSVGStyle();
1227     if (is<CSSPrimitiveValue>(value)) {
1228         ASSERT(downcast<CSSPrimitiveValue>(value).getValueID() == CSSValueNone);
1229         svgStyle.setShadow(nullptr);
1230         return;
1231     }
1232
1233     auto& shadowValue = downcast<CSSShadowValue>(*downcast<CSSValueList>(value).itemWithoutBoundsCheck(0));
1234     IntPoint location(shadowValue.x->computeLength<int>(styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f)),
1235         shadowValue.y->computeLength<int>(styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f)));
1236     int blur = shadowValue.blur ? shadowValue.blur->computeLength<int>(styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f)) : 0;
1237     Color color;
1238     if (shadowValue.color)
1239         color = styleResolver.colorFromPrimitiveValue(*shadowValue.color);
1240
1241     // -webkit-svg-shadow does should not have a spread or style
1242     ASSERT(!shadowValue.spread);
1243     ASSERT(!shadowValue.style);
1244
1245     svgStyle.setShadow(std::make_unique<ShadowData>(location, blur, 0, Normal, false, color.isValid() ? color : Color::transparent));
1246 }
1247
1248 inline void StyleBuilderCustom::applyInitialFontWeight(StyleResolver& styleResolver)
1249 {
1250     auto fontDescription = styleResolver.fontDescription();
1251     fontDescription.setWeight(FontWeightNormal);
1252     styleResolver.setFontDescription(fontDescription);
1253 }
1254
1255 inline void StyleBuilderCustom::applyInheritFontWeight(StyleResolver& styleResolver)
1256 {
1257     auto fontDescription = styleResolver.fontDescription();
1258     fontDescription.setWeight(styleResolver.parentFontDescription().weight());
1259     styleResolver.setFontDescription(fontDescription);
1260 }
1261
1262 inline void StyleBuilderCustom::applyValueFontWeight(StyleResolver& styleResolver, CSSValue& value)
1263 {
1264     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
1265     auto fontDescription = styleResolver.fontDescription();
1266     switch (primitiveValue.getValueID()) {
1267     case CSSValueInvalid:
1268         ASSERT_NOT_REACHED();
1269         break;
1270     case CSSValueBolder:
1271         fontDescription.setWeight(styleResolver.parentStyle()->fontDescription().weight());
1272         fontDescription.setWeight(fontDescription.bolderWeight());
1273         break;
1274     case CSSValueLighter:
1275         fontDescription.setWeight(styleResolver.parentStyle()->fontDescription().weight());
1276         fontDescription.setWeight(fontDescription.lighterWeight());
1277         break;
1278     default:
1279         fontDescription.setWeight(primitiveValue);
1280     }
1281     styleResolver.setFontDescription(fontDescription);
1282 }
1283
1284 inline void StyleBuilderCustom::applyInitialColumnGap(StyleResolver& styleResolver)
1285 {
1286     styleResolver.style()->setHasNormalColumnGap();
1287 }
1288
1289 inline void StyleBuilderCustom::applyInheritColumnGap(StyleResolver& styleResolver)
1290 {
1291     if (styleResolver.parentStyle()->hasNormalColumnGap())
1292         styleResolver.style()->setHasNormalColumnGap();
1293     else
1294         styleResolver.style()->setColumnGap(styleResolver.parentStyle()->columnGap());
1295 }
1296
1297 inline void StyleBuilderCustom::applyValueColumnGap(StyleResolver& styleResolver, CSSValue& value)
1298 {
1299     if (downcast<CSSPrimitiveValue>(value).getValueID() == CSSValueNormal)
1300         styleResolver.style()->setHasNormalColumnGap();
1301     else
1302         styleResolver.style()->setColumnGap(StyleBuilderConverter::convertComputedLength<float>(styleResolver, value));
1303 }
1304
1305 inline void StyleBuilderCustom::applyInitialContent(StyleResolver& styleResolver)
1306 {
1307     styleResolver.style()->clearContent();
1308 }
1309
1310 inline void StyleBuilderCustom::applyInheritContent(StyleResolver&)
1311 {
1312     // FIXME: In CSS3, it will be possible to inherit content. In CSS2 it is not. This
1313     // note is a reminder that eventually "inherit" needs to be supported.
1314 }
1315
1316 inline void StyleBuilderCustom::applyValueContent(StyleResolver& styleResolver, CSSValue& value)
1317 {
1318     bool didSet = false;
1319     for (auto& item : downcast<CSSValueList>(value)) {
1320         if (is<CSSImageGeneratorValue>(item.get())) {
1321             if (is<CSSGradientValue>(item.get()))
1322                 styleResolver.style()->setContent(StyleGeneratedImage::create(*downcast<CSSGradientValue>(item.get()).gradientWithStylesResolved(&styleResolver)), didSet);
1323             else
1324                 styleResolver.style()->setContent(StyleGeneratedImage::create(downcast<CSSImageGeneratorValue>(item.get())), didSet);
1325             didSet = true;
1326 #if ENABLE(CSS_IMAGE_SET)
1327         } else if (is<CSSImageSetValue>(item.get())) {
1328             styleResolver.style()->setContent(styleResolver.setOrPendingFromValue(CSSPropertyContent, downcast<CSSImageSetValue>(item.get())), didSet);
1329             didSet = true;
1330 #endif
1331         }
1332
1333         if (is<CSSImageValue>(item.get())) {
1334             styleResolver.style()->setContent(styleResolver.cachedOrPendingFromValue(CSSPropertyContent, downcast<CSSImageValue>(item.get())), didSet);
1335             didSet = true;
1336             continue;
1337         }
1338
1339         if (!is<CSSPrimitiveValue>(item.get()))
1340             continue;
1341
1342         auto& contentValue = downcast<CSSPrimitiveValue>(item.get());
1343         if (contentValue.isString()) {
1344             styleResolver.style()->setContent(contentValue.getStringValue().impl(), didSet);
1345             didSet = true;
1346         } else if (contentValue.isAttr()) {
1347             // FIXME: Can a namespace be specified for an attr(foo)?
1348             if (styleResolver.style()->styleType() == NOPSEUDO)
1349                 styleResolver.style()->setUnique();
1350             else
1351                 styleResolver.parentStyle()->setUnique();
1352             QualifiedName attr(nullAtom, contentValue.getStringValue().impl(), nullAtom);
1353             const AtomicString& value = styleResolver.element()->getAttribute(attr);
1354             styleResolver.style()->setContent(value.isNull() ? emptyAtom : value.impl(), didSet);
1355             didSet = true;
1356             // Register the fact that the attribute value affects the style.
1357             styleResolver.ruleSets().features().attributeCanonicalLocalNamesInRules.add(attr.localName().impl());
1358             styleResolver.ruleSets().features().attributeLocalNamesInRules.add(attr.localName().impl());
1359         } else if (contentValue.isCounter()) {
1360             Counter* counterValue = contentValue.getCounterValue();
1361             EListStyleType listStyleType = NoneListStyle;
1362             CSSValueID listStyleIdent = counterValue->listStyleIdent();
1363             if (listStyleIdent != CSSValueNone)
1364                 listStyleType = static_cast<EListStyleType>(listStyleIdent - CSSValueDisc);
1365             auto counter = std::make_unique<CounterContent>(counterValue->identifier(), listStyleType, counterValue->separator());
1366             styleResolver.style()->setContent(WTF::move(counter), didSet);
1367             didSet = true;
1368         } else {
1369             switch (contentValue.getValueID()) {
1370             case CSSValueOpenQuote:
1371                 styleResolver.style()->setContent(OPEN_QUOTE, didSet);
1372                 didSet = true;
1373                 break;
1374             case CSSValueCloseQuote:
1375                 styleResolver.style()->setContent(CLOSE_QUOTE, didSet);
1376                 didSet = true;
1377                 break;
1378             case CSSValueNoOpenQuote:
1379                 styleResolver.style()->setContent(NO_OPEN_QUOTE, didSet);
1380                 didSet = true;
1381                 break;
1382             case CSSValueNoCloseQuote:
1383                 styleResolver.style()->setContent(NO_CLOSE_QUOTE, didSet);
1384                 didSet = true;
1385                 break;
1386             default:
1387                 // normal and none do not have any effect.
1388                 break;
1389             }
1390         }
1391     }
1392     if (!didSet)
1393         styleResolver.style()->clearContent();
1394 }
1395
1396 inline void StyleBuilderCustom::applyInheritFontVariantLigatures(StyleResolver& styleResolver)
1397 {
1398     auto fontDescription = styleResolver.fontDescription();
1399     fontDescription.setVariantCommonLigatures(styleResolver.parentFontDescription().variantCommonLigatures());
1400     fontDescription.setVariantDiscretionaryLigatures(styleResolver.parentFontDescription().variantDiscretionaryLigatures());
1401     fontDescription.setVariantHistoricalLigatures(styleResolver.parentFontDescription().variantHistoricalLigatures());
1402     fontDescription.setVariantContextualAlternates(styleResolver.parentFontDescription().variantContextualAlternates());
1403     styleResolver.setFontDescription(fontDescription);
1404 }
1405
1406 inline void StyleBuilderCustom::applyInitialFontVariantLigatures(StyleResolver& styleResolver)
1407 {
1408     auto fontDescription = styleResolver.fontDescription();
1409     fontDescription.setVariantCommonLigatures(FontVariantLigatures::Normal);
1410     fontDescription.setVariantDiscretionaryLigatures(FontVariantLigatures::Normal);
1411     fontDescription.setVariantHistoricalLigatures(FontVariantLigatures::Normal);
1412     fontDescription.setVariantContextualAlternates(FontVariantLigatures::Normal);
1413     styleResolver.setFontDescription(fontDescription);
1414 }
1415
1416 inline void StyleBuilderCustom::applyValueFontVariantLigatures(StyleResolver& styleResolver, CSSValue& value)
1417 {
1418     auto fontDescription = styleResolver.fontDescription();
1419     WebCore::applyValueFontVariantLigatures(fontDescription, value);
1420     styleResolver.setFontDescription(fontDescription);
1421 }
1422
1423 inline void StyleBuilderCustom::applyInheritFontVariantNumeric(StyleResolver& styleResolver)
1424 {
1425     auto fontDescription = styleResolver.fontDescription();
1426     fontDescription.setVariantNumericFigure(styleResolver.parentFontDescription().variantNumericFigure());
1427     fontDescription.setVariantNumericSpacing(styleResolver.parentFontDescription().variantNumericSpacing());
1428     fontDescription.setVariantNumericFraction(styleResolver.parentFontDescription().variantNumericFraction());
1429     fontDescription.setVariantNumericOrdinal(styleResolver.parentFontDescription().variantNumericOrdinal());
1430     fontDescription.setVariantNumericSlashedZero(styleResolver.parentFontDescription().variantNumericSlashedZero());
1431     styleResolver.setFontDescription(fontDescription);
1432 }
1433
1434 inline void StyleBuilderCustom::applyInitialFontVariantNumeric(StyleResolver& styleResolver)
1435 {
1436     auto fontDescription = styleResolver.fontDescription();
1437     fontDescription.setVariantNumericFigure(FontVariantNumericFigure::Normal);
1438     fontDescription.setVariantNumericSpacing(FontVariantNumericSpacing::Normal);
1439     fontDescription.setVariantNumericFraction(FontVariantNumericFraction::Normal);
1440     fontDescription.setVariantNumericOrdinal(FontVariantNumericOrdinal::Normal);
1441     fontDescription.setVariantNumericSlashedZero(FontVariantNumericSlashedZero::Normal);
1442     styleResolver.setFontDescription(fontDescription);
1443 }
1444
1445 inline void StyleBuilderCustom::applyValueFontVariantNumeric(StyleResolver& styleResolver, CSSValue& value)
1446 {
1447     auto fontDescription = styleResolver.fontDescription();
1448     WebCore::applyValueFontVariantNumeric(fontDescription, value);
1449     styleResolver.setFontDescription(fontDescription);
1450 }
1451
1452 inline void StyleBuilderCustom::applyInheritFontVariantEastAsian(StyleResolver& styleResolver)
1453 {
1454     auto fontDescription = styleResolver.fontDescription();
1455     fontDescription.setVariantEastAsianVariant(styleResolver.parentFontDescription().variantEastAsianVariant());
1456     fontDescription.setVariantEastAsianWidth(styleResolver.parentFontDescription().variantEastAsianWidth());
1457     fontDescription.setVariantEastAsianRuby(styleResolver.parentFontDescription().variantEastAsianRuby());
1458     styleResolver.setFontDescription(fontDescription);
1459 }
1460
1461 inline void StyleBuilderCustom::applyInitialFontVariantEastAsian(StyleResolver& styleResolver)
1462 {
1463     auto fontDescription = styleResolver.fontDescription();
1464     fontDescription.setVariantEastAsianVariant(FontVariantEastAsianVariant::Normal);
1465     fontDescription.setVariantEastAsianWidth(FontVariantEastAsianWidth::Normal);
1466     fontDescription.setVariantEastAsianRuby(FontVariantEastAsianRuby::Normal);
1467     styleResolver.setFontDescription(fontDescription);
1468 }
1469
1470 inline void StyleBuilderCustom::applyValueFontVariantEastAsian(StyleResolver& styleResolver, CSSValue& value)
1471 {
1472     auto fontDescription = styleResolver.fontDescription();
1473     WebCore::applyValueFontVariantEastAsian(fontDescription, value);
1474     styleResolver.setFontDescription(fontDescription);
1475 }
1476
1477 inline void StyleBuilderCustom::applyInitialFontSize(StyleResolver& styleResolver)
1478 {
1479     auto fontDescription = styleResolver.style()->fontDescription();
1480     float size = Style::fontSizeForKeyword(CSSValueMedium, fontDescription.useFixedDefaultSize(), styleResolver.document());
1481
1482     if (size < 0)
1483         return;
1484
1485     fontDescription.setKeywordSizeFromIdentifier(CSSValueMedium);
1486     styleResolver.setFontSize(fontDescription, size);
1487     styleResolver.setFontDescription(fontDescription);
1488 }
1489
1490 inline void StyleBuilderCustom::applyInheritFontSize(StyleResolver& styleResolver)
1491 {
1492     const auto& parentFontDescription = styleResolver.parentStyle()->fontDescription();
1493     float size = parentFontDescription.specifiedSize();
1494
1495     if (size < 0)
1496         return;
1497
1498     auto fontDescription = styleResolver.style()->fontDescription();
1499     fontDescription.setKeywordSize(parentFontDescription.keywordSize());
1500     styleResolver.setFontSize(fontDescription, size);
1501     styleResolver.setFontDescription(fontDescription);
1502 }
1503
1504 // When the CSS keyword "larger" is used, this function will attempt to match within the keyword
1505 // table, and failing that, will simply multiply by 1.2.
1506 inline float StyleBuilderCustom::largerFontSize(float size)
1507 {
1508     // FIXME: Figure out where we fall in the size ranges (xx-small to xxx-large) and scale up to
1509     // the next size level.
1510     return size * 1.2f;
1511 }
1512
1513 // Like the previous function, but for the keyword "smaller".
1514 inline float StyleBuilderCustom::smallerFontSize(float size)
1515 {
1516     // FIXME: Figure out where we fall in the size ranges (xx-small to xxx-large) and scale down to
1517     // the next size level.
1518     return size / 1.2f;
1519 }
1520
1521 inline float StyleBuilderCustom::determineRubyTextSizeMultiplier(StyleResolver& styleResolver)
1522 {
1523     if (styleResolver.style()->rubyPosition() != RubyPositionInterCharacter)
1524         return 0.5f;
1525
1526     // FIXME: This hack is to ensure tone marks are the same size as
1527     // the bopomofo. This code will go away if we make a special renderer
1528     // for the tone marks eventually.
1529     if (auto* element = styleResolver.state().element()) {
1530         for (auto& ancestor : ancestorsOfType<HTMLElement>(*element)) {
1531             if (ancestor.hasTagName(HTMLNames::rtTag))
1532                 return 1.0f;
1533         }
1534     }
1535     return 0.25f;
1536 }
1537
1538 inline void StyleBuilderCustom::applyValueFontSize(StyleResolver& styleResolver, CSSValue& value)
1539 {
1540     auto fontDescription = styleResolver.style()->fontDescription();
1541     fontDescription.setKeywordSizeFromIdentifier(CSSValueInvalid);
1542
1543     float parentSize = 0;
1544     bool parentIsAbsoluteSize = false;
1545     if (auto* parentStyle = styleResolver.parentStyle()) {
1546         parentSize = parentStyle->fontDescription().specifiedSize();
1547         parentIsAbsoluteSize = parentStyle->fontDescription().isAbsoluteSize();
1548     }
1549
1550     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
1551     float size;
1552     if (CSSValueID ident = primitiveValue.getValueID()) {
1553         fontDescription.setIsAbsoluteSize(parentIsAbsoluteSize && (ident == CSSValueLarger || ident == CSSValueSmaller || ident == CSSValueWebkitRubyText));
1554
1555         // Keywords are being used.
1556         switch (ident) {
1557         case CSSValueXxSmall:
1558         case CSSValueXSmall:
1559         case CSSValueSmall:
1560         case CSSValueMedium:
1561         case CSSValueLarge:
1562         case CSSValueXLarge:
1563         case CSSValueXxLarge:
1564         case CSSValueWebkitXxxLarge:
1565             size = Style::fontSizeForKeyword(ident, fontDescription.useFixedDefaultSize(), styleResolver.document());
1566             fontDescription.setKeywordSizeFromIdentifier(ident);
1567             break;
1568         case CSSValueLarger:
1569             size = largerFontSize(parentSize);
1570             break;
1571         case CSSValueSmaller:
1572             size = smallerFontSize(parentSize);
1573             break;
1574         case CSSValueWebkitRubyText:
1575             size = determineRubyTextSizeMultiplier(styleResolver) * parentSize;
1576             break;
1577         default:
1578             return;
1579         }
1580     } else {
1581         fontDescription.setIsAbsoluteSize(parentIsAbsoluteSize || !(primitiveValue.isPercentage() || primitiveValue.isFontRelativeLength()));
1582         if (primitiveValue.isLength()) {
1583             size = primitiveValue.computeLength<float>(CSSToLengthConversionData(styleResolver.parentStyle(), styleResolver.rootElementStyle(), styleResolver.document().renderView(), 1.0f, true));
1584             styleResolver.state().setFontSizeHasViewportUnits(primitiveValue.isViewportPercentageLength());
1585         } else if (primitiveValue.isPercentage())
1586             size = (primitiveValue.getFloatValue() * parentSize) / 100.0f;
1587         else if (primitiveValue.isCalculatedPercentageWithLength())
1588             size = primitiveValue.cssCalcValue()->createCalculationValue(styleResolver.state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f))->evaluate(parentSize);
1589         else
1590             return;
1591     }
1592
1593     if (size < 0)
1594         return;
1595
1596     styleResolver.setFontSize(fontDescription, std::min(maximumAllowedFontSize, size));
1597     styleResolver.setFontDescription(fontDescription);
1598 }
1599
1600 #if ENABLE(CSS_GRID_LAYOUT)
1601 inline void StyleBuilderCustom::applyInitialWebkitGridTemplateAreas(StyleResolver& styleResolver)
1602 {
1603     styleResolver.style()->setNamedGridArea(RenderStyle::initialNamedGridArea());
1604     styleResolver.style()->setNamedGridAreaRowCount(RenderStyle::initialNamedGridAreaCount());
1605     styleResolver.style()->setNamedGridAreaColumnCount(RenderStyle::initialNamedGridAreaCount());
1606 }
1607
1608 inline void StyleBuilderCustom::applyInheritWebkitGridTemplateAreas(StyleResolver& styleResolver)
1609 {
1610     styleResolver.style()->setNamedGridArea(styleResolver.parentStyle()->namedGridArea());
1611     styleResolver.style()->setNamedGridAreaRowCount(styleResolver.parentStyle()->namedGridAreaRowCount());
1612     styleResolver.style()->setNamedGridAreaColumnCount(styleResolver.parentStyle()->namedGridAreaColumnCount());
1613 }
1614
1615 inline void StyleBuilderCustom::applyValueWebkitGridTemplateAreas(StyleResolver& styleResolver, CSSValue& value)
1616 {
1617     if (is<CSSPrimitiveValue>(value)) {
1618         ASSERT(downcast<CSSPrimitiveValue>(value).getValueID() == CSSValueNone);
1619         return;
1620     }
1621
1622     auto& gridTemplateAreasValue = downcast<CSSGridTemplateAreasValue>(value);
1623     const NamedGridAreaMap& newNamedGridAreas = gridTemplateAreasValue.gridAreaMap();
1624
1625     NamedGridLinesMap namedGridColumnLines = styleResolver.style()->namedGridColumnLines();
1626     NamedGridLinesMap namedGridRowLines = styleResolver.style()->namedGridRowLines();
1627     StyleBuilderConverter::createImplicitNamedGridLinesFromGridArea(newNamedGridAreas, namedGridColumnLines, ForColumns);
1628     StyleBuilderConverter::createImplicitNamedGridLinesFromGridArea(newNamedGridAreas, namedGridRowLines, ForRows);
1629     styleResolver.style()->setNamedGridColumnLines(namedGridColumnLines);
1630     styleResolver.style()->setNamedGridRowLines(namedGridRowLines);
1631
1632     styleResolver.style()->setNamedGridArea(gridTemplateAreasValue.gridAreaMap());
1633     styleResolver.style()->setNamedGridAreaRowCount(gridTemplateAreasValue.rowCount());
1634     styleResolver.style()->setNamedGridAreaColumnCount(gridTemplateAreasValue.columnCount());
1635 }
1636
1637 inline void StyleBuilderCustom::applyInitialWebkitGridTemplateColumns(StyleResolver& styleResolver)
1638 {
1639     styleResolver.style()->setGridColumns(RenderStyle::initialGridColumns());
1640     styleResolver.style()->setNamedGridColumnLines(RenderStyle::initialNamedGridColumnLines());
1641     styleResolver.style()->setOrderedNamedGridColumnLines(RenderStyle::initialOrderedNamedGridColumnLines());
1642 }
1643
1644 inline void StyleBuilderCustom::applyInheritWebkitGridTemplateColumns(StyleResolver& styleResolver)
1645 {
1646     styleResolver.style()->setGridColumns(styleResolver.parentStyle()->gridColumns());
1647     styleResolver.style()->setNamedGridColumnLines(styleResolver.parentStyle()->namedGridColumnLines());
1648     styleResolver.style()->setOrderedNamedGridColumnLines(styleResolver.parentStyle()->orderedNamedGridColumnLines());
1649 }
1650
1651 inline void StyleBuilderCustom::applyValueWebkitGridTemplateColumns(StyleResolver& styleResolver, CSSValue& value)
1652 {
1653     Vector<GridTrackSize> trackSizes;
1654     NamedGridLinesMap namedGridLines;
1655     OrderedNamedGridLinesMap orderedNamedGridLines;
1656     if (!StyleBuilderConverter::createGridTrackList(value, trackSizes, namedGridLines, orderedNamedGridLines, styleResolver))
1657         return;
1658     const NamedGridAreaMap& namedGridAreas = styleResolver.style()->namedGridArea();
1659     if (!namedGridAreas.isEmpty())
1660         StyleBuilderConverter::createImplicitNamedGridLinesFromGridArea(namedGridAreas, namedGridLines, ForColumns);
1661
1662     styleResolver.style()->setGridColumns(trackSizes);
1663     styleResolver.style()->setNamedGridColumnLines(namedGridLines);
1664     styleResolver.style()->setOrderedNamedGridColumnLines(orderedNamedGridLines);
1665 }
1666
1667 inline void StyleBuilderCustom::applyInitialWebkitGridTemplateRows(StyleResolver& styleResolver)
1668 {
1669     styleResolver.style()->setGridRows(RenderStyle::initialGridRows());
1670     styleResolver.style()->setNamedGridRowLines(RenderStyle::initialNamedGridRowLines());
1671     styleResolver.style()->setOrderedNamedGridRowLines(RenderStyle::initialOrderedNamedGridRowLines());
1672 }
1673
1674 inline void StyleBuilderCustom::applyInheritWebkitGridTemplateRows(StyleResolver& styleResolver)
1675 {
1676     styleResolver.style()->setGridRows(styleResolver.parentStyle()->gridRows());
1677     styleResolver.style()->setNamedGridRowLines(styleResolver.parentStyle()->namedGridRowLines());
1678     styleResolver.style()->setOrderedNamedGridRowLines(styleResolver.parentStyle()->orderedNamedGridRowLines());
1679 }
1680
1681 inline void StyleBuilderCustom::applyValueWebkitGridTemplateRows(StyleResolver& styleResolver, CSSValue& value)
1682 {
1683     Vector<GridTrackSize> trackSizes;
1684     NamedGridLinesMap namedGridLines;
1685     OrderedNamedGridLinesMap orderedNamedGridLines;
1686     if (!StyleBuilderConverter::createGridTrackList(value, trackSizes, namedGridLines, orderedNamedGridLines, styleResolver))
1687         return;
1688     const NamedGridAreaMap& namedGridAreas = styleResolver.style()->namedGridArea();
1689     if (!namedGridAreas.isEmpty())
1690         StyleBuilderConverter::createImplicitNamedGridLinesFromGridArea(namedGridAreas, namedGridLines, ForRows);
1691
1692     styleResolver.style()->setGridRows(trackSizes);
1693     styleResolver.style()->setNamedGridRowLines(namedGridLines);
1694     styleResolver.style()->setOrderedNamedGridRowLines(orderedNamedGridLines);
1695 }
1696 #endif // ENABLE(CSS_GRID_LAYOUT)
1697
1698 void StyleBuilderCustom::applyValueAlt(StyleResolver& styleResolver, CSSValue& value)
1699 {
1700     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
1701     if (primitiveValue.isString())
1702         styleResolver.style()->setContentAltText(primitiveValue.getStringValue());
1703     else if (primitiveValue.isAttr()) {
1704         // FIXME: Can a namespace be specified for an attr(foo)?
1705         if (styleResolver.style()->styleType() == NOPSEUDO)
1706             styleResolver.style()->setUnique();
1707         else
1708             styleResolver.parentStyle()->setUnique();
1709
1710         QualifiedName attr(nullAtom, primitiveValue.getStringValue(), nullAtom);
1711         const AtomicString& value = styleResolver.element()->getAttribute(attr);
1712         styleResolver.style()->setContentAltText(value.isNull() ? emptyAtom : value);
1713
1714         // Register the fact that the attribute value affects the style.
1715         styleResolver.ruleSets().features().attributeCanonicalLocalNamesInRules.add(attr.localName().impl());
1716         styleResolver.ruleSets().features().attributeLocalNamesInRules.add(attr.localName().impl());
1717     } else
1718         styleResolver.style()->setContentAltText(emptyAtom);
1719 }
1720
1721 #if ENABLE(CSS_SCROLL_SNAP)
1722 inline void StyleBuilderCustom::applyInitialWebkitScrollSnapPointsX(StyleResolver& styleResolver)
1723 {
1724     styleResolver.style()->setScrollSnapPointsX(nullptr);
1725 }
1726
1727 inline void StyleBuilderCustom::applyInheritWebkitScrollSnapPointsX(StyleResolver& styleResolver)
1728 {
1729     styleResolver.style()->setScrollSnapPointsX(styleResolver.parentStyle()->scrollSnapPointsX() ? std::make_unique<ScrollSnapPoints>(*styleResolver.parentStyle()->scrollSnapPointsX()) : nullptr);
1730 }
1731
1732 inline void StyleBuilderCustom::applyInitialWebkitScrollSnapPointsY(StyleResolver& styleResolver)
1733 {
1734     styleResolver.style()->setScrollSnapPointsY(nullptr);
1735 }
1736
1737 inline void StyleBuilderCustom::applyInheritWebkitScrollSnapPointsY(StyleResolver& styleResolver)
1738 {
1739     styleResolver.style()->setScrollSnapPointsY(styleResolver.parentStyle()->scrollSnapPointsY() ? std::make_unique<ScrollSnapPoints>(*styleResolver.parentStyle()->scrollSnapPointsY()) : nullptr);
1740 }
1741 #endif
1742
1743 inline void StyleBuilderCustom::applyValueWillChange(StyleResolver& styleResolver, CSSValue& value)
1744 {
1745     if (is<CSSPrimitiveValue>(value)) {
1746         ASSERT(downcast<CSSPrimitiveValue>(value).getValueID() == CSSValueAuto);
1747         styleResolver.style()->setWillChange(nullptr);
1748         return;
1749     }
1750
1751     Ref<WillChangeData> willChange = WillChangeData::create();
1752     for (auto& item : downcast<CSSValueList>(value)) {
1753         if (!is<CSSPrimitiveValue>(item.get()))
1754             continue;
1755
1756         const auto& primitiveValue = downcast<CSSPrimitiveValue>(item.get());
1757         switch (primitiveValue.getValueID()) {
1758         case CSSValueScrollPosition:
1759             willChange->addFeature(WillChangeData::Feature::ScrollPosition);
1760             break;
1761         case CSSValueContents:
1762             willChange->addFeature(WillChangeData::Feature::Contents);
1763             break;
1764         default:
1765             if (primitiveValue.isPropertyID())
1766                 willChange->addFeature(WillChangeData::Feature::Property, primitiveValue.getPropertyID());
1767             break;
1768         }
1769     }
1770
1771     styleResolver.style()->setWillChange(WTF::move(willChange));
1772 }
1773
1774 } // namespace WebCore
1775
1776 #endif // StyleBuilderCustom_h