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