• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #include "config.h"
23 #include "RenderStyle.h"
24 
25 #include "CSSStyleSelector.h"
26 #include "CachedImage.h"
27 #include "CounterContent.h"
28 #include "FontSelector.h"
29 #include "RenderArena.h"
30 #include "RenderObject.h"
31 #include "StyleImage.h"
32 #include <wtf/StdLibExtras.h>
33 #include <algorithm>
34 
35 using namespace std;
36 
37 namespace WebCore {
38 
defaultStyle()39 inline RenderStyle* defaultStyle()
40 {
41     static RenderStyle* s_defaultStyle = RenderStyle::createDefaultStyle().releaseRef();
42     return s_defaultStyle;
43 }
44 
create()45 PassRefPtr<RenderStyle> RenderStyle::create()
46 {
47     return adoptRef(new RenderStyle());
48 }
49 
createDefaultStyle()50 PassRefPtr<RenderStyle> RenderStyle::createDefaultStyle()
51 {
52     return adoptRef(new RenderStyle(true));
53 }
54 
clone(const RenderStyle * other)55 PassRefPtr<RenderStyle> RenderStyle::clone(const RenderStyle* other)
56 {
57     return adoptRef(new RenderStyle(*other));
58 }
59 
RenderStyle()60 RenderStyle::RenderStyle()
61     : m_pseudoState(PseudoUnknown)
62     , m_affectedByAttributeSelectors(false)
63     , m_unique(false)
64     , m_affectedByEmpty(false)
65     , m_emptyState(false)
66     , m_childrenAffectedByFirstChildRules(false)
67     , m_childrenAffectedByLastChildRules(false)
68     , m_childrenAffectedByDirectAdjacentRules(false)
69     , m_childrenAffectedByForwardPositionalRules(false)
70     , m_childrenAffectedByBackwardPositionalRules(false)
71     , m_firstChildState(false)
72     , m_lastChildState(false)
73     , m_childIndex(0)
74     , box(defaultStyle()->box)
75     , visual(defaultStyle()->visual)
76     , background(defaultStyle()->background)
77     , surround(defaultStyle()->surround)
78     , rareNonInheritedData(defaultStyle()->rareNonInheritedData)
79     , rareInheritedData(defaultStyle()->rareInheritedData)
80     , inherited(defaultStyle()->inherited)
81 #if ENABLE(SVG)
82     , m_svgStyle(defaultStyle()->m_svgStyle)
83 #endif
84 {
85     setBitDefaults(); // Would it be faster to copy this from the default style?
86 }
87 
RenderStyle(bool)88 RenderStyle::RenderStyle(bool)
89     : m_pseudoState(PseudoUnknown)
90     , m_affectedByAttributeSelectors(false)
91     , m_unique(false)
92     , m_affectedByEmpty(false)
93     , m_emptyState(false)
94     , m_childrenAffectedByFirstChildRules(false)
95     , m_childrenAffectedByLastChildRules(false)
96     , m_childrenAffectedByDirectAdjacentRules(false)
97     , m_childrenAffectedByForwardPositionalRules(false)
98     , m_childrenAffectedByBackwardPositionalRules(false)
99     , m_firstChildState(false)
100     , m_lastChildState(false)
101     , m_childIndex(0)
102 {
103     setBitDefaults();
104 
105     box.init();
106     visual.init();
107     background.init();
108     surround.init();
109     rareNonInheritedData.init();
110     rareNonInheritedData.access()->flexibleBox.init();
111     rareNonInheritedData.access()->marquee.init();
112     rareNonInheritedData.access()->m_multiCol.init();
113     rareNonInheritedData.access()->m_transform.init();
114     rareInheritedData.init();
115     inherited.init();
116 
117 #if ENABLE(SVG)
118     m_svgStyle.init();
119 #endif
120 }
121 
RenderStyle(const RenderStyle & o)122 RenderStyle::RenderStyle(const RenderStyle& o)
123     : RefCounted<RenderStyle>()
124     , m_pseudoState(o.m_pseudoState)
125     , m_affectedByAttributeSelectors(false)
126     , m_unique(false)
127     , m_affectedByEmpty(false)
128     , m_emptyState(false)
129     , m_childrenAffectedByFirstChildRules(false)
130     , m_childrenAffectedByLastChildRules(false)
131     , m_childrenAffectedByDirectAdjacentRules(false)
132     , m_childrenAffectedByForwardPositionalRules(false)
133     , m_childrenAffectedByBackwardPositionalRules(false)
134     , m_firstChildState(false)
135     , m_lastChildState(false)
136     , m_childIndex(0)
137     , box(o.box)
138     , visual(o.visual)
139     , background(o.background)
140     , surround(o.surround)
141     , rareNonInheritedData(o.rareNonInheritedData)
142     , rareInheritedData(o.rareInheritedData)
143     , inherited(o.inherited)
144 #if ENABLE(SVG)
145     , m_svgStyle(o.m_svgStyle)
146 #endif
147     , inherited_flags(o.inherited_flags)
148     , noninherited_flags(o.noninherited_flags)
149 {
150 }
151 
inheritFrom(const RenderStyle * inheritParent)152 void RenderStyle::inheritFrom(const RenderStyle* inheritParent)
153 {
154     rareInheritedData = inheritParent->rareInheritedData;
155     inherited = inheritParent->inherited;
156     inherited_flags = inheritParent->inherited_flags;
157 #if ENABLE(SVG)
158     if (m_svgStyle != inheritParent->m_svgStyle)
159         m_svgStyle.access()->inheritFrom(inheritParent->m_svgStyle.get());
160 #endif
161 }
162 
~RenderStyle()163 RenderStyle::~RenderStyle()
164 {
165 }
166 
operator ==(const RenderStyle & o) const167 bool RenderStyle::operator==(const RenderStyle& o) const
168 {
169     // compare everything except the pseudoStyle pointer
170     return inherited_flags == o.inherited_flags &&
171             noninherited_flags == o.noninherited_flags &&
172             box == o.box &&
173             visual == o.visual &&
174             background == o.background &&
175             surround == o.surround &&
176             rareNonInheritedData == o.rareNonInheritedData &&
177             rareInheritedData == o.rareInheritedData &&
178             inherited == o.inherited
179 #if ENABLE(SVG)
180             && m_svgStyle == o.m_svgStyle
181 #endif
182             ;
183 }
184 
isStyleAvailable() const185 bool RenderStyle::isStyleAvailable() const
186 {
187     return this != CSSStyleSelector::styleNotYetAvailable();
188 }
189 
pseudoBit(PseudoId pseudo)190 static inline int pseudoBit(PseudoId pseudo)
191 {
192     return 1 << (pseudo - 1);
193 }
194 
hasPseudoStyle(PseudoId pseudo) const195 bool RenderStyle::hasPseudoStyle(PseudoId pseudo) const
196 {
197     ASSERT(pseudo > NOPSEUDO);
198     ASSERT(pseudo < FIRST_INTERNAL_PSEUDOID);
199     return pseudoBit(pseudo) & noninherited_flags._pseudoBits;
200 }
201 
setHasPseudoStyle(PseudoId pseudo)202 void RenderStyle::setHasPseudoStyle(PseudoId pseudo)
203 {
204     ASSERT(pseudo > NOPSEUDO);
205     ASSERT(pseudo < FIRST_INTERNAL_PSEUDOID);
206     noninherited_flags._pseudoBits |= pseudoBit(pseudo);
207 }
208 
getCachedPseudoStyle(PseudoId pid) const209 RenderStyle* RenderStyle::getCachedPseudoStyle(PseudoId pid) const
210 {
211     if (!m_cachedPseudoStyle || styleType() != NOPSEUDO)
212         return 0;
213     RenderStyle* ps = m_cachedPseudoStyle.get();
214     while (ps && ps->styleType() != pid)
215         ps = ps->m_cachedPseudoStyle.get();
216     return ps;
217 }
218 
addCachedPseudoStyle(PassRefPtr<RenderStyle> pseudo)219 RenderStyle* RenderStyle::addCachedPseudoStyle(PassRefPtr<RenderStyle> pseudo)
220 {
221     if (!pseudo)
222         return 0;
223     pseudo->m_cachedPseudoStyle = m_cachedPseudoStyle;
224     m_cachedPseudoStyle = pseudo;
225     return m_cachedPseudoStyle.get();
226 }
227 
getPseudoStyleCache(PseudoStyleCache & cache) const228 void RenderStyle::getPseudoStyleCache(PseudoStyleCache& cache) const
229 {
230     ASSERT(cache.isEmpty());
231     for (RenderStyle* pseudoStyle = m_cachedPseudoStyle.get(); pseudoStyle; pseudoStyle = pseudoStyle->m_cachedPseudoStyle.get())
232         cache.append(pseudoStyle);
233 }
234 
inheritedNotEqual(const RenderStyle * other) const235 bool RenderStyle::inheritedNotEqual(const RenderStyle* other) const
236 {
237     return inherited_flags != other->inherited_flags ||
238            inherited != other->inherited ||
239 #if ENABLE(SVG)
240            m_svgStyle->inheritedNotEqual(other->m_svgStyle.get()) ||
241 #endif
242            rareInheritedData != other->rareInheritedData;
243 }
244 
positionedObjectMoved(const LengthBox & a,const LengthBox & b)245 static bool positionedObjectMoved(const LengthBox& a, const LengthBox& b)
246 {
247     // If any unit types are different, then we can't guarantee
248     // that this was just a movement.
249     if (a.left().type() != b.left().type() ||
250         a.right().type() != b.right().type() ||
251         a.top().type() != b.top().type() ||
252         a.bottom().type() != b.bottom().type())
253         return false;
254 
255     // Only one unit can be non-auto in the horizontal direction and
256     // in the vertical direction.  Otherwise the adjustment of values
257     // is changing the size of the box.
258     if (!a.left().isIntrinsicOrAuto() && !a.right().isIntrinsicOrAuto())
259         return false;
260     if (!a.top().isIntrinsicOrAuto() && !a.bottom().isIntrinsicOrAuto())
261         return false;
262 
263     // One of the units is fixed or percent in both directions and stayed
264     // that way in the new style.  Therefore all we are doing is moving.
265     return true;
266 }
267 
268 /*
269   compares two styles. The result gives an idea of the action that
270   needs to be taken when replacing the old style with a new one.
271 
272   CbLayout: The containing block of the object needs a relayout.
273   Layout: the RenderObject needs a relayout after the style change
274   Visible: The change is visible, but no relayout is needed
275   NonVisible: The object does need neither repaint nor relayout after
276        the change.
277 
278   ### TODO:
279   A lot can be optimised here based on the display type, lots of
280   optimisations are unimplemented, and currently result in the
281   worst case result causing a relayout of the containing block.
282 */
diff(const RenderStyle * other,unsigned & changedContextSensitiveProperties) const283 StyleDifference RenderStyle::diff(const RenderStyle* other, unsigned& changedContextSensitiveProperties) const
284 {
285     changedContextSensitiveProperties = ContextSensitivePropertyNone;
286 
287 #if ENABLE(SVG)
288     // This is horribly inefficient.  Eventually we'll have to integrate
289     // this more directly by calling: Diff svgDiff = svgStyle->diff(other)
290     // and then checking svgDiff and returning from the appropriate places below.
291     if (m_svgStyle != other->m_svgStyle)
292         return StyleDifferenceLayout;
293 #endif
294 
295     if (box->width != other->box->width ||
296         box->min_width != other->box->min_width ||
297         box->max_width != other->box->max_width ||
298         box->height != other->box->height ||
299         box->min_height != other->box->min_height ||
300         box->max_height != other->box->max_height)
301         return StyleDifferenceLayout;
302 
303     if (box->vertical_align != other->box->vertical_align || noninherited_flags._vertical_align != other->noninherited_flags._vertical_align)
304         return StyleDifferenceLayout;
305 
306     if (box->boxSizing != other->box->boxSizing)
307         return StyleDifferenceLayout;
308 
309     if (surround->margin != other->surround->margin)
310         return StyleDifferenceLayout;
311 
312     if (surround->padding != other->surround->padding)
313         return StyleDifferenceLayout;
314 
315     if (rareNonInheritedData.get() != other->rareNonInheritedData.get()) {
316         if (rareNonInheritedData->m_appearance != other->rareNonInheritedData->m_appearance ||
317             rareNonInheritedData->marginTopCollapse != other->rareNonInheritedData->marginTopCollapse ||
318             rareNonInheritedData->marginBottomCollapse != other->rareNonInheritedData->marginBottomCollapse ||
319             rareNonInheritedData->lineClamp != other->rareNonInheritedData->lineClamp ||
320             rareNonInheritedData->textOverflow != other->rareNonInheritedData->textOverflow)
321             return StyleDifferenceLayout;
322 
323         if (rareNonInheritedData->flexibleBox.get() != other->rareNonInheritedData->flexibleBox.get() &&
324             *rareNonInheritedData->flexibleBox.get() != *other->rareNonInheritedData->flexibleBox.get())
325             return StyleDifferenceLayout;
326 
327         if (!rareNonInheritedData->shadowDataEquivalent(*other->rareNonInheritedData.get()))
328             return StyleDifferenceLayout;
329 
330         if (!rareNonInheritedData->reflectionDataEquivalent(*other->rareNonInheritedData.get()))
331             return StyleDifferenceLayout;
332 
333         if (rareNonInheritedData->m_multiCol.get() != other->rareNonInheritedData->m_multiCol.get() &&
334             *rareNonInheritedData->m_multiCol.get() != *other->rareNonInheritedData->m_multiCol.get())
335             return StyleDifferenceLayout;
336 
337         if (rareNonInheritedData->m_transform.get() != other->rareNonInheritedData->m_transform.get() &&
338             *rareNonInheritedData->m_transform.get() != *other->rareNonInheritedData->m_transform.get()) {
339 #if USE(ACCELERATED_COMPOSITING)
340             changedContextSensitiveProperties |= ContextSensitivePropertyTransform;
341             // Don't return; keep looking for another change
342 #else
343             return StyleDifferenceLayout;
344 #endif
345         }
346 
347 #if !USE(ACCELERATED_COMPOSITING)
348         if (rareNonInheritedData.get() != other->rareNonInheritedData.get()) {
349             if (rareNonInheritedData->m_transformStyle3D != other->rareNonInheritedData->m_transformStyle3D ||
350                 rareNonInheritedData->m_backfaceVisibility != other->rareNonInheritedData->m_backfaceVisibility ||
351                 rareNonInheritedData->m_perspective != other->rareNonInheritedData->m_perspective ||
352                 rareNonInheritedData->m_perspectiveOriginX != other->rareNonInheritedData->m_perspectiveOriginX ||
353                 rareNonInheritedData->m_perspectiveOriginY != other->rareNonInheritedData->m_perspectiveOriginY)
354                 return StyleDifferenceLayout;
355         }
356 #endif
357 
358 #if ENABLE(DASHBOARD_SUPPORT)
359         // If regions change, trigger a relayout to re-calc regions.
360         if (rareNonInheritedData->m_dashboardRegions != other->rareNonInheritedData->m_dashboardRegions)
361             return StyleDifferenceLayout;
362 #endif
363     }
364 
365     if (rareInheritedData.get() != other->rareInheritedData.get()) {
366         if (rareInheritedData->highlight != other->rareInheritedData->highlight ||
367             rareInheritedData->textSizeAdjust != other->rareInheritedData->textSizeAdjust ||
368             rareInheritedData->wordBreak != other->rareInheritedData->wordBreak ||
369             rareInheritedData->wordWrap != other->rareInheritedData->wordWrap ||
370             rareInheritedData->nbspMode != other->rareInheritedData->nbspMode ||
371             rareInheritedData->khtmlLineBreak != other->rareInheritedData->khtmlLineBreak ||
372             rareInheritedData->textSecurity != other->rareInheritedData->textSecurity)
373             return StyleDifferenceLayout;
374 
375         if (!rareInheritedData->shadowDataEquivalent(*other->rareInheritedData.get()))
376             return StyleDifferenceLayout;
377 
378         if (textStrokeWidth() != other->textStrokeWidth())
379             return StyleDifferenceLayout;
380     }
381 
382     if (inherited->indent != other->inherited->indent ||
383         inherited->line_height != other->inherited->line_height ||
384         inherited->list_style_image != other->inherited->list_style_image ||
385         inherited->font != other->inherited->font ||
386         inherited->horizontal_border_spacing != other->inherited->horizontal_border_spacing ||
387         inherited->vertical_border_spacing != other->inherited->vertical_border_spacing ||
388         inherited_flags._box_direction != other->inherited_flags._box_direction ||
389         inherited_flags._visuallyOrdered != other->inherited_flags._visuallyOrdered ||
390         inherited_flags._htmlHacks != other->inherited_flags._htmlHacks ||
391         noninherited_flags._position != other->noninherited_flags._position ||
392         noninherited_flags._floating != other->noninherited_flags._floating ||
393         noninherited_flags._originalDisplay != other->noninherited_flags._originalDisplay)
394         return StyleDifferenceLayout;
395 
396 
397     if (((int)noninherited_flags._effectiveDisplay) >= TABLE) {
398         if (inherited_flags._border_collapse != other->inherited_flags._border_collapse ||
399             inherited_flags._empty_cells != other->inherited_flags._empty_cells ||
400             inherited_flags._caption_side != other->inherited_flags._caption_side ||
401             noninherited_flags._table_layout != other->noninherited_flags._table_layout)
402             return StyleDifferenceLayout;
403 
404         // In the collapsing border model, 'hidden' suppresses other borders, while 'none'
405         // does not, so these style differences can be width differences.
406         if (inherited_flags._border_collapse &&
407             ((borderTopStyle() == BHIDDEN && other->borderTopStyle() == BNONE) ||
408              (borderTopStyle() == BNONE && other->borderTopStyle() == BHIDDEN) ||
409              (borderBottomStyle() == BHIDDEN && other->borderBottomStyle() == BNONE) ||
410              (borderBottomStyle() == BNONE && other->borderBottomStyle() == BHIDDEN) ||
411              (borderLeftStyle() == BHIDDEN && other->borderLeftStyle() == BNONE) ||
412              (borderLeftStyle() == BNONE && other->borderLeftStyle() == BHIDDEN) ||
413              (borderRightStyle() == BHIDDEN && other->borderRightStyle() == BNONE) ||
414              (borderRightStyle() == BNONE && other->borderRightStyle() == BHIDDEN)))
415             return StyleDifferenceLayout;
416     }
417 
418     if (noninherited_flags._effectiveDisplay == LIST_ITEM) {
419         if (inherited_flags._list_style_type != other->inherited_flags._list_style_type ||
420             inherited_flags._list_style_position != other->inherited_flags._list_style_position)
421             return StyleDifferenceLayout;
422     }
423 
424     if (inherited_flags._text_align != other->inherited_flags._text_align ||
425         inherited_flags._text_transform != other->inherited_flags._text_transform ||
426         inherited_flags._direction != other->inherited_flags._direction ||
427         inherited_flags._white_space != other->inherited_flags._white_space ||
428         noninherited_flags._clear != other->noninherited_flags._clear)
429         return StyleDifferenceLayout;
430 
431     // Overflow returns a layout hint.
432     if (noninherited_flags._overflowX != other->noninherited_flags._overflowX ||
433         noninherited_flags._overflowY != other->noninherited_flags._overflowY)
434         return StyleDifferenceLayout;
435 
436     // If our border widths change, then we need to layout.  Other changes to borders
437     // only necessitate a repaint.
438     if (borderLeftWidth() != other->borderLeftWidth() ||
439         borderTopWidth() != other->borderTopWidth() ||
440         borderBottomWidth() != other->borderBottomWidth() ||
441         borderRightWidth() != other->borderRightWidth())
442         return StyleDifferenceLayout;
443 
444     // If the counter directives change, trigger a relayout to re-calculate counter values and rebuild the counter node tree.
445     const CounterDirectiveMap* mapA = rareNonInheritedData->m_counterDirectives.get();
446     const CounterDirectiveMap* mapB = other->rareNonInheritedData->m_counterDirectives.get();
447     if (!(mapA == mapB || (mapA && mapB && *mapA == *mapB)))
448         return StyleDifferenceLayout;
449     if (visual->counterIncrement != other->visual->counterIncrement ||
450         visual->counterReset != other->visual->counterReset)
451         return StyleDifferenceLayout;
452 
453     if (inherited->m_effectiveZoom != other->inherited->m_effectiveZoom)
454         return StyleDifferenceLayout;
455 
456     // Make sure these left/top/right/bottom checks stay below all layout checks and above
457     // all visible checks.
458     if (position() != StaticPosition) {
459         if (surround->offset != other->surround->offset) {
460              // Optimize for the case where a positioned layer is moving but not changing size.
461             if (position() == AbsolutePosition && positionedObjectMoved(surround->offset, other->surround->offset))
462                 return StyleDifferenceLayoutPositionedMovementOnly;
463 
464             // FIXME: We will need to do a bit of work in RenderObject/Box::setStyle before we
465             // can stop doing a layout when relative positioned objects move.  In particular, we'll need
466             // to update scrolling positions and figure out how to do a repaint properly of the updated layer.
467             //if (other->position() == RelativePosition)
468             //    return RepaintLayer;
469             //else
470                 return StyleDifferenceLayout;
471         } else if (box->z_index != other->box->z_index || box->z_auto != other->box->z_auto ||
472                  visual->clip != other->visual->clip || visual->hasClip != other->visual->hasClip)
473             return StyleDifferenceRepaintLayer;
474     }
475 
476     if (rareNonInheritedData->opacity != other->rareNonInheritedData->opacity) {
477 #if USE(ACCELERATED_COMPOSITING)
478         changedContextSensitiveProperties |= ContextSensitivePropertyOpacity;
479         // Don't return; keep looking for another change.
480 #else
481         return StyleDifferenceRepaintLayer;
482 #endif
483     }
484 
485     if (rareNonInheritedData->m_mask != other->rareNonInheritedData->m_mask ||
486         rareNonInheritedData->m_maskBoxImage != other->rareNonInheritedData->m_maskBoxImage)
487         return StyleDifferenceRepaintLayer;
488 
489     if (inherited->color != other->inherited->color ||
490         inherited_flags._visibility != other->inherited_flags._visibility ||
491         inherited_flags._text_decorations != other->inherited_flags._text_decorations ||
492         inherited_flags._force_backgrounds_to_white != other->inherited_flags._force_backgrounds_to_white ||
493         surround->border != other->surround->border ||
494         *background.get() != *other->background.get() ||
495         visual->textDecoration != other->visual->textDecoration ||
496         rareInheritedData->userModify != other->rareInheritedData->userModify ||
497         rareInheritedData->userSelect != other->rareInheritedData->userSelect ||
498         rareNonInheritedData->userDrag != other->rareNonInheritedData->userDrag ||
499         rareNonInheritedData->m_borderFit != other->rareNonInheritedData->m_borderFit ||
500         rareInheritedData->textFillColor != other->rareInheritedData->textFillColor ||
501         rareInheritedData->textStrokeColor != other->rareInheritedData->textStrokeColor)
502         return StyleDifferenceRepaint;
503 
504 #if USE(ACCELERATED_COMPOSITING)
505     if (rareNonInheritedData.get() != other->rareNonInheritedData.get()) {
506         if (rareNonInheritedData->m_transformStyle3D != other->rareNonInheritedData->m_transformStyle3D ||
507             rareNonInheritedData->m_backfaceVisibility != other->rareNonInheritedData->m_backfaceVisibility ||
508             rareNonInheritedData->m_perspective != other->rareNonInheritedData->m_perspective ||
509             rareNonInheritedData->m_perspectiveOriginX != other->rareNonInheritedData->m_perspectiveOriginX ||
510             rareNonInheritedData->m_perspectiveOriginY != other->rareNonInheritedData->m_perspectiveOriginY)
511             return StyleDifferenceRecompositeLayer;
512     }
513 #endif
514 
515     // Cursors are not checked, since they will be set appropriately in response to mouse events,
516     // so they don't need to cause any repaint or layout.
517 
518     // 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
519     // the resulting transition properly.
520     return StyleDifferenceEqual;
521 }
522 
setClip(Length top,Length right,Length bottom,Length left)523 void RenderStyle::setClip(Length top, Length right, Length bottom, Length left)
524 {
525     StyleVisualData* data = visual.access();
526     data->clip.m_top = top;
527     data->clip.m_right = right;
528     data->clip.m_bottom = bottom;
529     data->clip.m_left = left;
530 }
531 
addCursor(CachedImage * image,const IntPoint & hotSpot)532 void RenderStyle::addCursor(CachedImage* image, const IntPoint& hotSpot)
533 {
534     CursorData data;
535     data.cursorImage = image;
536     data.hotSpot = hotSpot;
537     if (!inherited.access()->cursorData)
538         inherited.access()->cursorData = CursorList::create();
539     inherited.access()->cursorData->append(data);
540 }
541 
setCursorList(PassRefPtr<CursorList> other)542 void RenderStyle::setCursorList(PassRefPtr<CursorList> other)
543 {
544     inherited.access()->cursorData = other;
545 }
546 
clearCursorList()547 void RenderStyle::clearCursorList()
548 {
549     if (inherited->cursorData)
550         inherited.access()->cursorData = 0;
551 }
552 
clearContent()553 void RenderStyle::clearContent()
554 {
555     if (rareNonInheritedData->m_content)
556         rareNonInheritedData->m_content->clear();
557 }
558 
setContent(PassRefPtr<StyleImage> image,bool add)559 void RenderStyle::setContent(PassRefPtr<StyleImage> image, bool add)
560 {
561     if (!image)
562         return; // The object is null. Nothing to do. Just bail.
563 
564     OwnPtr<ContentData>& content = rareNonInheritedData.access()->m_content;
565     ContentData* lastContent = content.get();
566     while (lastContent && lastContent->next())
567         lastContent = lastContent->next();
568 
569     bool reuseContent = !add;
570     ContentData* newContentData;
571     if (reuseContent && content) {
572         content->clear();
573         newContentData = content.release();
574     } else
575         newContentData = new ContentData;
576 
577     if (lastContent && !reuseContent)
578         lastContent->setNext(newContentData);
579     else
580         content.set(newContentData);
581 
582     newContentData->setImage(image);
583 }
584 
setContent(PassRefPtr<StringImpl> s,bool add)585 void RenderStyle::setContent(PassRefPtr<StringImpl> s, bool add)
586 {
587     if (!s)
588         return; // The string is null. Nothing to do. Just bail.
589 
590     OwnPtr<ContentData>& content = rareNonInheritedData.access()->m_content;
591     ContentData* lastContent = content.get();
592     while (lastContent && lastContent->next())
593         lastContent = lastContent->next();
594 
595     bool reuseContent = !add;
596     if (add && lastContent) {
597         if (lastContent->isText()) {
598             // We can augment the existing string and share this ContentData node.
599             String newStr = lastContent->text();
600             newStr.append(s.get());
601             lastContent->setText(newStr.impl());
602             return;
603         }
604     }
605 
606     ContentData* newContentData = 0;
607     if (reuseContent && content) {
608         content->clear();
609         newContentData = content.release();
610     } else
611         newContentData = new ContentData;
612 
613     if (lastContent && !reuseContent)
614         lastContent->setNext(newContentData);
615     else
616         content.set(newContentData);
617 
618     newContentData->setText(s);
619 }
620 
setContent(CounterContent * c,bool add)621 void RenderStyle::setContent(CounterContent* c, bool add)
622 {
623     if (!c)
624         return;
625 
626     OwnPtr<ContentData>& content = rareNonInheritedData.access()->m_content;
627     ContentData* lastContent = content.get();
628     while (lastContent && lastContent->next())
629         lastContent = lastContent->next();
630 
631     bool reuseContent = !add;
632     ContentData* newContentData = 0;
633     if (reuseContent && content) {
634         content->clear();
635         newContentData = content.release();
636     } else
637         newContentData = new ContentData;
638 
639     if (lastContent && !reuseContent)
640         lastContent->setNext(newContentData);
641     else
642         content.set(newContentData);
643 
644     newContentData->setCounter(c);
645 }
646 
applyTransform(TransformationMatrix & transform,const IntSize & borderBoxSize,ApplyTransformOrigin applyOrigin) const647 void RenderStyle::applyTransform(TransformationMatrix& transform, const IntSize& borderBoxSize, ApplyTransformOrigin applyOrigin) const
648 {
649     // transform-origin brackets the transform with translate operations.
650     // Optimize for the case where the only transform is a translation, since the transform-origin is irrelevant
651     // in that case.
652     bool applyTransformOrigin = false;
653     unsigned s = rareNonInheritedData->m_transform->m_operations.operations().size();
654     unsigned i;
655     if (applyOrigin == IncludeTransformOrigin) {
656         for (i = 0; i < s; i++) {
657             TransformOperation::OperationType type = rareNonInheritedData->m_transform->m_operations.operations()[i]->getOperationType();
658             if (type != TransformOperation::TRANSLATE_X &&
659                     type != TransformOperation::TRANSLATE_Y &&
660                     type != TransformOperation::TRANSLATE &&
661                     type != TransformOperation::TRANSLATE_Z &&
662                     type != TransformOperation::TRANSLATE_3D
663                     ) {
664                 applyTransformOrigin = true;
665                 break;
666             }
667         }
668     }
669 
670     if (applyTransformOrigin) {
671         transform.translate3d(transformOriginX().calcFloatValue(borderBoxSize.width()), transformOriginY().calcFloatValue(borderBoxSize.height()), transformOriginZ());
672     }
673 
674     for (i = 0; i < s; i++)
675         rareNonInheritedData->m_transform->m_operations.operations()[i]->apply(transform, borderBoxSize);
676 
677     if (applyTransformOrigin) {
678         transform.translate3d(-transformOriginX().calcFloatValue(borderBoxSize.width()), -transformOriginY().calcFloatValue(borderBoxSize.height()), -transformOriginZ());
679     }
680 }
681 
682 #if ENABLE(XBL)
addBindingURI(StringImpl * uri)683 void RenderStyle::addBindingURI(StringImpl* uri)
684 {
685     BindingURI* binding = new BindingURI(uri);
686     if (!bindingURIs())
687         SET_VAR(rareNonInheritedData, bindingURI, binding)
688     else
689         for (BindingURI* b = bindingURIs(); b; b = b->next()) {
690             if (!b->next())
691                 b->setNext(binding);
692         }
693 }
694 #endif
695 
setTextShadow(ShadowData * val,bool add)696 void RenderStyle::setTextShadow(ShadowData* val, bool add)
697 {
698     ASSERT(!val || !val->spread && val->style == Normal);
699 
700     StyleRareInheritedData* rareData = rareInheritedData.access();
701     if (!add) {
702         delete rareData->textShadow;
703         rareData->textShadow = val;
704         return;
705     }
706 
707     val->next = rareData->textShadow;
708     rareData->textShadow = val;
709 }
710 
setBoxShadow(ShadowData * shadowData,bool add)711 void RenderStyle::setBoxShadow(ShadowData* shadowData, bool add)
712 {
713     StyleRareNonInheritedData* rareData = rareNonInheritedData.access();
714     if (!add) {
715         rareData->m_boxShadow.set(shadowData);
716         return;
717     }
718 
719     shadowData->next = rareData->m_boxShadow.release();
720     rareData->m_boxShadow.set(shadowData);
721 }
722 
getBorderRadiiForRect(const IntRect & r,IntSize & topLeft,IntSize & topRight,IntSize & bottomLeft,IntSize & bottomRight) const723 void RenderStyle::getBorderRadiiForRect(const IntRect& r, IntSize& topLeft, IntSize& topRight, IntSize& bottomLeft, IntSize& bottomRight) const
724 {
725     topLeft = surround->border.topLeft;
726     topRight = surround->border.topRight;
727 
728     bottomLeft = surround->border.bottomLeft;
729     bottomRight = surround->border.bottomRight;
730 
731     // Constrain corner radii using CSS3 rules:
732     // http://www.w3.org/TR/css3-background/#the-border-radius
733 
734     float factor = 1;
735     unsigned radiiSum;
736 
737     // top
738     radiiSum = static_cast<unsigned>(topLeft.width()) + static_cast<unsigned>(topRight.width()); // Casts to avoid integer overflow.
739     if (radiiSum > static_cast<unsigned>(r.width()))
740         factor = min(static_cast<float>(r.width()) / radiiSum, factor);
741 
742     // bottom
743     radiiSum = static_cast<unsigned>(bottomLeft.width()) + static_cast<unsigned>(bottomRight.width());
744     if (radiiSum > static_cast<unsigned>(r.width()))
745         factor = min(static_cast<float>(r.width()) / radiiSum, factor);
746 
747     // left
748     radiiSum = static_cast<unsigned>(topLeft.height()) + static_cast<unsigned>(bottomLeft.height());
749     if (radiiSum > static_cast<unsigned>(r.height()))
750         factor = min(static_cast<float>(r.height()) / radiiSum, factor);
751 
752     // right
753     radiiSum = static_cast<unsigned>(topRight.height()) + static_cast<unsigned>(bottomRight.height());
754     if (radiiSum > static_cast<unsigned>(r.height()))
755         factor = min(static_cast<float>(r.height()) / radiiSum, factor);
756 
757     // Scale all radii by f if necessary.
758     if (factor < 1) {
759         // If either radius on a corner becomes zero, reset both radii on that corner.
760         topLeft.scale(factor);
761         if (!topLeft.width() || !topLeft.height())
762             topLeft = IntSize();
763         topRight.scale(factor);
764         if (!topRight.width() || !topRight.height())
765             topRight = IntSize();
766         bottomLeft.scale(factor);
767         if (!bottomLeft.width() || !bottomLeft.height())
768             bottomLeft = IntSize();
769         bottomRight.scale(factor);
770         if (!bottomRight.width() || !bottomRight.height())
771             bottomRight = IntSize();
772     }
773 }
774 
counterDirectives() const775 const CounterDirectiveMap* RenderStyle::counterDirectives() const
776 {
777     return rareNonInheritedData->m_counterDirectives.get();
778 }
779 
accessCounterDirectives()780 CounterDirectiveMap& RenderStyle::accessCounterDirectives()
781 {
782     OwnPtr<CounterDirectiveMap>& map = rareNonInheritedData.access()->m_counterDirectives;
783     if (!map)
784         map.set(new CounterDirectiveMap);
785     return *map.get();
786 }
787 
788 #if ENABLE(DASHBOARD_SUPPORT)
initialDashboardRegions()789 const Vector<StyleDashboardRegion>& RenderStyle::initialDashboardRegions()
790 {
791     DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, emptyList, ());
792     return emptyList;
793 }
794 
noneDashboardRegions()795 const Vector<StyleDashboardRegion>& RenderStyle::noneDashboardRegions()
796 {
797     DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, noneList, ());
798     static bool noneListInitialized = false;
799 
800     if (!noneListInitialized) {
801         StyleDashboardRegion region;
802         region.label = "";
803         region.offset.m_top  = Length();
804         region.offset.m_right = Length();
805         region.offset.m_bottom = Length();
806         region.offset.m_left = Length();
807         region.type = StyleDashboardRegion::None;
808         noneList.append(region);
809         noneListInitialized = true;
810     }
811     return noneList;
812 }
813 #endif
814 
adjustAnimations()815 void RenderStyle::adjustAnimations()
816 {
817     AnimationList* animationList = rareNonInheritedData->m_animations.get();
818     if (!animationList)
819         return;
820 
821     // Get rid of empty animations and anything beyond them
822     for (size_t i = 0; i < animationList->size(); ++i) {
823         if (animationList->animation(i)->isEmpty()) {
824             animationList->resize(i);
825             break;
826         }
827     }
828 
829     if (animationList->isEmpty()) {
830         clearAnimations();
831         return;
832     }
833 
834     // Repeat patterns into layers that don't have some properties set.
835     animationList->fillUnsetProperties();
836 }
837 
adjustTransitions()838 void RenderStyle::adjustTransitions()
839 {
840     AnimationList* transitionList = rareNonInheritedData->m_transitions.get();
841     if (!transitionList)
842         return;
843 
844     // Get rid of empty transitions and anything beyond them
845     for (size_t i = 0; i < transitionList->size(); ++i) {
846         if (transitionList->animation(i)->isEmpty()) {
847             transitionList->resize(i);
848             break;
849         }
850     }
851 
852     if (transitionList->isEmpty()) {
853         clearTransitions();
854         return;
855     }
856 
857     // Repeat patterns into layers that don't have some properties set.
858     transitionList->fillUnsetProperties();
859 
860     // Make sure there are no duplicate properties. This is an O(n^2) algorithm
861     // but the lists tend to be very short, so it is probably ok
862     for (size_t i = 0; i < transitionList->size(); ++i) {
863         for (size_t j = i+1; j < transitionList->size(); ++j) {
864             if (transitionList->animation(i)->property() == transitionList->animation(j)->property()) {
865                 // toss i
866                 transitionList->remove(i);
867                 j = i;
868             }
869         }
870     }
871 }
872 
accessAnimations()873 AnimationList* RenderStyle::accessAnimations()
874 {
875     if (!rareNonInheritedData.access()->m_animations)
876         rareNonInheritedData.access()->m_animations.set(new AnimationList());
877     return rareNonInheritedData->m_animations.get();
878 }
879 
accessTransitions()880 AnimationList* RenderStyle::accessTransitions()
881 {
882     if (!rareNonInheritedData.access()->m_transitions)
883         rareNonInheritedData.access()->m_transitions.set(new AnimationList());
884     return rareNonInheritedData->m_transitions.get();
885 }
886 
transitionForProperty(int property) const887 const Animation* RenderStyle::transitionForProperty(int property) const
888 {
889     if (transitions()) {
890         for (size_t i = 0; i < transitions()->size(); ++i) {
891             const Animation* p = transitions()->animation(i);
892             if (p->property() == cAnimateAll || p->property() == property) {
893                 return p;
894             }
895         }
896     }
897     return 0;
898 }
899 
setBlendedFontSize(int size)900 void RenderStyle::setBlendedFontSize(int size)
901 {
902     FontDescription desc(fontDescription());
903     desc.setSpecifiedSize(size);
904     desc.setComputedSize(size);
905     setFontDescription(desc);
906     font().update(font().fontSelector());
907 }
908 
getBoxShadowExtent(int & top,int & right,int & bottom,int & left) const909 void RenderStyle::getBoxShadowExtent(int &top, int &right, int &bottom, int &left) const
910 {
911     top = 0;
912     right = 0;
913     bottom = 0;
914     left = 0;
915 
916     for (ShadowData* boxShadow = this->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
917         if (boxShadow->style == Inset)
918             continue;
919         int blurAndSpread = boxShadow->blur + boxShadow->spread;
920 
921         top = min(top, boxShadow->y - blurAndSpread);
922         right = max(right, boxShadow->x + blurAndSpread);
923         bottom = max(bottom, boxShadow->y + blurAndSpread);
924         left = min(left, boxShadow->x - blurAndSpread);
925     }
926 }
927 
getBoxShadowHorizontalExtent(int & left,int & right) const928 void RenderStyle::getBoxShadowHorizontalExtent(int &left, int &right) const
929 {
930     left = 0;
931     right = 0;
932 
933     for (ShadowData* boxShadow = this->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
934         if (boxShadow->style == Inset)
935             continue;
936         int blurAndSpread = boxShadow->blur + boxShadow->spread;
937 
938         left = min(left, boxShadow->x - blurAndSpread);
939         right = max(right, boxShadow->x + blurAndSpread);
940     }
941 }
942 
getBoxShadowVerticalExtent(int & top,int & bottom) const943 void RenderStyle::getBoxShadowVerticalExtent(int &top, int &bottom) const
944 {
945     top = 0;
946     bottom = 0;
947 
948     for (ShadowData* boxShadow = this->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
949         if (boxShadow->style == Inset)
950             continue;
951         int blurAndSpread = boxShadow->blur + boxShadow->spread;
952 
953         top = min(top, boxShadow->y - blurAndSpread);
954         bottom = max(bottom, boxShadow->y + blurAndSpread);
955     }
956 }
957 
958 } // namespace WebCore
959