Add support for the CSS 'unset' keyword.
[WebKit-https.git] / Source / WebCore / css / CSSToStyleMap.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4  * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
6  * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7  * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9  * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public License
23  * along with this library; see the file COPYING.LIB.  If not, write to
24  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  * Boston, MA 02110-1301, USA.
26  */
27
28 #include "config.h"
29 #include "CSSToStyleMap.h"
30
31 #include "Animation.h"
32 #include "CSSAnimationTriggerScrollValue.h"
33 #include "CSSBorderImageSliceValue.h"
34 #include "CSSImageGeneratorValue.h"
35 #include "CSSImageSetValue.h"
36 #include "CSSImageValue.h"
37 #include "CSSPrimitiveValue.h"
38 #include "CSSPrimitiveValueMappings.h"
39 #include "CSSTimingFunctionValue.h"
40 #include "CSSValueKeywords.h"
41 #include "FillLayer.h"
42 #include "Pair.h"
43 #include "Rect.h"
44 #include "RenderView.h"
45 #include "StyleResolver.h"
46
47 namespace WebCore {
48
49 CSSToStyleMap::CSSToStyleMap(StyleResolver* resolver)
50     : m_resolver(resolver)
51 {
52 }
53
54 RenderStyle* CSSToStyleMap::style() const
55 {
56     return m_resolver->style();
57 }
58
59 RenderStyle* CSSToStyleMap::rootElementStyle() const
60 {
61     return m_resolver->rootElementStyle();
62 }
63
64 bool CSSToStyleMap::useSVGZoomRules() const
65 {
66     return m_resolver->useSVGZoomRules();
67 }
68
69 RefPtr<StyleImage> CSSToStyleMap::styleImage(CSSPropertyID propertyId, CSSValue& value)
70 {
71     return m_resolver->styleImage(propertyId, value);
72 }
73
74 void CSSToStyleMap::mapFillAttachment(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
75 {
76     if (value.treatAsInitialValue(propertyID)) {
77         layer.setAttachment(FillLayer::initialFillAttachment(layer.type()));
78         return;
79     }
80
81     if (!is<CSSPrimitiveValue>(value))
82         return;
83
84     switch (downcast<CSSPrimitiveValue>(value).getValueID()) {
85     case CSSValueFixed:
86         layer.setAttachment(FixedBackgroundAttachment);
87         break;
88     case CSSValueScroll:
89         layer.setAttachment(ScrollBackgroundAttachment);
90         break;
91     case CSSValueLocal:
92         layer.setAttachment(LocalBackgroundAttachment);
93         break;
94     default:
95         return;
96     }
97 }
98
99 void CSSToStyleMap::mapFillClip(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
100 {
101     if (value.treatAsInitialValue(propertyID)) {
102         layer.setClip(FillLayer::initialFillClip(layer.type()));
103         return;
104     }
105
106     if (!is<CSSPrimitiveValue>(value))
107         return;
108
109     layer.setClip(downcast<CSSPrimitiveValue>(value));
110 }
111
112 void CSSToStyleMap::mapFillComposite(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
113 {
114     if (value.treatAsInitialValue(propertyID)) {
115         layer.setComposite(FillLayer::initialFillComposite(layer.type()));
116         return;
117     }
118
119     if (!is<CSSPrimitiveValue>(value))
120         return;
121
122     layer.setComposite(downcast<CSSPrimitiveValue>(value));
123 }
124
125 void CSSToStyleMap::mapFillBlendMode(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
126 {
127     if (value.treatAsInitialValue(propertyID)) {
128         layer.setBlendMode(FillLayer::initialFillBlendMode(layer.type()));
129         return;
130     }
131
132     if (!is<CSSPrimitiveValue>(value))
133         return;
134
135     layer.setBlendMode(downcast<CSSPrimitiveValue>(value));
136 }
137
138 void CSSToStyleMap::mapFillOrigin(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
139 {
140     if (value.treatAsInitialValue(propertyID)) {
141         layer.setOrigin(FillLayer::initialFillOrigin(layer.type()));
142         return;
143     }
144
145     if (!is<CSSPrimitiveValue>(value))
146         return;
147
148     layer.setOrigin(downcast<CSSPrimitiveValue>(value));
149 }
150
151 void CSSToStyleMap::mapFillImage(CSSPropertyID propertyID, FillLayer& layer, CSSValue& value)
152 {
153     if (value.treatAsInitialValue(propertyID)) {
154         layer.setImage(FillLayer::initialFillImage(layer.type()));
155         return;
156     }
157
158     layer.setImage(styleImage(propertyID, value));
159 }
160
161 void CSSToStyleMap::mapFillRepeatX(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
162 {
163     if (value.treatAsInitialValue(propertyID)) {
164         layer.setRepeatX(FillLayer::initialFillRepeatX(layer.type()));
165         return;
166     }
167
168     if (!is<CSSPrimitiveValue>(value))
169         return;
170
171     layer.setRepeatX(downcast<CSSPrimitiveValue>(value));
172 }
173
174 void CSSToStyleMap::mapFillRepeatY(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
175 {
176     if (value.treatAsInitialValue(propertyID)) {
177         layer.setRepeatY(FillLayer::initialFillRepeatY(layer.type()));
178         return;
179     }
180
181     if (!is<CSSPrimitiveValue>(value))
182         return;
183
184     layer.setRepeatY(downcast<CSSPrimitiveValue>(value));
185 }
186
187 static inline bool convertToLengthSize(const CSSPrimitiveValue& primitiveValue, CSSToLengthConversionData conversionData, LengthSize& size)
188 {
189     if (auto* pair = primitiveValue.getPairValue()) {
190         size.setWidth(pair->first()->convertToLength<AnyConversion>(conversionData));
191         size.setHeight(pair->second()->convertToLength<AnyConversion>(conversionData));
192     } else
193         size.setWidth(primitiveValue.convertToLength<AnyConversion>(conversionData));
194
195     return !size.width().isUndefined() && !size.height().isUndefined();
196 }
197
198 void CSSToStyleMap::mapFillSize(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
199 {
200     if (value.treatAsInitialValue(propertyID)) {
201         layer.setSize(FillLayer::initialFillSize(layer.type()));
202         return;
203     }
204
205     if (!is<CSSPrimitiveValue>(value))
206         return;
207
208     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
209     FillSize fillSize;
210     switch (primitiveValue.getValueID()) {
211     case CSSValueContain:
212         fillSize.type = Contain;
213         break;
214     case CSSValueCover:
215         fillSize.type = Cover;
216         break;
217     default:
218         ASSERT(fillSize.type == SizeLength);
219         if (!convertToLengthSize(primitiveValue, m_resolver->state().cssToLengthConversionData(), fillSize.size))
220             return;
221         break;
222     }
223     layer.setSize(fillSize);
224 }
225
226 void CSSToStyleMap::mapFillXPosition(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
227 {
228     if (value.treatAsInitialValue(propertyID)) {
229         layer.setXPosition(FillLayer::initialFillXPosition(layer.type()));
230         return;
231     }
232
233     if (!is<CSSPrimitiveValue>(value))
234         return;
235
236     auto* primitiveValue = &downcast<CSSPrimitiveValue>(value);
237     Pair* pair = primitiveValue->getPairValue();
238     if (pair) {
239         ASSERT_UNUSED(propertyID, propertyID == CSSPropertyBackgroundPositionX || propertyID == CSSPropertyWebkitMaskPositionX);
240         primitiveValue = pair->second();
241     }
242
243     Length length;
244     if (primitiveValue->isLength())
245         length = primitiveValue->computeLength<Length>(m_resolver->state().cssToLengthConversionData());
246     else if (primitiveValue->isPercentage())
247         length = Length(primitiveValue->getDoubleValue(), Percent);
248     else if (primitiveValue->isCalculatedPercentageWithLength())
249         length = Length(primitiveValue->cssCalcValue()->createCalculationValue(m_resolver->state().cssToLengthConversionData()));
250     else
251         return;
252
253     layer.setXPosition(length);
254     if (pair)
255         layer.setBackgroundXOrigin(*pair->first());
256 }
257
258 void CSSToStyleMap::mapFillYPosition(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
259 {
260     if (value.treatAsInitialValue(propertyID)) {
261         layer.setYPosition(FillLayer::initialFillYPosition(layer.type()));
262         return;
263     }
264
265     if (!is<CSSPrimitiveValue>(value))
266         return;
267
268     auto* primitiveValue = &downcast<CSSPrimitiveValue>(value);
269     Pair* pair = primitiveValue->getPairValue();
270     if (pair) {
271         ASSERT_UNUSED(propertyID, propertyID == CSSPropertyBackgroundPositionY || propertyID == CSSPropertyWebkitMaskPositionY);
272         primitiveValue = pair->second();
273     }
274
275     Length length;
276     if (primitiveValue->isLength())
277         length = primitiveValue->computeLength<Length>(m_resolver->state().cssToLengthConversionData());
278     else if (primitiveValue->isPercentage())
279         length = Length(primitiveValue->getDoubleValue(), Percent);
280     else if (primitiveValue->isCalculatedPercentageWithLength())
281         length = Length(primitiveValue->cssCalcValue()->createCalculationValue(m_resolver->state().cssToLengthConversionData()));
282     else
283         return;
284
285     layer.setYPosition(length);
286     if (pair)
287         layer.setBackgroundYOrigin(*pair->first());
288 }
289
290 void CSSToStyleMap::mapFillMaskSourceType(CSSPropertyID propertyID, FillLayer& layer, const CSSValue& value)
291 {
292     EMaskSourceType type = FillLayer::initialFillMaskSourceType(layer.type());
293     if (value.treatAsInitialValue(propertyID)) {
294         layer.setMaskSourceType(type);
295         return;
296     }
297
298     if (!is<CSSPrimitiveValue>(value))
299         return;
300
301     switch (downcast<CSSPrimitiveValue>(value).getValueID()) {
302     case CSSValueAlpha:
303         type = EMaskSourceType::MaskAlpha;
304         break;
305     case CSSValueLuminance:
306         type = EMaskSourceType::MaskLuminance;
307         break;
308     case CSSValueAuto:
309         break;
310     default:
311         ASSERT_NOT_REACHED();
312     }
313
314     layer.setMaskSourceType(type);
315 }
316
317 void CSSToStyleMap::mapAnimationDelay(Animation& animation, const CSSValue& value)
318 {
319     if (value.treatAsInitialValue(CSSPropertyWebkitAnimationDelay)) {
320         animation.setDelay(Animation::initialDelay());
321         return;
322     }
323
324     if (!is<CSSPrimitiveValue>(value))
325         return;
326
327     animation.setDelay(downcast<CSSPrimitiveValue>(value).computeTime<double, CSSPrimitiveValue::Seconds>());
328 }
329
330 void CSSToStyleMap::mapAnimationDirection(Animation& layer, const CSSValue& value)
331 {
332     if (value.treatAsInitialValue(CSSPropertyWebkitAnimationDirection)) {
333         layer.setDirection(Animation::initialDirection());
334         return;
335     }
336
337     if (!is<CSSPrimitiveValue>(value))
338         return;
339
340     switch (downcast<CSSPrimitiveValue>(value).getValueID()) {
341     case CSSValueNormal:
342         layer.setDirection(Animation::AnimationDirectionNormal);
343         break;
344     case CSSValueAlternate:
345         layer.setDirection(Animation::AnimationDirectionAlternate);
346         break;
347     case CSSValueReverse:
348         layer.setDirection(Animation::AnimationDirectionReverse);
349         break;
350     case CSSValueAlternateReverse:
351         layer.setDirection(Animation::AnimationDirectionAlternateReverse);
352         break;
353     default:
354         break;
355     }
356 }
357
358 void CSSToStyleMap::mapAnimationDuration(Animation& animation, const CSSValue& value)
359 {
360     if (value.treatAsInitialValue(CSSPropertyWebkitAnimationDuration)) {
361         animation.setDuration(Animation::initialDuration());
362         return;
363     }
364
365     if (!is<CSSPrimitiveValue>(value))
366         return;
367
368     animation.setDuration(downcast<CSSPrimitiveValue>(value).computeTime<double, CSSPrimitiveValue::Seconds>());
369 }
370
371 void CSSToStyleMap::mapAnimationFillMode(Animation& layer, const CSSValue& value)
372 {
373     if (value.treatAsInitialValue(CSSPropertyWebkitAnimationFillMode)) {
374         layer.setFillMode(Animation::initialFillMode());
375         return;
376     }
377
378     if (!is<CSSPrimitiveValue>(value))
379         return;
380
381     switch (downcast<CSSPrimitiveValue>(value).getValueID()) {
382     case CSSValueNone:
383         layer.setFillMode(AnimationFillModeNone);
384         break;
385     case CSSValueForwards:
386         layer.setFillMode(AnimationFillModeForwards);
387         break;
388     case CSSValueBackwards:
389         layer.setFillMode(AnimationFillModeBackwards);
390         break;
391     case CSSValueBoth:
392         layer.setFillMode(AnimationFillModeBoth);
393         break;
394     default:
395         break;
396     }
397 }
398
399 void CSSToStyleMap::mapAnimationIterationCount(Animation& animation, const CSSValue& value)
400 {
401     if (value.treatAsInitialValue(CSSPropertyWebkitAnimationIterationCount)) {
402         animation.setIterationCount(Animation::initialIterationCount());
403         return;
404     }
405
406     if (!is<CSSPrimitiveValue>(value))
407         return;
408
409     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
410     if (primitiveValue.getValueID() == CSSValueInfinite)
411         animation.setIterationCount(Animation::IterationCountInfinite);
412     else
413         animation.setIterationCount(primitiveValue.getFloatValue());
414 }
415
416 void CSSToStyleMap::mapAnimationName(Animation& layer, const CSSValue& value)
417 {
418     if (value.treatAsInitialValue(CSSPropertyWebkitAnimationName)) {
419         layer.setName(Animation::initialName());
420         return;
421     }
422
423     if (!is<CSSPrimitiveValue>(value))
424         return;
425
426     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
427     if (primitiveValue.getValueID() == CSSValueNone)
428         layer.setIsNoneAnimation(true);
429     else
430         layer.setName(primitiveValue.getStringValue());
431 }
432
433 void CSSToStyleMap::mapAnimationPlayState(Animation& layer, const CSSValue& value)
434 {
435     if (value.treatAsInitialValue(CSSPropertyWebkitAnimationPlayState)) {
436         layer.setPlayState(Animation::initialPlayState());
437         return;
438     }
439
440     if (!is<CSSPrimitiveValue>(value))
441         return;
442
443     EAnimPlayState playState = (downcast<CSSPrimitiveValue>(value).getValueID() == CSSValuePaused) ? AnimPlayStatePaused : AnimPlayStatePlaying;
444     layer.setPlayState(playState);
445 }
446
447 void CSSToStyleMap::mapAnimationProperty(Animation& animation, const CSSValue& value)
448 {
449     if (value.treatAsInitialValue(CSSPropertyWebkitAnimation)) {
450         animation.setAnimationMode(Animation::AnimateAll);
451         animation.setProperty(CSSPropertyInvalid);
452         return;
453     }
454
455     if (!is<CSSPrimitiveValue>(value))
456         return;
457
458     auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
459     if (primitiveValue.getValueID() == CSSValueAll) {
460         animation.setAnimationMode(Animation::AnimateAll);
461         animation.setProperty(CSSPropertyInvalid);
462     } else if (primitiveValue.getValueID() == CSSValueNone) {
463         animation.setAnimationMode(Animation::AnimateNone);
464         animation.setProperty(CSSPropertyInvalid);
465     } else {
466         animation.setAnimationMode(Animation::AnimateSingleProperty);
467         animation.setProperty(primitiveValue.getPropertyID());
468     }
469 }
470
471 void CSSToStyleMap::mapAnimationTimingFunction(Animation& animation, const CSSValue& value)
472 {
473     if (value.treatAsInitialValue(CSSPropertyWebkitAnimationTimingFunction)) {
474         animation.setTimingFunction(Animation::initialTimingFunction());
475         return;
476     }
477
478     if (is<CSSPrimitiveValue>(value)) {
479         switch (downcast<CSSPrimitiveValue>(value).getValueID()) {
480         case CSSValueLinear:
481             animation.setTimingFunction(LinearTimingFunction::create());
482             break;
483         case CSSValueEase:
484             animation.setTimingFunction(CubicBezierTimingFunction::create());
485             break;
486         case CSSValueEaseIn:
487             animation.setTimingFunction(CubicBezierTimingFunction::create(CubicBezierTimingFunction::EaseIn));
488             break;
489         case CSSValueEaseOut:
490             animation.setTimingFunction(CubicBezierTimingFunction::create(CubicBezierTimingFunction::EaseOut));
491             break;
492         case CSSValueEaseInOut:
493             animation.setTimingFunction(CubicBezierTimingFunction::create(CubicBezierTimingFunction::EaseInOut));
494             break;
495         case CSSValueStepStart:
496             animation.setTimingFunction(StepsTimingFunction::create(1, true));
497             break;
498         case CSSValueStepEnd:
499             animation.setTimingFunction(StepsTimingFunction::create(1, false));
500             break;
501         default:
502             break;
503         }
504         return;
505     }
506
507     if (is<CSSCubicBezierTimingFunctionValue>(value)) {
508         auto& cubicTimingFunction = downcast<CSSCubicBezierTimingFunctionValue>(value);
509         animation.setTimingFunction(CubicBezierTimingFunction::create(cubicTimingFunction.x1(), cubicTimingFunction.y1(), cubicTimingFunction.x2(), cubicTimingFunction.y2()));
510     } else if (is<CSSStepsTimingFunctionValue>(value)) {
511         auto& stepsTimingFunction = downcast<CSSStepsTimingFunctionValue>(value);
512         animation.setTimingFunction(StepsTimingFunction::create(stepsTimingFunction.numberOfSteps(), stepsTimingFunction.stepAtStart()));
513     }
514 }
515
516 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
517 void CSSToStyleMap::mapAnimationTrigger(Animation& animation, const CSSValue& value)
518 {
519     if (value.treatAsInitialValue(CSSPropertyWebkitAnimationTrigger)) {
520         animation.setTrigger(Animation::initialTrigger());
521         return;
522     }
523
524     if (value.isPrimitiveValue()) {
525         auto& primitiveValue = downcast<CSSPrimitiveValue>(value);
526         if (primitiveValue.getValueID() == CSSValueAuto)
527             animation.setTrigger(AutoAnimationTrigger::create());
528         return;
529     }
530
531     if (value.isAnimationTriggerScrollValue()) {
532         auto& scrollTrigger = downcast<CSSAnimationTriggerScrollValue>(value);
533
534         const CSSPrimitiveValue* startValue = downcast<CSSPrimitiveValue>(scrollTrigger.startValue());
535         Length startLength = startValue->computeLength<Length>(m_resolver->state().cssToLengthConversionData());
536
537         Length endLength;
538         if (scrollTrigger.hasEndValue()) {
539             const CSSPrimitiveValue* endValue = downcast<CSSPrimitiveValue>(scrollTrigger.endValue());
540             endLength = endValue->computeLength<Length>(m_resolver->state().cssToLengthConversionData());
541         }
542
543         animation.setTrigger(ScrollAnimationTrigger::create(startLength, endLength));
544     }
545 }
546 #endif
547
548 void CSSToStyleMap::mapNinePieceImage(CSSPropertyID property, CSSValue* value, NinePieceImage& image)
549 {
550     // If we're not a value list, then we are "none" and don't need to alter the empty image at all.
551     if (!is<CSSValueList>(value))
552         return;
553
554     // Retrieve the border image value.
555     CSSValueList& borderImage = downcast<CSSValueList>(*value);
556
557     // Set the image (this kicks off the load).
558     CSSPropertyID imageProperty;
559     if (property == CSSPropertyWebkitBorderImage)
560         imageProperty = CSSPropertyBorderImageSource;
561     else if (property == CSSPropertyWebkitMaskBoxImage)
562         imageProperty = CSSPropertyWebkitMaskBoxImageSource;
563     else
564         imageProperty = property;
565
566     for (auto& current : borderImage) {
567         if (is<CSSImageValue>(current.get()) || is<CSSImageGeneratorValue>(current.get())
568 #if ENABLE(CSS_IMAGE_SET)
569             || is<CSSImageSetValue>(current.get())
570 #endif
571             )
572             image.setImage(styleImage(imageProperty, current.get()));
573         else if (is<CSSBorderImageSliceValue>(current.get()))
574             mapNinePieceImageSlice(current, image);
575         else if (is<CSSValueList>(current.get())) {
576             CSSValueList& slashList = downcast<CSSValueList>(current.get());
577             // Map in the image slices.
578             if (is<CSSBorderImageSliceValue>(slashList.item(0)))
579                 mapNinePieceImageSlice(*slashList.item(0), image);
580
581             // Map in the border slices.
582             if (slashList.item(1))
583                 image.setBorderSlices(mapNinePieceImageQuad(*slashList.item(1)));
584
585             // Map in the outset.
586             if (slashList.item(2))
587                 image.setOutset(mapNinePieceImageQuad(*slashList.item(2)));
588         } else if (is<CSSPrimitiveValue>(current.get())) {
589             // Set the appropriate rules for stretch/round/repeat of the slices.
590             mapNinePieceImageRepeat(current, image);
591         }
592     }
593
594     if (property == CSSPropertyWebkitBorderImage) {
595         // We have to preserve the legacy behavior of -webkit-border-image and make the border slices
596         // also set the border widths. We don't need to worry about percentages, since we don't even support
597         // those on real borders yet.
598         if (image.borderSlices().top().isFixed())
599             style()->setBorderTopWidth(image.borderSlices().top().value());
600         if (image.borderSlices().right().isFixed())
601             style()->setBorderRightWidth(image.borderSlices().right().value());
602         if (image.borderSlices().bottom().isFixed())
603             style()->setBorderBottomWidth(image.borderSlices().bottom().value());
604         if (image.borderSlices().left().isFixed())
605             style()->setBorderLeftWidth(image.borderSlices().left().value());
606     }
607 }
608
609 void CSSToStyleMap::mapNinePieceImageSlice(CSSValue& value, NinePieceImage& image)
610 {
611     if (!is<CSSBorderImageSliceValue>(value))
612         return;
613
614     // Retrieve the border image value.
615     auto& borderImageSlice = downcast<CSSBorderImageSliceValue>(value);
616
617     // Set up a length box to represent our image slices.
618     LengthBox box;
619     Quad* slices = borderImageSlice.slices();
620     if (slices->top()->isPercentage())
621         box.top() = Length(slices->top()->getDoubleValue(), Percent);
622     else
623         box.top() = Length(slices->top()->getIntValue(CSSPrimitiveValue::CSS_NUMBER), Fixed);
624     if (slices->bottom()->isPercentage())
625         box.bottom() = Length(slices->bottom()->getDoubleValue(), Percent);
626     else
627         box.bottom() = Length((int)slices->bottom()->getFloatValue(CSSPrimitiveValue::CSS_NUMBER), Fixed);
628     if (slices->left()->isPercentage())
629         box.left() = Length(slices->left()->getDoubleValue(), Percent);
630     else
631         box.left() = Length(slices->left()->getIntValue(CSSPrimitiveValue::CSS_NUMBER), Fixed);
632     if (slices->right()->isPercentage())
633         box.right() = Length(slices->right()->getDoubleValue(), Percent);
634     else
635         box.right() = Length(slices->right()->getIntValue(CSSPrimitiveValue::CSS_NUMBER), Fixed);
636     image.setImageSlices(box);
637
638     // Set our fill mode.
639     image.setFill(borderImageSlice.m_fill);
640 }
641
642 LengthBox CSSToStyleMap::mapNinePieceImageQuad(CSSValue& value)
643 {
644     if (!is<CSSPrimitiveValue>(value))
645         return LengthBox();
646
647     // Get our zoom value.
648     CSSToLengthConversionData conversionData = useSVGZoomRules() ? m_resolver->state().cssToLengthConversionData().copyWithAdjustedZoom(1.0f) : m_resolver->state().cssToLengthConversionData();
649
650     // Retrieve the primitive value.
651     auto& borderWidths = downcast<CSSPrimitiveValue>(value);
652
653     // Set up a length box to represent our image slices.
654     LengthBox box; // Defaults to 'auto' so we don't have to handle that explicitly below.
655     Quad* slices = borderWidths.getQuadValue();
656     if (slices->top()->isNumber())
657         box.top() = Length(slices->top()->getIntValue(), Relative);
658     else if (slices->top()->isPercentage())
659         box.top() = Length(slices->top()->getDoubleValue(CSSPrimitiveValue::CSS_PERCENTAGE), Percent);
660     else if (slices->top()->getValueID() != CSSValueAuto)
661         box.top() = slices->top()->computeLength<Length>(conversionData);
662
663     if (slices->right()->isNumber())
664         box.right() = Length(slices->right()->getIntValue(), Relative);
665     else if (slices->right()->isPercentage())
666         box.right() = Length(slices->right()->getDoubleValue(CSSPrimitiveValue::CSS_PERCENTAGE), Percent);
667     else if (slices->right()->getValueID() != CSSValueAuto)
668         box.right() = slices->right()->computeLength<Length>(conversionData);
669
670     if (slices->bottom()->isNumber())
671         box.bottom() = Length(slices->bottom()->getIntValue(), Relative);
672     else if (slices->bottom()->isPercentage())
673         box.bottom() = Length(slices->bottom()->getDoubleValue(CSSPrimitiveValue::CSS_PERCENTAGE), Percent);
674     else if (slices->bottom()->getValueID() != CSSValueAuto)
675         box.bottom() = slices->bottom()->computeLength<Length>(conversionData);
676
677     if (slices->left()->isNumber())
678         box.left() = Length(slices->left()->getIntValue(), Relative);
679     else if (slices->left()->isPercentage())
680         box.left() = Length(slices->left()->getDoubleValue(CSSPrimitiveValue::CSS_PERCENTAGE), Percent);
681     else if (slices->left()->getValueID() != CSSValueAuto)
682         box.left() = slices->left()->computeLength<Length>(conversionData);
683
684     return box;
685 }
686
687 void CSSToStyleMap::mapNinePieceImageRepeat(CSSValue& value, NinePieceImage& image)
688 {
689     if (!is<CSSPrimitiveValue>(value))
690         return;
691
692     CSSPrimitiveValue& primitiveValue = downcast<CSSPrimitiveValue>(value);
693     Pair* pair = primitiveValue.getPairValue();
694     if (!pair || !pair->first() || !pair->second())
695         return;
696
697     CSSValueID firstIdentifier = pair->first()->getValueID();
698     CSSValueID secondIdentifier = pair->second()->getValueID();
699
700     ENinePieceImageRule horizontalRule;
701     switch (firstIdentifier) {
702     case CSSValueStretch:
703         horizontalRule = StretchImageRule;
704         break;
705     case CSSValueRound:
706         horizontalRule = RoundImageRule;
707         break;
708     case CSSValueSpace:
709         horizontalRule = SpaceImageRule;
710         break;
711     default: // CSSValueRepeat
712         horizontalRule = RepeatImageRule;
713         break;
714     }
715     image.setHorizontalRule(horizontalRule);
716
717     ENinePieceImageRule verticalRule;
718     switch (secondIdentifier) {
719     case CSSValueStretch:
720         verticalRule = StretchImageRule;
721         break;
722     case CSSValueRound:
723         verticalRule = RoundImageRule;
724         break;
725     case CSSValueSpace:
726         verticalRule = SpaceImageRule;
727         break;
728     default: // CSSValueRepeat
729         verticalRule = RepeatImageRule;
730         break;
731     }
732     image.setVerticalRule(verticalRule);
733 }
734
735 };