Optimize RenderStyle::diff() and clean up the code
[WebKit-https.git] / Source / WebCore / rendering / style / RenderStyle.cpp
1 /*
2  * Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
3  * Copyright (C) 2004-2017 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     Color m_color;
62     float m_width;
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     } m_inheritedFlags;
75
76     struct NonInheritedFlags {
77         unsigned m_bitfields[2];
78     } m_nonInheritedFlags;
79 #if !ASSERT_DISABLED || ENABLE(SECURITY_ASSERTIONS)
80     bool deletionCheck;
81 #endif
82 };
83
84 static_assert(sizeof(RenderStyle) == sizeof(SameSizeAsRenderStyle), "RenderStyle should stay small");
85
86 RenderStyle& RenderStyle::defaultStyle()
87 {
88     static NeverDestroyed<RenderStyle> style { 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, DisplayType 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() == PseudoId::Before || pseudoStyle.styleType() == PseudoId::After);
124
125     auto style = create();
126     style.inheritFrom(pseudoStyle);
127     return style;
128 }
129
130 RenderStyle::RenderStyle(RenderStyle&&) = default;
131 RenderStyle& RenderStyle::operator=(RenderStyle&&) = default;
132
133 RenderStyle::RenderStyle(CreateDefaultStyleTag)
134     : m_boxData(StyleBoxData::create())
135     , m_visualData(StyleVisualData::create())
136     , m_backgroundData(StyleBackgroundData::create())
137     , m_surroundData(StyleSurroundData::create())
138     , m_rareNonInheritedData(StyleRareNonInheritedData::create())
139     , m_rareInheritedData(StyleRareInheritedData::create())
140     , m_inheritedData(StyleInheritedData::create())
141     , m_svgStyle(SVGRenderStyle::create())
142 {
143     m_inheritedFlags.emptyCells = static_cast<unsigned>(initialEmptyCells());
144     m_inheritedFlags.captionSide = static_cast<unsigned>(initialCaptionSide());
145     m_inheritedFlags.listStyleType = static_cast<unsigned>(initialListStyleType());
146     m_inheritedFlags.listStylePosition = static_cast<unsigned>(initialListStylePosition());
147     m_inheritedFlags.visibility = static_cast<unsigned>(initialVisibility());
148     m_inheritedFlags.textAlign = static_cast<unsigned>(initialTextAlign());
149     m_inheritedFlags.textTransform = static_cast<unsigned>(initialTextTransform());
150     m_inheritedFlags.textDecorations = initialTextDecoration().toRaw();
151     m_inheritedFlags.cursor = static_cast<unsigned>(initialCursor());
152 #if ENABLE(CURSOR_VISIBILITY)
153     m_inheritedFlags.cursorVisibility = static_cast<unsigned>(initialCursorVisibility());
154 #endif
155     m_inheritedFlags.direction = static_cast<unsigned>(initialDirection());
156     m_inheritedFlags.whiteSpace = static_cast<unsigned>(initialWhiteSpace());
157     m_inheritedFlags.borderCollapse = static_cast<unsigned>(initialBorderCollapse());
158     m_inheritedFlags.rtlOrdering = static_cast<unsigned>(initialRTLOrdering());
159     m_inheritedFlags.boxDirection = static_cast<unsigned>(initialBoxDirection());
160     m_inheritedFlags.printColorAdjust = static_cast<unsigned>(initialPrintColorAdjust());
161     m_inheritedFlags.pointerEvents = static_cast<unsigned>(initialPointerEvents());
162     m_inheritedFlags.insideLink = static_cast<unsigned>(InsideLink::NotInside);
163     m_inheritedFlags.insideDefaultButton = false;
164     m_inheritedFlags.writingMode = initialWritingMode();
165
166     m_nonInheritedFlags.effectiveDisplay = static_cast<unsigned>(initialDisplay());
167     m_nonInheritedFlags.originalDisplay = static_cast<unsigned>(initialDisplay());
168     m_nonInheritedFlags.overflowX = static_cast<unsigned>(initialOverflowX());
169     m_nonInheritedFlags.overflowY = static_cast<unsigned>(initialOverflowY());
170     m_nonInheritedFlags.verticalAlign = static_cast<unsigned>(initialVerticalAlign());
171     m_nonInheritedFlags.clear = static_cast<unsigned>(initialClear());
172     m_nonInheritedFlags.position = static_cast<unsigned>(initialPosition());
173     m_nonInheritedFlags.unicodeBidi = initialUnicodeBidi();
174     m_nonInheritedFlags.floating = static_cast<unsigned>(initialFloating());
175     m_nonInheritedFlags.tableLayout = static_cast<unsigned>(initialTableLayout());
176     m_nonInheritedFlags.hasExplicitlySetDirection = false;
177     m_nonInheritedFlags.hasExplicitlySetWritingMode = false;
178     m_nonInheritedFlags.hasExplicitlySetTextAlign = false;
179     m_nonInheritedFlags.hasViewportUnits = false;
180     m_nonInheritedFlags.hasExplicitlyInheritedProperties = false;
181     m_nonInheritedFlags.isUnique = false;
182     m_nonInheritedFlags.emptyState = false;
183     m_nonInheritedFlags.firstChildState = false;
184     m_nonInheritedFlags.lastChildState = false;
185     m_nonInheritedFlags.affectedByHover = false;
186     m_nonInheritedFlags.affectedByActive = false;
187     m_nonInheritedFlags.affectedByDrag = false;
188     m_nonInheritedFlags.isLink = false;
189     m_nonInheritedFlags.styleType = static_cast<unsigned>(PseudoId::None);
190     m_nonInheritedFlags.pseudoBits = static_cast<unsigned>(PseudoId::None);
191
192     static_assert((sizeof(InheritedFlags) <= 8), "InheritedFlags does not grow");
193     static_assert((sizeof(NonInheritedFlags) <= 8), "NonInheritedFlags does not grow");
194 }
195
196 inline RenderStyle::RenderStyle(const RenderStyle& other, CloneTag)
197     : m_boxData(other.m_boxData)
198     , m_visualData(other.m_visualData)
199     , m_backgroundData(other.m_backgroundData)
200     , m_surroundData(other.m_surroundData)
201     , m_rareNonInheritedData(other.m_rareNonInheritedData)
202     , m_nonInheritedFlags(other.m_nonInheritedFlags)
203     , m_rareInheritedData(other.m_rareInheritedData)
204     , m_inheritedData(other.m_inheritedData)
205     , m_inheritedFlags(other.m_inheritedFlags)
206     , m_svgStyle(other.m_svgStyle)
207 {
208 }
209
210 inline RenderStyle::RenderStyle(RenderStyle& a, RenderStyle&& b)
211     : m_boxData(a.m_boxData.replace(WTFMove(b.m_boxData)))
212     , m_visualData(a.m_visualData.replace(WTFMove(b.m_visualData)))
213     , m_backgroundData(a.m_backgroundData.replace(WTFMove(b.m_backgroundData)))
214     , m_surroundData(a.m_surroundData.replace(WTFMove(b.m_surroundData)))
215     , m_rareNonInheritedData(a.m_rareNonInheritedData.replace(WTFMove(b.m_rareNonInheritedData)))
216     , m_nonInheritedFlags(std::exchange(a.m_nonInheritedFlags, b.m_nonInheritedFlags))
217     , m_rareInheritedData(a.m_rareInheritedData.replace(WTFMove(b.m_rareInheritedData)))
218     , m_inheritedData(a.m_inheritedData.replace(WTFMove(b.m_inheritedData)))
219     , m_inheritedFlags(std::exchange(a.m_inheritedFlags, b.m_inheritedFlags))
220     , m_cachedPseudoStyles(std::exchange(a.m_cachedPseudoStyles, WTFMove(b.m_cachedPseudoStyles)))
221     , m_svgStyle(a.m_svgStyle.replace(WTFMove(b.m_svgStyle)))
222 {
223 }
224
225 RenderStyle::~RenderStyle()
226 {
227 #if !ASSERT_DISABLED || ENABLE(SECURITY_ASSERTIONS)
228     ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun);
229     m_deletionHasBegun = true;
230 #endif
231 }
232
233 RenderStyle RenderStyle::replace(RenderStyle&& newStyle)
234 {
235     return RenderStyle { *this, WTFMove(newStyle) };
236 }
237
238 static StyleSelfAlignmentData resolvedSelfAlignment(const StyleSelfAlignmentData& value, ItemPosition normalValueBehavior)
239 {
240     if (value.position() == ItemPosition::Legacy || value.position() == ItemPosition::Normal || value.position() == ItemPosition::Auto)
241         return { normalValueBehavior, OverflowAlignment::Default };
242     return value;
243 }
244
245 StyleSelfAlignmentData RenderStyle::resolvedAlignItems(ItemPosition normalValueBehaviour) const
246 {
247     return resolvedSelfAlignment(alignItems(), normalValueBehaviour);
248 }
249
250 StyleSelfAlignmentData RenderStyle::resolvedAlignSelf(const RenderStyle* parentStyle, ItemPosition normalValueBehaviour) const
251 {
252     // The auto keyword computes to the parent's align-items computed value.
253     // We will return the behaviour of 'normal' value if needed, which is specific of each layout model.
254     if (!parentStyle || alignSelf().position() != ItemPosition::Auto)
255         return resolvedSelfAlignment(alignSelf(), normalValueBehaviour);
256     return parentStyle->resolvedAlignItems(normalValueBehaviour);
257 }
258
259 StyleSelfAlignmentData RenderStyle::resolvedJustifyItems(ItemPosition normalValueBehaviour) const
260 {
261     return resolvedSelfAlignment(justifyItems(), normalValueBehaviour);
262 }
263
264 StyleSelfAlignmentData RenderStyle::resolvedJustifySelf(const RenderStyle* parentStyle, ItemPosition normalValueBehaviour) const
265 {
266     // The auto keyword computes to the parent's justify-items computed value.
267     // We will return the behaviour of 'normal' value if needed, which is specific of each layout model.
268     if (!parentStyle || justifySelf().position() != ItemPosition::Auto)
269         return resolvedSelfAlignment(justifySelf(), normalValueBehaviour);
270     return parentStyle->resolvedJustifyItems(normalValueBehaviour);
271 }
272
273 static inline StyleContentAlignmentData resolvedContentAlignment(const StyleContentAlignmentData& value, const StyleContentAlignmentData& normalValueBehavior)
274 {
275     return (value.position() == ContentPosition::Normal && value.distribution() == ContentDistribution::Default) ? normalValueBehavior : value;
276 }
277
278 StyleContentAlignmentData RenderStyle::resolvedAlignContent(const StyleContentAlignmentData& normalValueBehavior) const
279 {
280     // We will return the behaviour of 'normal' value if needed, which is specific of each layout model.
281     return resolvedContentAlignment(alignContent(), normalValueBehavior);
282 }
283
284 StyleContentAlignmentData RenderStyle::resolvedJustifyContent(const StyleContentAlignmentData& normalValueBehavior) const
285 {
286     // We will return the behaviour of 'normal' value if needed, which is specific of each layout model.
287     return resolvedContentAlignment(justifyContent(), normalValueBehavior);
288 }
289
290 static inline ContentPosition resolvedContentAlignmentPosition(const StyleContentAlignmentData& value, const StyleContentAlignmentData& normalValueBehavior)
291 {
292     return (value.position() == ContentPosition::Normal && value.distribution() == ContentDistribution::Default) ? normalValueBehavior.position() : value.position();
293 }
294
295 static inline ContentDistribution resolvedContentAlignmentDistribution(const StyleContentAlignmentData& value, const StyleContentAlignmentData& normalValueBehavior)
296 {
297     return (value.position() == ContentPosition::Normal && value.distribution() == ContentDistribution::Default) ? normalValueBehavior.distribution() : value.distribution();
298 }
299
300 ContentPosition RenderStyle::resolvedJustifyContentPosition(const StyleContentAlignmentData& normalValueBehavior) const
301 {
302     return resolvedContentAlignmentPosition(justifyContent(), normalValueBehavior);
303 }
304
305 ContentDistribution RenderStyle::resolvedJustifyContentDistribution(const StyleContentAlignmentData& normalValueBehavior) const
306 {
307     return resolvedContentAlignmentDistribution(justifyContent(), normalValueBehavior);
308 }
309
310 ContentPosition RenderStyle::resolvedAlignContentPosition(const StyleContentAlignmentData& normalValueBehavior) const
311 {
312     return resolvedContentAlignmentPosition(alignContent(), normalValueBehavior);
313 }
314
315 ContentDistribution RenderStyle::resolvedAlignContentDistribution(const StyleContentAlignmentData& normalValueBehavior) const
316 {
317     return resolvedContentAlignmentDistribution(alignContent(), normalValueBehavior);
318 }
319
320 void RenderStyle::inheritFrom(const RenderStyle& inheritParent)
321 {
322     m_rareInheritedData = inheritParent.m_rareInheritedData;
323     m_inheritedData = inheritParent.m_inheritedData;
324     m_inheritedFlags = inheritParent.m_inheritedFlags;
325
326     if (m_svgStyle != inheritParent.m_svgStyle)
327         m_svgStyle.access().inheritFrom(inheritParent.m_svgStyle.get());
328 }
329
330 void RenderStyle::copyNonInheritedFrom(const RenderStyle& other)
331 {
332     m_boxData = other.m_boxData;
333     m_visualData = other.m_visualData;
334     m_backgroundData = other.m_backgroundData;
335     m_surroundData = other.m_surroundData;
336     m_rareNonInheritedData = other.m_rareNonInheritedData;
337     m_nonInheritedFlags.copyNonInheritedFrom(other.m_nonInheritedFlags);
338
339     if (m_svgStyle != other.m_svgStyle)
340         m_svgStyle.access().copyNonInheritedFrom(other.m_svgStyle.get());
341
342     ASSERT(zoom() == initialZoom());
343 }
344
345 void RenderStyle::copyContentFrom(const RenderStyle& other)
346 {
347     if (!other.m_rareNonInheritedData->content)
348         return;
349     m_rareNonInheritedData.access().content = other.m_rareNonInheritedData->content->clone();
350 }
351
352 bool RenderStyle::operator==(const RenderStyle& other) const
353 {
354     // compare everything except the pseudoStyle pointer
355     return m_inheritedFlags == other.m_inheritedFlags
356         && m_nonInheritedFlags == other.m_nonInheritedFlags
357         && m_boxData == other.m_boxData
358         && m_visualData == other.m_visualData
359         && m_backgroundData == other.m_backgroundData
360         && m_surroundData == other.m_surroundData
361         && m_rareNonInheritedData == other.m_rareNonInheritedData
362         && m_rareInheritedData == other.m_rareInheritedData
363         && m_inheritedData == other.m_inheritedData
364         && m_svgStyle == other.m_svgStyle;
365 }
366
367 bool RenderStyle::hasUniquePseudoStyle() const
368 {
369     if (!m_cachedPseudoStyles || styleType() != PseudoId::None)
370         return false;
371
372     for (auto& pseudoStyle : *m_cachedPseudoStyles) {
373         if (pseudoStyle->unique())
374             return true;
375     }
376
377     return false;
378 }
379
380 RenderStyle* RenderStyle::getCachedPseudoStyle(PseudoId pid) const
381 {
382     if (!m_cachedPseudoStyles || !m_cachedPseudoStyles->size())
383         return nullptr;
384
385     if (styleType() != PseudoId::None) 
386         return nullptr;
387
388     for (auto& pseudoStyle : *m_cachedPseudoStyles) {
389         if (pseudoStyle->styleType() == pid)
390             return pseudoStyle.get();
391     }
392
393     return nullptr;
394 }
395
396 RenderStyle* RenderStyle::addCachedPseudoStyle(std::unique_ptr<RenderStyle> pseudo)
397 {
398     if (!pseudo)
399         return nullptr;
400
401     ASSERT(pseudo->styleType() > PseudoId::None);
402
403     RenderStyle* result = pseudo.get();
404
405     if (!m_cachedPseudoStyles)
406         m_cachedPseudoStyles = std::make_unique<PseudoStyleCache>();
407
408     m_cachedPseudoStyles->append(WTFMove(pseudo));
409
410     return result;
411 }
412
413 void RenderStyle::removeCachedPseudoStyle(PseudoId pid)
414 {
415     if (!m_cachedPseudoStyles)
416         return;
417     for (size_t i = 0; i < m_cachedPseudoStyles->size(); ++i) {
418         RenderStyle* pseudoStyle = m_cachedPseudoStyles->at(i).get();
419         if (pseudoStyle->styleType() == pid) {
420             m_cachedPseudoStyles->remove(i);
421             return;
422         }
423     }
424 }
425
426 bool RenderStyle::inheritedNotEqual(const RenderStyle* other) const
427 {
428     return m_inheritedFlags != other->m_inheritedFlags
429         || m_inheritedData != other->m_inheritedData
430         || m_svgStyle->inheritedNotEqual(other->m_svgStyle)
431         || m_rareInheritedData != other->m_rareInheritedData;
432 }
433
434 #if ENABLE(TEXT_AUTOSIZING)
435
436 static inline unsigned computeFontHash(const FontCascade& font)
437 {
438     IntegerHasher hasher;
439     hasher.add(ASCIICaseInsensitiveHash::hash(font.fontDescription().firstFamily()));
440     hasher.add(font.fontDescription().specifiedSize());
441     return hasher.hash();
442 }
443
444 unsigned RenderStyle::hashForTextAutosizing() const
445 {
446     // FIXME: Not a very smart hash. Could be improved upon. See <https://bugs.webkit.org/show_bug.cgi?id=121131>.
447     unsigned hash = m_rareNonInheritedData->appearance;
448     hash ^= m_rareNonInheritedData->marginBeforeCollapse;
449     hash ^= m_rareNonInheritedData->marginAfterCollapse;
450     hash ^= m_rareNonInheritedData->lineClamp.value();
451     hash ^= m_rareInheritedData->overflowWrap;
452     hash ^= m_rareInheritedData->nbspMode;
453     hash ^= m_rareInheritedData->lineBreak;
454     hash ^= WTF::FloatHash<float>::hash(m_inheritedData->specifiedLineHeight.value());
455     hash ^= computeFontHash(m_inheritedData->fontCascade);
456     hash ^= WTF::FloatHash<float>::hash(m_inheritedData->horizontalBorderSpacing);
457     hash ^= WTF::FloatHash<float>::hash(m_inheritedData->verticalBorderSpacing);
458     hash ^= m_inheritedFlags.boxDirection;
459     hash ^= m_inheritedFlags.rtlOrdering;
460     hash ^= m_nonInheritedFlags.position;
461     hash ^= m_nonInheritedFlags.floating;
462     hash ^= m_rareNonInheritedData->textOverflow;
463     hash ^= m_rareInheritedData->textSecurity;
464     return hash;
465 }
466
467 bool RenderStyle::equalForTextAutosizing(const RenderStyle& other) const
468 {
469     return m_rareNonInheritedData->appearance == other.m_rareNonInheritedData->appearance
470         && m_rareNonInheritedData->marginBeforeCollapse == other.m_rareNonInheritedData->marginBeforeCollapse
471         && m_rareNonInheritedData->marginAfterCollapse == other.m_rareNonInheritedData->marginAfterCollapse
472         && m_rareNonInheritedData->lineClamp == other.m_rareNonInheritedData->lineClamp
473         && m_rareInheritedData->textSizeAdjust == other.m_rareInheritedData->textSizeAdjust
474         && m_rareInheritedData->overflowWrap == other.m_rareInheritedData->overflowWrap
475         && m_rareInheritedData->nbspMode == other.m_rareInheritedData->nbspMode
476         && m_rareInheritedData->lineBreak == other.m_rareInheritedData->lineBreak
477         && m_rareInheritedData->textSecurity == other.m_rareInheritedData->textSecurity
478         && m_inheritedData->specifiedLineHeight == other.m_inheritedData->specifiedLineHeight
479         && m_inheritedData->fontCascade.equalForTextAutoSizing(other.m_inheritedData->fontCascade)
480         && m_inheritedData->horizontalBorderSpacing == other.m_inheritedData->horizontalBorderSpacing
481         && m_inheritedData->verticalBorderSpacing == other.m_inheritedData->verticalBorderSpacing
482         && m_inheritedFlags.boxDirection == other.m_inheritedFlags.boxDirection
483         && m_inheritedFlags.rtlOrdering == other.m_inheritedFlags.rtlOrdering
484         && m_nonInheritedFlags.position == other.m_nonInheritedFlags.position
485         && m_nonInheritedFlags.floating == other.m_nonInheritedFlags.floating
486         && m_rareNonInheritedData->textOverflow == other.m_rareNonInheritedData->textOverflow;
487 }
488
489 #endif // ENABLE(TEXT_AUTOSIZING)
490
491 bool RenderStyle::inheritedDataShared(const RenderStyle* other) const
492 {
493     // This is a fast check that only looks if the data structures are shared.
494     return m_inheritedFlags == other->m_inheritedFlags
495         && m_inheritedData.ptr() == other->m_inheritedData.ptr()
496         && m_svgStyle.ptr() == other->m_svgStyle.ptr()
497         && m_rareInheritedData.ptr() == other->m_rareInheritedData.ptr();
498 }
499
500 static bool positionChangeIsMovementOnly(const LengthBox& a, const LengthBox& b, const Length& width)
501 {
502     // If any unit types are different, then we can't guarantee
503     // that this was just a movement.
504     if (a.left().type() != b.left().type()
505         || a.right().type() != b.right().type()
506         || a.top().type() != b.top().type()
507         || a.bottom().type() != b.bottom().type())
508         return false;
509
510     // Only one unit can be non-auto in the horizontal direction and
511     // in the vertical direction.  Otherwise the adjustment of values
512     // is changing the size of the box.
513     if (!a.left().isIntrinsicOrAuto() && !a.right().isIntrinsicOrAuto())
514         return false;
515     if (!a.top().isIntrinsicOrAuto() && !a.bottom().isIntrinsicOrAuto())
516         return false;
517     // If our width is auto and left or right is specified then this 
518     // is not just a movement - we need to resize to our container.
519     if ((!a.left().isIntrinsicOrAuto() || !a.right().isIntrinsicOrAuto()) && width.isIntrinsicOrAuto())
520         return false;
521
522     // One of the units is fixed or percent in both directions and stayed
523     // that way in the new style.  Therefore all we are doing is moving.
524     return true;
525 }
526
527 inline bool RenderStyle::changeAffectsVisualOverflow(const RenderStyle& other) const
528 {
529     if (m_rareNonInheritedData.ptr() != other.m_rareNonInheritedData.ptr()
530         && !arePointingToEqualData(m_rareNonInheritedData->boxShadow, other.m_rareNonInheritedData->boxShadow))
531         return true;
532
533     if (m_rareInheritedData.ptr() != other.m_rareInheritedData.ptr()
534         && !arePointingToEqualData(m_rareInheritedData->textShadow, other.m_rareInheritedData->textShadow))
535         return true;
536
537     if (m_inheritedFlags.textDecorations != other.m_inheritedFlags.textDecorations
538         || m_visualData->textDecoration != other.m_visualData->textDecoration
539         || m_rareNonInheritedData->textDecorationStyle != other.m_rareNonInheritedData->textDecorationStyle) {
540         // Underlines are always drawn outside of their textbox bounds when text-underline-position: under;
541         // is specified. We can take an early out here.
542         if (textUnderlinePosition() == TextUnderlinePosition::Under
543             || other.textUnderlinePosition() == TextUnderlinePosition::Under)
544             return true;
545         return visualOverflowForDecorations(*this, nullptr) != visualOverflowForDecorations(other, nullptr);
546     }
547
548     if (hasOutlineInVisualOverflow() != other.hasOutlineInVisualOverflow())
549         return true;
550     return false;
551 }
552
553 static bool rareNonInheritedDataChangeRequiresLayout(const StyleRareNonInheritedData& first, const StyleRareNonInheritedData& second, OptionSet<StyleDifferenceContextSensitiveProperty>& changedContextSensitiveProperties)
554 {
555     ASSERT(&first != &second);
556
557     if (first.appearance != second.appearance
558         || first.marginBeforeCollapse != second.marginBeforeCollapse
559         || first.marginAfterCollapse != second.marginAfterCollapse
560         || first.lineClamp != second.lineClamp
561         || first.initialLetter != second.initialLetter
562         || first.textOverflow != second.textOverflow)
563         return true;
564
565     if (first.shapeMargin != second.shapeMargin)
566         return true;
567
568     if (first.deprecatedFlexibleBox != second.deprecatedFlexibleBox)
569         return true;
570
571     if (first.flexibleBox != second.flexibleBox)
572         return true;
573
574     if (first.order != second.order
575         || first.alignContent != second.alignContent
576         || first.alignItems != second.alignItems
577         || first.alignSelf != second.alignSelf
578         || first.justifyContent != second.justifyContent
579         || first.justifyItems != second.justifyItems
580         || first.justifySelf != second.justifySelf)
581         return true;
582
583     if (!arePointingToEqualData(first.boxReflect, second.boxReflect))
584         return true;
585
586     if (first.multiCol != second.multiCol)
587         return true;
588
589     if (first.transform.ptr() != second.transform.ptr()) {
590         if (first.transform->hasTransform() != second.transform->hasTransform())
591             return true;
592         if (*first.transform != *second.transform) {
593             changedContextSensitiveProperties.add(StyleDifferenceContextSensitiveProperty::Transform);
594             // Don't return; keep looking for another change
595         }
596     }
597
598     if (first.grid != second.grid
599         || first.gridItem != second.gridItem)
600         return true;
601
602 #if ENABLE(DASHBOARD_SUPPORT)
603     // If regions change, trigger a relayout to re-calc regions.
604     if (first.dashboardRegions != second.dashboardRegions)
605         return true;
606 #endif
607
608     if (!arePointingToEqualData(first.willChange, second.willChange)) {
609         changedContextSensitiveProperties.add(StyleDifferenceContextSensitiveProperty::WillChange);
610         // Don't return; keep looking for another change
611     }
612
613     if (first.textCombine != second.textCombine)
614         return true;
615
616     if (first.breakBefore != second.breakBefore
617         || first.breakAfter != second.breakAfter
618         || first.breakInside != second.breakInside)
619         return true;
620
621     if (first.hasOpacity() != second.hasOpacity()) {
622         // FIXME: We would like to use SimplifiedLayout here, but we can't quite do that yet.
623         // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
624         // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
625         // In addition we need to solve the floating object issue when layers come and go. Right now
626         // a full layout is necessary to keep floating object lists sane.
627         return true;
628     }
629
630 #if ENABLE(CSS_COMPOSITING)
631     if (first.isolation != second.isolation) {
632         // Ideally this would trigger a cheaper layout that just updates layer z-order trees (webit.org/b/190088).
633         return true;
634     }
635 #endif
636
637     if (first.hasFilters() != second.hasFilters())
638         return true;
639
640 #if ENABLE(FILTERS_LEVEL_2)
641     if (first.hasBackdropFilters() != second.hasBackdropFilters())
642         return true;
643 #endif
644
645     return false;
646 }
647
648 static bool rareInheritedDataChangeRequiresLayout(const StyleRareInheritedData& first, const StyleRareInheritedData& second)
649 {
650     ASSERT(&first != &second);
651
652     if (first.indent != second.indent
653 #if ENABLE(CSS3_TEXT)
654         || first.textAlignLast != second.textAlignLast
655         || first.textJustify != second.textJustify
656         || first.textIndentLine != second.textIndentLine
657 #endif
658         || first.effectiveZoom != second.effectiveZoom
659         || first.textZoom != second.textZoom
660 #if ENABLE(TEXT_AUTOSIZING)
661         || first.textSizeAdjust != second.textSizeAdjust
662 #endif
663         || first.wordBreak != second.wordBreak
664         || first.overflowWrap != second.overflowWrap
665         || first.nbspMode != second.nbspMode
666         || first.lineBreak != second.lineBreak
667         || first.textSecurity != second.textSecurity
668         || first.hyphens != second.hyphens
669         || first.hyphenationLimitBefore != second.hyphenationLimitBefore
670         || first.hyphenationLimitAfter != second.hyphenationLimitAfter
671         || first.hyphenationString != second.hyphenationString
672         || first.rubyPosition != second.rubyPosition
673         || first.textEmphasisMark != second.textEmphasisMark
674         || first.textEmphasisPosition != second.textEmphasisPosition
675         || first.textEmphasisCustomMark != second.textEmphasisCustomMark
676         || first.textOrientation != second.textOrientation
677         || first.tabSize != second.tabSize
678         || first.lineBoxContain != second.lineBoxContain
679         || first.lineGrid != second.lineGrid
680 #if ENABLE(CSS_IMAGE_ORIENTATION)
681         || first.imageOrientation != second.imageOrientation
682 #endif
683 #if ENABLE(CSS_IMAGE_RESOLUTION)
684         || first.imageResolutionSource != second.imageResolutionSource
685         || first.imageResolutionSnap != second.imageResolutionSnap
686         || first.imageResolution != second.imageResolution
687 #endif
688         || first.lineSnap != second.lineSnap
689         || first.lineAlign != second.lineAlign
690         || first.hangingPunctuation != second.hangingPunctuation
691 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
692         || first.useTouchOverflowScrolling != second.useTouchOverflowScrolling
693 #endif
694         || first.listStyleImage != second.listStyleImage) // FIXME: needs arePointingToEqualData()?
695         return true;
696
697     if (first.textStrokeWidth != second.textStrokeWidth)
698         return true;
699
700     // These properties affect the cached stroke bounding box rects.
701     if (first.capStyle != second.capStyle
702         || first.joinStyle != second.joinStyle
703         || first.strokeWidth != second.strokeWidth
704         || first.miterLimit != second.miterLimit)
705         return true;
706
707     if (!arePointingToEqualData(first.quotes, second.quotes))
708         return true;
709
710     return false;
711 }
712
713 bool RenderStyle::changeRequiresLayout(const RenderStyle& other, OptionSet<StyleDifferenceContextSensitiveProperty>& changedContextSensitiveProperties) const
714 {
715     if (m_boxData.ptr() != other.m_boxData.ptr()) {
716         if (m_boxData->width() != other.m_boxData->width()
717             || m_boxData->minWidth() != other.m_boxData->minWidth()
718             || m_boxData->maxWidth() != other.m_boxData->maxWidth()
719             || m_boxData->height() != other.m_boxData->height()
720             || m_boxData->minHeight() != other.m_boxData->minHeight()
721             || m_boxData->maxHeight() != other.m_boxData->maxHeight())
722             return true;
723
724         if (m_boxData->verticalAlign() != other.m_boxData->verticalAlign())
725             return true;
726
727         if (m_boxData->boxSizing() != other.m_boxData->boxSizing())
728             return true;
729     }
730
731     if (m_surroundData->margin != other.m_surroundData->margin)
732         return true;
733
734     if (m_surroundData->padding != other.m_surroundData->padding)
735         return true;
736
737     // FIXME: We should add an optimized form of layout that just recomputes visual overflow.
738     if (changeAffectsVisualOverflow(other))
739         return true;
740
741     if (m_rareNonInheritedData.ptr() != other.m_rareNonInheritedData.ptr()
742         && rareNonInheritedDataChangeRequiresLayout(*m_rareNonInheritedData, *other.m_rareNonInheritedData, changedContextSensitiveProperties))
743         return true;
744
745     if (m_rareInheritedData.ptr() != other.m_rareInheritedData.ptr()
746         && rareInheritedDataChangeRequiresLayout(*m_rareInheritedData, *other.m_rareInheritedData))
747         return true;
748
749     if (m_inheritedData.ptr() != other.m_inheritedData.ptr()) {
750         if (m_inheritedData->lineHeight != other.m_inheritedData->lineHeight
751 #if ENABLE(TEXT_AUTOSIZING)
752             || m_inheritedData->specifiedLineHeight != other.m_inheritedData->specifiedLineHeight
753 #endif
754             || m_inheritedData->fontCascade != other.m_inheritedData->fontCascade
755             || m_inheritedData->horizontalBorderSpacing != other.m_inheritedData->horizontalBorderSpacing
756             || m_inheritedData->verticalBorderSpacing != other.m_inheritedData->verticalBorderSpacing)
757             return true;
758     }
759
760     if (m_inheritedFlags.boxDirection != other.m_inheritedFlags.boxDirection
761         || m_inheritedFlags.rtlOrdering != other.m_inheritedFlags.rtlOrdering
762         || m_nonInheritedFlags.position != other.m_nonInheritedFlags.position
763         || m_nonInheritedFlags.floating != other.m_nonInheritedFlags.floating
764         || m_nonInheritedFlags.originalDisplay != other.m_nonInheritedFlags.originalDisplay
765         || m_nonInheritedFlags.verticalAlign != other.m_nonInheritedFlags.verticalAlign)
766         return true;
767
768     if (static_cast<DisplayType>(m_nonInheritedFlags.effectiveDisplay) >= DisplayType::Table) {
769         if (m_inheritedFlags.borderCollapse != other.m_inheritedFlags.borderCollapse
770             || m_inheritedFlags.emptyCells != other.m_inheritedFlags.emptyCells
771             || m_inheritedFlags.captionSide != other.m_inheritedFlags.captionSide
772             || m_nonInheritedFlags.tableLayout != other.m_nonInheritedFlags.tableLayout)
773             return true;
774
775         // In the collapsing border model, 'hidden' suppresses other borders, while 'none'
776         // does not, so these style differences can be width differences.
777         if (m_inheritedFlags.borderCollapse
778             && ((borderTopStyle() == BorderStyle::Hidden && other.borderTopStyle() == BorderStyle::None)
779                 || (borderTopStyle() == BorderStyle::None && other.borderTopStyle() == BorderStyle::Hidden)
780                 || (borderBottomStyle() == BorderStyle::Hidden && other.borderBottomStyle() == BorderStyle::None)
781                 || (borderBottomStyle() == BorderStyle::None && other.borderBottomStyle() == BorderStyle::Hidden)
782                 || (borderLeftStyle() == BorderStyle::Hidden && other.borderLeftStyle() == BorderStyle::None)
783                 || (borderLeftStyle() == BorderStyle::None && other.borderLeftStyle() == BorderStyle::Hidden)
784                 || (borderRightStyle() == BorderStyle::Hidden && other.borderRightStyle() == BorderStyle::None)
785                 || (borderRightStyle() == BorderStyle::None && other.borderRightStyle() == BorderStyle::Hidden)))
786             return true;
787     }
788
789     if (static_cast<DisplayType>(m_nonInheritedFlags.effectiveDisplay) == DisplayType::ListItem) {
790         if (m_inheritedFlags.listStyleType != other.m_inheritedFlags.listStyleType
791             || m_inheritedFlags.listStylePosition != other.m_inheritedFlags.listStylePosition)
792             return true;
793     }
794
795     if (m_inheritedFlags.textAlign != other.m_inheritedFlags.textAlign
796         || m_inheritedFlags.textTransform != other.m_inheritedFlags.textTransform
797         || m_inheritedFlags.direction != other.m_inheritedFlags.direction
798         || m_inheritedFlags.whiteSpace != other.m_inheritedFlags.whiteSpace
799         || m_nonInheritedFlags.clear != other.m_nonInheritedFlags.clear
800         || m_nonInheritedFlags.unicodeBidi != other.m_nonInheritedFlags.unicodeBidi)
801         return true;
802
803     // Check block flow direction.
804     if (m_inheritedFlags.writingMode != other.m_inheritedFlags.writingMode)
805         return true;
806
807     // Overflow returns a layout hint.
808     if (m_nonInheritedFlags.overflowX != other.m_nonInheritedFlags.overflowX
809         || m_nonInheritedFlags.overflowY != other.m_nonInheritedFlags.overflowY)
810         return true;
811
812     // If our border widths change, then we need to layout.  Other changes to borders
813     // only necessitate a repaint.
814     if (borderLeftWidth() != other.borderLeftWidth()
815         || borderTopWidth() != other.borderTopWidth()
816         || borderBottomWidth() != other.borderBottomWidth()
817         || borderRightWidth() != other.borderRightWidth())
818         return true;
819
820     // If the counter directives change, trigger a relayout to re-calculate counter values and rebuild the counter node tree.
821     if (!arePointingToEqualData(m_rareNonInheritedData->counterDirectives, other.m_rareNonInheritedData->counterDirectives))
822         return true;
823
824     if ((visibility() == Visibility::Collapse) != (other.visibility() == Visibility::Collapse))
825         return true;
826
827     if (position() != PositionType::Static) {
828         if (m_surroundData->offset != other.m_surroundData->offset) {
829             // FIXME: We would like to use SimplifiedLayout for relative positioning, but we can't quite do that yet.
830             // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
831             // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
832             if (position() != PositionType::Absolute)
833                 return true;
834
835             // Optimize for the case where a positioned layer is moving but not changing size.
836             if (!positionChangeIsMovementOnly(m_surroundData->offset, other.m_surroundData->offset, m_boxData->width()))
837                 return true;
838         }
839     }
840
841     bool hasFirstLineStyle = hasPseudoStyle(PseudoId::FirstLine);
842     if (hasFirstLineStyle != other.hasPseudoStyle(PseudoId::FirstLine))
843         return true;
844     if (hasFirstLineStyle) {
845         auto* firstLineStyle = getCachedPseudoStyle(PseudoId::FirstLine);
846         if (!firstLineStyle)
847             return true;
848         auto* otherFirstLineStyle = other.getCachedPseudoStyle(PseudoId::FirstLine);
849         if (!otherFirstLineStyle)
850             return true;
851         // FIXME: Not all first line style changes actually need layout.
852         if (*firstLineStyle != *otherFirstLineStyle)
853             return true;
854     }
855
856     return false;
857 }
858
859 bool RenderStyle::changeRequiresPositionedLayoutOnly(const RenderStyle& other, OptionSet<StyleDifferenceContextSensitiveProperty>&) const
860 {
861     if (position() == PositionType::Static)
862         return false;
863
864     if (m_surroundData->offset != other.m_surroundData->offset) {
865         // Optimize for the case where a positioned layer is moving but not changing size.
866         if (position() == PositionType::Absolute && positionChangeIsMovementOnly(m_surroundData->offset, other.m_surroundData->offset, m_boxData->width()))
867             return true;
868     }
869     
870     return false;
871 }
872
873 static bool rareNonInheritedDataChangeRequiresLayerRepaint(const StyleRareNonInheritedData& first, const StyleRareNonInheritedData& second, OptionSet<StyleDifferenceContextSensitiveProperty>& changedContextSensitiveProperties)
874 {
875 #if ENABLE(CSS_COMPOSITING)
876     if (first.effectiveBlendMode != second.effectiveBlendMode)
877         return true;
878 #endif
879
880     if (first.opacity != second.opacity) {
881         changedContextSensitiveProperties.add(StyleDifferenceContextSensitiveProperty::Opacity);
882         // Don't return true; keep looking for another change.
883     }
884
885     if (first.filter != second.filter) {
886         changedContextSensitiveProperties.add(StyleDifferenceContextSensitiveProperty::Filter);
887         // Don't return true; keep looking for another change.
888     }
889
890 #if ENABLE(FILTERS_LEVEL_2)
891     if (first.backdropFilter != second.backdropFilter) {
892         changedContextSensitiveProperties.add(StyleDifferenceContextSensitiveProperty::Filter);
893         // Don't return true; keep looking for another change.
894     }
895 #endif
896
897     if (first.mask != second.mask || first.maskBoxImage != second.maskBoxImage)
898         return true;
899
900     return false;
901 }
902
903 bool RenderStyle::changeRequiresLayerRepaint(const RenderStyle& other, OptionSet<StyleDifferenceContextSensitiveProperty>& changedContextSensitiveProperties) const
904 {
905     // StyleResolver has ensured that zIndex is non-auto only if it's applicable.
906     if (m_boxData->zIndex() != other.m_boxData->zIndex() || m_boxData->hasAutoZIndex() != other.m_boxData->hasAutoZIndex())
907         return true;
908
909     if (position() != PositionType::Static) {
910         if (m_visualData->clip != other.m_visualData->clip || m_visualData->hasClip != other.m_visualData->hasClip) {
911             changedContextSensitiveProperties.add(StyleDifferenceContextSensitiveProperty::ClipRect);
912             return true;
913         }
914     }
915
916     if (m_rareNonInheritedData.ptr() != other.m_rareNonInheritedData.ptr()
917         && rareNonInheritedDataChangeRequiresLayerRepaint(*m_rareNonInheritedData, *other.m_rareNonInheritedData, changedContextSensitiveProperties))
918         return true;
919
920     return false;
921 }
922
923 static bool requiresPainting(const RenderStyle& style)
924 {
925     if (style.visibility() == Visibility::Hidden)
926         return false;
927     if (!style.opacity())
928         return false;
929     return true;
930 }
931
932 static bool rareNonInheritedDataChangeRequiresRepaint(const StyleRareNonInheritedData& first, const StyleRareNonInheritedData& second, OptionSet<StyleDifferenceContextSensitiveProperty>& changedContextSensitiveProperties)
933 {
934     if (first.userDrag != second.userDrag
935         || first.borderFit != second.borderFit
936         || first.objectFit != second.objectFit
937         || first.objectPosition != second.objectPosition)
938         return true;
939
940     if (first.isNotFinal != second.isNotFinal)
941         return true;
942
943     if (first.shapeOutside != second.shapeOutside)
944         return true;
945
946     // FIXME: this should probably be moved to changeRequiresLayerRepaint().
947     if (first.clipPath != second.clipPath) {
948         changedContextSensitiveProperties.add(StyleDifferenceContextSensitiveProperty::ClipPath);
949         // Don't return true; keep looking for another change.
950     }
951
952     return false;
953 }
954
955 static bool rareInheritedDataChangeRequiresRepaint(const StyleRareInheritedData& first, const StyleRareInheritedData& second)
956 {
957     return first.userModify != second.userModify
958         || first.userSelect != second.userSelect
959         || first.appleColorFilter != second.appleColorFilter
960         || first.imageRendering != second.imageRendering;
961 }
962
963 bool RenderStyle::changeRequiresRepaint(const RenderStyle& other, OptionSet<StyleDifferenceContextSensitiveProperty>& changedContextSensitiveProperties) const
964 {
965     if (!requiresPainting(*this) && !requiresPainting(other))
966         return false;
967
968     if (m_inheritedFlags.visibility != other.m_inheritedFlags.visibility
969         || m_inheritedFlags.printColorAdjust != other.m_inheritedFlags.printColorAdjust
970         || m_inheritedFlags.insideLink != other.m_inheritedFlags.insideLink
971         || m_inheritedFlags.insideDefaultButton != other.m_inheritedFlags.insideDefaultButton
972         || m_surroundData->border != other.m_surroundData->border
973         || !m_backgroundData->isEquivalentForPainting(*other.m_backgroundData))
974         return true;
975
976     if (m_rareNonInheritedData.ptr() != other.m_rareNonInheritedData.ptr()
977         && rareNonInheritedDataChangeRequiresRepaint(*m_rareNonInheritedData, *other.m_rareNonInheritedData, changedContextSensitiveProperties))
978         return true;
979
980     if (m_rareInheritedData.ptr() != other.m_rareInheritedData.ptr()
981         && rareInheritedDataChangeRequiresRepaint(*m_rareInheritedData, *other.m_rareInheritedData))
982         return true;
983
984     return false;
985 }
986
987 bool RenderStyle::changeRequiresRepaintIfTextOrBorderOrOutline(const RenderStyle& other, OptionSet<StyleDifferenceContextSensitiveProperty>&) const
988 {
989     if (m_inheritedData->color != other.m_inheritedData->color
990         || m_inheritedFlags.textDecorations != other.m_inheritedFlags.textDecorations
991         || m_visualData->textDecoration != other.m_visualData->textDecoration
992         || m_rareNonInheritedData->textDecorationStyle != other.m_rareNonInheritedData->textDecorationStyle
993         || m_rareNonInheritedData->textDecorationColor != other.m_rareNonInheritedData->textDecorationColor
994         || m_rareInheritedData->textDecorationSkip != other.m_rareInheritedData->textDecorationSkip
995         || m_rareInheritedData->textFillColor != other.m_rareInheritedData->textFillColor
996         || m_rareInheritedData->textStrokeColor != other.m_rareInheritedData->textStrokeColor
997         || m_rareInheritedData->textEmphasisColor != other.m_rareInheritedData->textEmphasisColor
998         || m_rareInheritedData->textEmphasisFill != other.m_rareInheritedData->textEmphasisFill
999         || m_rareInheritedData->strokeColor != other.m_rareInheritedData->strokeColor
1000         || m_rareInheritedData->caretColor != other.m_rareInheritedData->caretColor)
1001         return true;
1002
1003     return false;
1004 }
1005
1006 bool RenderStyle::changeRequiresRecompositeLayer(const RenderStyle& other, OptionSet<StyleDifferenceContextSensitiveProperty>&) const
1007 {
1008     if (m_rareNonInheritedData.ptr() != other.m_rareNonInheritedData.ptr()) {
1009         if (m_rareNonInheritedData->transformStyle3D != other.m_rareNonInheritedData->transformStyle3D
1010             || m_rareNonInheritedData->backfaceVisibility != other.m_rareNonInheritedData->backfaceVisibility
1011             || m_rareNonInheritedData->perspective != other.m_rareNonInheritedData->perspective
1012             || m_rareNonInheritedData->perspectiveOriginX != other.m_rareNonInheritedData->perspectiveOriginX
1013             || m_rareNonInheritedData->perspectiveOriginY != other.m_rareNonInheritedData->perspectiveOriginY)
1014             return true;
1015     }
1016
1017     return false;
1018 }
1019
1020 StyleDifference RenderStyle::diff(const RenderStyle& other, OptionSet<StyleDifferenceContextSensitiveProperty>& changedContextSensitiveProperties) const
1021 {
1022     changedContextSensitiveProperties = OptionSet<StyleDifferenceContextSensitiveProperty>();
1023
1024     StyleDifference svgChange = StyleDifference::Equal;
1025     if (m_svgStyle != other.m_svgStyle) {
1026         svgChange = m_svgStyle->diff(other.m_svgStyle.get());
1027         if (svgChange == StyleDifference::Layout)
1028             return svgChange;
1029     }
1030
1031     if (changeRequiresLayout(other, changedContextSensitiveProperties))
1032         return StyleDifference::Layout;
1033
1034     // SVGRenderStyle::diff() might have returned StyleDifference::Repaint, eg. if fill changes.
1035     // If eg. the font-size changed at the same time, we're not allowed to return StyleDifference::Repaint,
1036     // but have to return StyleDifference::Layout, that's why  this if branch comes after all branches
1037     // that are relevant for SVG and might return StyleDifference::Layout.
1038     if (svgChange != StyleDifference::Equal)
1039         return svgChange;
1040
1041     if (changeRequiresPositionedLayoutOnly(other, changedContextSensitiveProperties))
1042         return StyleDifference::LayoutPositionedMovementOnly;
1043
1044     if (changeRequiresLayerRepaint(other, changedContextSensitiveProperties))
1045         return StyleDifference::RepaintLayer;
1046
1047     if (changeRequiresRepaint(other, changedContextSensitiveProperties))
1048         return StyleDifference::Repaint;
1049
1050     if (changeRequiresRecompositeLayer(other, changedContextSensitiveProperties))
1051         return StyleDifference::RecompositeLayer;
1052
1053     if (changeRequiresRepaintIfTextOrBorderOrOutline(other, changedContextSensitiveProperties))
1054         return StyleDifference::RepaintIfTextOrBorderOrOutline;
1055
1056     // Cursors are not checked, since they will be set appropriately in response to mouse events,
1057     // so they don't need to cause any repaint or layout.
1058
1059     // 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
1060     // the resulting transition properly.
1061     return StyleDifference::Equal;
1062 }
1063
1064 bool RenderStyle::diffRequiresLayerRepaint(const RenderStyle& style, bool isComposited) const
1065 {
1066     OptionSet<StyleDifferenceContextSensitiveProperty> changedContextSensitiveProperties;
1067
1068     if (changeRequiresRepaint(style, changedContextSensitiveProperties))
1069         return true;
1070
1071     if (isComposited && changeRequiresLayerRepaint(style, changedContextSensitiveProperties))
1072         return changedContextSensitiveProperties.contains(StyleDifferenceContextSensitiveProperty::ClipRect);
1073
1074     return false;
1075 }
1076
1077 void RenderStyle::setClip(Length&& top, Length&& right, Length&& bottom, Length&& left)
1078 {
1079     auto& data = m_visualData.access();
1080     data.clip.top() = WTFMove(top);
1081     data.clip.right() = WTFMove(right);
1082     data.clip.bottom() = WTFMove(bottom);
1083     data.clip.left() = WTFMove(left);
1084 }
1085
1086 void RenderStyle::addCursor(RefPtr<StyleImage>&& image, const IntPoint& hotSpot)
1087 {
1088     auto& cursorData = m_rareInheritedData.access().cursorData;
1089     if (!cursorData)
1090         cursorData = CursorList::create();
1091     cursorData->append(CursorData(WTFMove(image), hotSpot));
1092 }
1093
1094 void RenderStyle::setCursorList(RefPtr<CursorList>&& list)
1095 {
1096     m_rareInheritedData.access().cursorData = WTFMove(list);
1097 }
1098
1099 void RenderStyle::setQuotes(RefPtr<QuotesData>&& q)
1100 {
1101     if (m_rareInheritedData->quotes == q || (m_rareInheritedData->quotes && q && *m_rareInheritedData->quotes == *q))
1102         return;
1103
1104     m_rareInheritedData.access().quotes = WTFMove(q);
1105 }
1106
1107 void RenderStyle::setWillChange(RefPtr<WillChangeData>&& willChangeData)
1108 {
1109     if (arePointingToEqualData(m_rareNonInheritedData->willChange.get(), willChangeData.get()))
1110         return;
1111
1112     m_rareNonInheritedData.access().willChange = WTFMove(willChangeData);
1113 }
1114
1115 void RenderStyle::clearCursorList()
1116 {
1117     if (m_rareInheritedData->cursorData)
1118         m_rareInheritedData.access().cursorData = nullptr;
1119 }
1120
1121 void RenderStyle::clearContent()
1122 {
1123     if (m_rareNonInheritedData->content)
1124         m_rareNonInheritedData.access().content = nullptr;
1125 }
1126
1127 static inline ContentData& lastContent(ContentData& firstContent)
1128 {
1129     auto* lastContent = &firstContent;
1130     for (auto* content = &firstContent; content; content = content->next())
1131         lastContent = content;
1132     return *lastContent;
1133 }
1134
1135 void RenderStyle::setContent(std::unique_ptr<ContentData> contentData, bool add)
1136 {
1137     auto& data = m_rareNonInheritedData.access();
1138     if (add && data.content)
1139         lastContent(*data.content).setNext(WTFMove(contentData));
1140     else {
1141         data.content = WTFMove(contentData);
1142         auto& altText = data.altText;
1143         if (!altText.isNull())
1144             data.content->setAltText(altText);
1145     }
1146 }
1147
1148 void RenderStyle::setContent(RefPtr<StyleImage>&& image, bool add)
1149 {
1150     if (!image)
1151         return;
1152     setContent(std::make_unique<ImageContentData>(image.releaseNonNull()), add);
1153 }
1154
1155 void RenderStyle::setContent(const String& string, bool add)
1156 {
1157     auto& data = m_rareNonInheritedData.access();
1158     if (add && data.content) {
1159         auto& last = lastContent(*data.content);
1160         if (!is<TextContentData>(last))
1161             last.setNext(std::make_unique<TextContentData>(string));
1162         else {
1163             auto& textContent = downcast<TextContentData>(last);
1164             textContent.setText(textContent.text() + string);
1165         }
1166     } else {
1167         data.content = std::make_unique<TextContentData>(string);
1168         auto& altText = data.altText;
1169         if (!altText.isNull())
1170             data.content->setAltText(altText);
1171     }
1172 }
1173
1174 void RenderStyle::setContent(std::unique_ptr<CounterContent> counter, bool add)
1175 {
1176     if (!counter)
1177         return;
1178     setContent(std::make_unique<CounterContentData>(WTFMove(counter)), add);
1179 }
1180
1181 void RenderStyle::setContent(QuoteType quote, bool add)
1182 {
1183     setContent(std::make_unique<QuoteContentData>(quote), add);
1184 }
1185
1186 void RenderStyle::setContentAltText(const String& string)
1187 {
1188     auto& data = m_rareNonInheritedData.access();
1189     data.altText = string;
1190     if (data.content)
1191         data.content->setAltText(string);
1192 }
1193
1194 const String& RenderStyle::contentAltText() const
1195 {
1196     return m_rareNonInheritedData->altText;
1197 }
1198
1199 void RenderStyle::setHasAttrContent()
1200 {
1201     setUnique();
1202     SET_VAR(m_rareNonInheritedData, hasAttrContent, true);
1203 }
1204
1205 static inline bool requireTransformOrigin(const Vector<RefPtr<TransformOperation>>& transformOperations, RenderStyle::ApplyTransformOrigin applyOrigin)
1206 {
1207     // The transform-origin property brackets the transform with translate operations.
1208     // When the only transform is a translation, the transform-origin is irrelevant.
1209
1210     if (applyOrigin != RenderStyle::IncludeTransformOrigin)
1211         return false;
1212
1213     for (auto& operation : transformOperations) {
1214         // FIXME: Use affectedByTransformOrigin().
1215         auto type = operation->type();
1216         if (type != TransformOperation::TRANSLATE
1217             && type != TransformOperation::TRANSLATE_3D
1218             && type != TransformOperation::TRANSLATE_X
1219             && type != TransformOperation::TRANSLATE_Y
1220             && type != TransformOperation::TRANSLATE_Z)
1221             return true;
1222     }
1223
1224     return false;
1225 }
1226
1227 void RenderStyle::applyTransform(TransformationMatrix& transform, const FloatRect& boundingBox, ApplyTransformOrigin applyOrigin) const
1228 {
1229     auto& operations = m_rareNonInheritedData->transform->operations.operations();
1230     bool applyTransformOrigin = requireTransformOrigin(operations, applyOrigin);
1231     
1232     FloatPoint3D originTranslate;
1233     if (applyTransformOrigin) {
1234         originTranslate.setX(boundingBox.x() + floatValueForLength(transformOriginX(), boundingBox.width()));
1235         originTranslate.setY(boundingBox.y() + floatValueForLength(transformOriginY(), boundingBox.height()));
1236         originTranslate.setZ(transformOriginZ());
1237         transform.translate3d(originTranslate.x(), originTranslate.y(), originTranslate.z());
1238     }
1239
1240     for (auto& operation : operations)
1241         operation->apply(transform, boundingBox.size());
1242
1243     if (applyTransformOrigin)
1244         transform.translate3d(-originTranslate.x(), -originTranslate.y(), -originTranslate.z());
1245 }
1246
1247 void RenderStyle::setPageScaleTransform(float scale)
1248 {
1249     if (scale == 1)
1250         return;
1251     TransformOperations transform;
1252     transform.operations().append(ScaleTransformOperation::create(scale, scale, ScaleTransformOperation::SCALE));
1253     setTransform(transform);
1254     setTransformOriginX(Length(0, Fixed));
1255     setTransformOriginY(Length(0, Fixed));
1256 }
1257
1258 void RenderStyle::setTextShadow(std::unique_ptr<ShadowData> shadowData, bool add)
1259 {
1260     ASSERT(!shadowData || (!shadowData->spread() && shadowData->style() == Normal));
1261
1262     auto& rareData = m_rareInheritedData.access();
1263     if (!add) {
1264         rareData.textShadow = WTFMove(shadowData);
1265         return;
1266     }
1267
1268     shadowData->setNext(WTFMove(rareData.textShadow));
1269     rareData.textShadow = WTFMove(shadowData);
1270 }
1271
1272 void RenderStyle::setBoxShadow(std::unique_ptr<ShadowData> shadowData, bool add)
1273 {
1274     auto& rareData = m_rareNonInheritedData.access();
1275     if (!add) {
1276         rareData.boxShadow = WTFMove(shadowData);
1277         return;
1278     }
1279
1280     shadowData->setNext(WTFMove(rareData.boxShadow));
1281     rareData.boxShadow = WTFMove(shadowData);
1282 }
1283
1284 static RoundedRect::Radii calcRadiiFor(const BorderData& border, const LayoutSize& size)
1285 {
1286     return {
1287         sizeForLengthSize(border.topLeft(), size),
1288         sizeForLengthSize(border.topRight(), size),
1289         sizeForLengthSize(border.bottomLeft(), size),
1290         sizeForLengthSize(border.bottomRight(), size)
1291     };
1292 }
1293
1294 StyleImage* RenderStyle::listStyleImage() const
1295 {
1296     return m_rareInheritedData->listStyleImage.get();
1297 }
1298
1299 void RenderStyle::setListStyleImage(RefPtr<StyleImage>&& v)
1300 {
1301     if (m_rareInheritedData->listStyleImage != v)
1302         m_rareInheritedData.access().listStyleImage = WTFMove(v);
1303 }
1304
1305 const Color& RenderStyle::color() const
1306 {
1307     return m_inheritedData->color;
1308 }
1309
1310 const Color& RenderStyle::visitedLinkColor() const
1311 {
1312     return m_inheritedData->visitedLinkColor;
1313 }
1314
1315 void RenderStyle::setColor(const Color& v)
1316 {
1317     SET_VAR(m_inheritedData, color, v);
1318 }
1319
1320 void RenderStyle::setVisitedLinkColor(const Color& v)
1321 {
1322     SET_VAR(m_inheritedData, visitedLinkColor, v);
1323 }
1324
1325 float RenderStyle::horizontalBorderSpacing() const
1326 {
1327     return m_inheritedData->horizontalBorderSpacing;
1328 }
1329
1330 float RenderStyle::verticalBorderSpacing() const
1331 {
1332     return m_inheritedData->verticalBorderSpacing;
1333 }
1334
1335 void RenderStyle::setHorizontalBorderSpacing(float v)
1336 {
1337     SET_VAR(m_inheritedData, horizontalBorderSpacing, v);
1338 }
1339
1340 void RenderStyle::setVerticalBorderSpacing(float v)
1341 {
1342     SET_VAR(m_inheritedData, verticalBorderSpacing, v);
1343 }
1344
1345 RoundedRect RenderStyle::getRoundedBorderFor(const LayoutRect& borderRect, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1346 {
1347     RoundedRect roundedRect(borderRect);
1348     if (hasBorderRadius()) {
1349         RoundedRect::Radii radii = calcRadiiFor(m_surroundData->border, borderRect.size());
1350         radii.scale(calcBorderRadiiConstraintScaleFor(borderRect, radii));
1351         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
1352     }
1353     return roundedRect;
1354 }
1355
1356 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1357 {
1358     bool horizontal = isHorizontalWritingMode();
1359     auto leftWidth = (!horizontal || includeLogicalLeftEdge) ? borderLeftWidth() : 0;
1360     auto rightWidth = (!horizontal || includeLogicalRightEdge) ? borderRightWidth() : 0;
1361     auto topWidth = (horizontal || includeLogicalLeftEdge) ? borderTopWidth() : 0;
1362     auto bottomWidth = (horizontal || includeLogicalRightEdge) ? borderBottomWidth() : 0;
1363     return getRoundedInnerBorderFor(borderRect, topWidth, bottomWidth, leftWidth, rightWidth, includeLogicalLeftEdge, includeLogicalRightEdge);
1364 }
1365
1366 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect, LayoutUnit topWidth, LayoutUnit bottomWidth,
1367     LayoutUnit leftWidth, LayoutUnit rightWidth, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1368 {
1369     RoundedRect roundedRect { { borderRect.x() + leftWidth, borderRect.y() + topWidth,
1370         borderRect.width() - leftWidth - rightWidth, borderRect.height() - topWidth - bottomWidth } };
1371     if (hasBorderRadius()) {
1372         auto radii = getRoundedBorderFor(borderRect).radii();
1373         radii.shrink(topWidth, bottomWidth, leftWidth, rightWidth);
1374         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
1375     }
1376     return roundedRect;
1377 }
1378
1379 static bool allLayersAreFixed(const FillLayer& layers)
1380 {
1381     for (auto* layer = &layers; layer; layer = layer->next()) {
1382         if (!(layer->image() && layer->attachment() == FillAttachment::FixedBackground))
1383             return false;
1384     }
1385     return true;
1386 }
1387
1388 bool RenderStyle::hasEntirelyFixedBackground() const
1389 {
1390     return allLayersAreFixed(backgroundLayers());
1391 }
1392
1393 const CounterDirectiveMap* RenderStyle::counterDirectives() const
1394 {
1395     return m_rareNonInheritedData->counterDirectives.get();
1396 }
1397
1398 CounterDirectiveMap& RenderStyle::accessCounterDirectives()
1399 {
1400     auto& map = m_rareNonInheritedData.access().counterDirectives;
1401     if (!map)
1402         map = std::make_unique<CounterDirectiveMap>();
1403     return *map;
1404 }
1405
1406 const AtomicString& RenderStyle::hyphenString() const
1407 {
1408     ASSERT(hyphens() != Hyphens::None);
1409
1410     auto& hyphenationString = m_rareInheritedData->hyphenationString;
1411     if (!hyphenationString.isNull())
1412         return hyphenationString;
1413
1414     // FIXME: This should depend on locale.
1415     static NeverDestroyed<AtomicString> hyphenMinusString(&hyphenMinus, 1);
1416     static NeverDestroyed<AtomicString> hyphenString(&hyphen, 1);
1417     return fontCascade().primaryFont().glyphForCharacter(hyphen) ? hyphenString : hyphenMinusString;
1418 }
1419
1420 const AtomicString& RenderStyle::textEmphasisMarkString() const
1421 {
1422     switch (textEmphasisMark()) {
1423     case TextEmphasisMark::None:
1424         return nullAtom();
1425     case TextEmphasisMark::Custom:
1426         return textEmphasisCustomMark();
1427     case TextEmphasisMark::Dot: {
1428         static NeverDestroyed<AtomicString> filledDotString(&bullet, 1);
1429         static NeverDestroyed<AtomicString> openDotString(&whiteBullet, 1);
1430         return textEmphasisFill() == TextEmphasisFill::Filled ? filledDotString : openDotString;
1431     }
1432     case TextEmphasisMark::Circle: {
1433         static NeverDestroyed<AtomicString> filledCircleString(&blackCircle, 1);
1434         static NeverDestroyed<AtomicString> openCircleString(&whiteCircle, 1);
1435         return textEmphasisFill() == TextEmphasisFill::Filled ? filledCircleString : openCircleString;
1436     }
1437     case TextEmphasisMark::DoubleCircle: {
1438         static NeverDestroyed<AtomicString> filledDoubleCircleString(&fisheye, 1);
1439         static NeverDestroyed<AtomicString> openDoubleCircleString(&bullseye, 1);
1440         return textEmphasisFill() == TextEmphasisFill::Filled ? filledDoubleCircleString : openDoubleCircleString;
1441     }
1442     case TextEmphasisMark::Triangle: {
1443         static NeverDestroyed<AtomicString> filledTriangleString(&blackUpPointingTriangle, 1);
1444         static NeverDestroyed<AtomicString> openTriangleString(&whiteUpPointingTriangle, 1);
1445         return textEmphasisFill() == TextEmphasisFill::Filled ? filledTriangleString : openTriangleString;
1446     }
1447     case TextEmphasisMark::Sesame: {
1448         static NeverDestroyed<AtomicString> filledSesameString(&sesameDot, 1);
1449         static NeverDestroyed<AtomicString> openSesameString(&whiteSesameDot, 1);
1450         return textEmphasisFill() == TextEmphasisFill::Filled ? filledSesameString : openSesameString;
1451     }
1452     case TextEmphasisMark::Auto:
1453         ASSERT_NOT_REACHED();
1454         return nullAtom();
1455     }
1456
1457     ASSERT_NOT_REACHED();
1458     return nullAtom();
1459 }
1460
1461 #if ENABLE(DASHBOARD_SUPPORT)
1462
1463 const Vector<StyleDashboardRegion>& RenderStyle::initialDashboardRegions()
1464 {
1465     static NeverDestroyed<Vector<StyleDashboardRegion>> emptyList;
1466     return emptyList;
1467 }
1468
1469 const Vector<StyleDashboardRegion>& RenderStyle::noneDashboardRegions()
1470 {
1471     static NeverDestroyed<Vector<StyleDashboardRegion>> noneList;
1472     static bool noneListInitialized = false;
1473     if (!noneListInitialized) {
1474         noneList.get().append(StyleDashboardRegion { emptyString(), { }, StyleDashboardRegion::None });
1475         noneListInitialized = true;
1476     }
1477     return noneList;
1478 }
1479
1480 #endif
1481
1482 void RenderStyle::adjustAnimations()
1483 {
1484     auto* animationList = m_rareNonInheritedData->animations.get();
1485     if (!animationList)
1486         return;
1487
1488     // Get rid of empty animations and anything beyond them
1489     for (size_t i = 0, size = animationList->size(); i < size; ++i) {
1490         if (animationList->animation(i).isEmpty()) {
1491             animationList->resize(i);
1492             break;
1493         }
1494     }
1495
1496     if (animationList->isEmpty()) {
1497         clearAnimations();
1498         return;
1499     }
1500
1501     // Repeat patterns into layers that don't have some properties set.
1502     animationList->fillUnsetProperties();
1503 }
1504
1505 void RenderStyle::adjustTransitions()
1506 {
1507     auto* transitionList = m_rareNonInheritedData->transitions.get();
1508     if (!transitionList)
1509         return;
1510
1511     // Get rid of empty transitions and anything beyond them
1512     for (size_t i = 0, size = transitionList->size(); i < size; ++i) {
1513         if (transitionList->animation(i).isEmpty()) {
1514             transitionList->resize(i);
1515             break;
1516         }
1517     }
1518
1519     if (transitionList->isEmpty()) {
1520         clearTransitions();
1521         return;
1522     }
1523
1524     // Repeat patterns into layers that don't have some properties set.
1525     transitionList->fillUnsetProperties();
1526
1527     // Make sure there are no duplicate properties.
1528     // This is an O(n^2) algorithm but the lists tend to be short, so it is probably OK.
1529     for (size_t i = 0; i < transitionList->size(); ++i) {
1530         for (size_t j = i + 1; j < transitionList->size(); ++j) {
1531             if (transitionList->animation(i).property() == transitionList->animation(j).property()) {
1532                 // toss i
1533                 transitionList->remove(i);
1534                 j = i;
1535             }
1536         }
1537     }
1538 }
1539
1540 AnimationList& RenderStyle::ensureAnimations()
1541 {
1542     if (!m_rareNonInheritedData.access().animations)
1543         m_rareNonInheritedData.access().animations = std::make_unique<AnimationList>();
1544     return *m_rareNonInheritedData->animations;
1545 }
1546
1547 AnimationList& RenderStyle::ensureTransitions()
1548 {
1549     if (!m_rareNonInheritedData.access().transitions)
1550         m_rareNonInheritedData.access().transitions = std::make_unique<AnimationList>();
1551     return *m_rareNonInheritedData->transitions;
1552 }
1553
1554 const Animation* RenderStyle::transitionForProperty(CSSPropertyID property) const
1555 {
1556     auto* transitions = this->transitions();
1557     if (!transitions)
1558         return nullptr;
1559     for (size_t i = 0, size = transitions->size(); i < size; ++i) {
1560         auto& animation = transitions->animation(i);
1561         if (animation.animationMode() == Animation::AnimateAll || animation.property() == property)
1562             return &animation;
1563     }
1564     return nullptr;
1565 }
1566
1567 const FontCascade& RenderStyle::fontCascade() const
1568 {
1569     return m_inheritedData->fontCascade;
1570 }
1571
1572 const FontMetrics& RenderStyle::fontMetrics() const
1573 {
1574     return m_inheritedData->fontCascade.fontMetrics();
1575 }
1576
1577 const FontCascadeDescription& RenderStyle::fontDescription() const
1578 {
1579     return m_inheritedData->fontCascade.fontDescription();
1580 }
1581
1582 float RenderStyle::specifiedFontSize() const
1583 {
1584     return fontDescription().specifiedSize();
1585 }
1586
1587 float RenderStyle::computedFontSize() const
1588 {
1589     return fontDescription().computedSize();
1590 }
1591
1592 unsigned RenderStyle::computedFontPixelSize() const
1593 {
1594     return fontDescription().computedPixelSize();
1595 }
1596
1597 const Length& RenderStyle::wordSpacing() const
1598 {
1599     return m_rareInheritedData->wordSpacing;
1600 }
1601
1602 float RenderStyle::letterSpacing() const
1603 {
1604     return m_inheritedData->fontCascade.letterSpacing();
1605 }
1606
1607 bool RenderStyle::setFontDescription(FontCascadeDescription&& description)
1608 {
1609     if (m_inheritedData->fontCascade.fontDescription() == description)
1610         return false;
1611     auto& cascade = m_inheritedData.access().fontCascade;
1612     cascade = { WTFMove(description), cascade.letterSpacing(), cascade.wordSpacing() };
1613     return true;
1614 }
1615
1616 const Length& RenderStyle::specifiedLineHeight() const
1617 {
1618 #if ENABLE(TEXT_AUTOSIZING)
1619     return m_inheritedData->specifiedLineHeight;
1620 #else
1621     return m_inheritedData->lineHeight;
1622 #endif
1623 }
1624
1625 #if ENABLE(TEXT_AUTOSIZING)
1626
1627 void RenderStyle::setSpecifiedLineHeight(Length&& height)
1628 {
1629     SET_VAR(m_inheritedData, specifiedLineHeight, WTFMove(height));
1630 }
1631
1632 #endif
1633
1634 const Length& RenderStyle::lineHeight() const
1635 {
1636     return m_inheritedData->lineHeight;
1637 }
1638
1639 void RenderStyle::setLineHeight(Length&& height)
1640 {
1641     SET_VAR(m_inheritedData, lineHeight, WTFMove(height));
1642 }
1643
1644 int RenderStyle::computedLineHeight() const
1645 {
1646     const Length& lh = lineHeight();
1647
1648     // Negative value means the line height is not set. Use the font's built-in spacing.
1649     if (lh.isNegative())
1650         return fontMetrics().lineSpacing();
1651
1652     if (lh.isPercentOrCalculated())
1653         return minimumValueForLength(lh, computedFontPixelSize());
1654
1655     return clampTo<int>(lh.value());
1656 }
1657
1658 void RenderStyle::setWordSpacing(Length&& value)
1659 {
1660     float fontWordSpacing;
1661     switch (value.type()) {
1662     case Auto:
1663         fontWordSpacing = 0;
1664         break;
1665     case Percent:
1666         fontWordSpacing = value.percent() * fontCascade().spaceWidth() / 100;
1667         break;
1668     case Fixed:
1669         fontWordSpacing = value.value();
1670         break;
1671     case Calculated:
1672         fontWordSpacing = value.nonNanCalculatedValue(maxValueForCssLength);
1673         break;
1674     default:
1675         ASSERT_NOT_REACHED();
1676         fontWordSpacing = 0;
1677         break;
1678     }
1679     m_inheritedData.access().fontCascade.setWordSpacing(fontWordSpacing);
1680     m_rareInheritedData.access().wordSpacing = WTFMove(value);
1681 }
1682
1683 void RenderStyle::setLetterSpacing(float v) { m_inheritedData.access().fontCascade.setLetterSpacing(v); }
1684
1685 void RenderStyle::setFontSize(float size)
1686 {
1687     // size must be specifiedSize if Text Autosizing is enabled, but computedSize if text
1688     // zoom is enabled (if neither is enabled it's irrelevant as they're probably the same).
1689
1690     ASSERT(std::isfinite(size));
1691     if (!std::isfinite(size) || size < 0)
1692         size = 0;
1693     else
1694         size = std::min(maximumAllowedFontSize, size);
1695
1696     FontSelector* currentFontSelector = fontCascade().fontSelector();
1697     auto description = fontDescription();
1698     description.setSpecifiedSize(size);
1699     description.setComputedSize(size);
1700
1701     setFontDescription(WTFMove(description));
1702     fontCascade().update(currentFontSelector);
1703 }
1704
1705 #if ENABLE(VARIATION_FONTS)
1706 void RenderStyle::setFontVariationSettings(FontVariationSettings settings)
1707 {
1708     FontSelector* currentFontSelector = fontCascade().fontSelector();
1709     auto description = fontDescription();
1710     description.setVariationSettings(WTFMove(settings));
1711
1712     setFontDescription(WTFMove(description));
1713     fontCascade().update(currentFontSelector);
1714 }
1715 #endif
1716
1717 void RenderStyle::setFontWeight(FontSelectionValue value)
1718 {
1719     FontSelector* currentFontSelector = fontCascade().fontSelector();
1720     auto description = fontDescription();
1721     description.setWeight(value);
1722
1723     setFontDescription(WTFMove(description));
1724     fontCascade().update(currentFontSelector);
1725 }
1726
1727 void RenderStyle::setFontStretch(FontSelectionValue value)
1728 {
1729     FontSelector* currentFontSelector = fontCascade().fontSelector();
1730     auto description = fontDescription();
1731     description.setStretch(value);
1732
1733     setFontDescription(WTFMove(description));
1734     fontCascade().update(currentFontSelector);
1735 }
1736
1737 void RenderStyle::setFontItalic(std::optional<FontSelectionValue> value)
1738 {
1739     FontSelector* currentFontSelector = fontCascade().fontSelector();
1740     auto description = fontDescription();
1741     description.setItalic(value);
1742
1743     setFontDescription(WTFMove(description));
1744     fontCascade().update(currentFontSelector);
1745 }
1746
1747 void RenderStyle::getShadowExtent(const ShadowData* shadow, LayoutUnit& top, LayoutUnit& right, LayoutUnit& bottom, LayoutUnit& left) const
1748 {
1749     top = 0;
1750     right = 0;
1751     bottom = 0;
1752     left = 0;
1753
1754     for ( ; shadow; shadow = shadow->next()) {
1755         if (shadow->style() == Inset)
1756             continue;
1757
1758         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1759         top = std::min<LayoutUnit>(top, shadow->y() - extentAndSpread);
1760         right = std::max<LayoutUnit>(right, shadow->x() + extentAndSpread);
1761         bottom = std::max<LayoutUnit>(bottom, shadow->y() + extentAndSpread);
1762         left = std::min<LayoutUnit>(left, shadow->x() - extentAndSpread);
1763     }
1764 }
1765
1766 LayoutBoxExtent RenderStyle::getShadowInsetExtent(const ShadowData* shadow) const
1767 {
1768     LayoutUnit top = 0;
1769     LayoutUnit right = 0;
1770     LayoutUnit bottom = 0;
1771     LayoutUnit left = 0;
1772
1773     for ( ; shadow; shadow = shadow->next()) {
1774         if (shadow->style() == Normal)
1775             continue;
1776
1777         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1778         top = std::max<LayoutUnit>(top, shadow->y() + extentAndSpread);
1779         right = std::min<LayoutUnit>(right, shadow->x() - extentAndSpread);
1780         bottom = std::min<LayoutUnit>(bottom, shadow->y() - extentAndSpread);
1781         left = std::max<LayoutUnit>(left, shadow->x() + extentAndSpread);
1782     }
1783
1784     return LayoutBoxExtent(WTFMove(top), WTFMove(right), WTFMove(bottom), WTFMove(left));
1785 }
1786
1787 void RenderStyle::getShadowHorizontalExtent(const ShadowData* shadow, LayoutUnit &left, LayoutUnit &right) const
1788 {
1789     left = 0;
1790     right = 0;
1791
1792     for ( ; shadow; shadow = shadow->next()) {
1793         if (shadow->style() == Inset)
1794             continue;
1795
1796         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1797         left = std::min<LayoutUnit>(left, shadow->x() - extentAndSpread);
1798         right = std::max<LayoutUnit>(right, shadow->x() + extentAndSpread);
1799     }
1800 }
1801
1802 void RenderStyle::getShadowVerticalExtent(const ShadowData* shadow, LayoutUnit &top, LayoutUnit &bottom) const
1803 {
1804     top = 0;
1805     bottom = 0;
1806
1807     for ( ; shadow; shadow = shadow->next()) {
1808         if (shadow->style() == Inset)
1809             continue;
1810
1811         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1812         top = std::min<LayoutUnit>(top, shadow->y() - extentAndSpread);
1813         bottom = std::max<LayoutUnit>(bottom, shadow->y() + extentAndSpread);
1814     }
1815 }
1816
1817 Color RenderStyle::colorIncludingFallback(CSSPropertyID colorProperty, bool visitedLink) const
1818 {
1819     Color result;
1820     BorderStyle borderStyle = BorderStyle::None;
1821     switch (colorProperty) {
1822     case CSSPropertyBackgroundColor:
1823         return visitedLink ? visitedLinkBackgroundColor() : backgroundColor(); // Background color doesn't fall back.
1824     case CSSPropertyBorderLeftColor:
1825         result = visitedLink ? visitedLinkBorderLeftColor() : borderLeftColor();
1826         borderStyle = borderLeftStyle();
1827         break;
1828     case CSSPropertyBorderRightColor:
1829         result = visitedLink ? visitedLinkBorderRightColor() : borderRightColor();
1830         borderStyle = borderRightStyle();
1831         break;
1832     case CSSPropertyBorderTopColor:
1833         result = visitedLink ? visitedLinkBorderTopColor() : borderTopColor();
1834         borderStyle = borderTopStyle();
1835         break;
1836     case CSSPropertyBorderBottomColor:
1837         result = visitedLink ? visitedLinkBorderBottomColor() : borderBottomColor();
1838         borderStyle = borderBottomStyle();
1839         break;
1840     case CSSPropertyCaretColor:
1841         result = visitedLink ? visitedLinkCaretColor() : caretColor();
1842         break;
1843     case CSSPropertyColor:
1844         result = visitedLink ? visitedLinkColor() : color();
1845         break;
1846     case CSSPropertyOutlineColor:
1847         result = visitedLink ? visitedLinkOutlineColor() : outlineColor();
1848         break;
1849     case CSSPropertyColumnRuleColor:
1850         result = visitedLink ? visitedLinkColumnRuleColor() : columnRuleColor();
1851         break;
1852     case CSSPropertyWebkitTextDecorationColor:
1853         // Text decoration color fallback is handled in RenderObject::decorationColor.
1854         return visitedLink ? visitedLinkTextDecorationColor() : textDecorationColor();
1855     case CSSPropertyWebkitTextEmphasisColor:
1856         result = visitedLink ? visitedLinkTextEmphasisColor() : textEmphasisColor();
1857         break;
1858     case CSSPropertyWebkitTextFillColor:
1859         result = visitedLink ? visitedLinkTextFillColor() : textFillColor();
1860         break;
1861     case CSSPropertyWebkitTextStrokeColor:
1862         result = visitedLink ? visitedLinkTextStrokeColor() : textStrokeColor();
1863         break;
1864     case CSSPropertyStrokeColor:
1865         result = visitedLink ? visitedLinkStrokeColor() : strokeColor();
1866         break;
1867     default:
1868         ASSERT_NOT_REACHED();
1869         break;
1870     }
1871
1872     if (!result.isValid()) {
1873         if (!visitedLink && (borderStyle == BorderStyle::Inset || borderStyle == BorderStyle::Outset || borderStyle == BorderStyle::Ridge || borderStyle == BorderStyle::Groove))
1874             result = Color(238, 238, 238);
1875         else
1876             result = visitedLink ? visitedLinkColor() : color();
1877     }
1878     return result;
1879 }
1880
1881 Color RenderStyle::visitedDependentColor(CSSPropertyID colorProperty) const
1882 {
1883     Color unvisitedColor = colorIncludingFallback(colorProperty, false);
1884     if (insideLink() != InsideLink::InsideVisited)
1885         return unvisitedColor;
1886
1887     Color visitedColor = colorIncludingFallback(colorProperty, true);
1888
1889     // Text decoration color validity is preserved (checked in RenderObject::decorationColor).
1890     if (colorProperty == CSSPropertyWebkitTextDecorationColor)
1891         return visitedColor;
1892
1893     // FIXME: Technically someone could explicitly specify the color transparent, but for now we'll just
1894     // assume that if the background color is transparent that it wasn't set. Note that it's weird that
1895     // we're returning unvisited info for a visited link, but given our restriction that the alpha values
1896     // have to match, it makes more sense to return the unvisited background color if specified than it
1897     // does to return black. This behavior matches what Firefox 4 does as well.
1898     if (colorProperty == CSSPropertyBackgroundColor && visitedColor == Color::transparent)
1899         return unvisitedColor;
1900
1901     // Take the alpha from the unvisited color, but get the RGB values from the visited color.
1902     return visitedColor.colorWithAlpha(unvisitedColor.alphaAsFloat());
1903 }
1904
1905 Color RenderStyle::visitedDependentColorWithColorFilter(CSSPropertyID colorProperty) const
1906 {
1907     if (!hasAppleColorFilter())
1908         return visitedDependentColor(colorProperty);
1909
1910     return colorByApplyingColorFilter(visitedDependentColor(colorProperty));
1911 }
1912
1913 Color RenderStyle::colorByApplyingColorFilter(const Color& color) const
1914 {
1915     Color transformedColor = color;
1916     appleColorFilter().transformColor(transformedColor);
1917     return transformedColor;
1918 }
1919
1920 const BorderValue& RenderStyle::borderBefore() const
1921 {
1922     switch (writingMode()) {
1923     case TopToBottomWritingMode:
1924         return borderTop();
1925     case BottomToTopWritingMode:
1926         return borderBottom();
1927     case LeftToRightWritingMode:
1928         return borderLeft();
1929     case RightToLeftWritingMode:
1930         return borderRight();
1931     }
1932     ASSERT_NOT_REACHED();
1933     return borderTop();
1934 }
1935
1936 const BorderValue& RenderStyle::borderAfter() const
1937 {
1938     switch (writingMode()) {
1939     case TopToBottomWritingMode:
1940         return borderBottom();
1941     case BottomToTopWritingMode:
1942         return borderTop();
1943     case LeftToRightWritingMode:
1944         return borderRight();
1945     case RightToLeftWritingMode:
1946         return borderLeft();
1947     }
1948     ASSERT_NOT_REACHED();
1949     return borderBottom();
1950 }
1951
1952 const BorderValue& RenderStyle::borderStart() const
1953 {
1954     if (isHorizontalWritingMode())
1955         return isLeftToRightDirection() ? borderLeft() : borderRight();
1956     return isLeftToRightDirection() ? borderTop() : borderBottom();
1957 }
1958
1959 const BorderValue& RenderStyle::borderEnd() const
1960 {
1961     if (isHorizontalWritingMode())
1962         return isLeftToRightDirection() ? borderRight() : borderLeft();
1963     return isLeftToRightDirection() ? borderBottom() : borderTop();
1964 }
1965
1966 float RenderStyle::borderBeforeWidth() const
1967 {
1968     switch (writingMode()) {
1969     case TopToBottomWritingMode:
1970         return borderTopWidth();
1971     case BottomToTopWritingMode:
1972         return borderBottomWidth();
1973     case LeftToRightWritingMode:
1974         return borderLeftWidth();
1975     case RightToLeftWritingMode:
1976         return borderRightWidth();
1977     }
1978     ASSERT_NOT_REACHED();
1979     return borderTopWidth();
1980 }
1981
1982 float RenderStyle::borderAfterWidth() const
1983 {
1984     switch (writingMode()) {
1985     case TopToBottomWritingMode:
1986         return borderBottomWidth();
1987     case BottomToTopWritingMode:
1988         return borderTopWidth();
1989     case LeftToRightWritingMode:
1990         return borderRightWidth();
1991     case RightToLeftWritingMode:
1992         return borderLeftWidth();
1993     }
1994     ASSERT_NOT_REACHED();
1995     return borderBottomWidth();
1996 }
1997
1998 float RenderStyle::borderStartWidth() const
1999 {
2000     if (isHorizontalWritingMode())
2001         return isLeftToRightDirection() ? borderLeftWidth() : borderRightWidth();
2002     return isLeftToRightDirection() ? borderTopWidth() : borderBottomWidth();
2003 }
2004
2005 float RenderStyle::borderEndWidth() const
2006 {
2007     if (isHorizontalWritingMode())
2008         return isLeftToRightDirection() ? borderRightWidth() : borderLeftWidth();
2009     return isLeftToRightDirection() ? borderBottomWidth() : borderTopWidth();
2010 }
2011
2012 void RenderStyle::setMarginStart(Length&& margin)
2013 {
2014     if (isHorizontalWritingMode()) {
2015         if (isLeftToRightDirection())
2016             setMarginLeft(WTFMove(margin));
2017         else
2018             setMarginRight(WTFMove(margin));
2019     } else {
2020         if (isLeftToRightDirection())
2021             setMarginTop(WTFMove(margin));
2022         else
2023             setMarginBottom(WTFMove(margin));
2024     }
2025 }
2026
2027 void RenderStyle::setMarginEnd(Length&& margin)
2028 {
2029     if (isHorizontalWritingMode()) {
2030         if (isLeftToRightDirection())
2031             setMarginRight(WTFMove(margin));
2032         else
2033             setMarginLeft(WTFMove(margin));
2034     } else {
2035         if (isLeftToRightDirection())
2036             setMarginBottom(WTFMove(margin));
2037         else
2038             setMarginTop(WTFMove(margin));
2039     }
2040 }
2041
2042 TextEmphasisMark RenderStyle::textEmphasisMark() const
2043 {
2044     auto mark = static_cast<TextEmphasisMark>(m_rareInheritedData->textEmphasisMark);
2045     if (mark != TextEmphasisMark::Auto)
2046         return mark;
2047     if (isHorizontalWritingMode())
2048         return TextEmphasisMark::Dot;
2049     return TextEmphasisMark::Sesame;
2050 }
2051
2052 #if ENABLE(TOUCH_EVENTS)
2053
2054 Color RenderStyle::initialTapHighlightColor()
2055 {
2056     return RenderTheme::tapHighlightColor();
2057 }
2058
2059 #endif
2060
2061 LayoutBoxExtent RenderStyle::imageOutsets(const NinePieceImage& image) const
2062 {
2063     return LayoutBoxExtent(NinePieceImage::computeOutset(image.outset().top(), borderTopWidth()),
2064                            NinePieceImage::computeOutset(image.outset().right(), borderRightWidth()),
2065                            NinePieceImage::computeOutset(image.outset().bottom(), borderBottomWidth()),
2066                            NinePieceImage::computeOutset(image.outset().left(), borderLeftWidth()));
2067 }
2068
2069 std::pair<FontOrientation, NonCJKGlyphOrientation> RenderStyle::fontAndGlyphOrientation()
2070 {
2071     // FIXME: TextOrientationSideways should map to sideways-left in vertical-lr, which is not supported yet.
2072
2073     if (isHorizontalWritingMode())
2074         return { FontOrientation::Horizontal, NonCJKGlyphOrientation::Mixed };
2075
2076     switch (textOrientation()) {
2077     case TextOrientation::Mixed:
2078         return { FontOrientation::Vertical, NonCJKGlyphOrientation::Mixed };
2079     case TextOrientation::Upright:
2080         return { FontOrientation::Vertical, NonCJKGlyphOrientation::Upright };
2081     case TextOrientation::Sideways:
2082         return { FontOrientation::Horizontal, NonCJKGlyphOrientation::Mixed };
2083     default:
2084         ASSERT_NOT_REACHED();
2085         return { FontOrientation::Horizontal, NonCJKGlyphOrientation::Mixed };
2086     }
2087 }
2088
2089 void RenderStyle::setBorderImageSource(RefPtr<StyleImage>&& image)
2090 {
2091     if (m_surroundData->border.m_image.image() == image.get())
2092         return;
2093     m_surroundData.access().border.m_image.setImage(WTFMove(image));
2094 }
2095
2096 void RenderStyle::setBorderImageSlices(LengthBox&& slices)
2097 {
2098     if (m_surroundData->border.m_image.imageSlices() == slices)
2099         return;
2100     m_surroundData.access().border.m_image.setImageSlices(WTFMove(slices));
2101 }
2102
2103 void RenderStyle::setBorderImageWidth(LengthBox&& slices)
2104 {
2105     if (m_surroundData->border.m_image.borderSlices() == slices)
2106         return;
2107     m_surroundData.access().border.m_image.setBorderSlices(WTFMove(slices));
2108 }
2109
2110 void RenderStyle::setBorderImageOutset(LengthBox&& outset)
2111 {
2112     if (m_surroundData->border.m_image.outset() == outset)
2113         return;
2114     m_surroundData.access().border.m_image.setOutset(WTFMove(outset));
2115 }
2116
2117 void RenderStyle::setColumnStylesFromPaginationMode(const Pagination::Mode& paginationMode)
2118 {
2119     if (paginationMode == Pagination::Unpaginated)
2120         return;
2121     
2122     setColumnFill(ColumnFill::Auto);
2123     
2124     switch (paginationMode) {
2125     case Pagination::LeftToRightPaginated:
2126         setColumnAxis(ColumnAxis::Horizontal);
2127         if (isHorizontalWritingMode())
2128             setColumnProgression(isLeftToRightDirection() ? ColumnProgression::Normal : ColumnProgression::Reverse);
2129         else
2130             setColumnProgression(isFlippedBlocksWritingMode() ? ColumnProgression::Reverse : ColumnProgression::Normal);
2131         break;
2132     case Pagination::RightToLeftPaginated:
2133         setColumnAxis(ColumnAxis::Horizontal);
2134         if (isHorizontalWritingMode())
2135             setColumnProgression(isLeftToRightDirection() ? ColumnProgression::Reverse : ColumnProgression::Normal);
2136         else
2137             setColumnProgression(isFlippedBlocksWritingMode() ? ColumnProgression::Normal : ColumnProgression::Reverse);
2138         break;
2139     case Pagination::TopToBottomPaginated:
2140         setColumnAxis(ColumnAxis::Vertical);
2141         if (isHorizontalWritingMode())
2142             setColumnProgression(isFlippedBlocksWritingMode() ? ColumnProgression::Reverse : ColumnProgression::Normal);
2143         else
2144             setColumnProgression(isLeftToRightDirection() ? ColumnProgression::Normal : ColumnProgression::Reverse);
2145         break;
2146     case Pagination::BottomToTopPaginated:
2147         setColumnAxis(ColumnAxis::Vertical);
2148         if (isHorizontalWritingMode())
2149             setColumnProgression(isFlippedBlocksWritingMode() ? ColumnProgression::Normal : ColumnProgression::Reverse);
2150         else
2151             setColumnProgression(isLeftToRightDirection() ? ColumnProgression::Reverse : ColumnProgression::Normal);
2152         break;
2153     case Pagination::Unpaginated:
2154         ASSERT_NOT_REACHED();
2155         break;
2156     }
2157 }
2158
2159 #if ENABLE(CSS_SCROLL_SNAP)
2160
2161 ScrollSnapType RenderStyle::initialScrollSnapType()
2162 {
2163     return { };
2164 }
2165
2166 ScrollSnapAlign RenderStyle::initialScrollSnapAlign()
2167 {
2168     return { };
2169 }
2170
2171 const StyleScrollSnapArea& RenderStyle::scrollSnapArea() const
2172 {
2173     return *m_rareNonInheritedData->scrollSnapArea;
2174 }
2175
2176 const StyleScrollSnapPort& RenderStyle::scrollSnapPort() const
2177 {
2178     return *m_rareNonInheritedData->scrollSnapPort;
2179 }
2180
2181 const ScrollSnapType& RenderStyle::scrollSnapType() const
2182 {
2183     return m_rareNonInheritedData->scrollSnapPort->type;
2184 }
2185
2186 const LengthBox& RenderStyle::scrollPadding() const
2187 {
2188     return m_rareNonInheritedData->scrollSnapPort->scrollPadding;
2189 }
2190
2191 const Length& RenderStyle::scrollPaddingTop() const
2192 {
2193     return scrollPadding().top();
2194 }
2195
2196 const Length& RenderStyle::scrollPaddingBottom() const
2197 {
2198     return scrollPadding().bottom();
2199 }
2200
2201 const Length& RenderStyle::scrollPaddingLeft() const
2202 {
2203     return scrollPadding().left();
2204 }
2205
2206 const Length& RenderStyle::scrollPaddingRight() const
2207 {
2208     return scrollPadding().right();
2209 }
2210
2211 const ScrollSnapAlign& RenderStyle::scrollSnapAlign() const
2212 {
2213     return m_rareNonInheritedData->scrollSnapArea->alignment;
2214 }
2215
2216 const LengthBox& RenderStyle::scrollSnapMargin() const
2217 {
2218     return m_rareNonInheritedData->scrollSnapArea->scrollSnapMargin;
2219 }
2220
2221 const Length& RenderStyle::scrollSnapMarginTop() const
2222 {
2223     return scrollSnapMargin().top();
2224 }
2225
2226 const Length& RenderStyle::scrollSnapMarginBottom() const
2227 {
2228     return scrollSnapMargin().bottom();
2229 }
2230
2231 const Length& RenderStyle::scrollSnapMarginLeft() const
2232 {
2233     return scrollSnapMargin().left();
2234 }
2235
2236 const Length& RenderStyle::scrollSnapMarginRight() const
2237 {
2238     return scrollSnapMargin().right();
2239 }
2240
2241 void RenderStyle::setScrollSnapType(const ScrollSnapType& type)
2242 {
2243     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapPort, type, type);
2244 }
2245
2246 void RenderStyle::setScrollPaddingTop(Length&& length)
2247 {
2248     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapPort, scrollPadding.top(), WTFMove(length));
2249 }
2250
2251 void RenderStyle::setScrollPaddingBottom(Length&& length)
2252 {
2253     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapPort, scrollPadding.bottom(), WTFMove(length));
2254 }
2255
2256 void RenderStyle::setScrollPaddingLeft(Length&& length)
2257 {
2258     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapPort, scrollPadding.left(), WTFMove(length));
2259 }
2260
2261 void RenderStyle::setScrollPaddingRight(Length&& length)
2262 {
2263     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapPort, scrollPadding.right(), WTFMove(length));
2264 }
2265
2266 void RenderStyle::setScrollSnapAlign(const ScrollSnapAlign& alignment)
2267 {
2268     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapArea, alignment, alignment);
2269 }
2270
2271 void RenderStyle::setScrollSnapMarginTop(Length&& length)
2272 {
2273     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapArea, scrollSnapMargin.top(), WTFMove(length));
2274 }
2275
2276 void RenderStyle::setScrollSnapMarginBottom(Length&& length)
2277 {
2278     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapArea, scrollSnapMargin.bottom(), WTFMove(length));
2279 }
2280
2281 void RenderStyle::setScrollSnapMarginLeft(Length&& length)
2282 {
2283     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapArea, scrollSnapMargin.left(), WTFMove(length));
2284 }
2285
2286 void RenderStyle::setScrollSnapMarginRight(Length&& length)
2287 {
2288     SET_NESTED_VAR(m_rareNonInheritedData, scrollSnapArea, scrollSnapMargin.right(), WTFMove(length));
2289 }
2290
2291 #endif
2292
2293 bool RenderStyle::hasReferenceFilterOnly() const
2294 {
2295     if (!hasFilter())
2296         return false;
2297     auto& filterOperations = m_rareNonInheritedData->filter->operations;
2298     return filterOperations.size() == 1 && filterOperations.at(0)->type() == FilterOperation::REFERENCE;
2299 }
2300
2301 void RenderStyle::checkVariablesInCustomProperties(const CSSRegisteredCustomPropertySet& registeredProperties)
2302 {
2303     if (!m_rareInheritedData->customProperties->containsVariables)
2304         return;
2305
2306     auto& customPropertyData = m_rareInheritedData.access().customProperties.access();
2307
2308     // Our first pass checks the variables for validity and replaces any properties that became
2309     // invalid with empty values.
2310     auto& customProperties = customPropertyData.values;
2311     HashSet<AtomicString> invalidProperties;
2312     for (auto entry : customProperties) {
2313         if (!entry.value->containsVariables())
2314             continue;
2315         HashSet<AtomicString> seenProperties;
2316         entry.value->checkVariablesForCycles(entry.key, customProperties, seenProperties, invalidProperties);
2317     }
2318     
2319     // Now insert invalid values.
2320     if (!invalidProperties.isEmpty()) {
2321         auto invalidValue = CSSCustomPropertyValue::createInvalid();
2322         for (auto& property : invalidProperties)
2323             customProperties.set(property, invalidValue.copyRef());
2324     }
2325
2326     // Now that all of the properties have been tested for validity and replaced with
2327     // invalid values if they failed, we can perform variable substitution on the valid values.
2328     Vector<Ref<CSSCustomPropertyValue>> resolvedValues;
2329     for (auto entry : customProperties) {
2330         if (!entry.value->containsVariables())
2331             continue;
2332         entry.value->resolveVariableReferences(customProperties, registeredProperties, resolvedValues);
2333     }
2334     
2335     // With all results computed, we can now mutate our table to eliminate the variables and
2336     // hold the final values. This way when we inherit, we don't end up resubstituting variables, etc.
2337     for (auto& resolvedValue : resolvedValues)
2338         customProperties.set(resolvedValue->name(), resolvedValue.copyRef());
2339
2340     customPropertyData.containsVariables = false;
2341 }
2342
2343 float RenderStyle::outlineWidth() const
2344 {
2345     if (m_backgroundData->outline.style() == BorderStyle::None)
2346         return 0;
2347     if (outlineStyleIsAuto() == OutlineIsAuto::On)
2348         return std::max(m_backgroundData->outline.width(), RenderTheme::platformFocusRingWidth());
2349     return m_backgroundData->outline.width();
2350 }
2351
2352 float RenderStyle::outlineOffset() const
2353 {
2354     if (m_backgroundData->outline.style() == BorderStyle::None)
2355         return 0;
2356     if (outlineStyleIsAuto() == OutlineIsAuto::On)
2357         return (m_backgroundData->outline.offset() + RenderTheme::platformFocusRingOffset(outlineWidth()));
2358     return m_backgroundData->outline.offset();
2359 }
2360
2361 bool RenderStyle::shouldPlaceBlockDirectionScrollbarOnLeft() const
2362 {
2363     return !isLeftToRightDirection() && isHorizontalWritingMode();
2364 }
2365
2366 #if ENABLE(DASHBOARD_SUPPORT)
2367
2368 void RenderStyle::setDashboardRegion(int type, const String& label, Length&& top, Length&& right, Length&& bottom, Length&& left, bool append)
2369 {
2370     if (!append)
2371         m_rareNonInheritedData.access().dashboardRegions.clear();
2372     m_rareNonInheritedData.access().dashboardRegions.append({ label, { WTFMove(top), WTFMove(right), WTFMove(bottom), WTFMove(left) }, type });
2373 }
2374
2375 #endif
2376
2377 Vector<PaintType, 3> RenderStyle::paintTypesForPaintOrder(PaintOrder order)
2378 {
2379     Vector<PaintType, 3> paintOrder;
2380     switch (order) {
2381     case PaintOrder::Normal:
2382         FALLTHROUGH;
2383     case PaintOrder::Fill:
2384         paintOrder.append(PaintType::Fill);
2385         paintOrder.append(PaintType::Stroke);
2386         paintOrder.append(PaintType::Markers);
2387         break;
2388     case PaintOrder::FillMarkers:
2389         paintOrder.append(PaintType::Fill);
2390         paintOrder.append(PaintType::Markers);
2391         paintOrder.append(PaintType::Stroke);
2392         break;
2393     case PaintOrder::Stroke:
2394         paintOrder.append(PaintType::Stroke);
2395         paintOrder.append(PaintType::Fill);
2396         paintOrder.append(PaintType::Markers);
2397         break;
2398     case PaintOrder::StrokeMarkers:
2399         paintOrder.append(PaintType::Stroke);
2400         paintOrder.append(PaintType::Markers);
2401         paintOrder.append(PaintType::Fill);
2402         break;
2403     case PaintOrder::Markers:
2404         paintOrder.append(PaintType::Markers);
2405         paintOrder.append(PaintType::Fill);
2406         paintOrder.append(PaintType::Stroke);
2407         break;
2408     case PaintOrder::MarkersStroke:
2409         paintOrder.append(PaintType::Markers);
2410         paintOrder.append(PaintType::Stroke);
2411         paintOrder.append(PaintType::Fill);
2412         break;
2413     };
2414     return paintOrder;
2415 }
2416
2417 float RenderStyle::computedStrokeWidth(const IntSize& viewportSize) const
2418 {
2419     // Use the stroke-width and stroke-color value combination only if stroke-color has been explicitly specified.
2420     // Since there will be no visible stroke when stroke-color is not specified (transparent by default), we fall
2421     // back to the legacy Webkit text stroke combination in that case.
2422     if (!hasExplicitlySetStrokeColor())
2423         return textStrokeWidth();
2424     
2425     const Length& length = strokeWidth();
2426
2427     if (length.isPercent()) {
2428         // According to the spec, https://drafts.fxtf.org/paint/#stroke-width, the percentage is relative to the scaled viewport size.
2429         // The scaled viewport size is the geometric mean of the viewport width and height.
2430         ExceptionOr<float> result = length.value() * (viewportSize.width() + viewportSize.height()) / 200.0f;
2431         if (result.hasException())
2432             return 0;
2433         return result.releaseReturnValue();
2434     }
2435     
2436     if (length.isAuto() || !length.isSpecified())
2437         return 0;
2438     
2439     return floatValueForLength(length, viewportSize.width());
2440 }
2441
2442 bool RenderStyle::hasPositiveStrokeWidth() const
2443 {
2444     if (!hasExplicitlySetStrokeWidth())
2445         return textStrokeWidth() > 0;
2446
2447     return strokeWidth().isPositive();
2448 }
2449
2450 Color RenderStyle::computedStrokeColor() const
2451 {
2452     CSSPropertyID propertyID = CSSPropertyStrokeColor;
2453     if (!hasExplicitlySetStrokeColor())
2454         propertyID = CSSPropertyWebkitTextStrokeColor;
2455     return visitedDependentColor(propertyID);
2456 }
2457
2458 } // namespace WebCore