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