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