Update to match text-orientation spec
[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->locale != other.rareInheritedData->locale
599             || rareInheritedData->m_rubyPosition != other.rareInheritedData->m_rubyPosition
600             || rareInheritedData->textEmphasisMark != other.rareInheritedData->textEmphasisMark
601             || rareInheritedData->textEmphasisPosition != other.rareInheritedData->textEmphasisPosition
602             || rareInheritedData->textEmphasisCustomMark != other.rareInheritedData->textEmphasisCustomMark
603             || rareInheritedData->m_textOrientation != other.rareInheritedData->m_textOrientation
604             || rareInheritedData->m_tabSize != other.rareInheritedData->m_tabSize
605             || rareInheritedData->m_lineBoxContain != other.rareInheritedData->m_lineBoxContain
606             || rareInheritedData->m_lineGrid != other.rareInheritedData->m_lineGrid
607 #if ENABLE(CSS_IMAGE_ORIENTATION)
608             || rareInheritedData->m_imageOrientation != other.rareInheritedData->m_imageOrientation
609 #endif
610 #if ENABLE(CSS_IMAGE_RESOLUTION)
611             || rareInheritedData->m_imageResolutionSource != other.rareInheritedData->m_imageResolutionSource
612             || rareInheritedData->m_imageResolutionSnap != other.rareInheritedData->m_imageResolutionSnap
613             || rareInheritedData->m_imageResolution != other.rareInheritedData->m_imageResolution
614 #endif
615             || rareInheritedData->m_lineSnap != other.rareInheritedData->m_lineSnap
616             || rareInheritedData->m_lineAlign != other.rareInheritedData->m_lineAlign
617 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
618             || rareInheritedData->useTouchOverflowScrolling != other.rareInheritedData->useTouchOverflowScrolling
619 #endif
620             || rareInheritedData->listStyleImage != other.rareInheritedData->listStyleImage) // FIXME: needs arePointingToEqualData()?
621             return true;
622
623         if (textStrokeWidth() != other.textStrokeWidth())
624             return true;
625     }
626
627 #if ENABLE(TEXT_AUTOSIZING)
628     if (visual->m_textAutosizingMultiplier != other.visual->m_textAutosizingMultiplier)
629         return true;
630 #endif
631
632     if (inherited->line_height != other.inherited->line_height
633 #if ENABLE(IOS_TEXT_AUTOSIZING)
634         || inherited->specifiedLineHeight != other.inherited->specifiedLineHeight
635 #endif
636         || inherited->fontCascade != other.inherited->fontCascade
637         || inherited->horizontal_border_spacing != other.inherited->horizontal_border_spacing
638         || inherited->vertical_border_spacing != other.inherited->vertical_border_spacing
639         || inherited_flags._box_direction != other.inherited_flags._box_direction
640         || inherited_flags.m_rtlOrdering != other.inherited_flags.m_rtlOrdering
641         || noninherited_flags.position() != other.noninherited_flags.position()
642         || noninherited_flags.floating() != other.noninherited_flags.floating()
643         || noninherited_flags.originalDisplay() != other.noninherited_flags.originalDisplay())
644         return true;
645
646
647     if ((noninherited_flags.effectiveDisplay()) >= TABLE) {
648         if (inherited_flags._border_collapse != other.inherited_flags._border_collapse
649             || inherited_flags._empty_cells != other.inherited_flags._empty_cells
650             || inherited_flags._caption_side != other.inherited_flags._caption_side
651             || noninherited_flags.tableLayout() != other.noninherited_flags.tableLayout())
652             return true;
653
654         // In the collapsing border model, 'hidden' suppresses other borders, while 'none'
655         // does not, so these style differences can be width differences.
656         if (inherited_flags._border_collapse
657             && ((borderTopStyle() == BHIDDEN && other.borderTopStyle() == BNONE)
658                 || (borderTopStyle() == BNONE && other.borderTopStyle() == BHIDDEN)
659                 || (borderBottomStyle() == BHIDDEN && other.borderBottomStyle() == BNONE)
660                 || (borderBottomStyle() == BNONE && other.borderBottomStyle() == BHIDDEN)
661                 || (borderLeftStyle() == BHIDDEN && other.borderLeftStyle() == BNONE)
662                 || (borderLeftStyle() == BNONE && other.borderLeftStyle() == BHIDDEN)
663                 || (borderRightStyle() == BHIDDEN && other.borderRightStyle() == BNONE)
664                 || (borderRightStyle() == BNONE && other.borderRightStyle() == BHIDDEN)))
665             return true;
666     }
667
668     if (noninherited_flags.effectiveDisplay() == LIST_ITEM) {
669         if (inherited_flags._list_style_type != other.inherited_flags._list_style_type
670             || inherited_flags._list_style_position != other.inherited_flags._list_style_position)
671             return true;
672     }
673
674     if (inherited_flags._text_align != other.inherited_flags._text_align
675         || inherited_flags._text_transform != other.inherited_flags._text_transform
676         || inherited_flags._direction != other.inherited_flags._direction
677         || inherited_flags._white_space != other.inherited_flags._white_space
678         || noninherited_flags.clear() != other.noninherited_flags.clear()
679         || noninherited_flags.unicodeBidi() != other.noninherited_flags.unicodeBidi())
680         return true;
681
682     // Check block flow direction.
683     if (inherited_flags.m_writingMode != other.inherited_flags.m_writingMode)
684         return true;
685
686     // Check text combine mode.
687     if (rareNonInheritedData->m_textCombine != other.rareNonInheritedData->m_textCombine)
688         return true;
689
690     // Overflow returns a layout hint.
691     if (noninherited_flags.overflowX() != other.noninherited_flags.overflowX()
692         || noninherited_flags.overflowY() != other.noninherited_flags.overflowY())
693         return true;
694
695     // If our border widths change, then we need to layout.  Other changes to borders
696     // only necessitate a repaint.
697     if (borderLeftWidth() != other.borderLeftWidth()
698         || borderTopWidth() != other.borderTopWidth()
699         || borderBottomWidth() != other.borderBottomWidth()
700         || borderRightWidth() != other.borderRightWidth())
701         return true;
702
703     // If the counter directives change, trigger a relayout to re-calculate counter values and rebuild the counter node tree.
704     if (!arePointingToEqualData(rareNonInheritedData->m_counterDirectives, other.rareNonInheritedData->m_counterDirectives))
705         return true;
706
707     if ((visibility() == COLLAPSE) != (other.visibility() == COLLAPSE))
708         return true;
709
710     if (rareNonInheritedData->hasOpacity() != other.rareNonInheritedData->hasOpacity()) {
711         // FIXME: We would like to use SimplifiedLayout here, but we can't quite do that yet.
712         // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
713         // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
714         // In addition we need to solve the floating object issue when layers come and go. Right now
715         // a full layout is necessary to keep floating object lists sane.
716         return true;
717     }
718
719     if (rareNonInheritedData->hasFilters() != other.rareNonInheritedData->hasFilters())
720         return true;
721
722 #if ENABLE(FILTERS_LEVEL_2)
723     if (rareNonInheritedData->hasBackdropFilters() != other.rareNonInheritedData->hasBackdropFilters())
724         return true;
725 #endif
726
727     if (!arePointingToEqualData(rareInheritedData->quotes, other.rareInheritedData->quotes))
728         return true;
729
730     if (position() != StaticPosition) {
731         if (surround->offset != other.surround->offset) {
732             // FIXME: We would like to use SimplifiedLayout for relative positioning, but we can't quite do that yet.
733             // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
734             // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
735             if (position() != AbsolutePosition)
736                 return true;
737
738             // Optimize for the case where a positioned layer is moving but not changing size.
739             if (!positionChangeIsMovementOnly(surround->offset, other.surround->offset, m_box->width()))
740                 return true;
741         }
742     }
743     
744     return false;
745 }
746
747 bool RenderStyle::changeRequiresPositionedLayoutOnly(const RenderStyle& other, unsigned&) const
748 {
749     if (position() == StaticPosition)
750         return false;
751
752     if (surround->offset != other.surround->offset) {
753         // Optimize for the case where a positioned layer is moving but not changing size.
754         if (position() == AbsolutePosition && positionChangeIsMovementOnly(surround->offset, other.surround->offset, m_box->width()))
755             return true;
756     }
757     
758     return false;
759 }
760
761 bool RenderStyle::changeRequiresLayerRepaint(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
762 {
763     // StyleResolver has ensured that zIndex is non-auto only if it's applicable.
764     if (m_box->zIndex() != other.m_box->zIndex() || m_box->hasAutoZIndex() != other.m_box->hasAutoZIndex())
765         return true;
766
767     if (position() != StaticPosition) {
768         if (visual->clip != other.visual->clip || visual->hasClip != other.visual->hasClip) {
769             changedContextSensitiveProperties |= ContextSensitivePropertyClipRect;
770             return true;
771         }
772     }
773
774 #if ENABLE(CSS_COMPOSITING)
775     if (rareNonInheritedData->m_effectiveBlendMode != other.rareNonInheritedData->m_effectiveBlendMode)
776         return true;
777 #endif
778
779     if (rareNonInheritedData->opacity != other.rareNonInheritedData->opacity) {
780         changedContextSensitiveProperties |= ContextSensitivePropertyOpacity;
781         // Don't return; keep looking for another change.
782     }
783
784     if (rareNonInheritedData->m_filter != other.rareNonInheritedData->m_filter) {
785         changedContextSensitiveProperties |= ContextSensitivePropertyFilter;
786         // Don't return; keep looking for another change.
787     }
788
789     if (rareNonInheritedData->m_mask != other.rareNonInheritedData->m_mask
790         || rareNonInheritedData->m_maskBoxImage != other.rareNonInheritedData->m_maskBoxImage)
791         return true;
792
793     return false;
794 }
795
796 bool RenderStyle::changeRequiresRepaint(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
797 {
798     if (inherited_flags._visibility != other.inherited_flags._visibility
799         || inherited_flags.m_printColorAdjust != other.inherited_flags.m_printColorAdjust
800         || inherited_flags._insideLink != other.inherited_flags._insideLink
801         || inherited_flags._insideDefaultButton != other.inherited_flags._insideDefaultButton
802         || surround->border != other.surround->border
803         || !m_background->isEquivalentForPainting(*other.m_background)
804         || rareInheritedData->userModify != other.rareInheritedData->userModify
805         || rareInheritedData->userSelect != other.rareInheritedData->userSelect
806         || rareNonInheritedData->userDrag != other.rareNonInheritedData->userDrag
807         || rareNonInheritedData->m_borderFit != other.rareNonInheritedData->m_borderFit
808         || rareNonInheritedData->m_objectFit != other.rareNonInheritedData->m_objectFit
809         || rareInheritedData->m_imageRendering != other.rareInheritedData->m_imageRendering)
810         return true;
811
812 #if ENABLE(CSS_SHAPES)
813     if (rareNonInheritedData->m_shapeOutside != other.rareNonInheritedData->m_shapeOutside)
814         return true;
815 #endif
816
817     // FIXME: this should probably be moved to changeRequiresLayerRepaint().
818     if (rareNonInheritedData->m_clipPath != other.rareNonInheritedData->m_clipPath) {
819         changedContextSensitiveProperties |= ContextSensitivePropertyClipPath;
820         // Don't return; keep looking for another change.
821     }
822
823     return false;
824 }
825
826 bool RenderStyle::changeRequiresRepaintIfTextOrBorderOrOutline(const RenderStyle& other, unsigned&) const
827 {
828     if (inherited->color != other.inherited->color
829         || inherited_flags._text_decorations != other.inherited_flags._text_decorations
830         || visual->textDecoration != other.visual->textDecoration
831         || rareNonInheritedData->m_textDecorationStyle != other.rareNonInheritedData->m_textDecorationStyle
832         || rareNonInheritedData->m_textDecorationColor != other.rareNonInheritedData->m_textDecorationColor
833         || rareInheritedData->m_textDecorationSkip != other.rareInheritedData->m_textDecorationSkip
834         || rareInheritedData->textFillColor != other.rareInheritedData->textFillColor
835         || rareInheritedData->textStrokeColor != other.rareInheritedData->textStrokeColor
836         || rareInheritedData->textEmphasisColor != other.rareInheritedData->textEmphasisColor
837         || rareInheritedData->textEmphasisFill != other.rareInheritedData->textEmphasisFill)
838         return true;
839
840     return false;
841 }
842
843 bool RenderStyle::changeRequiresRecompositeLayer(const RenderStyle& other, unsigned&) const
844 {
845     if (rareNonInheritedData.get() != other.rareNonInheritedData.get()) {
846         if (rareNonInheritedData->m_transformStyle3D != other.rareNonInheritedData->m_transformStyle3D
847             || rareNonInheritedData->m_backfaceVisibility != other.rareNonInheritedData->m_backfaceVisibility
848             || rareNonInheritedData->m_perspective != other.rareNonInheritedData->m_perspective
849             || rareNonInheritedData->m_perspectiveOriginX != other.rareNonInheritedData->m_perspectiveOriginX
850             || rareNonInheritedData->m_perspectiveOriginY != other.rareNonInheritedData->m_perspectiveOriginY)
851             return true;
852     }
853
854     return false;
855 }
856
857 StyleDifference RenderStyle::diff(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
858 {
859     changedContextSensitiveProperties = ContextSensitivePropertyNone;
860
861     StyleDifference svgChange = StyleDifferenceEqual;
862     if (m_svgStyle != other.m_svgStyle) {
863         svgChange = m_svgStyle->diff(other.m_svgStyle.get());
864         if (svgChange == StyleDifferenceLayout)
865             return svgChange;
866     }
867
868     if (changeRequiresLayout(other, changedContextSensitiveProperties))
869         return StyleDifferenceLayout;
870
871     // SVGRenderStyle::diff() might have returned StyleDifferenceRepaint, eg. if fill changes.
872     // If eg. the font-size changed at the same time, we're not allowed to return StyleDifferenceRepaint,
873     // but have to return StyleDifferenceLayout, that's why  this if branch comes after all branches
874     // that are relevant for SVG and might return StyleDifferenceLayout.
875     if (svgChange != StyleDifferenceEqual)
876         return svgChange;
877
878     if (changeRequiresPositionedLayoutOnly(other, changedContextSensitiveProperties))
879         return StyleDifferenceLayoutPositionedMovementOnly;
880
881     if (changeRequiresLayerRepaint(other, changedContextSensitiveProperties))
882         return StyleDifferenceRepaintLayer;
883
884     if (changeRequiresRepaint(other, changedContextSensitiveProperties))
885         return StyleDifferenceRepaint;
886
887     if (changeRequiresRecompositeLayer(other, changedContextSensitiveProperties))
888         return StyleDifferenceRecompositeLayer;
889
890     if (changeRequiresRepaintIfTextOrBorderOrOutline(other, changedContextSensitiveProperties))
891         return StyleDifferenceRepaintIfTextOrBorderOrOutline;
892
893     // Cursors are not checked, since they will be set appropriately in response to mouse events,
894     // so they don't need to cause any repaint or layout.
895
896     // 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
897     // the resulting transition properly.
898     return StyleDifferenceEqual;
899 }
900
901 bool RenderStyle::diffRequiresLayerRepaint(const RenderStyle& style, bool isComposited) const
902 {
903     unsigned changedContextSensitiveProperties = 0;
904
905     if (changeRequiresRepaint(style, changedContextSensitiveProperties))
906         return true;
907
908     if (isComposited && changeRequiresLayerRepaint(style, changedContextSensitiveProperties))
909         return changedContextSensitiveProperties & ContextSensitivePropertyClipRect;
910
911     return false;
912 }
913
914 void RenderStyle::setClip(Length top, Length right, Length bottom, Length left)
915 {
916     StyleVisualData* data = visual.access();
917     data->clip.top() = top;
918     data->clip.right() = right;
919     data->clip.bottom() = bottom;
920     data->clip.left() = left;
921 }
922
923 void RenderStyle::addCursor(PassRefPtr<StyleImage> image, const IntPoint& hotSpot)
924 {
925     if (!rareInheritedData.access()->cursorData)
926         rareInheritedData.access()->cursorData = CursorList::create();
927     rareInheritedData.access()->cursorData->append(CursorData(image, hotSpot));
928 }
929
930 void RenderStyle::setCursorList(PassRefPtr<CursorList> other)
931 {
932     rareInheritedData.access()->cursorData = other;
933 }
934
935 void RenderStyle::setQuotes(PassRefPtr<QuotesData> q)
936 {
937     if (rareInheritedData->quotes == q || (rareInheritedData->quotes && q && *rareInheritedData->quotes == *q))
938         return;
939
940     rareInheritedData.access()->quotes = q;
941 }
942
943 void RenderStyle::setWillChange(PassRefPtr<WillChangeData> willChangeData)
944 {
945     if (arePointingToEqualData(rareNonInheritedData->m_willChange.get(), willChangeData.get()))
946         return;
947
948     rareNonInheritedData.access()->m_willChange = WTF::move(willChangeData);
949 }
950
951 void RenderStyle::clearCursorList()
952 {
953     if (rareInheritedData->cursorData)
954         rareInheritedData.access()->cursorData = nullptr;
955 }
956
957 void RenderStyle::clearContent()
958 {
959     if (rareNonInheritedData->m_content)
960         rareNonInheritedData.access()->m_content = nullptr;
961 }
962
963 void RenderStyle::appendContent(std::unique_ptr<ContentData> contentData)
964 {
965     auto& content = rareNonInheritedData.access()->m_content;
966     ContentData* lastContent = content.get();
967     while (lastContent && lastContent->next())
968         lastContent = lastContent->next();
969
970     if (lastContent)
971         lastContent->setNext(WTF::move(contentData));
972     else
973         content = WTF::move(contentData);
974 }
975
976 void RenderStyle::setContent(PassRefPtr<StyleImage> image, bool add)
977 {
978     if (!image)
979         return;
980         
981     if (add) {
982         appendContent(std::make_unique<ImageContentData>(image));
983         return;
984     }
985
986     rareNonInheritedData.access()->m_content = std::make_unique<ImageContentData>(image);
987     if (!rareNonInheritedData.access()->m_altText.isNull())
988         rareNonInheritedData.access()->m_content->setAltText(rareNonInheritedData.access()->m_altText);
989 }
990
991 void RenderStyle::setContent(const String& string, bool add)
992 {
993     auto& content = rareNonInheritedData.access()->m_content;
994     if (add) {
995         ContentData* lastContent = content.get();
996         while (lastContent && lastContent->next())
997             lastContent = lastContent->next();
998
999         if (lastContent) {
1000             // We attempt to merge with the last ContentData if possible.
1001             if (is<TextContentData>(*lastContent)) {
1002                 TextContentData& textContent = downcast<TextContentData>(*lastContent);
1003                 textContent.setText(textContent.text() + string);
1004             } else
1005                 lastContent->setNext(std::make_unique<TextContentData>(string));
1006
1007             if (!rareNonInheritedData.access()->m_altText.isNull())
1008                 lastContent->setAltText(rareNonInheritedData.access()->m_altText);
1009             return;
1010         }
1011     }
1012
1013     content = std::make_unique<TextContentData>(string);
1014     if (!rareNonInheritedData.access()->m_altText.isNull())
1015         content->setAltText(rareNonInheritedData.access()->m_altText);
1016 }
1017
1018 void RenderStyle::setContent(std::unique_ptr<CounterContent> counter, bool add)
1019 {
1020     if (!counter)
1021         return;
1022
1023     if (add) {
1024         appendContent(std::make_unique<CounterContentData>(WTF::move(counter)));
1025         return;
1026     }
1027
1028     rareNonInheritedData.access()->m_content = std::make_unique<CounterContentData>(WTF::move(counter));
1029 }
1030
1031 void RenderStyle::setContent(QuoteType quote, bool add)
1032 {
1033     if (add) {
1034         appendContent(std::make_unique<QuoteContentData>(quote));
1035         return;
1036     }
1037
1038     rareNonInheritedData.access()->m_content = std::make_unique<QuoteContentData>(quote);
1039 }
1040
1041 void RenderStyle::setContentAltText(const String& string)
1042 {
1043     rareNonInheritedData.access()->m_altText = string;
1044     
1045     if (rareNonInheritedData.access()->m_content)
1046         rareNonInheritedData.access()->m_content->setAltText(string);
1047 }
1048
1049 const String& RenderStyle::contentAltText() const
1050 {
1051     return rareNonInheritedData->m_altText;
1052 }
1053
1054 // FIXME: use affectedByTransformOrigin().
1055 static inline bool requireTransformOrigin(const Vector<RefPtr<TransformOperation>>& transformOperations, RenderStyle::ApplyTransformOrigin applyOrigin)
1056 {
1057     // transform-origin brackets the transform with translate operations.
1058     // Optimize for the case where the only transform is a translation, since the transform-origin is irrelevant
1059     // in that case.
1060     if (applyOrigin != RenderStyle::IncludeTransformOrigin)
1061         return false;
1062
1063     for (auto& operation : transformOperations) {
1064         TransformOperation::OperationType type = operation->type();
1065         if (type != TransformOperation::TRANSLATE_X
1066             && type != TransformOperation::TRANSLATE_Y
1067             && type != TransformOperation::TRANSLATE 
1068             && type != TransformOperation::TRANSLATE_Z
1069             && type != TransformOperation::TRANSLATE_3D)
1070             return true;
1071     }
1072
1073     return false;
1074 }
1075
1076 void RenderStyle::applyTransform(TransformationMatrix& transform, const FloatRect& boundingBox, ApplyTransformOrigin applyOrigin) const
1077 {
1078     auto& operations = rareNonInheritedData->m_transform->m_operations.operations();
1079     bool applyTransformOrigin = requireTransformOrigin(operations, applyOrigin);
1080
1081     float offsetX = transformOriginX().isPercent() ? boundingBox.x() : 0;
1082     float offsetY = transformOriginY().isPercent() ? boundingBox.y() : 0;
1083
1084     if (applyTransformOrigin) {
1085         transform.translate3d(floatValueForLength(transformOriginX(), boundingBox.width()) + offsetX,
1086                               floatValueForLength(transformOriginY(), boundingBox.height()) + offsetY,
1087                               transformOriginZ());
1088     }
1089
1090     for (auto& operation : operations)
1091         operation->apply(transform, boundingBox.size());
1092
1093     if (applyTransformOrigin) {
1094         transform.translate3d(-floatValueForLength(transformOriginX(), boundingBox.width()) - offsetX,
1095                               -floatValueForLength(transformOriginY(), boundingBox.height()) - offsetY,
1096                               -transformOriginZ());
1097     }
1098 }
1099
1100 void RenderStyle::setPageScaleTransform(float scale)
1101 {
1102     if (scale == 1)
1103         return;
1104     TransformOperations transform;
1105     transform.operations().append(ScaleTransformOperation::create(scale, scale, ScaleTransformOperation::SCALE));
1106     setTransform(transform);
1107     setTransformOriginX(Length(0, Fixed));
1108     setTransformOriginY(Length(0, Fixed));
1109 }
1110
1111 void RenderStyle::setTextShadow(std::unique_ptr<ShadowData> shadowData, bool add)
1112 {
1113     ASSERT(!shadowData || (!shadowData->spread() && shadowData->style() == Normal));
1114
1115     StyleRareInheritedData* rareData = rareInheritedData.access();
1116     if (!add) {
1117         rareData->textShadow = WTF::move(shadowData);
1118         return;
1119     }
1120
1121     shadowData->setNext(WTF::move(rareData->textShadow));
1122     rareData->textShadow = WTF::move(shadowData);
1123 }
1124
1125 void RenderStyle::setBoxShadow(std::unique_ptr<ShadowData> shadowData, bool add)
1126 {
1127     StyleRareNonInheritedData* rareData = rareNonInheritedData.access();
1128     if (!add) {
1129         rareData->m_boxShadow = WTF::move(shadowData);
1130         return;
1131     }
1132
1133     shadowData->setNext(WTF::move(rareData->m_boxShadow));
1134     rareData->m_boxShadow = WTF::move(shadowData);
1135 }
1136
1137 static RoundedRect::Radii calcRadiiFor(const BorderData& border, const LayoutSize& size)
1138 {
1139     return RoundedRect::Radii(
1140         LayoutSize(valueForLength(border.topLeft().width(), size.width()),
1141             valueForLength(border.topLeft().height(), size.height())),
1142         LayoutSize(valueForLength(border.topRight().width(), size.width()),
1143             valueForLength(border.topRight().height(), size.height())),
1144         LayoutSize(valueForLength(border.bottomLeft().width(), size.width()),
1145             valueForLength(border.bottomLeft().height(), size.height())),
1146         LayoutSize(valueForLength(border.bottomRight().width(), size.width()),
1147             valueForLength(border.bottomRight().height(), size.height())));
1148 }
1149
1150 StyleImage* RenderStyle::listStyleImage() const { return rareInheritedData->listStyleImage.get(); }
1151 void RenderStyle::setListStyleImage(PassRefPtr<StyleImage> v)
1152 {
1153     if (rareInheritedData->listStyleImage != v)
1154         rareInheritedData.access()->listStyleImage = v;
1155 }
1156
1157 Color RenderStyle::color() const { return inherited->color; }
1158 Color RenderStyle::visitedLinkColor() const { return inherited->visitedLinkColor; }
1159 void RenderStyle::setColor(const Color& v) { SET_VAR(inherited, color, v); }
1160 void RenderStyle::setVisitedLinkColor(const Color& v) { SET_VAR(inherited, visitedLinkColor, v); }
1161
1162 short RenderStyle::horizontalBorderSpacing() const { return inherited->horizontal_border_spacing; }
1163 short RenderStyle::verticalBorderSpacing() const { return inherited->vertical_border_spacing; }
1164 void RenderStyle::setHorizontalBorderSpacing(short v) { SET_VAR(inherited, horizontal_border_spacing, v); }
1165 void RenderStyle::setVerticalBorderSpacing(short v) { SET_VAR(inherited, vertical_border_spacing, v); }
1166
1167 RoundedRect RenderStyle::getRoundedBorderFor(const LayoutRect& borderRect, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1168 {
1169     RoundedRect roundedRect(borderRect);
1170     if (hasBorderRadius()) {
1171         RoundedRect::Radii radii = calcRadiiFor(surround->border, borderRect.size());
1172         radii.scale(calcBorderRadiiConstraintScaleFor(borderRect, radii));
1173         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
1174     }
1175     return roundedRect;
1176 }
1177
1178 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1179 {
1180     bool horizontal = isHorizontalWritingMode();
1181
1182     LayoutUnit leftWidth = (!horizontal || includeLogicalLeftEdge) ? borderLeftWidth() : 0;
1183     LayoutUnit rightWidth = (!horizontal || includeLogicalRightEdge) ? borderRightWidth() : 0;
1184     LayoutUnit topWidth = (horizontal || includeLogicalLeftEdge) ? borderTopWidth() : 0;
1185     LayoutUnit bottomWidth = (horizontal || includeLogicalRightEdge) ? borderBottomWidth() : 0;
1186
1187     return getRoundedInnerBorderFor(borderRect, topWidth, bottomWidth, leftWidth, rightWidth, includeLogicalLeftEdge, includeLogicalRightEdge);
1188 }
1189
1190 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect, LayoutUnit topWidth, LayoutUnit bottomWidth,
1191     LayoutUnit leftWidth, LayoutUnit rightWidth, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1192 {
1193     LayoutRect innerRect(borderRect.x() + leftWidth, 
1194                borderRect.y() + topWidth, 
1195                borderRect.width() - leftWidth - rightWidth, 
1196                borderRect.height() - topWidth - bottomWidth);
1197
1198     RoundedRect roundedRect(innerRect);
1199
1200     if (hasBorderRadius()) {
1201         RoundedRect::Radii radii = getRoundedBorderFor(borderRect).radii();
1202         radii.shrink(topWidth, bottomWidth, leftWidth, rightWidth);
1203         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
1204     }
1205     return roundedRect;
1206 }
1207
1208 static bool allLayersAreFixed(const FillLayer* layer)
1209 {
1210     bool allFixed = true;
1211     
1212     for (const FillLayer* currLayer = layer; currLayer; currLayer = currLayer->next())
1213         allFixed &= (currLayer->image() && currLayer->attachment() == FixedBackgroundAttachment);
1214
1215     return layer && allFixed;
1216 }
1217
1218 bool RenderStyle::hasEntirelyFixedBackground() const
1219 {
1220     return allLayersAreFixed(backgroundLayers());
1221 }
1222
1223 const CounterDirectiveMap* RenderStyle::counterDirectives() const
1224 {
1225     return rareNonInheritedData->m_counterDirectives.get();
1226 }
1227
1228 CounterDirectiveMap& RenderStyle::accessCounterDirectives()
1229 {
1230     auto& map = rareNonInheritedData.access()->m_counterDirectives;
1231     if (!map)
1232         map = std::make_unique<CounterDirectiveMap>();
1233     return *map;
1234 }
1235
1236 const CounterDirectives RenderStyle::getCounterDirectives(const AtomicString& identifier) const
1237 {
1238     if (const CounterDirectiveMap* directives = counterDirectives())
1239         return directives->get(identifier);
1240     return CounterDirectives();
1241 }
1242
1243 const AtomicString& RenderStyle::hyphenString() const
1244 {
1245     ASSERT(hyphens() != HyphensNone);
1246
1247     const AtomicString& hyphenationString = rareInheritedData.get()->hyphenationString;
1248     if (!hyphenationString.isNull())
1249         return hyphenationString;
1250
1251     // FIXME: This should depend on locale.
1252     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, hyphenMinusString, (&hyphenMinus, 1));
1253     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, hyphenString, (&hyphen, 1));
1254     return fontCascade().primaryFont().glyphForCharacter(hyphen) ? hyphenString : hyphenMinusString;
1255 }
1256
1257 const AtomicString& RenderStyle::textEmphasisMarkString() const
1258 {
1259     switch (textEmphasisMark()) {
1260     case TextEmphasisMarkNone:
1261         return nullAtom;
1262     case TextEmphasisMarkCustom:
1263         return textEmphasisCustomMark();
1264     case TextEmphasisMarkDot: {
1265         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledDotString, (&bullet, 1));
1266         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openDotString, (&whiteBullet, 1));
1267         return textEmphasisFill() == TextEmphasisFillFilled ? filledDotString : openDotString;
1268     }
1269     case TextEmphasisMarkCircle: {
1270         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledCircleString, (&blackCircle, 1));
1271         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openCircleString, (&whiteCircle, 1));
1272         return textEmphasisFill() == TextEmphasisFillFilled ? filledCircleString : openCircleString;
1273     }
1274     case TextEmphasisMarkDoubleCircle: {
1275         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledDoubleCircleString, (&fisheye, 1));
1276         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openDoubleCircleString, (&bullseye, 1));
1277         return textEmphasisFill() == TextEmphasisFillFilled ? filledDoubleCircleString : openDoubleCircleString;
1278     }
1279     case TextEmphasisMarkTriangle: {
1280         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledTriangleString, (&blackUpPointingTriangle, 1));
1281         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openTriangleString, (&whiteUpPointingTriangle, 1));
1282         return textEmphasisFill() == TextEmphasisFillFilled ? filledTriangleString : openTriangleString;
1283     }
1284     case TextEmphasisMarkSesame: {
1285         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledSesameString, (&sesameDot, 1));
1286         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openSesameString, (&whiteSesameDot, 1));
1287         return textEmphasisFill() == TextEmphasisFillFilled ? filledSesameString : openSesameString;
1288     }
1289     case TextEmphasisMarkAuto:
1290         ASSERT_NOT_REACHED();
1291         return nullAtom;
1292     }
1293
1294     ASSERT_NOT_REACHED();
1295     return nullAtom;
1296 }
1297
1298 #if ENABLE(DASHBOARD_SUPPORT)
1299 const Vector<StyleDashboardRegion>& RenderStyle::initialDashboardRegions()
1300 {
1301     DEPRECATED_DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, emptyList, ());
1302     return emptyList;
1303 }
1304
1305 const Vector<StyleDashboardRegion>& RenderStyle::noneDashboardRegions()
1306 {
1307     DEPRECATED_DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, noneList, ());
1308     static bool noneListInitialized = false;
1309
1310     if (!noneListInitialized) {
1311         StyleDashboardRegion region;
1312         region.label = "";
1313         region.offset.top()  = Length();
1314         region.offset.right() = Length();
1315         region.offset.bottom() = Length();
1316         region.offset.left() = Length();
1317         region.type = StyleDashboardRegion::None;
1318         noneList.append(region);
1319         noneListInitialized = true;
1320     }
1321     return noneList;
1322 }
1323 #endif
1324
1325 void RenderStyle::adjustAnimations()
1326 {
1327     AnimationList* animationList = rareNonInheritedData->m_animations.get();
1328     if (!animationList)
1329         return;
1330
1331     // Get rid of empty animations and anything beyond them
1332     for (size_t i = 0; i < animationList->size(); ++i) {
1333         if (animationList->animation(i).isEmpty()) {
1334             animationList->resize(i);
1335             break;
1336         }
1337     }
1338
1339     if (animationList->isEmpty()) {
1340         clearAnimations();
1341         return;
1342     }
1343
1344     // Repeat patterns into layers that don't have some properties set.
1345     animationList->fillUnsetProperties();
1346 }
1347
1348 void RenderStyle::adjustTransitions()
1349 {
1350     AnimationList* transitionList = rareNonInheritedData->m_transitions.get();
1351     if (!transitionList)
1352         return;
1353
1354     // Get rid of empty transitions and anything beyond them
1355     for (size_t i = 0; i < transitionList->size(); ++i) {
1356         if (transitionList->animation(i).isEmpty()) {
1357             transitionList->resize(i);
1358             break;
1359         }
1360     }
1361
1362     if (transitionList->isEmpty()) {
1363         clearTransitions();
1364         return;
1365     }
1366
1367     // Repeat patterns into layers that don't have some properties set.
1368     transitionList->fillUnsetProperties();
1369
1370     // Make sure there are no duplicate properties. This is an O(n^2) algorithm
1371     // but the lists tend to be very short, so it is probably ok
1372     for (size_t i = 0; i < transitionList->size(); ++i) {
1373         for (size_t j = i+1; j < transitionList->size(); ++j) {
1374             if (transitionList->animation(i).property() == transitionList->animation(j).property()) {
1375                 // toss i
1376                 transitionList->remove(i);
1377                 j = i;
1378             }
1379         }
1380     }
1381 }
1382
1383 AnimationList& RenderStyle::ensureAnimations()
1384 {
1385     if (!rareNonInheritedData.access()->m_animations)
1386         rareNonInheritedData.access()->m_animations = std::make_unique<AnimationList>();
1387     return *rareNonInheritedData->m_animations;
1388 }
1389
1390 AnimationList& RenderStyle::ensureTransitions()
1391 {
1392     if (!rareNonInheritedData.access()->m_transitions)
1393         rareNonInheritedData.access()->m_transitions = std::make_unique<AnimationList>();
1394     return *rareNonInheritedData->m_transitions;
1395 }
1396
1397 const Animation* RenderStyle::transitionForProperty(CSSPropertyID property) const
1398 {
1399     if (transitions()) {
1400         for (size_t i = 0; i < transitions()->size(); ++i) {
1401             const Animation& p = transitions()->animation(i);
1402             if (p.animationMode() == Animation::AnimateAll || p.property() == property) {
1403                 return &p;
1404             }
1405         }
1406     }
1407     return 0;
1408 }
1409
1410 const FontCascade& RenderStyle::fontCascade() const { return inherited->fontCascade; }
1411 const FontMetrics& RenderStyle::fontMetrics() const { return inherited->fontCascade.fontMetrics(); }
1412 const FontCascadeDescription& RenderStyle::fontDescription() const { return inherited->fontCascade.fontDescription(); }
1413 float RenderStyle::specifiedFontSize() const { return fontDescription().specifiedSize(); }
1414 float RenderStyle::computedFontSize() const { return fontDescription().computedSize(); }
1415 int RenderStyle::fontSize() const { return inherited->fontCascade.pixelSize(); }
1416
1417 const Length& RenderStyle::wordSpacing() const { return rareInheritedData->wordSpacing; }
1418 float RenderStyle::letterSpacing() const { return inherited->fontCascade.letterSpacing(); }
1419
1420 bool RenderStyle::setFontDescription(const FontCascadeDescription& v)
1421 {
1422     if (inherited->fontCascade.fontDescription() != v) {
1423         inherited.access()->fontCascade = FontCascade(v, inherited->fontCascade.letterSpacing(), inherited->fontCascade.wordSpacing());
1424         return true;
1425     }
1426     return false;
1427 }
1428
1429 #if ENABLE(IOS_TEXT_AUTOSIZING)
1430 const Length& RenderStyle::specifiedLineHeight() const { return inherited->specifiedLineHeight; }
1431 void RenderStyle::setSpecifiedLineHeight(Length v) { SET_VAR(inherited, specifiedLineHeight, v); }
1432 #else
1433 const Length& RenderStyle::specifiedLineHeight() const { return inherited->line_height; }
1434 #endif
1435
1436 Length RenderStyle::lineHeight() const
1437 {
1438     const Length& lh = inherited->line_height;
1439 #if ENABLE(TEXT_AUTOSIZING)
1440     // Unlike fontDescription().computedSize() and hence fontSize(), this is
1441     // recalculated on demand as we only store the specified line height.
1442     // FIXME: Should consider scaling the fixed part of any calc expressions
1443     // too, though this involves messily poking into CalcExpressionLength.
1444     float multiplier = textAutosizingMultiplier();
1445     if (multiplier > 1 && lh.isFixed())
1446         return Length(TextAutosizer::computeAutosizedFontSize(lh.value(), multiplier), Fixed);
1447 #endif
1448     return lh;
1449 }
1450 void RenderStyle::setLineHeight(Length specifiedLineHeight) { SET_VAR(inherited, line_height, specifiedLineHeight); }
1451
1452 int RenderStyle::computedLineHeight() const
1453 {
1454     const Length& lh = lineHeight();
1455
1456     // Negative value means the line height is not set. Use the font's built-in spacing.
1457     if (lh.isNegative())
1458         return fontMetrics().lineSpacing();
1459
1460     if (lh.isPercentOrCalculated())
1461         return minimumValueForLength(lh, fontSize());
1462
1463     return clampTo<int>(lh.value());
1464 }
1465
1466 void RenderStyle::setWordSpacing(Length value)
1467 {
1468     float fontWordSpacing;
1469     switch (value.type()) {
1470     case Auto:
1471         fontWordSpacing = 0;
1472         break;
1473     case Percent:
1474         fontWordSpacing = value.percent() * fontCascade().spaceWidth() / 100;
1475         break;
1476     case Fixed:
1477         fontWordSpacing = value.value();
1478         break;
1479     case Calculated:
1480         fontWordSpacing = value.nonNanCalculatedValue(maxValueForCssLength);
1481         break;
1482     default:
1483         ASSERT_NOT_REACHED();
1484         fontWordSpacing = 0;
1485         break;
1486     }
1487     inherited.access()->fontCascade.setWordSpacing(fontWordSpacing);
1488     rareInheritedData.access()->wordSpacing = WTF::move(value);
1489 }
1490
1491 void RenderStyle::setLetterSpacing(float v) { inherited.access()->fontCascade.setLetterSpacing(v); }
1492
1493 void RenderStyle::setFontSize(float size)
1494 {
1495     // size must be specifiedSize if Text Autosizing is enabled, but computedSize if text
1496     // zoom is enabled (if neither is enabled it's irrelevant as they're probably the same).
1497
1498     ASSERT(std::isfinite(size));
1499     if (!std::isfinite(size) || size < 0)
1500         size = 0;
1501     else
1502         size = std::min(maximumAllowedFontSize, size);
1503
1504     FontSelector* currentFontSelector = fontCascade().fontSelector();
1505     auto description = fontDescription();
1506     description.setSpecifiedSize(size);
1507     description.setComputedSize(size);
1508
1509 #if ENABLE(TEXT_AUTOSIZING)
1510     float multiplier = textAutosizingMultiplier();
1511     if (multiplier > 1) {
1512         float autosizedFontSize = TextAutosizer::computeAutosizedFontSize(size, multiplier);
1513         description.setComputedSize(min(maximumAllowedFontSize, autosizedFontSize));
1514     }
1515 #endif
1516
1517     setFontDescription(description);
1518     fontCascade().update(currentFontSelector);
1519 }
1520
1521 void RenderStyle::getShadowExtent(const ShadowData* shadow, LayoutUnit &top, LayoutUnit &right, LayoutUnit &bottom, LayoutUnit &left) const
1522 {
1523     top = 0;
1524     right = 0;
1525     bottom = 0;
1526     left = 0;
1527
1528     for ( ; shadow; shadow = shadow->next()) {
1529         if (shadow->style() == Inset)
1530             continue;
1531
1532         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1533         top = std::min<LayoutUnit>(top, shadow->y() - extentAndSpread);
1534         right = std::max<LayoutUnit>(right, shadow->x() + extentAndSpread);
1535         bottom = std::max<LayoutUnit>(bottom, shadow->y() + extentAndSpread);
1536         left = std::min<LayoutUnit>(left, shadow->x() - extentAndSpread);
1537     }
1538 }
1539
1540 LayoutBoxExtent RenderStyle::getShadowInsetExtent(const ShadowData* shadow) const
1541 {
1542     LayoutUnit top = 0;
1543     LayoutUnit right = 0;
1544     LayoutUnit bottom = 0;
1545     LayoutUnit left = 0;
1546
1547     for ( ; shadow; shadow = shadow->next()) {
1548         if (shadow->style() == Normal)
1549             continue;
1550
1551         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1552         top = std::max<LayoutUnit>(top, shadow->y() + extentAndSpread);
1553         right = std::min<LayoutUnit>(right, shadow->x() - extentAndSpread);
1554         bottom = std::min<LayoutUnit>(bottom, shadow->y() - extentAndSpread);
1555         left = std::max<LayoutUnit>(left, shadow->x() + extentAndSpread);
1556     }
1557
1558     return LayoutBoxExtent(top, right, bottom, left);
1559 }
1560
1561 void RenderStyle::getShadowHorizontalExtent(const ShadowData* shadow, LayoutUnit &left, LayoutUnit &right) const
1562 {
1563     left = 0;
1564     right = 0;
1565
1566     for ( ; shadow; shadow = shadow->next()) {
1567         if (shadow->style() == Inset)
1568             continue;
1569
1570         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1571         left = std::min<LayoutUnit>(left, shadow->x() - extentAndSpread);
1572         right = std::max<LayoutUnit>(right, shadow->x() + extentAndSpread);
1573     }
1574 }
1575
1576 void RenderStyle::getShadowVerticalExtent(const ShadowData* shadow, LayoutUnit &top, LayoutUnit &bottom) const
1577 {
1578     top = 0;
1579     bottom = 0;
1580
1581     for ( ; shadow; shadow = shadow->next()) {
1582         if (shadow->style() == Inset)
1583             continue;
1584
1585         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1586         top = std::min<LayoutUnit>(top, shadow->y() - extentAndSpread);
1587         bottom = std::max<LayoutUnit>(bottom, shadow->y() + extentAndSpread);
1588     }
1589 }
1590
1591 Color RenderStyle::colorIncludingFallback(int colorProperty, bool visitedLink) const
1592 {
1593     Color result;
1594     EBorderStyle borderStyle = BNONE;
1595     switch (colorProperty) {
1596     case CSSPropertyBackgroundColor:
1597         return visitedLink ? visitedLinkBackgroundColor() : backgroundColor(); // Background color doesn't fall back.
1598     case CSSPropertyBorderLeftColor:
1599         result = visitedLink ? visitedLinkBorderLeftColor() : borderLeftColor();
1600         borderStyle = borderLeftStyle();
1601         break;
1602     case CSSPropertyBorderRightColor:
1603         result = visitedLink ? visitedLinkBorderRightColor() : borderRightColor();
1604         borderStyle = borderRightStyle();
1605         break;
1606     case CSSPropertyBorderTopColor:
1607         result = visitedLink ? visitedLinkBorderTopColor() : borderTopColor();
1608         borderStyle = borderTopStyle();
1609         break;
1610     case CSSPropertyBorderBottomColor:
1611         result = visitedLink ? visitedLinkBorderBottomColor() : borderBottomColor();
1612         borderStyle = borderBottomStyle();
1613         break;
1614     case CSSPropertyColor:
1615         result = visitedLink ? visitedLinkColor() : color();
1616         break;
1617     case CSSPropertyOutlineColor:
1618         result = visitedLink ? visitedLinkOutlineColor() : outlineColor();
1619         break;
1620     case CSSPropertyColumnRuleColor:
1621         result = visitedLink ? visitedLinkColumnRuleColor() : columnRuleColor();
1622         break;
1623     case CSSPropertyWebkitTextDecorationColor:
1624         // Text decoration color fallback is handled in RenderObject::decorationColor.
1625         return visitedLink ? visitedLinkTextDecorationColor() : textDecorationColor();
1626     case CSSPropertyWebkitTextEmphasisColor:
1627         result = visitedLink ? visitedLinkTextEmphasisColor() : textEmphasisColor();
1628         break;
1629     case CSSPropertyWebkitTextFillColor:
1630         result = visitedLink ? visitedLinkTextFillColor() : textFillColor();
1631         break;
1632     case CSSPropertyWebkitTextStrokeColor:
1633         result = visitedLink ? visitedLinkTextStrokeColor() : textStrokeColor();
1634         break;
1635     default:
1636         ASSERT_NOT_REACHED();
1637         break;
1638     }
1639
1640     if (!result.isValid()) {
1641         if (!visitedLink && (borderStyle == INSET || borderStyle == OUTSET || borderStyle == RIDGE || borderStyle == GROOVE))
1642             result.setRGB(238, 238, 238);
1643         else
1644             result = visitedLink ? visitedLinkColor() : color();
1645     }
1646     return result;
1647 }
1648
1649 Color RenderStyle::visitedDependentColor(int colorProperty) const
1650 {
1651     Color unvisitedColor = colorIncludingFallback(colorProperty, false);
1652     if (insideLink() != InsideVisitedLink)
1653         return unvisitedColor;
1654
1655     Color visitedColor = colorIncludingFallback(colorProperty, true);
1656
1657     // Text decoration color validity is preserved (checked in RenderObject::decorationColor).
1658     if (colorProperty == CSSPropertyWebkitTextDecorationColor)
1659         return visitedColor;
1660
1661     // FIXME: Technically someone could explicitly specify the color transparent, but for now we'll just
1662     // assume that if the background color is transparent that it wasn't set. Note that it's weird that
1663     // we're returning unvisited info for a visited link, but given our restriction that the alpha values
1664     // have to match, it makes more sense to return the unvisited background color if specified than it
1665     // does to return black. This behavior matches what Firefox 4 does as well.
1666     if (colorProperty == CSSPropertyBackgroundColor && visitedColor == Color::transparent)
1667         return unvisitedColor;
1668
1669     // Take the alpha from the unvisited color, but get the RGB values from the visited color.
1670     return Color(visitedColor.red(), visitedColor.green(), visitedColor.blue(), unvisitedColor.alpha());
1671 }
1672
1673 const BorderValue& RenderStyle::borderBefore() const
1674 {
1675     switch (writingMode()) {
1676     case TopToBottomWritingMode:
1677         return borderTop();
1678     case BottomToTopWritingMode:
1679         return borderBottom();
1680     case LeftToRightWritingMode:
1681         return borderLeft();
1682     case RightToLeftWritingMode:
1683         return borderRight();
1684     }
1685     ASSERT_NOT_REACHED();
1686     return borderTop();
1687 }
1688
1689 const BorderValue& RenderStyle::borderAfter() const
1690 {
1691     switch (writingMode()) {
1692     case TopToBottomWritingMode:
1693         return borderBottom();
1694     case BottomToTopWritingMode:
1695         return borderTop();
1696     case LeftToRightWritingMode:
1697         return borderRight();
1698     case RightToLeftWritingMode:
1699         return borderLeft();
1700     }
1701     ASSERT_NOT_REACHED();
1702     return borderBottom();
1703 }
1704
1705 const BorderValue& RenderStyle::borderStart() const
1706 {
1707     if (isHorizontalWritingMode())
1708         return isLeftToRightDirection() ? borderLeft() : borderRight();
1709     return isLeftToRightDirection() ? borderTop() : borderBottom();
1710 }
1711
1712 const BorderValue& RenderStyle::borderEnd() const
1713 {
1714     if (isHorizontalWritingMode())
1715         return isLeftToRightDirection() ? borderRight() : borderLeft();
1716     return isLeftToRightDirection() ? borderBottom() : borderTop();
1717 }
1718
1719 float RenderStyle::borderBeforeWidth() const
1720 {
1721     switch (writingMode()) {
1722     case TopToBottomWritingMode:
1723         return borderTopWidth();
1724     case BottomToTopWritingMode:
1725         return borderBottomWidth();
1726     case LeftToRightWritingMode:
1727         return borderLeftWidth();
1728     case RightToLeftWritingMode:
1729         return borderRightWidth();
1730     }
1731     ASSERT_NOT_REACHED();
1732     return borderTopWidth();
1733 }
1734
1735 float RenderStyle::borderAfterWidth() const
1736 {
1737     switch (writingMode()) {
1738     case TopToBottomWritingMode:
1739         return borderBottomWidth();
1740     case BottomToTopWritingMode:
1741         return borderTopWidth();
1742     case LeftToRightWritingMode:
1743         return borderRightWidth();
1744     case RightToLeftWritingMode:
1745         return borderLeftWidth();
1746     }
1747     ASSERT_NOT_REACHED();
1748     return borderBottomWidth();
1749 }
1750
1751 float RenderStyle::borderStartWidth() const
1752 {
1753     if (isHorizontalWritingMode())
1754         return isLeftToRightDirection() ? borderLeftWidth() : borderRightWidth();
1755     return isLeftToRightDirection() ? borderTopWidth() : borderBottomWidth();
1756 }
1757
1758 float RenderStyle::borderEndWidth() const
1759 {
1760     if (isHorizontalWritingMode())
1761         return isLeftToRightDirection() ? borderRightWidth() : borderLeftWidth();
1762     return isLeftToRightDirection() ? borderBottomWidth() : borderTopWidth();
1763 }
1764
1765 void RenderStyle::setMarginStart(Length margin)
1766 {
1767     if (isHorizontalWritingMode()) {
1768         if (isLeftToRightDirection())
1769             setMarginLeft(margin);
1770         else
1771             setMarginRight(margin);
1772     } else {
1773         if (isLeftToRightDirection())
1774             setMarginTop(margin);
1775         else
1776             setMarginBottom(margin);
1777     }
1778 }
1779
1780 void RenderStyle::setMarginEnd(Length margin)
1781 {
1782     if (isHorizontalWritingMode()) {
1783         if (isLeftToRightDirection())
1784             setMarginRight(margin);
1785         else
1786             setMarginLeft(margin);
1787     } else {
1788         if (isLeftToRightDirection())
1789             setMarginBottom(margin);
1790         else
1791             setMarginTop(margin);
1792     }
1793 }
1794
1795 TextEmphasisMark RenderStyle::textEmphasisMark() const
1796 {
1797     TextEmphasisMark mark = static_cast<TextEmphasisMark>(rareInheritedData->textEmphasisMark);
1798     if (mark != TextEmphasisMarkAuto)
1799         return mark;
1800
1801     if (isHorizontalWritingMode())
1802         return TextEmphasisMarkDot;
1803
1804     return TextEmphasisMarkSesame;
1805 }
1806
1807 #if ENABLE(TOUCH_EVENTS)
1808 Color RenderStyle::initialTapHighlightColor()
1809 {
1810     return RenderTheme::tapHighlightColor();
1811 }
1812 #endif
1813
1814 LayoutBoxExtent RenderStyle::imageOutsets(const NinePieceImage& image) const
1815 {
1816     return LayoutBoxExtent(NinePieceImage::computeOutset(image.outset().top(), borderTopWidth()),
1817                            NinePieceImage::computeOutset(image.outset().right(), borderRightWidth()),
1818                            NinePieceImage::computeOutset(image.outset().bottom(), borderBottomWidth()),
1819                            NinePieceImage::computeOutset(image.outset().left(), borderLeftWidth()));
1820 }
1821
1822 std::pair<FontOrientation, NonCJKGlyphOrientation> RenderStyle::fontAndGlyphOrientation()
1823 {
1824     // FIXME: TextOrientationSideways should map to sideways-left in vertical-lr, which is not supported yet.
1825
1826     if (isHorizontalWritingMode())
1827         return { Horizontal, NonCJKGlyphOrientation::Mixed };
1828
1829     switch (textOrientation()) {
1830     case TextOrientation::Mixed:
1831         return { Vertical, NonCJKGlyphOrientation::Mixed };
1832     case TextOrientation::Upright:
1833         return { Vertical, NonCJKGlyphOrientation::Upright };
1834     case TextOrientation::Sideways:
1835         return { Horizontal, NonCJKGlyphOrientation::Mixed };
1836     default:
1837         ASSERT_NOT_REACHED();
1838         return { Horizontal, NonCJKGlyphOrientation::Mixed };
1839     }
1840 }
1841
1842 void RenderStyle::setBorderImageSource(PassRefPtr<StyleImage> image)
1843 {
1844     if (surround->border.m_image.image() == image.get())
1845         return;
1846     surround.access()->border.m_image.setImage(image);
1847 }
1848
1849 void RenderStyle::setBorderImageSlices(LengthBox slices)
1850 {
1851     if (surround->border.m_image.imageSlices() == slices)
1852         return;
1853     surround.access()->border.m_image.setImageSlices(slices);
1854 }
1855
1856 void RenderStyle::setBorderImageWidth(LengthBox slices)
1857 {
1858     if (surround->border.m_image.borderSlices() == slices)
1859         return;
1860     surround.access()->border.m_image.setBorderSlices(slices);
1861 }
1862
1863 void RenderStyle::setBorderImageOutset(LengthBox outset)
1864 {
1865     if (surround->border.m_image.outset() == outset)
1866         return;
1867     surround.access()->border.m_image.setOutset(outset);
1868 }
1869
1870 void RenderStyle::setColumnStylesFromPaginationMode(const Pagination::Mode& paginationMode)
1871 {
1872     if (paginationMode == Pagination::Unpaginated)
1873         return;
1874     
1875     setColumnFill(ColumnFillAuto);
1876     
1877     switch (paginationMode) {
1878     case Pagination::LeftToRightPaginated:
1879         setColumnAxis(HorizontalColumnAxis);
1880         if (isHorizontalWritingMode())
1881             setColumnProgression(isLeftToRightDirection() ? NormalColumnProgression : ReverseColumnProgression);
1882         else
1883             setColumnProgression(isFlippedBlocksWritingMode() ? ReverseColumnProgression : NormalColumnProgression);
1884         break;
1885     case Pagination::RightToLeftPaginated:
1886         setColumnAxis(HorizontalColumnAxis);
1887         if (isHorizontalWritingMode())
1888             setColumnProgression(isLeftToRightDirection() ? ReverseColumnProgression : NormalColumnProgression);
1889         else
1890             setColumnProgression(isFlippedBlocksWritingMode() ? NormalColumnProgression : ReverseColumnProgression);
1891         break;
1892     case Pagination::TopToBottomPaginated:
1893         setColumnAxis(VerticalColumnAxis);
1894         if (isHorizontalWritingMode())
1895             setColumnProgression(isFlippedBlocksWritingMode() ? ReverseColumnProgression : NormalColumnProgression);
1896         else
1897             setColumnProgression(isLeftToRightDirection() ? NormalColumnProgression : ReverseColumnProgression);
1898         break;
1899     case Pagination::BottomToTopPaginated:
1900         setColumnAxis(VerticalColumnAxis);
1901         if (isHorizontalWritingMode())
1902             setColumnProgression(isFlippedBlocksWritingMode() ? NormalColumnProgression : ReverseColumnProgression);
1903         else
1904             setColumnProgression(isLeftToRightDirection() ? ReverseColumnProgression : NormalColumnProgression);
1905         break;
1906     case Pagination::Unpaginated:
1907         ASSERT_NOT_REACHED();
1908         break;
1909     }
1910 }
1911
1912 #if ENABLE(CSS_SCROLL_SNAP)
1913 LengthSize RenderStyle::initialScrollSnapDestination()
1914 {
1915     return defaultScrollSnapDestination();
1916 }
1917
1918 Vector<LengthSize> RenderStyle::initialScrollSnapCoordinates()
1919 {
1920     return Vector<LengthSize>();
1921 }
1922
1923 const ScrollSnapPoints* RenderStyle::scrollSnapPointsX() const
1924 {
1925     return rareNonInheritedData->m_scrollSnapPoints->xPoints.get();
1926 }
1927
1928 const ScrollSnapPoints* RenderStyle::scrollSnapPointsY() const
1929 {
1930     return rareNonInheritedData->m_scrollSnapPoints->yPoints.get();
1931 }
1932
1933 const LengthSize& RenderStyle::scrollSnapDestination() const
1934 {
1935     return rareNonInheritedData->m_scrollSnapPoints->destination;
1936 }
1937
1938 const Vector<LengthSize>& RenderStyle::scrollSnapCoordinates() const
1939 {
1940     return rareNonInheritedData->m_scrollSnapPoints->coordinates;
1941 }
1942
1943 void RenderStyle::setScrollSnapPointsX(std::unique_ptr<ScrollSnapPoints> points)
1944 {
1945     if (rareNonInheritedData->m_scrollSnapPoints->xPoints.get() == points.get())
1946         return;
1947     rareNonInheritedData.access()->m_scrollSnapPoints.access()->xPoints = WTF::move(points);
1948 }
1949
1950 void RenderStyle::setScrollSnapPointsY(std::unique_ptr<ScrollSnapPoints> points)
1951 {
1952     if (rareNonInheritedData->m_scrollSnapPoints->yPoints.get() == points.get())
1953         return;
1954     rareNonInheritedData.access()->m_scrollSnapPoints.access()->yPoints = WTF::move(points);
1955 }
1956
1957 void RenderStyle::setScrollSnapDestination(LengthSize destination)
1958 {
1959     if (rareNonInheritedData->m_scrollSnapPoints->destination == destination)
1960         return;
1961     rareNonInheritedData.access()->m_scrollSnapPoints.access()->destination = WTF::move(destination);
1962 }
1963
1964 void RenderStyle::setScrollSnapCoordinates(Vector<LengthSize> coordinates)
1965 {
1966     if (rareNonInheritedData->m_scrollSnapPoints->coordinates == coordinates)
1967         return;
1968     rareNonInheritedData.access()->m_scrollSnapPoints.access()->coordinates = WTF::move(coordinates);
1969 }
1970
1971 #endif
1972
1973 void RenderStyle::checkVariablesInCustomProperties()
1974 {
1975     if (!rareInheritedData->m_customProperties->containsVariables())
1976         return;
1977     
1978     // Our first pass checks the variables for validity and replaces any properties that became
1979     // invalid with empty values.
1980     auto& customProperties = rareInheritedData.access()->m_customProperties.access()->values();
1981     HashSet<AtomicString> invalidProperties;
1982     for (auto entry : customProperties) {
1983         if (!entry.value->isVariableDependentValue())
1984             continue;
1985         HashSet<AtomicString> seenProperties;
1986         downcast<CSSVariableDependentValue>(*entry.value).checkVariablesForCycles(entry.key, customProperties, seenProperties, invalidProperties);
1987     }
1988     
1989     // Now insert invalid values.
1990     if (!invalidProperties.isEmpty()) {
1991         RefPtr<CSSValue> invalidValue = CSSCustomPropertyValue::createInvalid();
1992         for (auto& property : invalidProperties)
1993             customProperties.set(property, invalidValue);
1994     }
1995
1996     // Now that all of the properties have been tested for validity and replaced with
1997     // invalid values if they failed, we can perform variable substitution on the valid values.
1998     Vector<RefPtr<CSSCustomPropertyValue>> resolvedValues;
1999     for (auto entry : customProperties) {
2000         if (!entry.value->isVariableDependentValue())
2001             continue;
2002         
2003         CSSParserValueList parserList;
2004         RefPtr<CSSCustomPropertyValue> result;
2005         if (!downcast<CSSVariableDependentValue>(*entry.value).valueList()->buildParserValueListSubstitutingVariables(&parserList, customProperties)) {
2006             RefPtr<CSSValue> invalidResult = CSSCustomPropertyValue::createInvalid();
2007             result = CSSCustomPropertyValue::create(entry.key, invalidResult);
2008         } else {
2009             RefPtr<CSSValue> newValueList = CSSValueList::createFromParserValueList(parserList);
2010             result = CSSCustomPropertyValue::create(entry.key, newValueList);
2011         }
2012         resolvedValues.append(result);
2013     }
2014     
2015     // With all results computed, we can now mutate our table to eliminate the variables and
2016     // hold the final values. This way when we inherit, we don't end up resubstituting variables, etc.
2017     for (auto& resolvedValue : resolvedValues)
2018         customProperties.set(resolvedValue->name(), resolvedValue->value());
2019
2020     rareInheritedData.access()->m_customProperties.access()->setContainsVariables(false);
2021 }
2022
2023 } // namespace WebCore