Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / rendering / style / RenderStyle.cpp
1 /*
2  * Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
3  * Copyright (C) 2004-2015 Apple Inc. All rights reserved.
4  * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22
23 #include "config.h"
24 #include "RenderStyle.h"
25
26 #include "ContentData.h"
27 #include "CSSCustomPropertyValue.h"
28 #include "CSSPropertyNames.h"
29 #include "CSSVariableDependentValue.h"
30 #include "CursorList.h"
31 #include "FloatRoundedRect.h"
32 #include "FontCascade.h"
33 #include "FontSelector.h"
34 #include "InlineTextBoxStyle.h"
35 #include "Pagination.h"
36 #include "QuotesData.h"
37 #include "RenderObject.h"
38 #include "ScaleTransformOperation.h"
39 #include "ShadowData.h"
40 #include "StyleImage.h"
41 #include "StyleInheritedData.h"
42 #include "StyleResolver.h"
43 #include "StyleScrollSnapPoints.h"
44 #include "StyleSelfAlignmentData.h"
45 #include "WillChangeData.h"
46 #include <wtf/MathExtras.h>
47 #include <wtf/PointerComparison.h>
48 #include <wtf/StdLibExtras.h>
49 #include <algorithm>
50
51 #if ENABLE(IOS_TEXT_AUTOSIZING)
52 #include <wtf/text/StringHash.h>
53 #endif
54
55 #if ENABLE(TEXT_AUTOSIZING)
56 #include "TextAutosizer.h"
57 #endif
58
59 #if ENABLE(TOUCH_EVENTS)
60 #include "RenderTheme.h"
61 #endif
62
63 namespace WebCore {
64
65 struct SameSizeAsBorderValue {
66     float m_width;
67     RGBA32 m_color;
68     int m_restBits;
69 };
70
71 COMPILE_ASSERT(sizeof(BorderValue) == sizeof(SameSizeAsBorderValue), BorderValue_should_not_grow);
72
73 struct SameSizeAsRenderStyle : public RefCounted<SameSizeAsRenderStyle> {
74     void* dataRefs[7];
75     void* ownPtrs[1];
76     void* dataRefSvgStyle;
77     struct InheritedFlags {
78         unsigned m_bitfields[2];
79     } inherited_flags;
80
81     struct NonInheritedFlags {
82         uint64_t m_flags;
83     } noninherited_flags;
84 };
85
86 COMPILE_ASSERT(sizeof(RenderStyle) == sizeof(SameSizeAsRenderStyle), RenderStyle_should_stay_small);
87
88 inline RenderStyle& defaultStyle()
89 {
90     static RenderStyle& style = RenderStyle::createDefaultStyle().leakRef();
91     return style;
92 }
93
94 Ref<RenderStyle> RenderStyle::create()
95 {
96     return adoptRef(*new RenderStyle(defaultStyle()));
97 }
98
99 Ref<RenderStyle> RenderStyle::createDefaultStyle()
100 {
101     return adoptRef(*new RenderStyle(true));
102 }
103
104 Ref<RenderStyle> RenderStyle::createAnonymousStyleWithDisplay(const RenderStyle* parentStyle, EDisplay display)
105 {
106     auto newStyle = RenderStyle::create();
107     newStyle.get().inheritFrom(parentStyle);
108     newStyle.get().inheritUnicodeBidiFrom(parentStyle);
109     newStyle.get().setDisplay(display);
110     return newStyle;
111 }
112
113 Ref<RenderStyle> RenderStyle::clone(const RenderStyle* other)
114 {
115     return adoptRef(*new RenderStyle(*other));
116 }
117
118 Ref<RenderStyle> RenderStyle::createStyleInheritingFromPseudoStyle(const RenderStyle& pseudoStyle)
119 {
120     ASSERT(pseudoStyle.styleType() == BEFORE || pseudoStyle.styleType() == AFTER);
121
122     auto style = RenderStyle::create();
123     style.get().inheritFrom(&pseudoStyle);
124     return style;
125 }
126
127 ALWAYS_INLINE RenderStyle::RenderStyle(bool)
128     : m_box(StyleBoxData::create())
129     , visual(StyleVisualData::create())
130     , m_background(StyleBackgroundData::create())
131     , surround(StyleSurroundData::create())
132     , rareNonInheritedData(StyleRareNonInheritedData::create())
133     , rareInheritedData(StyleRareInheritedData::create())
134     , inherited(StyleInheritedData::create())
135     , m_svgStyle(SVGRenderStyle::create())
136 {
137     inherited_flags._empty_cells = initialEmptyCells();
138     inherited_flags._caption_side = initialCaptionSide();
139     inherited_flags._list_style_type = initialListStyleType();
140     inherited_flags._list_style_position = initialListStylePosition();
141     inherited_flags._visibility = initialVisibility();
142     inherited_flags._text_align = initialTextAlign();
143     inherited_flags._text_transform = initialTextTransform();
144     inherited_flags._text_decorations = initialTextDecoration();
145     inherited_flags._cursor_style = initialCursor();
146 #if ENABLE(CURSOR_VISIBILITY)
147     inherited_flags.m_cursorVisibility = initialCursorVisibility();
148 #endif
149     inherited_flags._direction = initialDirection();
150     inherited_flags._white_space = initialWhiteSpace();
151     inherited_flags._border_collapse = initialBorderCollapse();
152     inherited_flags.m_rtlOrdering = initialRTLOrdering();
153     inherited_flags._box_direction = initialBoxDirection();
154     inherited_flags.m_printColorAdjust = initialPrintColorAdjust();
155     inherited_flags._pointerEvents = initialPointerEvents();
156     inherited_flags._insideLink = NotInsideLink;
157     inherited_flags._insideDefaultButton = false;
158     inherited_flags.m_writingMode = initialWritingMode();
159
160     static_assert((sizeof(InheritedFlags) <= 8), "InheritedFlags does not grow");
161     static_assert((sizeof(NonInheritedFlags) <= 8), "NonInheritedFlags does not grow");
162 }
163
164 ALWAYS_INLINE RenderStyle::RenderStyle(const RenderStyle& o)
165     : RefCounted<RenderStyle>()
166     , m_box(o.m_box)
167     , visual(o.visual)
168     , m_background(o.m_background)
169     , surround(o.surround)
170     , rareNonInheritedData(o.rareNonInheritedData)
171     , rareInheritedData(o.rareInheritedData)
172     , inherited(o.inherited)
173     , m_svgStyle(o.m_svgStyle)
174     , inherited_flags(o.inherited_flags)
175     , noninherited_flags(o.noninherited_flags)
176 {
177 }
178
179 static inline StyleSelfAlignmentData resolveAlignmentData(const RenderStyle& parentStyle, const RenderStyle& childStyle, ItemPosition resolvedAutoPositionForRenderer)
180 {
181     // The auto keyword computes to the parent's align-items computed value, or to "stretch", if not set or "auto".
182     if (childStyle.alignSelfPosition() == ItemPositionAuto)
183         return (parentStyle.alignItemsPosition() == ItemPositionAuto) ? StyleSelfAlignmentData(resolvedAutoPositionForRenderer, OverflowAlignmentDefault) : parentStyle.alignItems();
184     return childStyle.alignSelf();
185 }
186
187 static inline StyleSelfAlignmentData resolveJustificationData(const RenderStyle& parentStyle, const RenderStyle& childStyle, ItemPosition resolvedAutoPositionForRenderer)
188 {
189     // The auto keyword computes to the parent's justify-items computed value, or to "stretch", if not set or "auto".
190     if (childStyle.justifySelfPosition() == ItemPositionAuto)
191         return (parentStyle.justifyItemsPosition() == ItemPositionAuto) ? StyleSelfAlignmentData(resolvedAutoPositionForRenderer, OverflowAlignmentDefault) : parentStyle.justifyItems();
192     return childStyle.justifySelf();
193 }
194
195 ItemPosition RenderStyle::resolveAlignment(const RenderStyle& parentStyle, const RenderStyle& childStyle, ItemPosition resolvedAutoPositionForRenderer)
196 {
197     return resolveAlignmentData(parentStyle, childStyle, resolvedAutoPositionForRenderer).position();
198 }
199
200 OverflowAlignment RenderStyle::resolveAlignmentOverflow(const RenderStyle& parentStyle, const RenderStyle& childStyle)
201 {
202     return resolveAlignmentData(parentStyle, childStyle, ItemPositionStretch).overflow();
203 }
204
205 ItemPosition RenderStyle::resolveJustification(const RenderStyle& parentStyle, const RenderStyle& childStyle, ItemPosition resolvedAutoPositionForRenderer)
206 {
207     return resolveJustificationData(parentStyle, childStyle, resolvedAutoPositionForRenderer).position();
208 }
209
210 OverflowAlignment RenderStyle::resolveJustificationOverflow(const RenderStyle& parentStyle, const RenderStyle& childStyle)
211 {
212     return resolveJustificationData(parentStyle, childStyle, ItemPositionStretch).overflow();
213 }
214
215 ContentPosition RenderStyle::resolvedAlignContentPosition() const
216 {
217     const StyleContentAlignmentData& align = alignContent();
218     if (align.position() != ContentPositionAuto || align.distribution() != ContentDistributionDefault)
219         return align.position();
220     // 'auto' computes to 'stretch' for flexbox, hence it's managed by distribution().
221     return isDisplayFlexibleBox() ? ContentPositionAuto : ContentPositionStart;
222 }
223
224 ContentDistributionType RenderStyle::resolvedAlignContentDistribution() const
225 {
226     const StyleContentAlignmentData& align = alignContent();
227     if (align.position() != ContentPositionAuto || align.distribution() != ContentDistributionDefault)
228         return align.distribution();
229     return isDisplayFlexibleBox() ? ContentDistributionStretch : ContentDistributionDefault;
230 }
231
232 ContentPosition RenderStyle::resolvedJustifyContentPosition() const
233 {
234     const StyleContentAlignmentData& justify = justifyContent();
235     if (justify.position() != ContentPositionAuto || justify.distribution() != ContentDistributionDefault)
236         return justify.position();
237     // 'auto' computes to 'stretch' for flexbox, but since flexing in the main axis is controlled by flex, it behaves as flex-start.
238     return isDisplayFlexibleBox() ? ContentPositionFlexStart : ContentPositionStart;
239 }
240
241 ContentDistributionType RenderStyle::resolvedJustifyContentDistribution() const
242 {
243     // even that 'auto' computes to 'stretch' for flexbox it behaves as flex-start, hence it's managed by position().
244     return justifyContentDistribution();
245 }
246
247 void RenderStyle::inheritFrom(const RenderStyle* inheritParent, IsAtShadowBoundary isAtShadowBoundary)
248 {
249     if (isAtShadowBoundary == AtShadowBoundary) {
250         // Even if surrounding content is user-editable, shadow DOM should act as a single unit, and not necessarily be editable
251         EUserModify currentUserModify = userModify();
252         rareInheritedData = inheritParent->rareInheritedData;
253         setUserModify(currentUserModify);
254     } else
255         rareInheritedData = inheritParent->rareInheritedData;
256     inherited = inheritParent->inherited;
257     inherited_flags = inheritParent->inherited_flags;
258
259     if (m_svgStyle != inheritParent->m_svgStyle)
260         m_svgStyle.access()->inheritFrom(inheritParent->m_svgStyle.get());
261 }
262
263 void RenderStyle::copyNonInheritedFrom(const RenderStyle* other)
264 {
265     m_box = other->m_box;
266     visual = other->visual;
267     m_background = other->m_background;
268     surround = other->surround;
269     rareNonInheritedData = other->rareNonInheritedData;
270     noninherited_flags.copyNonInheritedFrom(other->noninherited_flags);
271
272     if (m_svgStyle != other->m_svgStyle)
273         m_svgStyle.access()->copyNonInheritedFrom(other->m_svgStyle.get());
274
275     ASSERT(zoom() == initialZoom());
276 }
277
278 bool RenderStyle::operator==(const RenderStyle& o) const
279 {
280     // compare everything except the pseudoStyle pointer
281     return inherited_flags == o.inherited_flags
282         && noninherited_flags == o.noninherited_flags
283         && m_box == o.m_box
284         && visual == o.visual
285         && m_background == o.m_background
286         && surround == o.surround
287         && rareNonInheritedData == o.rareNonInheritedData
288         && rareInheritedData == o.rareInheritedData
289         && inherited == o.inherited
290         && m_svgStyle == o.m_svgStyle
291             ;
292 }
293
294 bool RenderStyle::isStyleAvailable() const
295 {
296     return this != StyleResolver::styleNotYetAvailable();
297 }
298
299 bool RenderStyle::hasUniquePseudoStyle() const
300 {
301     if (!m_cachedPseudoStyles || styleType() != NOPSEUDO)
302         return false;
303
304     for (size_t i = 0; i < m_cachedPseudoStyles->size(); ++i) {
305         RenderStyle* pseudoStyle = m_cachedPseudoStyles->at(i).get();
306         if (pseudoStyle->unique())
307             return true;
308     }
309
310     return false;
311 }
312
313 RenderStyle* RenderStyle::getCachedPseudoStyle(PseudoId pid) const
314 {
315     if (!m_cachedPseudoStyles || !m_cachedPseudoStyles->size())
316         return nullptr;
317
318     if (styleType() != NOPSEUDO) 
319         return nullptr;
320
321     for (size_t i = 0; i < m_cachedPseudoStyles->size(); ++i) {
322         RenderStyle* pseudoStyle = m_cachedPseudoStyles->at(i).get();
323         if (pseudoStyle->styleType() == pid)
324             return pseudoStyle;
325     }
326
327     return nullptr;
328 }
329
330 RenderStyle* RenderStyle::addCachedPseudoStyle(PassRefPtr<RenderStyle> pseudo)
331 {
332     if (!pseudo)
333         return nullptr;
334
335     ASSERT(pseudo->styleType() > NOPSEUDO);
336
337     RenderStyle* result = pseudo.get();
338
339     if (!m_cachedPseudoStyles)
340         m_cachedPseudoStyles = std::make_unique<PseudoStyleCache>();
341
342     m_cachedPseudoStyles->append(pseudo);
343
344     return result;
345 }
346
347 void RenderStyle::removeCachedPseudoStyle(PseudoId pid)
348 {
349     if (!m_cachedPseudoStyles)
350         return;
351     for (size_t i = 0; i < m_cachedPseudoStyles->size(); ++i) {
352         RenderStyle* pseudoStyle = m_cachedPseudoStyles->at(i).get();
353         if (pseudoStyle->styleType() == pid) {
354             m_cachedPseudoStyles->remove(i);
355             return;
356         }
357     }
358 }
359
360 bool RenderStyle::inheritedNotEqual(const RenderStyle* other) const
361 {
362     return inherited_flags != other->inherited_flags
363            || inherited != other->inherited
364            || m_svgStyle->inheritedNotEqual(other->m_svgStyle.get())
365            || rareInheritedData != other->rareInheritedData;
366 }
367
368 #if ENABLE(IOS_TEXT_AUTOSIZING)
369 inline unsigned computeFontHash(const FontCascade& font)
370 {
371     unsigned hashCodes[2] = {
372         CaseFoldingHash::hash(font.fontDescription().firstFamily().impl()),
373         static_cast<unsigned>(font.fontDescription().specifiedSize())
374     };
375     return StringHasher::computeHash(reinterpret_cast<UChar*>(hashCodes), 2 * sizeof(unsigned) / sizeof(UChar));
376 }
377
378 uint32_t RenderStyle::hashForTextAutosizing() const
379 {
380     // FIXME: Not a very smart hash. Could be improved upon. See <https://bugs.webkit.org/show_bug.cgi?id=121131>.
381     uint32_t hash = 0;
382     
383     hash ^= rareNonInheritedData->m_appearance;
384     hash ^= rareNonInheritedData->marginBeforeCollapse;
385     hash ^= rareNonInheritedData->marginAfterCollapse;
386     hash ^= rareNonInheritedData->lineClamp.value();
387     hash ^= rareInheritedData->overflowWrap;
388     hash ^= rareInheritedData->nbspMode;
389     hash ^= rareInheritedData->lineBreak;
390     hash ^= WTF::FloatHash<float>::hash(inherited->specifiedLineHeight.value());
391     hash ^= computeFontHash(inherited->fontCascade);
392     hash ^= inherited->horizontal_border_spacing;
393     hash ^= inherited->vertical_border_spacing;
394     hash ^= inherited_flags._box_direction;
395     hash ^= inherited_flags.m_rtlOrdering;
396     hash ^= noninherited_flags.position();
397     hash ^= noninherited_flags.floating();
398     hash ^= rareNonInheritedData->textOverflow;
399     hash ^= rareInheritedData->textSecurity;
400     return hash;
401 }
402
403 bool RenderStyle::equalForTextAutosizing(const RenderStyle* other) const
404 {
405     return rareNonInheritedData->m_appearance == other->rareNonInheritedData->m_appearance
406         && rareNonInheritedData->marginBeforeCollapse == other->rareNonInheritedData->marginBeforeCollapse
407         && rareNonInheritedData->marginAfterCollapse == other->rareNonInheritedData->marginAfterCollapse
408         && rareNonInheritedData->lineClamp == other->rareNonInheritedData->lineClamp
409         && rareInheritedData->textSizeAdjust == other->rareInheritedData->textSizeAdjust
410         && rareInheritedData->overflowWrap == other->rareInheritedData->overflowWrap
411         && rareInheritedData->nbspMode == other->rareInheritedData->nbspMode
412         && rareInheritedData->lineBreak == other->rareInheritedData->lineBreak
413         && rareInheritedData->textSecurity == other->rareInheritedData->textSecurity
414         && inherited->specifiedLineHeight == other->inherited->specifiedLineHeight
415         && inherited->fontCascade.equalForTextAutoSizing(other->inherited->fontCascade)
416         && inherited->horizontal_border_spacing == other->inherited->horizontal_border_spacing
417         && inherited->vertical_border_spacing == other->inherited->vertical_border_spacing
418         && inherited_flags._box_direction == other->inherited_flags._box_direction
419         && inherited_flags.m_rtlOrdering == other->inherited_flags.m_rtlOrdering
420         && noninherited_flags.position() == other->noninherited_flags.position()
421         && noninherited_flags.floating() == other->noninherited_flags.floating()
422         && rareNonInheritedData->textOverflow == other->rareNonInheritedData->textOverflow;
423 }
424 #endif // ENABLE(IOS_TEXT_AUTOSIZING)
425
426 bool RenderStyle::inheritedDataShared(const RenderStyle* other) const
427 {
428     // This is a fast check that only looks if the data structures are shared.
429     return inherited_flags == other->inherited_flags
430         && inherited.get() == other->inherited.get()
431         && m_svgStyle.get() == other->m_svgStyle.get()
432         && rareInheritedData.get() == other->rareInheritedData.get();
433 }
434
435 static bool positionChangeIsMovementOnly(const LengthBox& a, const LengthBox& b, const Length& width)
436 {
437     // If any unit types are different, then we can't guarantee
438     // that this was just a movement.
439     if (a.left().type() != b.left().type()
440         || a.right().type() != b.right().type()
441         || a.top().type() != b.top().type()
442         || a.bottom().type() != b.bottom().type())
443         return false;
444
445     // Only one unit can be non-auto in the horizontal direction and
446     // in the vertical direction.  Otherwise the adjustment of values
447     // is changing the size of the box.
448     if (!a.left().isIntrinsicOrAuto() && !a.right().isIntrinsicOrAuto())
449         return false;
450     if (!a.top().isIntrinsicOrAuto() && !a.bottom().isIntrinsicOrAuto())
451         return false;
452     // If our width is auto and left or right is specified then this 
453     // is not just a movement - we need to resize to our container.
454     if ((!a.left().isIntrinsicOrAuto() || !a.right().isIntrinsicOrAuto()) && width.isIntrinsicOrAuto())
455         return false;
456
457     // One of the units is fixed or percent in both directions and stayed
458     // that way in the new style.  Therefore all we are doing is moving.
459     return true;
460 }
461
462 inline bool RenderStyle::changeAffectsVisualOverflow(const RenderStyle& other) const
463 {
464     if (rareNonInheritedData.get() != other.rareNonInheritedData.get()
465         && !arePointingToEqualData(rareNonInheritedData->m_boxShadow, other.rareNonInheritedData->m_boxShadow))
466         return true;
467
468     if (rareInheritedData.get() != other.rareInheritedData.get()
469         && !arePointingToEqualData(rareInheritedData->textShadow, other.rareInheritedData->textShadow))
470         return true;
471
472     if (inherited_flags._text_decorations != other.inherited_flags._text_decorations
473         || visual->textDecoration != other.visual->textDecoration
474         || rareNonInheritedData->m_textDecorationStyle != other.rareNonInheritedData->m_textDecorationStyle) {
475         // Underlines are always drawn outside of their textbox bounds when text-underline-position: under;
476         // is specified. We can take an early out here.
477         if (textUnderlinePosition() == TextUnderlinePositionUnder
478             || other.textUnderlinePosition() == TextUnderlinePositionUnder)
479             return true;
480         return visualOverflowForDecorations(*this, nullptr) != visualOverflowForDecorations(other, nullptr);
481     }
482
483     return false;
484 }
485
486 bool RenderStyle::changeRequiresLayout(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
487 {
488     if (m_box->width() != other.m_box->width()
489         || m_box->minWidth() != other.m_box->minWidth()
490         || m_box->maxWidth() != other.m_box->maxWidth()
491         || m_box->height() != other.m_box->height()
492         || m_box->minHeight() != other.m_box->minHeight()
493         || m_box->maxHeight() != other.m_box->maxHeight())
494         return true;
495
496     if (m_box->verticalAlign() != other.m_box->verticalAlign() || noninherited_flags.verticalAlign() != other.noninherited_flags.verticalAlign())
497         return true;
498
499     if (m_box->boxSizing() != other.m_box->boxSizing())
500         return true;
501
502     if (surround->margin != other.surround->margin)
503         return true;
504
505     if (surround->padding != other.surround->padding)
506         return true;
507
508     // FIXME: We should add an optimized form of layout that just recomputes visual overflow.
509     if (changeAffectsVisualOverflow(other))
510         return true;
511
512     if (rareNonInheritedData.get() != other.rareNonInheritedData.get()) {
513         if (rareNonInheritedData->m_appearance != other.rareNonInheritedData->m_appearance
514             || rareNonInheritedData->marginBeforeCollapse != other.rareNonInheritedData->marginBeforeCollapse
515             || rareNonInheritedData->marginAfterCollapse != other.rareNonInheritedData->marginAfterCollapse
516             || rareNonInheritedData->lineClamp != other.rareNonInheritedData->lineClamp
517             || rareNonInheritedData->m_initialLetter != other.rareNonInheritedData->m_initialLetter
518             || rareNonInheritedData->textOverflow != other.rareNonInheritedData->textOverflow)
519             return true;
520
521         if (rareNonInheritedData->m_regionFragment != other.rareNonInheritedData->m_regionFragment)
522             return true;
523
524 #if ENABLE(CSS_SHAPES)
525         if (rareNonInheritedData->m_shapeMargin != other.rareNonInheritedData->m_shapeMargin)
526             return true;
527 #endif
528
529         if (rareNonInheritedData->m_deprecatedFlexibleBox != other.rareNonInheritedData->m_deprecatedFlexibleBox)
530             return true;
531
532         if (rareNonInheritedData->m_flexibleBox != other.rareNonInheritedData->m_flexibleBox)
533             return true;
534
535         if (rareNonInheritedData->m_order != other.rareNonInheritedData->m_order
536             || rareNonInheritedData->m_alignContent != other.rareNonInheritedData->m_alignContent
537             || rareNonInheritedData->m_alignItems != other.rareNonInheritedData->m_alignItems
538             || rareNonInheritedData->m_alignSelf != other.rareNonInheritedData->m_alignSelf
539             || rareNonInheritedData->m_justifyContent != other.rareNonInheritedData->m_justifyContent
540             || rareNonInheritedData->m_justifyItems != other.rareNonInheritedData->m_justifyItems
541             || rareNonInheritedData->m_justifySelf != other.rareNonInheritedData->m_justifySelf)
542             return true;
543
544         if (!arePointingToEqualData(rareNonInheritedData->m_boxReflect, other.rareNonInheritedData->m_boxReflect))
545             return true;
546
547         if (rareNonInheritedData->m_multiCol != other.rareNonInheritedData->m_multiCol)
548             return true;
549
550         if (rareNonInheritedData->m_transform != other.rareNonInheritedData->m_transform) {
551             if (rareNonInheritedData->m_transform->hasTransform() != other.rareNonInheritedData->m_transform->hasTransform())
552                 return true;
553             if (*rareNonInheritedData->m_transform != *other.rareNonInheritedData->m_transform) {
554                 changedContextSensitiveProperties |= ContextSensitivePropertyTransform;
555                 // Don't return; keep looking for another change
556             }
557         }
558
559 #if ENABLE(CSS_GRID_LAYOUT)
560         if (rareNonInheritedData->m_grid != other.rareNonInheritedData->m_grid
561             || rareNonInheritedData->m_gridItem != other.rareNonInheritedData->m_gridItem)
562             return true;
563 #endif
564
565 #if ENABLE(DASHBOARD_SUPPORT)
566         // If regions change, trigger a relayout to re-calc regions.
567         if (rareNonInheritedData->m_dashboardRegions != other.rareNonInheritedData->m_dashboardRegions)
568             return true;
569 #endif
570
571         if (!arePointingToEqualData(rareNonInheritedData->m_willChange, other.rareNonInheritedData->m_willChange)) {
572             changedContextSensitiveProperties |= ContextSensitivePropertyWillChange;
573             // Don't return; keep looking for another change
574         }
575     }
576
577     if (rareInheritedData.get() != other.rareInheritedData.get()) {
578         if (rareInheritedData->indent != other.rareInheritedData->indent
579 #if ENABLE(CSS3_TEXT)
580             || rareInheritedData->m_textAlignLast != other.rareInheritedData->m_textAlignLast
581             || rareInheritedData->m_textJustify != other.rareInheritedData->m_textJustify
582             || rareInheritedData->m_textIndentLine != other.rareInheritedData->m_textIndentLine
583 #endif
584             || rareInheritedData->m_effectiveZoom != other.rareInheritedData->m_effectiveZoom
585             || rareInheritedData->m_textZoom != other.rareInheritedData->m_textZoom
586 #if ENABLE(IOS_TEXT_AUTOSIZING)
587             || rareInheritedData->textSizeAdjust != other.rareInheritedData->textSizeAdjust
588 #endif
589             || rareInheritedData->wordBreak != other.rareInheritedData->wordBreak
590             || rareInheritedData->overflowWrap != other.rareInheritedData->overflowWrap
591             || rareInheritedData->nbspMode != other.rareInheritedData->nbspMode
592             || rareInheritedData->lineBreak != other.rareInheritedData->lineBreak
593             || rareInheritedData->textSecurity != other.rareInheritedData->textSecurity
594             || rareInheritedData->hyphens != other.rareInheritedData->hyphens
595             || rareInheritedData->hyphenationLimitBefore != other.rareInheritedData->hyphenationLimitBefore
596             || rareInheritedData->hyphenationLimitAfter != other.rareInheritedData->hyphenationLimitAfter
597             || rareInheritedData->hyphenationString != other.rareInheritedData->hyphenationString
598             || rareInheritedData->m_rubyPosition != other.rareInheritedData->m_rubyPosition
599             || rareInheritedData->textEmphasisMark != other.rareInheritedData->textEmphasisMark
600             || rareInheritedData->textEmphasisPosition != other.rareInheritedData->textEmphasisPosition
601             || rareInheritedData->textEmphasisCustomMark != other.rareInheritedData->textEmphasisCustomMark
602             || rareInheritedData->m_textOrientation != other.rareInheritedData->m_textOrientation
603             || rareInheritedData->m_tabSize != other.rareInheritedData->m_tabSize
604             || rareInheritedData->m_lineBoxContain != other.rareInheritedData->m_lineBoxContain
605             || rareInheritedData->m_lineGrid != other.rareInheritedData->m_lineGrid
606 #if ENABLE(CSS_IMAGE_ORIENTATION)
607             || rareInheritedData->m_imageOrientation != other.rareInheritedData->m_imageOrientation
608 #endif
609 #if ENABLE(CSS_IMAGE_RESOLUTION)
610             || rareInheritedData->m_imageResolutionSource != other.rareInheritedData->m_imageResolutionSource
611             || rareInheritedData->m_imageResolutionSnap != other.rareInheritedData->m_imageResolutionSnap
612             || rareInheritedData->m_imageResolution != other.rareInheritedData->m_imageResolution
613 #endif
614             || rareInheritedData->m_lineSnap != other.rareInheritedData->m_lineSnap
615             || rareInheritedData->m_lineAlign != other.rareInheritedData->m_lineAlign
616 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
617             || rareInheritedData->useTouchOverflowScrolling != other.rareInheritedData->useTouchOverflowScrolling
618 #endif
619             || rareInheritedData->listStyleImage != other.rareInheritedData->listStyleImage) // FIXME: needs arePointingToEqualData()?
620             return true;
621
622         if (textStrokeWidth() != other.textStrokeWidth())
623             return true;
624     }
625
626 #if ENABLE(TEXT_AUTOSIZING)
627     if (visual->m_textAutosizingMultiplier != other.visual->m_textAutosizingMultiplier)
628         return true;
629 #endif
630
631     if (inherited->line_height != other.inherited->line_height
632 #if ENABLE(IOS_TEXT_AUTOSIZING)
633         || inherited->specifiedLineHeight != other.inherited->specifiedLineHeight
634 #endif
635         || inherited->fontCascade != other.inherited->fontCascade
636         || inherited->horizontal_border_spacing != other.inherited->horizontal_border_spacing
637         || inherited->vertical_border_spacing != other.inherited->vertical_border_spacing
638         || inherited_flags._box_direction != other.inherited_flags._box_direction
639         || inherited_flags.m_rtlOrdering != other.inherited_flags.m_rtlOrdering
640         || noninherited_flags.position() != other.noninherited_flags.position()
641         || noninherited_flags.floating() != other.noninherited_flags.floating()
642         || noninherited_flags.originalDisplay() != other.noninherited_flags.originalDisplay())
643         return true;
644
645
646     if ((noninherited_flags.effectiveDisplay()) >= TABLE) {
647         if (inherited_flags._border_collapse != other.inherited_flags._border_collapse
648             || inherited_flags._empty_cells != other.inherited_flags._empty_cells
649             || inherited_flags._caption_side != other.inherited_flags._caption_side
650             || noninherited_flags.tableLayout() != other.noninherited_flags.tableLayout())
651             return true;
652
653         // In the collapsing border model, 'hidden' suppresses other borders, while 'none'
654         // does not, so these style differences can be width differences.
655         if (inherited_flags._border_collapse
656             && ((borderTopStyle() == BHIDDEN && other.borderTopStyle() == BNONE)
657                 || (borderTopStyle() == BNONE && other.borderTopStyle() == BHIDDEN)
658                 || (borderBottomStyle() == BHIDDEN && other.borderBottomStyle() == BNONE)
659                 || (borderBottomStyle() == BNONE && other.borderBottomStyle() == BHIDDEN)
660                 || (borderLeftStyle() == BHIDDEN && other.borderLeftStyle() == BNONE)
661                 || (borderLeftStyle() == BNONE && other.borderLeftStyle() == BHIDDEN)
662                 || (borderRightStyle() == BHIDDEN && other.borderRightStyle() == BNONE)
663                 || (borderRightStyle() == BNONE && other.borderRightStyle() == BHIDDEN)))
664             return true;
665     }
666
667     if (noninherited_flags.effectiveDisplay() == LIST_ITEM) {
668         if (inherited_flags._list_style_type != other.inherited_flags._list_style_type
669             || inherited_flags._list_style_position != other.inherited_flags._list_style_position)
670             return true;
671     }
672
673     if (inherited_flags._text_align != other.inherited_flags._text_align
674         || inherited_flags._text_transform != other.inherited_flags._text_transform
675         || inherited_flags._direction != other.inherited_flags._direction
676         || inherited_flags._white_space != other.inherited_flags._white_space
677         || noninherited_flags.clear() != other.noninherited_flags.clear()
678         || noninherited_flags.unicodeBidi() != other.noninherited_flags.unicodeBidi())
679         return true;
680
681     // Check block flow direction.
682     if (inherited_flags.m_writingMode != other.inherited_flags.m_writingMode)
683         return true;
684
685     // Check text combine mode.
686     if (rareNonInheritedData->m_textCombine != other.rareNonInheritedData->m_textCombine)
687         return true;
688
689     // Overflow returns a layout hint.
690     if (noninherited_flags.overflowX() != other.noninherited_flags.overflowX()
691         || noninherited_flags.overflowY() != other.noninherited_flags.overflowY())
692         return true;
693
694     // If our border widths change, then we need to layout.  Other changes to borders
695     // only necessitate a repaint.
696     if (borderLeftWidth() != other.borderLeftWidth()
697         || borderTopWidth() != other.borderTopWidth()
698         || borderBottomWidth() != other.borderBottomWidth()
699         || borderRightWidth() != other.borderRightWidth())
700         return true;
701
702     // If the counter directives change, trigger a relayout to re-calculate counter values and rebuild the counter node tree.
703     if (!arePointingToEqualData(rareNonInheritedData->m_counterDirectives, other.rareNonInheritedData->m_counterDirectives))
704         return true;
705
706     if ((visibility() == COLLAPSE) != (other.visibility() == COLLAPSE))
707         return true;
708
709     if (rareNonInheritedData->hasOpacity() != other.rareNonInheritedData->hasOpacity()) {
710         // FIXME: We would like to use SimplifiedLayout here, but we can't quite do that yet.
711         // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
712         // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
713         // In addition we need to solve the floating object issue when layers come and go. Right now
714         // a full layout is necessary to keep floating object lists sane.
715         return true;
716     }
717
718     if (rareNonInheritedData->hasFilters() != other.rareNonInheritedData->hasFilters())
719         return true;
720
721 #if ENABLE(FILTERS_LEVEL_2)
722     if (rareNonInheritedData->hasBackdropFilters() != other.rareNonInheritedData->hasBackdropFilters())
723         return true;
724 #endif
725
726     if (!arePointingToEqualData(rareInheritedData->quotes, other.rareInheritedData->quotes))
727         return true;
728
729     if (position() != StaticPosition) {
730         if (surround->offset != other.surround->offset) {
731             // FIXME: We would like to use SimplifiedLayout for relative positioning, but we can't quite do that yet.
732             // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
733             // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
734             if (position() != AbsolutePosition)
735                 return true;
736
737             // Optimize for the case where a positioned layer is moving but not changing size.
738             if (!positionChangeIsMovementOnly(surround->offset, other.surround->offset, m_box->width()))
739                 return true;
740         }
741     }
742     
743     return false;
744 }
745
746 bool RenderStyle::changeRequiresPositionedLayoutOnly(const RenderStyle& other, unsigned&) const
747 {
748     if (position() == StaticPosition)
749         return false;
750
751     if (surround->offset != other.surround->offset) {
752         // Optimize for the case where a positioned layer is moving but not changing size.
753         if (position() == AbsolutePosition && positionChangeIsMovementOnly(surround->offset, other.surround->offset, m_box->width()))
754             return true;
755     }
756     
757     return false;
758 }
759
760 bool RenderStyle::changeRequiresLayerRepaint(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
761 {
762     // StyleResolver has ensured that zIndex is non-auto only if it's applicable.
763     if (m_box->zIndex() != other.m_box->zIndex() || m_box->hasAutoZIndex() != other.m_box->hasAutoZIndex())
764         return true;
765
766     if (position() != StaticPosition) {
767         if (visual->clip != other.visual->clip || visual->hasClip != other.visual->hasClip) {
768             changedContextSensitiveProperties |= ContextSensitivePropertyClipRect;
769             return true;
770         }
771     }
772
773 #if ENABLE(CSS_COMPOSITING)
774     if (rareNonInheritedData->m_effectiveBlendMode != other.rareNonInheritedData->m_effectiveBlendMode)
775         return true;
776 #endif
777
778     if (rareNonInheritedData->opacity != other.rareNonInheritedData->opacity) {
779         changedContextSensitiveProperties |= ContextSensitivePropertyOpacity;
780         // Don't return; keep looking for another change.
781     }
782
783     if (rareNonInheritedData->m_filter != other.rareNonInheritedData->m_filter) {
784         changedContextSensitiveProperties |= ContextSensitivePropertyFilter;
785         // Don't return; keep looking for another change.
786     }
787
788     if (rareNonInheritedData->m_mask != other.rareNonInheritedData->m_mask
789         || rareNonInheritedData->m_maskBoxImage != other.rareNonInheritedData->m_maskBoxImage)
790         return true;
791
792     return false;
793 }
794
795 bool RenderStyle::changeRequiresRepaint(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
796 {
797     if (inherited_flags._visibility != other.inherited_flags._visibility
798         || inherited_flags.m_printColorAdjust != other.inherited_flags.m_printColorAdjust
799         || inherited_flags._insideLink != other.inherited_flags._insideLink
800         || inherited_flags._insideDefaultButton != other.inherited_flags._insideDefaultButton
801         || surround->border != other.surround->border
802         || !m_background->isEquivalentForPainting(*other.m_background)
803         || rareInheritedData->userModify != other.rareInheritedData->userModify
804         || rareInheritedData->userSelect != other.rareInheritedData->userSelect
805         || rareNonInheritedData->userDrag != other.rareNonInheritedData->userDrag
806         || rareNonInheritedData->m_borderFit != other.rareNonInheritedData->m_borderFit
807         || rareNonInheritedData->m_objectFit != other.rareNonInheritedData->m_objectFit
808         || rareInheritedData->m_imageRendering != other.rareInheritedData->m_imageRendering)
809         return true;
810
811 #if ENABLE(CSS_SHAPES)
812     if (rareNonInheritedData->m_shapeOutside != other.rareNonInheritedData->m_shapeOutside)
813         return true;
814 #endif
815
816     // FIXME: this should probably be moved to changeRequiresLayerRepaint().
817     if (rareNonInheritedData->m_clipPath != other.rareNonInheritedData->m_clipPath) {
818         changedContextSensitiveProperties |= ContextSensitivePropertyClipPath;
819         // Don't return; keep looking for another change.
820     }
821
822     return false;
823 }
824
825 bool RenderStyle::changeRequiresRepaintIfTextOrBorderOrOutline(const RenderStyle& other, unsigned&) const
826 {
827     if (inherited->color != other.inherited->color
828         || inherited_flags._text_decorations != other.inherited_flags._text_decorations
829         || visual->textDecoration != other.visual->textDecoration
830         || rareNonInheritedData->m_textDecorationStyle != other.rareNonInheritedData->m_textDecorationStyle
831         || rareNonInheritedData->m_textDecorationColor != other.rareNonInheritedData->m_textDecorationColor
832         || rareInheritedData->m_textDecorationSkip != other.rareInheritedData->m_textDecorationSkip
833         || rareInheritedData->textFillColor != other.rareInheritedData->textFillColor
834         || rareInheritedData->textStrokeColor != other.rareInheritedData->textStrokeColor
835         || rareInheritedData->textEmphasisColor != other.rareInheritedData->textEmphasisColor
836         || rareInheritedData->textEmphasisFill != other.rareInheritedData->textEmphasisFill)
837         return true;
838
839     return false;
840 }
841
842 bool RenderStyle::changeRequiresRecompositeLayer(const RenderStyle& other, unsigned&) const
843 {
844     if (rareNonInheritedData.get() != other.rareNonInheritedData.get()) {
845         if (rareNonInheritedData->m_transformStyle3D != other.rareNonInheritedData->m_transformStyle3D
846             || rareNonInheritedData->m_backfaceVisibility != other.rareNonInheritedData->m_backfaceVisibility
847             || rareNonInheritedData->m_perspective != other.rareNonInheritedData->m_perspective
848             || rareNonInheritedData->m_perspectiveOriginX != other.rareNonInheritedData->m_perspectiveOriginX
849             || rareNonInheritedData->m_perspectiveOriginY != other.rareNonInheritedData->m_perspectiveOriginY)
850             return true;
851     }
852
853     return false;
854 }
855
856 StyleDifference RenderStyle::diff(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
857 {
858     changedContextSensitiveProperties = ContextSensitivePropertyNone;
859
860     StyleDifference svgChange = StyleDifferenceEqual;
861     if (m_svgStyle != other.m_svgStyle) {
862         svgChange = m_svgStyle->diff(other.m_svgStyle.get());
863         if (svgChange == StyleDifferenceLayout)
864             return svgChange;
865     }
866
867     if (changeRequiresLayout(other, changedContextSensitiveProperties))
868         return StyleDifferenceLayout;
869
870     // SVGRenderStyle::diff() might have returned StyleDifferenceRepaint, eg. if fill changes.
871     // If eg. the font-size changed at the same time, we're not allowed to return StyleDifferenceRepaint,
872     // but have to return StyleDifferenceLayout, that's why  this if branch comes after all branches
873     // that are relevant for SVG and might return StyleDifferenceLayout.
874     if (svgChange != StyleDifferenceEqual)
875         return svgChange;
876
877     if (changeRequiresPositionedLayoutOnly(other, changedContextSensitiveProperties))
878         return StyleDifferenceLayoutPositionedMovementOnly;
879
880     if (changeRequiresLayerRepaint(other, changedContextSensitiveProperties))
881         return StyleDifferenceRepaintLayer;
882
883     if (changeRequiresRepaint(other, changedContextSensitiveProperties))
884         return StyleDifferenceRepaint;
885
886     if (changeRequiresRecompositeLayer(other, changedContextSensitiveProperties))
887         return StyleDifferenceRecompositeLayer;
888
889     if (changeRequiresRepaintIfTextOrBorderOrOutline(other, changedContextSensitiveProperties))
890         return StyleDifferenceRepaintIfTextOrBorderOrOutline;
891
892     // Cursors are not checked, since they will be set appropriately in response to mouse events,
893     // so they don't need to cause any repaint or layout.
894
895     // Animations don't need to be checked either.  We always set the new style on the RenderObject, so we will get a chance to fire off
896     // the resulting transition properly.
897     return StyleDifferenceEqual;
898 }
899
900 bool RenderStyle::diffRequiresLayerRepaint(const RenderStyle& style, bool isComposited) const
901 {
902     unsigned changedContextSensitiveProperties = 0;
903
904     if (changeRequiresRepaint(style, changedContextSensitiveProperties))
905         return true;
906
907     if (isComposited && changeRequiresLayerRepaint(style, changedContextSensitiveProperties))
908         return changedContextSensitiveProperties & ContextSensitivePropertyClipRect;
909
910     return false;
911 }
912
913 void RenderStyle::setClip(Length top, Length right, Length bottom, Length left)
914 {
915     StyleVisualData* data = visual.access();
916     data->clip.top() = top;
917     data->clip.right() = right;
918     data->clip.bottom() = bottom;
919     data->clip.left() = left;
920 }
921
922 void RenderStyle::addCursor(PassRefPtr<StyleImage> image, const IntPoint& hotSpot)
923 {
924     if (!rareInheritedData.access()->cursorData)
925         rareInheritedData.access()->cursorData = CursorList::create();
926     rareInheritedData.access()->cursorData->append(CursorData(image, hotSpot));
927 }
928
929 void RenderStyle::setCursorList(PassRefPtr<CursorList> other)
930 {
931     rareInheritedData.access()->cursorData = other;
932 }
933
934 void RenderStyle::setQuotes(PassRefPtr<QuotesData> q)
935 {
936     if (rareInheritedData->quotes == q || (rareInheritedData->quotes && q && *rareInheritedData->quotes == *q))
937         return;
938
939     rareInheritedData.access()->quotes = q;
940 }
941
942 void RenderStyle::setWillChange(PassRefPtr<WillChangeData> willChangeData)
943 {
944     if (arePointingToEqualData(rareNonInheritedData->m_willChange.get(), willChangeData.get()))
945         return;
946
947     rareNonInheritedData.access()->m_willChange = WTFMove(willChangeData);
948 }
949
950 void RenderStyle::clearCursorList()
951 {
952     if (rareInheritedData->cursorData)
953         rareInheritedData.access()->cursorData = nullptr;
954 }
955
956 void RenderStyle::clearContent()
957 {
958     if (rareNonInheritedData->m_content)
959         rareNonInheritedData.access()->m_content = nullptr;
960 }
961
962 void RenderStyle::appendContent(std::unique_ptr<ContentData> contentData)
963 {
964     auto& content = rareNonInheritedData.access()->m_content;
965     ContentData* lastContent = content.get();
966     while (lastContent && lastContent->next())
967         lastContent = lastContent->next();
968
969     if (lastContent)
970         lastContent->setNext(WTFMove(contentData));
971     else
972         content = WTFMove(contentData);
973 }
974
975 void RenderStyle::setContent(PassRefPtr<StyleImage> image, bool add)
976 {
977     if (!image)
978         return;
979         
980     if (add) {
981         appendContent(std::make_unique<ImageContentData>(image));
982         return;
983     }
984
985     rareNonInheritedData.access()->m_content = std::make_unique<ImageContentData>(image);
986     if (!rareNonInheritedData.access()->m_altText.isNull())
987         rareNonInheritedData.access()->m_content->setAltText(rareNonInheritedData.access()->m_altText);
988 }
989
990 void RenderStyle::setContent(const String& string, bool add)
991 {
992     auto& content = rareNonInheritedData.access()->m_content;
993     if (add) {
994         ContentData* lastContent = content.get();
995         while (lastContent && lastContent->next())
996             lastContent = lastContent->next();
997
998         if (lastContent) {
999             // We attempt to merge with the last ContentData if possible.
1000             if (is<TextContentData>(*lastContent)) {
1001                 TextContentData& textContent = downcast<TextContentData>(*lastContent);
1002                 textContent.setText(textContent.text() + string);
1003             } else
1004                 lastContent->setNext(std::make_unique<TextContentData>(string));
1005
1006             if (!rareNonInheritedData.access()->m_altText.isNull())
1007                 lastContent->setAltText(rareNonInheritedData.access()->m_altText);
1008             return;
1009         }
1010     }
1011
1012     content = std::make_unique<TextContentData>(string);
1013     if (!rareNonInheritedData.access()->m_altText.isNull())
1014         content->setAltText(rareNonInheritedData.access()->m_altText);
1015 }
1016
1017 void RenderStyle::setContent(std::unique_ptr<CounterContent> counter, bool add)
1018 {
1019     if (!counter)
1020         return;
1021
1022     if (add) {
1023         appendContent(std::make_unique<CounterContentData>(WTFMove(counter)));
1024         return;
1025     }
1026
1027     rareNonInheritedData.access()->m_content = std::make_unique<CounterContentData>(WTFMove(counter));
1028 }
1029
1030 void RenderStyle::setContent(QuoteType quote, bool add)
1031 {
1032     if (add) {
1033         appendContent(std::make_unique<QuoteContentData>(quote));
1034         return;
1035     }
1036
1037     rareNonInheritedData.access()->m_content = std::make_unique<QuoteContentData>(quote);
1038 }
1039
1040 void RenderStyle::setContentAltText(const String& string)
1041 {
1042     rareNonInheritedData.access()->m_altText = string;
1043     
1044     if (rareNonInheritedData.access()->m_content)
1045         rareNonInheritedData.access()->m_content->setAltText(string);
1046 }
1047
1048 const String& RenderStyle::contentAltText() const
1049 {
1050     return rareNonInheritedData->m_altText;
1051 }
1052
1053 // FIXME: use affectedByTransformOrigin().
1054 static inline bool requireTransformOrigin(const Vector<RefPtr<TransformOperation>>& transformOperations, RenderStyle::ApplyTransformOrigin applyOrigin)
1055 {
1056     // transform-origin brackets the transform with translate operations.
1057     // Optimize for the case where the only transform is a translation, since the transform-origin is irrelevant
1058     // in that case.
1059     if (applyOrigin != RenderStyle::IncludeTransformOrigin)
1060         return false;
1061
1062     for (auto& operation : transformOperations) {
1063         TransformOperation::OperationType type = operation->type();
1064         if (type != TransformOperation::TRANSLATE_X
1065             && type != TransformOperation::TRANSLATE_Y
1066             && type != TransformOperation::TRANSLATE 
1067             && type != TransformOperation::TRANSLATE_Z
1068             && type != TransformOperation::TRANSLATE_3D)
1069             return true;
1070     }
1071
1072     return false;
1073 }
1074
1075 void RenderStyle::applyTransform(TransformationMatrix& transform, const FloatRect& boundingBox, ApplyTransformOrigin applyOrigin) const
1076 {
1077     auto& operations = rareNonInheritedData->m_transform->m_operations.operations();
1078     bool applyTransformOrigin = requireTransformOrigin(operations, applyOrigin);
1079
1080     float offsetX = transformOriginX().isPercent() ? boundingBox.x() : 0;
1081     float offsetY = transformOriginY().isPercent() ? boundingBox.y() : 0;
1082
1083     if (applyTransformOrigin) {
1084         transform.translate3d(floatValueForLength(transformOriginX(), boundingBox.width()) + offsetX,
1085                               floatValueForLength(transformOriginY(), boundingBox.height()) + offsetY,
1086                               transformOriginZ());
1087     }
1088
1089     for (auto& operation : operations)
1090         operation->apply(transform, boundingBox.size());
1091
1092     if (applyTransformOrigin) {
1093         transform.translate3d(-floatValueForLength(transformOriginX(), boundingBox.width()) - offsetX,
1094                               -floatValueForLength(transformOriginY(), boundingBox.height()) - offsetY,
1095                               -transformOriginZ());
1096     }
1097 }
1098
1099 void RenderStyle::setPageScaleTransform(float scale)
1100 {
1101     if (scale == 1)
1102         return;
1103     TransformOperations transform;
1104     transform.operations().append(ScaleTransformOperation::create(scale, scale, ScaleTransformOperation::SCALE));
1105     setTransform(transform);
1106     setTransformOriginX(Length(0, Fixed));
1107     setTransformOriginY(Length(0, Fixed));
1108 }
1109
1110 void RenderStyle::setTextShadow(std::unique_ptr<ShadowData> shadowData, bool add)
1111 {
1112     ASSERT(!shadowData || (!shadowData->spread() && shadowData->style() == Normal));
1113
1114     StyleRareInheritedData* rareData = rareInheritedData.access();
1115     if (!add) {
1116         rareData->textShadow = WTFMove(shadowData);
1117         return;
1118     }
1119
1120     shadowData->setNext(WTFMove(rareData->textShadow));
1121     rareData->textShadow = WTFMove(shadowData);
1122 }
1123
1124 void RenderStyle::setBoxShadow(std::unique_ptr<ShadowData> shadowData, bool add)
1125 {
1126     StyleRareNonInheritedData* rareData = rareNonInheritedData.access();
1127     if (!add) {
1128         rareData->m_boxShadow = WTFMove(shadowData);
1129         return;
1130     }
1131
1132     shadowData->setNext(WTFMove(rareData->m_boxShadow));
1133     rareData->m_boxShadow = WTFMove(shadowData);
1134 }
1135
1136 static RoundedRect::Radii calcRadiiFor(const BorderData& border, const LayoutSize& size)
1137 {
1138     return RoundedRect::Radii(
1139         LayoutSize(valueForLength(border.topLeft().width(), size.width()),
1140             valueForLength(border.topLeft().height(), size.height())),
1141         LayoutSize(valueForLength(border.topRight().width(), size.width()),
1142             valueForLength(border.topRight().height(), size.height())),
1143         LayoutSize(valueForLength(border.bottomLeft().width(), size.width()),
1144             valueForLength(border.bottomLeft().height(), size.height())),
1145         LayoutSize(valueForLength(border.bottomRight().width(), size.width()),
1146             valueForLength(border.bottomRight().height(), size.height())));
1147 }
1148
1149 StyleImage* RenderStyle::listStyleImage() const { return rareInheritedData->listStyleImage.get(); }
1150 void RenderStyle::setListStyleImage(PassRefPtr<StyleImage> v)
1151 {
1152     if (rareInheritedData->listStyleImage != v)
1153         rareInheritedData.access()->listStyleImage = v;
1154 }
1155
1156 Color RenderStyle::color() const { return inherited->color; }
1157 Color RenderStyle::visitedLinkColor() const { return inherited->visitedLinkColor; }
1158 void RenderStyle::setColor(const Color& v) { SET_VAR(inherited, color, v); }
1159 void RenderStyle::setVisitedLinkColor(const Color& v) { SET_VAR(inherited, visitedLinkColor, v); }
1160
1161 short RenderStyle::horizontalBorderSpacing() const { return inherited->horizontal_border_spacing; }
1162 short RenderStyle::verticalBorderSpacing() const { return inherited->vertical_border_spacing; }
1163 void RenderStyle::setHorizontalBorderSpacing(short v) { SET_VAR(inherited, horizontal_border_spacing, v); }
1164 void RenderStyle::setVerticalBorderSpacing(short v) { SET_VAR(inherited, vertical_border_spacing, v); }
1165
1166 RoundedRect RenderStyle::getRoundedBorderFor(const LayoutRect& borderRect, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1167 {
1168     RoundedRect roundedRect(borderRect);
1169     if (hasBorderRadius()) {
1170         RoundedRect::Radii radii = calcRadiiFor(surround->border, borderRect.size());
1171         radii.scale(calcBorderRadiiConstraintScaleFor(borderRect, radii));
1172         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
1173     }
1174     return roundedRect;
1175 }
1176
1177 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1178 {
1179     bool horizontal = isHorizontalWritingMode();
1180
1181     LayoutUnit leftWidth = (!horizontal || includeLogicalLeftEdge) ? borderLeftWidth() : 0;
1182     LayoutUnit rightWidth = (!horizontal || includeLogicalRightEdge) ? borderRightWidth() : 0;
1183     LayoutUnit topWidth = (horizontal || includeLogicalLeftEdge) ? borderTopWidth() : 0;
1184     LayoutUnit bottomWidth = (horizontal || includeLogicalRightEdge) ? borderBottomWidth() : 0;
1185
1186     return getRoundedInnerBorderFor(borderRect, topWidth, bottomWidth, leftWidth, rightWidth, includeLogicalLeftEdge, includeLogicalRightEdge);
1187 }
1188
1189 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect, LayoutUnit topWidth, LayoutUnit bottomWidth,
1190     LayoutUnit leftWidth, LayoutUnit rightWidth, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1191 {
1192     LayoutRect innerRect(borderRect.x() + leftWidth, 
1193                borderRect.y() + topWidth, 
1194                borderRect.width() - leftWidth - rightWidth, 
1195                borderRect.height() - topWidth - bottomWidth);
1196
1197     RoundedRect roundedRect(innerRect);
1198
1199     if (hasBorderRadius()) {
1200         RoundedRect::Radii radii = getRoundedBorderFor(borderRect).radii();
1201         radii.shrink(topWidth, bottomWidth, leftWidth, rightWidth);
1202         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
1203     }
1204     return roundedRect;
1205 }
1206
1207 static bool allLayersAreFixed(const FillLayer* layer)
1208 {
1209     bool allFixed = true;
1210     
1211     for (const FillLayer* currLayer = layer; currLayer; currLayer = currLayer->next())
1212         allFixed &= (currLayer->image() && currLayer->attachment() == FixedBackgroundAttachment);
1213
1214     return layer && allFixed;
1215 }
1216
1217 bool RenderStyle::hasEntirelyFixedBackground() const
1218 {
1219     return allLayersAreFixed(backgroundLayers());
1220 }
1221
1222 const CounterDirectiveMap* RenderStyle::counterDirectives() const
1223 {
1224     return rareNonInheritedData->m_counterDirectives.get();
1225 }
1226
1227 CounterDirectiveMap& RenderStyle::accessCounterDirectives()
1228 {
1229     auto& map = rareNonInheritedData.access()->m_counterDirectives;
1230     if (!map)
1231         map = std::make_unique<CounterDirectiveMap>();
1232     return *map;
1233 }
1234
1235 const CounterDirectives RenderStyle::getCounterDirectives(const AtomicString& identifier) const
1236 {
1237     if (const CounterDirectiveMap* directives = counterDirectives())
1238         return directives->get(identifier);
1239     return CounterDirectives();
1240 }
1241
1242 const AtomicString& RenderStyle::hyphenString() const
1243 {
1244     ASSERT(hyphens() != HyphensNone);
1245
1246     const AtomicString& hyphenationString = rareInheritedData.get()->hyphenationString;
1247     if (!hyphenationString.isNull())
1248         return hyphenationString;
1249
1250     // FIXME: This should depend on locale.
1251     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, hyphenMinusString, (&hyphenMinus, 1));
1252     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, hyphenString, (&hyphen, 1));
1253     return fontCascade().primaryFont().glyphForCharacter(hyphen) ? hyphenString : hyphenMinusString;
1254 }
1255
1256 const AtomicString& RenderStyle::textEmphasisMarkString() const
1257 {
1258     switch (textEmphasisMark()) {
1259     case TextEmphasisMarkNone:
1260         return nullAtom;
1261     case TextEmphasisMarkCustom:
1262         return textEmphasisCustomMark();
1263     case TextEmphasisMarkDot: {
1264         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledDotString, (&bullet, 1));
1265         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openDotString, (&whiteBullet, 1));
1266         return textEmphasisFill() == TextEmphasisFillFilled ? filledDotString : openDotString;
1267     }
1268     case TextEmphasisMarkCircle: {
1269         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledCircleString, (&blackCircle, 1));
1270         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openCircleString, (&whiteCircle, 1));
1271         return textEmphasisFill() == TextEmphasisFillFilled ? filledCircleString : openCircleString;
1272     }
1273     case TextEmphasisMarkDoubleCircle: {
1274         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledDoubleCircleString, (&fisheye, 1));
1275         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openDoubleCircleString, (&bullseye, 1));
1276         return textEmphasisFill() == TextEmphasisFillFilled ? filledDoubleCircleString : openDoubleCircleString;
1277     }
1278     case TextEmphasisMarkTriangle: {
1279         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledTriangleString, (&blackUpPointingTriangle, 1));
1280         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openTriangleString, (&whiteUpPointingTriangle, 1));
1281         return textEmphasisFill() == TextEmphasisFillFilled ? filledTriangleString : openTriangleString;
1282     }
1283     case TextEmphasisMarkSesame: {
1284         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledSesameString, (&sesameDot, 1));
1285         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openSesameString, (&whiteSesameDot, 1));
1286         return textEmphasisFill() == TextEmphasisFillFilled ? filledSesameString : openSesameString;
1287     }
1288     case TextEmphasisMarkAuto:
1289         ASSERT_NOT_REACHED();
1290         return nullAtom;
1291     }
1292
1293     ASSERT_NOT_REACHED();
1294     return nullAtom;
1295 }
1296
1297 #if ENABLE(DASHBOARD_SUPPORT)
1298 const Vector<StyleDashboardRegion>& RenderStyle::initialDashboardRegions()
1299 {
1300     DEPRECATED_DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, emptyList, ());
1301     return emptyList;
1302 }
1303
1304 const Vector<StyleDashboardRegion>& RenderStyle::noneDashboardRegions()
1305 {
1306     DEPRECATED_DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, noneList, ());
1307     static bool noneListInitialized = false;
1308
1309     if (!noneListInitialized) {
1310         StyleDashboardRegion region;
1311         region.label = "";
1312         region.offset.top()  = Length();
1313         region.offset.right() = Length();
1314         region.offset.bottom() = Length();
1315         region.offset.left() = Length();
1316         region.type = StyleDashboardRegion::None;
1317         noneList.append(region);
1318         noneListInitialized = true;
1319     }
1320     return noneList;
1321 }
1322 #endif
1323
1324 void RenderStyle::adjustAnimations()
1325 {
1326     AnimationList* animationList = rareNonInheritedData->m_animations.get();
1327     if (!animationList)
1328         return;
1329
1330     // Get rid of empty animations and anything beyond them
1331     for (size_t i = 0; i < animationList->size(); ++i) {
1332         if (animationList->animation(i).isEmpty()) {
1333             animationList->resize(i);
1334             break;
1335         }
1336     }
1337
1338     if (animationList->isEmpty()) {
1339         clearAnimations();
1340         return;
1341     }
1342
1343     // Repeat patterns into layers that don't have some properties set.
1344     animationList->fillUnsetProperties();
1345 }
1346
1347 void RenderStyle::adjustTransitions()
1348 {
1349     AnimationList* transitionList = rareNonInheritedData->m_transitions.get();
1350     if (!transitionList)
1351         return;
1352
1353     // Get rid of empty transitions and anything beyond them
1354     for (size_t i = 0; i < transitionList->size(); ++i) {
1355         if (transitionList->animation(i).isEmpty()) {
1356             transitionList->resize(i);
1357             break;
1358         }
1359     }
1360
1361     if (transitionList->isEmpty()) {
1362         clearTransitions();
1363         return;
1364     }
1365
1366     // Repeat patterns into layers that don't have some properties set.
1367     transitionList->fillUnsetProperties();
1368
1369     // Make sure there are no duplicate properties. This is an O(n^2) algorithm
1370     // but the lists tend to be very short, so it is probably ok
1371     for (size_t i = 0; i < transitionList->size(); ++i) {
1372         for (size_t j = i+1; j < transitionList->size(); ++j) {
1373             if (transitionList->animation(i).property() == transitionList->animation(j).property()) {
1374                 // toss i
1375                 transitionList->remove(i);
1376                 j = i;
1377             }
1378         }
1379     }
1380 }
1381
1382 AnimationList& RenderStyle::ensureAnimations()
1383 {
1384     if (!rareNonInheritedData.access()->m_animations)
1385         rareNonInheritedData.access()->m_animations = std::make_unique<AnimationList>();
1386     return *rareNonInheritedData->m_animations;
1387 }
1388
1389 AnimationList& RenderStyle::ensureTransitions()
1390 {
1391     if (!rareNonInheritedData.access()->m_transitions)
1392         rareNonInheritedData.access()->m_transitions = std::make_unique<AnimationList>();
1393     return *rareNonInheritedData->m_transitions;
1394 }
1395
1396 const Animation* RenderStyle::transitionForProperty(CSSPropertyID property) const
1397 {
1398     if (transitions()) {
1399         for (size_t i = 0; i < transitions()->size(); ++i) {
1400             const Animation& p = transitions()->animation(i);
1401             if (p.animationMode() == Animation::AnimateAll || p.property() == property) {
1402                 return &p;
1403             }
1404         }
1405     }
1406     return 0;
1407 }
1408
1409 const FontCascade& RenderStyle::fontCascade() const { return inherited->fontCascade; }
1410 const FontMetrics& RenderStyle::fontMetrics() const { return inherited->fontCascade.fontMetrics(); }
1411 const FontCascadeDescription& RenderStyle::fontDescription() const { return inherited->fontCascade.fontDescription(); }
1412 float RenderStyle::specifiedFontSize() const { return fontDescription().specifiedSize(); }
1413 float RenderStyle::computedFontSize() const { return fontDescription().computedSize(); }
1414 int RenderStyle::fontSize() const { return inherited->fontCascade.pixelSize(); }
1415
1416 const Length& RenderStyle::wordSpacing() const { return rareInheritedData->wordSpacing; }
1417 float RenderStyle::letterSpacing() const { return inherited->fontCascade.letterSpacing(); }
1418
1419 bool RenderStyle::setFontDescription(const FontCascadeDescription& v)
1420 {
1421     if (inherited->fontCascade.fontDescription() != v) {
1422         inherited.access()->fontCascade = FontCascade(v, inherited->fontCascade.letterSpacing(), inherited->fontCascade.wordSpacing());
1423         return true;
1424     }
1425     return false;
1426 }
1427
1428 #if ENABLE(IOS_TEXT_AUTOSIZING)
1429 const Length& RenderStyle::specifiedLineHeight() const { return inherited->specifiedLineHeight; }
1430 void RenderStyle::setSpecifiedLineHeight(Length v) { SET_VAR(inherited, specifiedLineHeight, v); }
1431 #else
1432 const Length& RenderStyle::specifiedLineHeight() const { return inherited->line_height; }
1433 #endif
1434
1435 Length RenderStyle::lineHeight() const
1436 {
1437     const Length& lh = inherited->line_height;
1438 #if ENABLE(TEXT_AUTOSIZING)
1439     // Unlike fontDescription().computedSize() and hence fontSize(), this is
1440     // recalculated on demand as we only store the specified line height.
1441     // FIXME: Should consider scaling the fixed part of any calc expressions
1442     // too, though this involves messily poking into CalcExpressionLength.
1443     float multiplier = textAutosizingMultiplier();
1444     if (multiplier > 1 && lh.isFixed())
1445         return Length(TextAutosizer::computeAutosizedFontSize(lh.value(), multiplier), Fixed);
1446 #endif
1447     return lh;
1448 }
1449 void RenderStyle::setLineHeight(Length specifiedLineHeight) { SET_VAR(inherited, line_height, specifiedLineHeight); }
1450
1451 int RenderStyle::computedLineHeight() const
1452 {
1453     const Length& lh = lineHeight();
1454
1455     // Negative value means the line height is not set. Use the font's built-in spacing.
1456     if (lh.isNegative())
1457         return fontMetrics().lineSpacing();
1458
1459     if (lh.isPercentOrCalculated())
1460         return minimumValueForLength(lh, fontSize());
1461
1462     return clampTo<int>(lh.value());
1463 }
1464
1465 void RenderStyle::setWordSpacing(Length value)
1466 {
1467     float fontWordSpacing;
1468     switch (value.type()) {
1469     case Auto:
1470         fontWordSpacing = 0;
1471         break;
1472     case Percent:
1473         fontWordSpacing = value.percent() * fontCascade().spaceWidth() / 100;
1474         break;
1475     case Fixed:
1476         fontWordSpacing = value.value();
1477         break;
1478     case Calculated:
1479         fontWordSpacing = value.nonNanCalculatedValue(maxValueForCssLength);
1480         break;
1481     default:
1482         ASSERT_NOT_REACHED();
1483         fontWordSpacing = 0;
1484         break;
1485     }
1486     inherited.access()->fontCascade.setWordSpacing(fontWordSpacing);
1487     rareInheritedData.access()->wordSpacing = WTFMove(value);
1488 }
1489
1490 void RenderStyle::setLetterSpacing(float v) { inherited.access()->fontCascade.setLetterSpacing(v); }
1491
1492 void RenderStyle::setFontSize(float size)
1493 {
1494     // size must be specifiedSize if Text Autosizing is enabled, but computedSize if text
1495     // zoom is enabled (if neither is enabled it's irrelevant as they're probably the same).
1496
1497     ASSERT(std::isfinite(size));
1498     if (!std::isfinite(size) || size < 0)
1499         size = 0;
1500     else
1501         size = std::min(maximumAllowedFontSize, size);
1502
1503     FontSelector* currentFontSelector = fontCascade().fontSelector();
1504     auto description = fontDescription();
1505     description.setSpecifiedSize(size);
1506     description.setComputedSize(size);
1507
1508 #if ENABLE(TEXT_AUTOSIZING)
1509     float multiplier = textAutosizingMultiplier();
1510     if (multiplier > 1) {
1511         float autosizedFontSize = TextAutosizer::computeAutosizedFontSize(size, multiplier);
1512         description.setComputedSize(min(maximumAllowedFontSize, autosizedFontSize));
1513     }
1514 #endif
1515
1516     setFontDescription(description);
1517     fontCascade().update(currentFontSelector);
1518 }
1519
1520 void RenderStyle::getShadowExtent(const ShadowData* shadow, LayoutUnit &top, LayoutUnit &right, LayoutUnit &bottom, LayoutUnit &left) const
1521 {
1522     top = 0;
1523     right = 0;
1524     bottom = 0;
1525     left = 0;
1526
1527     for ( ; shadow; shadow = shadow->next()) {
1528         if (shadow->style() == Inset)
1529             continue;
1530
1531         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1532         top = std::min<LayoutUnit>(top, shadow->y() - extentAndSpread);
1533         right = std::max<LayoutUnit>(right, shadow->x() + extentAndSpread);
1534         bottom = std::max<LayoutUnit>(bottom, shadow->y() + extentAndSpread);
1535         left = std::min<LayoutUnit>(left, shadow->x() - extentAndSpread);
1536     }
1537 }
1538
1539 LayoutBoxExtent RenderStyle::getShadowInsetExtent(const ShadowData* shadow) const
1540 {
1541     LayoutUnit top = 0;
1542     LayoutUnit right = 0;
1543     LayoutUnit bottom = 0;
1544     LayoutUnit left = 0;
1545
1546     for ( ; shadow; shadow = shadow->next()) {
1547         if (shadow->style() == Normal)
1548             continue;
1549
1550         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1551         top = std::max<LayoutUnit>(top, shadow->y() + extentAndSpread);
1552         right = std::min<LayoutUnit>(right, shadow->x() - extentAndSpread);
1553         bottom = std::min<LayoutUnit>(bottom, shadow->y() - extentAndSpread);
1554         left = std::max<LayoutUnit>(left, shadow->x() + extentAndSpread);
1555     }
1556
1557     return LayoutBoxExtent(top, right, bottom, left);
1558 }
1559
1560 void RenderStyle::getShadowHorizontalExtent(const ShadowData* shadow, LayoutUnit &left, LayoutUnit &right) const
1561 {
1562     left = 0;
1563     right = 0;
1564
1565     for ( ; shadow; shadow = shadow->next()) {
1566         if (shadow->style() == Inset)
1567             continue;
1568
1569         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1570         left = std::min<LayoutUnit>(left, shadow->x() - extentAndSpread);
1571         right = std::max<LayoutUnit>(right, shadow->x() + extentAndSpread);
1572     }
1573 }
1574
1575 void RenderStyle::getShadowVerticalExtent(const ShadowData* shadow, LayoutUnit &top, LayoutUnit &bottom) const
1576 {
1577     top = 0;
1578     bottom = 0;
1579
1580     for ( ; shadow; shadow = shadow->next()) {
1581         if (shadow->style() == Inset)
1582             continue;
1583
1584         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1585         top = std::min<LayoutUnit>(top, shadow->y() - extentAndSpread);
1586         bottom = std::max<LayoutUnit>(bottom, shadow->y() + extentAndSpread);
1587     }
1588 }
1589
1590 Color RenderStyle::colorIncludingFallback(int colorProperty, bool visitedLink) const
1591 {
1592     Color result;
1593     EBorderStyle borderStyle = BNONE;
1594     switch (colorProperty) {
1595     case CSSPropertyBackgroundColor:
1596         return visitedLink ? visitedLinkBackgroundColor() : backgroundColor(); // Background color doesn't fall back.
1597     case CSSPropertyBorderLeftColor:
1598         result = visitedLink ? visitedLinkBorderLeftColor() : borderLeftColor();
1599         borderStyle = borderLeftStyle();
1600         break;
1601     case CSSPropertyBorderRightColor:
1602         result = visitedLink ? visitedLinkBorderRightColor() : borderRightColor();
1603         borderStyle = borderRightStyle();
1604         break;
1605     case CSSPropertyBorderTopColor:
1606         result = visitedLink ? visitedLinkBorderTopColor() : borderTopColor();
1607         borderStyle = borderTopStyle();
1608         break;
1609     case CSSPropertyBorderBottomColor:
1610         result = visitedLink ? visitedLinkBorderBottomColor() : borderBottomColor();
1611         borderStyle = borderBottomStyle();
1612         break;
1613     case CSSPropertyColor:
1614         result = visitedLink ? visitedLinkColor() : color();
1615         break;
1616     case CSSPropertyOutlineColor:
1617         result = visitedLink ? visitedLinkOutlineColor() : outlineColor();
1618         break;
1619     case CSSPropertyColumnRuleColor:
1620         result = visitedLink ? visitedLinkColumnRuleColor() : columnRuleColor();
1621         break;
1622     case CSSPropertyWebkitTextDecorationColor:
1623         // Text decoration color fallback is handled in RenderObject::decorationColor.
1624         return visitedLink ? visitedLinkTextDecorationColor() : textDecorationColor();
1625     case CSSPropertyWebkitTextEmphasisColor:
1626         result = visitedLink ? visitedLinkTextEmphasisColor() : textEmphasisColor();
1627         break;
1628     case CSSPropertyWebkitTextFillColor:
1629         result = visitedLink ? visitedLinkTextFillColor() : textFillColor();
1630         break;
1631     case CSSPropertyWebkitTextStrokeColor:
1632         result = visitedLink ? visitedLinkTextStrokeColor() : textStrokeColor();
1633         break;
1634     default:
1635         ASSERT_NOT_REACHED();
1636         break;
1637     }
1638
1639     if (!result.isValid()) {
1640         if (!visitedLink && (borderStyle == INSET || borderStyle == OUTSET || borderStyle == RIDGE || borderStyle == GROOVE))
1641             result.setRGB(238, 238, 238);
1642         else
1643             result = visitedLink ? visitedLinkColor() : color();
1644     }
1645     return result;
1646 }
1647
1648 Color RenderStyle::visitedDependentColor(int colorProperty) const
1649 {
1650     Color unvisitedColor = colorIncludingFallback(colorProperty, false);
1651     if (insideLink() != InsideVisitedLink)
1652         return unvisitedColor;
1653
1654     Color visitedColor = colorIncludingFallback(colorProperty, true);
1655
1656     // Text decoration color validity is preserved (checked in RenderObject::decorationColor).
1657     if (colorProperty == CSSPropertyWebkitTextDecorationColor)
1658         return visitedColor;
1659
1660     // FIXME: Technically someone could explicitly specify the color transparent, but for now we'll just
1661     // assume that if the background color is transparent that it wasn't set. Note that it's weird that
1662     // we're returning unvisited info for a visited link, but given our restriction that the alpha values
1663     // have to match, it makes more sense to return the unvisited background color if specified than it
1664     // does to return black. This behavior matches what Firefox 4 does as well.
1665     if (colorProperty == CSSPropertyBackgroundColor && visitedColor == Color::transparent)
1666         return unvisitedColor;
1667
1668     // Take the alpha from the unvisited color, but get the RGB values from the visited color.
1669     return Color(visitedColor.red(), visitedColor.green(), visitedColor.blue(), unvisitedColor.alpha());
1670 }
1671
1672 const BorderValue& RenderStyle::borderBefore() const
1673 {
1674     switch (writingMode()) {
1675     case TopToBottomWritingMode:
1676         return borderTop();
1677     case BottomToTopWritingMode:
1678         return borderBottom();
1679     case LeftToRightWritingMode:
1680         return borderLeft();
1681     case RightToLeftWritingMode:
1682         return borderRight();
1683     }
1684     ASSERT_NOT_REACHED();
1685     return borderTop();
1686 }
1687
1688 const BorderValue& RenderStyle::borderAfter() const
1689 {
1690     switch (writingMode()) {
1691     case TopToBottomWritingMode:
1692         return borderBottom();
1693     case BottomToTopWritingMode:
1694         return borderTop();
1695     case LeftToRightWritingMode:
1696         return borderRight();
1697     case RightToLeftWritingMode:
1698         return borderLeft();
1699     }
1700     ASSERT_NOT_REACHED();
1701     return borderBottom();
1702 }
1703
1704 const BorderValue& RenderStyle::borderStart() const
1705 {
1706     if (isHorizontalWritingMode())
1707         return isLeftToRightDirection() ? borderLeft() : borderRight();
1708     return isLeftToRightDirection() ? borderTop() : borderBottom();
1709 }
1710
1711 const BorderValue& RenderStyle::borderEnd() const
1712 {
1713     if (isHorizontalWritingMode())
1714         return isLeftToRightDirection() ? borderRight() : borderLeft();
1715     return isLeftToRightDirection() ? borderBottom() : borderTop();
1716 }
1717
1718 float RenderStyle::borderBeforeWidth() const
1719 {
1720     switch (writingMode()) {
1721     case TopToBottomWritingMode:
1722         return borderTopWidth();
1723     case BottomToTopWritingMode:
1724         return borderBottomWidth();
1725     case LeftToRightWritingMode:
1726         return borderLeftWidth();
1727     case RightToLeftWritingMode:
1728         return borderRightWidth();
1729     }
1730     ASSERT_NOT_REACHED();
1731     return borderTopWidth();
1732 }
1733
1734 float RenderStyle::borderAfterWidth() const
1735 {
1736     switch (writingMode()) {
1737     case TopToBottomWritingMode:
1738         return borderBottomWidth();
1739     case BottomToTopWritingMode:
1740         return borderTopWidth();
1741     case LeftToRightWritingMode:
1742         return borderRightWidth();
1743     case RightToLeftWritingMode:
1744         return borderLeftWidth();
1745     }
1746     ASSERT_NOT_REACHED();
1747     return borderBottomWidth();
1748 }
1749
1750 float RenderStyle::borderStartWidth() const
1751 {
1752     if (isHorizontalWritingMode())
1753         return isLeftToRightDirection() ? borderLeftWidth() : borderRightWidth();
1754     return isLeftToRightDirection() ? borderTopWidth() : borderBottomWidth();
1755 }
1756
1757 float RenderStyle::borderEndWidth() const
1758 {
1759     if (isHorizontalWritingMode())
1760         return isLeftToRightDirection() ? borderRightWidth() : borderLeftWidth();
1761     return isLeftToRightDirection() ? borderBottomWidth() : borderTopWidth();
1762 }
1763
1764 void RenderStyle::setMarginStart(Length margin)
1765 {
1766     if (isHorizontalWritingMode()) {
1767         if (isLeftToRightDirection())
1768             setMarginLeft(margin);
1769         else
1770             setMarginRight(margin);
1771     } else {
1772         if (isLeftToRightDirection())
1773             setMarginTop(margin);
1774         else
1775             setMarginBottom(margin);
1776     }
1777 }
1778
1779 void RenderStyle::setMarginEnd(Length margin)
1780 {
1781     if (isHorizontalWritingMode()) {
1782         if (isLeftToRightDirection())
1783             setMarginRight(margin);
1784         else
1785             setMarginLeft(margin);
1786     } else {
1787         if (isLeftToRightDirection())
1788             setMarginBottom(margin);
1789         else
1790             setMarginTop(margin);
1791     }
1792 }
1793
1794 TextEmphasisMark RenderStyle::textEmphasisMark() const
1795 {
1796     TextEmphasisMark mark = static_cast<TextEmphasisMark>(rareInheritedData->textEmphasisMark);
1797     if (mark != TextEmphasisMarkAuto)
1798         return mark;
1799
1800     if (isHorizontalWritingMode())
1801         return TextEmphasisMarkDot;
1802
1803     return TextEmphasisMarkSesame;
1804 }
1805
1806 #if ENABLE(TOUCH_EVENTS)
1807 Color RenderStyle::initialTapHighlightColor()
1808 {
1809     return RenderTheme::tapHighlightColor();
1810 }
1811 #endif
1812
1813 LayoutBoxExtent RenderStyle::imageOutsets(const NinePieceImage& image) const
1814 {
1815     return LayoutBoxExtent(NinePieceImage::computeOutset(image.outset().top(), borderTopWidth()),
1816                            NinePieceImage::computeOutset(image.outset().right(), borderRightWidth()),
1817                            NinePieceImage::computeOutset(image.outset().bottom(), borderBottomWidth()),
1818                            NinePieceImage::computeOutset(image.outset().left(), borderLeftWidth()));
1819 }
1820
1821 std::pair<FontOrientation, NonCJKGlyphOrientation> RenderStyle::fontAndGlyphOrientation()
1822 {
1823     // FIXME: TextOrientationSideways should map to sideways-left in vertical-lr, which is not supported yet.
1824
1825     if (isHorizontalWritingMode())
1826         return { Horizontal, NonCJKGlyphOrientation::Mixed };
1827
1828     switch (textOrientation()) {
1829     case TextOrientation::Mixed:
1830         return { Vertical, NonCJKGlyphOrientation::Mixed };
1831     case TextOrientation::Upright:
1832         return { Vertical, NonCJKGlyphOrientation::Upright };
1833     case TextOrientation::Sideways:
1834         return { Horizontal, NonCJKGlyphOrientation::Mixed };
1835     default:
1836         ASSERT_NOT_REACHED();
1837         return { Horizontal, NonCJKGlyphOrientation::Mixed };
1838     }
1839 }
1840
1841 void RenderStyle::setBorderImageSource(PassRefPtr<StyleImage> image)
1842 {
1843     if (surround->border.m_image.image() == image.get())
1844         return;
1845     surround.access()->border.m_image.setImage(image);
1846 }
1847
1848 void RenderStyle::setBorderImageSlices(LengthBox slices)
1849 {
1850     if (surround->border.m_image.imageSlices() == slices)
1851         return;
1852     surround.access()->border.m_image.setImageSlices(slices);
1853 }
1854
1855 void RenderStyle::setBorderImageWidth(LengthBox slices)
1856 {
1857     if (surround->border.m_image.borderSlices() == slices)
1858         return;
1859     surround.access()->border.m_image.setBorderSlices(slices);
1860 }
1861
1862 void RenderStyle::setBorderImageOutset(LengthBox outset)
1863 {
1864     if (surround->border.m_image.outset() == outset)
1865         return;
1866     surround.access()->border.m_image.setOutset(outset);
1867 }
1868
1869 void RenderStyle::setColumnStylesFromPaginationMode(const Pagination::Mode& paginationMode)
1870 {
1871     if (paginationMode == Pagination::Unpaginated)
1872         return;
1873     
1874     setColumnFill(ColumnFillAuto);
1875     
1876     switch (paginationMode) {
1877     case Pagination::LeftToRightPaginated:
1878         setColumnAxis(HorizontalColumnAxis);
1879         if (isHorizontalWritingMode())
1880             setColumnProgression(isLeftToRightDirection() ? NormalColumnProgression : ReverseColumnProgression);
1881         else
1882             setColumnProgression(isFlippedBlocksWritingMode() ? ReverseColumnProgression : NormalColumnProgression);
1883         break;
1884     case Pagination::RightToLeftPaginated:
1885         setColumnAxis(HorizontalColumnAxis);
1886         if (isHorizontalWritingMode())
1887             setColumnProgression(isLeftToRightDirection() ? ReverseColumnProgression : NormalColumnProgression);
1888         else
1889             setColumnProgression(isFlippedBlocksWritingMode() ? NormalColumnProgression : ReverseColumnProgression);
1890         break;
1891     case Pagination::TopToBottomPaginated:
1892         setColumnAxis(VerticalColumnAxis);
1893         if (isHorizontalWritingMode())
1894             setColumnProgression(isFlippedBlocksWritingMode() ? ReverseColumnProgression : NormalColumnProgression);
1895         else
1896             setColumnProgression(isLeftToRightDirection() ? NormalColumnProgression : ReverseColumnProgression);
1897         break;
1898     case Pagination::BottomToTopPaginated:
1899         setColumnAxis(VerticalColumnAxis);
1900         if (isHorizontalWritingMode())
1901             setColumnProgression(isFlippedBlocksWritingMode() ? NormalColumnProgression : ReverseColumnProgression);
1902         else
1903             setColumnProgression(isLeftToRightDirection() ? ReverseColumnProgression : NormalColumnProgression);
1904         break;
1905     case Pagination::Unpaginated:
1906         ASSERT_NOT_REACHED();
1907         break;
1908     }
1909 }
1910
1911 #if ENABLE(CSS_SCROLL_SNAP)
1912 LengthSize RenderStyle::initialScrollSnapDestination()
1913 {
1914     return defaultScrollSnapDestination();
1915 }
1916
1917 Vector<LengthSize> RenderStyle::initialScrollSnapCoordinates()
1918 {
1919     return Vector<LengthSize>();
1920 }
1921
1922 const ScrollSnapPoints* RenderStyle::scrollSnapPointsX() const
1923 {
1924     return rareNonInheritedData->m_scrollSnapPoints->xPoints.get();
1925 }
1926
1927 const ScrollSnapPoints* RenderStyle::scrollSnapPointsY() const
1928 {
1929     return rareNonInheritedData->m_scrollSnapPoints->yPoints.get();
1930 }
1931
1932 const LengthSize& RenderStyle::scrollSnapDestination() const
1933 {
1934     return rareNonInheritedData->m_scrollSnapPoints->destination;
1935 }
1936
1937 const Vector<LengthSize>& RenderStyle::scrollSnapCoordinates() const
1938 {
1939     return rareNonInheritedData->m_scrollSnapPoints->coordinates;
1940 }
1941
1942 void RenderStyle::setScrollSnapPointsX(std::unique_ptr<ScrollSnapPoints> points)
1943 {
1944     if (rareNonInheritedData->m_scrollSnapPoints->xPoints.get() == points.get())
1945         return;
1946     rareNonInheritedData.access()->m_scrollSnapPoints.access()->xPoints = WTFMove(points);
1947 }
1948
1949 void RenderStyle::setScrollSnapPointsY(std::unique_ptr<ScrollSnapPoints> points)
1950 {
1951     if (rareNonInheritedData->m_scrollSnapPoints->yPoints.get() == points.get())
1952         return;
1953     rareNonInheritedData.access()->m_scrollSnapPoints.access()->yPoints = WTFMove(points);
1954 }
1955
1956 void RenderStyle::setScrollSnapDestination(LengthSize destination)
1957 {
1958     if (rareNonInheritedData->m_scrollSnapPoints->destination == destination)
1959         return;
1960     rareNonInheritedData.access()->m_scrollSnapPoints.access()->destination = WTFMove(destination);
1961 }
1962
1963 void RenderStyle::setScrollSnapCoordinates(Vector<LengthSize> coordinates)
1964 {
1965     if (rareNonInheritedData->m_scrollSnapPoints->coordinates == coordinates)
1966         return;
1967     rareNonInheritedData.access()->m_scrollSnapPoints.access()->coordinates = WTFMove(coordinates);
1968 }
1969
1970 #endif
1971
1972 void RenderStyle::checkVariablesInCustomProperties()
1973 {
1974     if (!rareInheritedData->m_customProperties->containsVariables())
1975         return;
1976     
1977     // Our first pass checks the variables for validity and replaces any properties that became
1978     // invalid with empty values.
1979     auto& customProperties = rareInheritedData.access()->m_customProperties.access()->values();
1980     HashSet<AtomicString> invalidProperties;
1981     for (auto entry : customProperties) {
1982         if (!entry.value->isVariableDependentValue())
1983             continue;
1984         HashSet<AtomicString> seenProperties;
1985         downcast<CSSVariableDependentValue>(*entry.value).checkVariablesForCycles(entry.key, customProperties, seenProperties, invalidProperties);
1986     }
1987     
1988     // Now insert invalid values.
1989     if (!invalidProperties.isEmpty()) {
1990         RefPtr<CSSValue> invalidValue = CSSCustomPropertyValue::createInvalid();
1991         for (auto& property : invalidProperties)
1992             customProperties.set(property, invalidValue);
1993     }
1994
1995     // Now that all of the properties have been tested for validity and replaced with
1996     // invalid values if they failed, we can perform variable substitution on the valid values.
1997     Vector<RefPtr<CSSCustomPropertyValue>> resolvedValues;
1998     for (auto entry : customProperties) {
1999         if (!entry.value->isVariableDependentValue())
2000             continue;
2001         
2002         CSSParserValueList parserList;
2003         RefPtr<CSSCustomPropertyValue> result;
2004         if (!downcast<CSSVariableDependentValue>(*entry.value).valueList()->buildParserValueListSubstitutingVariables(&parserList, customProperties)) {
2005             RefPtr<CSSValue> invalidResult = CSSCustomPropertyValue::createInvalid();
2006             result = CSSCustomPropertyValue::create(entry.key, invalidResult);
2007         } else {
2008             RefPtr<CSSValue> newValueList = CSSValueList::createFromParserValueList(parserList);
2009             result = CSSCustomPropertyValue::create(entry.key, newValueList);
2010         }
2011         resolvedValues.append(result);
2012     }
2013     
2014     // With all results computed, we can now mutate our table to eliminate the variables and
2015     // hold the final values. This way when we inherit, we don't end up resubstituting variables, etc.
2016     for (auto& resolvedValue : resolvedValues)
2017         customProperties.set(resolvedValue->name(), resolvedValue->value());
2018
2019     rareInheritedData.access()->m_customProperties.access()->setContainsVariables(false);
2020 }
2021
2022 } // namespace WebCore