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