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