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