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