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