1 {% from "macros.tmpl" import lower_first -%}
4 This file is for property handlers which use the templating engine to
5 reduce (handwritten) code duplication.
7 The `properties' dict can be used to access a property's parameters in
8 jinja2 templates (i.e. setter, getter, initial, type_name)
12 #include "StyleBuilderFunctions.h"
14 #include "CSSValueKeywords.h"
15 #include "core/animation/css/CSSAnimationDataList.h"
16 #include "core/css/BasicShapeFunctions.h"
17 #include "core/css/CSSPrimitiveValueMappings.h"
18 #include "core/css/Pair.h"
19 #include "core/css/resolver/StyleResolverState.h"
22 {%- macro declare_initial_function(property_id) -%}
23 void StyleBuilderFunctions::applyInitial{{property_id}}(StyleResolverState& state)
26 {%- macro declare_inherit_function(property_id) -%}
27 void StyleBuilderFunctions::applyInherit{{property_id}}(StyleResolverState& state)
30 {%- macro declare_value_function(property_id) -%}
31 void StyleBuilderFunctions::applyValue{{property_id}}(StyleResolverState& state, CSSValue* value)
34 // FIXME: This is duplicated in StyleBuilder.cpp.tmpl, but we'll move the
35 // function definitions there over to here later.
36 {%- macro set_value(property) %}
37 {%- if property.svg -%}
38 state.style()->accessSVGStyle()->{{property.setter}}
40 state.style()->{{property.setter}}
46 {%- macro apply_animation(property_id, attribute, animation) %}
47 {{ declare_initial_function(property_id) }}
49 CSSAnimationDataList* list = state.style()->access{{animation}}();
51 list->append(CSSAnimationData::create());
52 list->animation(0)->set{{attribute}}(CSSAnimationData::initialAnimation{{attribute}}());
53 {%- if property_id == "CSSPropertyWebkitTransitionProperty" %}
54 list->animation(0)->setAnimationMode(CSSAnimationData::AnimateAll);
56 for (size_t i = 1; i < list->size(); ++i)
57 list->animation(i)->clear{{attribute}}();
60 {{ declare_inherit_function(property_id) }}
62 CSSAnimationDataList* list = state.style()->access{{animation}}();
63 const CSSAnimationDataList* parentList = state.parentStyle()->{{animation|lower}}();
64 size_t i = 0, parentSize = parentList ? parentList->size() : 0;
65 for ( ; i < parentSize && parentList->animation(i)->is{{attribute}}Set(); ++i) {
66 ASSERT(list->size() >= i);
67 if (list->size() == i)
68 list->append(CSSAnimationData::create());
69 list->animation(i)->set{{attribute}}(parentList->animation(i)->{{lower_first(attribute)}}());
70 {%- if property_id == "CSSPropertyWebkitTransitionProperty" %}
71 list->animation(i)->setAnimationMode(parentList->animation(i)->animationMode());
75 // Reset any remaining animations to not have the property set.
76 for ( ; i < list->size(); ++i)
77 list->animation(i)->clear{{attribute}}();
80 {{ declare_value_function(property_id) }}
82 CSSAnimationDataList* list = state.style()->access{{animation}}();
83 size_t childIndex = 0;
84 if (value->isValueList()) {
85 // Walk each value and put it into an animation, creating new animations as needed.
86 for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
87 ASSERT(list->size() >= childIndex);
88 if (list->size() == childIndex)
89 list->append(CSSAnimationData::create());
90 state.styleMap().mapAnimation{{attribute}}(list->animation(childIndex), i.value());
95 list->append(CSSAnimationData::create());
96 state.styleMap().mapAnimation{{attribute}}(list->animation(childIndex), value);
99 for ( ; childIndex < list->size(); ++childIndex) {
100 // Reset all remaining animations to not have the property set.
101 list->animation(childIndex)->clear{{attribute}}();
106 {{ apply_animation("CSSPropertyWebkitAnimationDelay", "Delay", "Animations") }}
107 {{ apply_animation("CSSPropertyWebkitAnimationDirection", "Direction", "Animations") }}
108 {{ apply_animation("CSSPropertyWebkitAnimationDuration", "Duration", "Animations") }}
109 {{ apply_animation("CSSPropertyWebkitAnimationFillMode", "FillMode", "Animations") }}
110 {{ apply_animation("CSSPropertyWebkitAnimationIterationCount", "IterationCount", "Animations") }}
111 {{ apply_animation("CSSPropertyWebkitAnimationName", "Name", "Animations") }}
112 {{ apply_animation("CSSPropertyWebkitAnimationPlayState", "PlayState", "Animations") }}
113 {{ apply_animation("CSSPropertyWebkitAnimationTimingFunction", "TimingFunction", "Animations") }}
114 {{ apply_animation("CSSPropertyWebkitTransitionDelay", "Delay", "Transitions") }}
115 {{ apply_animation("CSSPropertyWebkitTransitionDuration", "Duration", "Transitions") }}
116 {{ apply_animation("CSSPropertyWebkitTransitionProperty", "Property", "Transitions") }}
117 {{ apply_animation("CSSPropertyWebkitTransitionTimingFunction", "TimingFunction", "Transitions") }}
119 {%- macro apply_auto(property_id, auto_getter=none, auto_setter=none, auto_identity="CSSValueAuto", compute_length=false) %}
120 {%- set property = properties[property_id] %}
121 {%- set auto_getter = auto_getter or "hasAuto" + property.camel_case_name %}
122 {%- set auto_setter = auto_setter or "setHasAuto" + property.camel_case_name %}
123 {{ declare_initial_function(property_id) }}
125 state.style()->{{auto_setter}}();
128 {{ declare_inherit_function(property_id) }}
130 if (state.parentStyle()->{{auto_getter}}())
131 state.style()->{{auto_setter}}();
133 {{ set_value(property) }}(state.parentStyle()->{{property.getter}}());
136 {{ declare_value_function(property_id) }}
138 if (!value->isPrimitiveValue())
141 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
142 if (primitiveValue->getValueID() == {{auto_identity}})
143 state.style()->{{auto_setter}}();
145 {%- if compute_length %}
146 {{ set_value(property) }}(primitiveValue->computeLength<{{property.type_name}}>(state.cssToLengthConversionData()));
148 {{ set_value(property) }}(*primitiveValue);
153 {{ apply_auto("CSSPropertyOrphans") }}
154 {{ apply_auto("CSSPropertyWebkitColumnCount") }}
155 {{ apply_auto("CSSPropertyWebkitColumnGap", auto_getter="hasNormalColumnGap", auto_setter="setHasNormalColumnGap", auto_identity="CSSValueNormal", compute_length=true) }}
156 {{ apply_auto("CSSPropertyWebkitColumnWidth", compute_length=true) }}
157 {{ apply_auto("CSSPropertyWidows") }}
158 {{ apply_auto("CSSPropertyZIndex") }}
160 {%- macro apply_border_image_modifier(property_id, modifier_type) %}
161 {%- set is_mask_box = "MaskBox" in property_id %}
162 {%- set getter = "maskBoxImage" if is_mask_box else "borderImage" %}
163 {%- set setter = "setMaskBoxImage" if is_mask_box else "setBorderImage" %}
164 {{ declare_initial_function(property_id) }}
166 NinePieceImage image(state.style()->{{getter}}());
167 {%- if modifier_type == "Outset" %}
168 image.setOutset(Length(0, Fixed));
169 {%- elif modifier_type == "Repeat" %}
170 image.setHorizontalRule(StretchImageRule);
171 image.setVerticalRule(StretchImageRule);
172 {%- elif modifier_type == "Slice" and is_mask_box %}
173 // Masks have a different initial value for slices. Preserve the value of 0 for backwards compatibility.
174 image.setImageSlices(LengthBox({{ (["Length(0, Fixed)"]*4) | join(", ") }}));
176 {%- elif modifier_type == "Slice" and not is_mask_box %}
177 image.setImageSlices(LengthBox({{ (["Length(100, Percent)"]*4) | join(", ") }}));
178 image.setFill(false);
179 {%- elif modifier_type == "Width" %}
180 // Masks have a different initial value for widths. Preserve the value of 'auto' for backwards compatibility.
181 image.setBorderSlices({{ "Length(Auto)" if is_mask_box else "1.0" }});
183 state.style()->{{setter}}(image);
186 {{ declare_inherit_function(property_id) }}
188 NinePieceImage image(state.style()->{{getter}}());
189 {%- if modifier_type == "Outset" %}
190 image.copyOutsetFrom(state.parentStyle()->{{getter}}());
191 {%- elif modifier_type == "Repeat" %}
192 image.copyRepeatFrom(state.parentStyle()->{{getter}}());
193 {%- elif modifier_type == "Slice" %}
194 image.copyImageSlicesFrom(state.parentStyle()->{{getter}}());
195 {%- elif modifier_type == "Width" %}
196 image.copyBorderSlicesFrom(state.parentStyle()->{{getter}}());
198 state.style()->{{setter}}(image);
201 {{ declare_value_function(property_id) }}
203 NinePieceImage image(state.style()->{{getter}}());
204 {%- if modifier_type == "Outset" %}
205 image.setOutset(state.styleMap().mapNinePieceImageQuad(value));
206 {%- elif modifier_type == "Repeat" %}
207 state.styleMap().mapNinePieceImageRepeat(value, image);
208 {%- elif modifier_type == "Slice" %}
209 state.styleMap().mapNinePieceImageSlice(value, image);
210 {%- elif modifier_type == "Width" %}
211 image.setBorderSlices(state.styleMap().mapNinePieceImageQuad(value));
213 state.style()->{{setter}}(image);
217 {{ apply_border_image_modifier("CSSPropertyBorderImageOutset", "Outset") }}
218 {{ apply_border_image_modifier("CSSPropertyBorderImageRepeat", "Repeat") }}
219 {{ apply_border_image_modifier("CSSPropertyBorderImageSlice", "Slice") }}
220 {{ apply_border_image_modifier("CSSPropertyBorderImageWidth", "Width") }}
221 {{ apply_border_image_modifier("CSSPropertyWebkitMaskBoxImageOutset", "Outset") }}
222 {{ apply_border_image_modifier("CSSPropertyWebkitMaskBoxImageRepeat", "Repeat") }}
223 {{ apply_border_image_modifier("CSSPropertyWebkitMaskBoxImageSlice", "Slice") }}
224 {{ apply_border_image_modifier("CSSPropertyWebkitMaskBoxImageWidth", "Width") }}
226 {%- macro apply_value_border_image_source(property_id) %}
227 {{ declare_value_function(property_id) }}
229 {%- set property = properties[property_id] %}
230 {{ set_value(property) }}(state.styleImage({{property_id}}, value));
234 {{ apply_value_border_image_source("CSSPropertyBorderImageSource") }}
235 {{ apply_value_border_image_source("CSSPropertyWebkitMaskBoxImageSource") }}
237 {%- macro apply_color(property_id, initial_color="StyleColor::currentColor") %}
238 {%- set property = properties[property_id] %}
239 {%- set visited_link_setter = "setVisitedLink" + property.camel_case_name %}
240 {{ declare_initial_function(property_id) }}
242 StyleColor color = {{initial_color}}();
243 if (state.applyPropertyToRegularStyle())
244 {{ set_value(property) }}(color);
245 if (state.applyPropertyToVisitedLinkStyle())
246 state.style()->{{visited_link_setter}}(color);
249 {{ declare_inherit_function(property_id) }}
251 // Visited link style can never explicitly inherit from parent visited link style so no separate getters are needed.
252 StyleColor color = state.parentStyle()->{{property.getter}}();
253 Color resolvedColor = color.resolve(state.parentStyle()->color());
254 if (state.applyPropertyToRegularStyle())
255 {{ set_value(property) }}(resolvedColor);
256 if (state.applyPropertyToVisitedLinkStyle())
257 state.style()->{{visited_link_setter}}(resolvedColor);
260 {{ declare_value_function(property_id) }}
262 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
264 if (state.applyPropertyToRegularStyle())
265 {{ set_value(property) }}(state.document().textLinkColors().colorFromPrimitiveValue(primitiveValue, state.style()->color()));
266 if (state.applyPropertyToVisitedLinkStyle())
267 state.style()->{{visited_link_setter}}(state.document().textLinkColors().colorFromPrimitiveValue(primitiveValue, state.style()->color(), state.element()->isLink() /* forVisitedLink */));
271 {{ apply_color("CSSPropertyBackgroundColor", initial_color="RenderStyle::initialBackgroundColor") }}
272 {{ apply_color("CSSPropertyBorderBottomColor") }}
273 {{ apply_color("CSSPropertyBorderLeftColor") }}
274 {{ apply_color("CSSPropertyBorderRightColor") }}
275 {{ apply_color("CSSPropertyBorderTopColor") }}
276 {{ apply_color("CSSPropertyOutlineColor") }}
277 {{ apply_color("CSSPropertyTextDecorationColor") }}
278 {{ apply_color("CSSPropertyWebkitColumnRuleColor") }}
279 {{ apply_color("CSSPropertyWebkitTextEmphasisColor") }}
280 {{ apply_color("CSSPropertyWebkitTextFillColor") }}
281 {{ apply_color("CSSPropertyWebkitTextStrokeColor") }}
283 {%- macro apply_counter(property_id, action) %}
284 {%- set property = properties[property_id] %}
285 {{ declare_initial_function(property_id) }} { }
287 {{ declare_inherit_function(property_id) }}
289 CounterDirectiveMap& map = state.style()->accessCounterDirectives();
290 CounterDirectiveMap& parentMap = state.parentStyle()->accessCounterDirectives();
292 typedef CounterDirectiveMap::iterator Iterator;
293 Iterator end = parentMap.end();
294 for (Iterator it = parentMap.begin(); it != end; ++it) {
295 CounterDirectives& directives = map.add(it->key, CounterDirectives()).storedValue->value;
296 directives.inherit{{action}}(it->value);
300 {{ declare_value_function(property_id) }}
302 CounterDirectiveMap& map = state.style()->accessCounterDirectives();
303 typedef CounterDirectiveMap::iterator Iterator;
305 Iterator end = map.end();
306 for (Iterator it = map.begin(); it != end; ++it)
307 it->value.clear{{action}}();
309 if (!value->isValueList()) {
310 ASSERT(value->isPrimitiveValue() && toCSSPrimitiveValue(value)->getValueID() == CSSValueNone);
314 CSSValueList* list = toCSSValueList(value);
316 int length = list ? list->length() : 0;
317 for (int i = 0; i < length; ++i) {
318 CSSValue* currValue = list->itemWithoutBoundsCheck(i);
319 if (!currValue->isPrimitiveValue())
322 Pair* pair = toCSSPrimitiveValue(currValue)->getPairValue();
323 if (!pair || !pair->first() || !pair->second())
326 AtomicString identifier(pair->first()->getStringValue());
327 int value = pair->second()->getIntValue();
328 CounterDirectives& directives = map.add(identifier, CounterDirectives()).storedValue->value;
329 {%- if action == "Reset" %}
330 directives.setResetValue(value);
332 directives.addIncrementValue(value);
338 {{ apply_counter("CSSPropertyCounterIncrement", "Increment") }}
339 {{ apply_counter("CSSPropertyCounterReset", "Reset") }}
341 {%- macro apply_fill_layer(property_id, fill_type) %}
342 {%- set layer_type = "Background" if "Background" in property_id else "Mask" %}
343 {%- set fill_layer_type = layer_type + "FillLayer" %}
344 {%- set access_layers = "access" + layer_type + "Layers" %}
345 {%- set map_fill = "mapFill" + fill_type %}
346 {{ declare_initial_function(property_id) }}
348 FillLayer* currChild = state.style()->{{access_layers}}();
349 currChild->set{{fill_type}}(FillLayer::initialFill{{fill_type}}({{fill_layer_type}}));
350 for (currChild = currChild->next(); currChild; currChild = currChild->next())
351 currChild->clear{{fill_type}}();
354 {{ declare_inherit_function(property_id) }}
356 FillLayer* currChild = state.style()->{{access_layers}}();
357 FillLayer* prevChild = 0;
358 const FillLayer* currParent = state.parentStyle()->{{layer_type|lower}}Layers();
359 while (currParent && currParent->is{{fill_type}}Set()) {
361 /* Need to make a new layer.*/
362 currChild = new FillLayer({{fill_layer_type}});
363 prevChild->setNext(currChild);
365 currChild->set{{fill_type}}(currParent->{{lower_first(fill_type)}}());
366 prevChild = currChild;
367 currChild = prevChild->next();
368 currParent = currParent->next();
372 /* Reset any remaining layers to not have the property set. */
373 currChild->clear{{fill_type}}();
374 currChild = currChild->next();
378 {{ declare_value_function(property_id) }}
380 FillLayer* currChild = state.style()->{{access_layers}}();
381 FillLayer* prevChild = 0;
382 if (value->isValueList() && !value->isImageSetValue()) {
383 /* Walk each value and put it into a layer, creating new layers as needed. */
384 CSSValueList* valueList = toCSSValueList(value);
385 for (unsigned int i = 0; i < valueList->length(); i++) {
387 /* Need to make a new layer to hold this value */
388 currChild = new FillLayer({{fill_layer_type}});
389 prevChild->setNext(currChild);
391 state.styleMap().{{map_fill}}({{property_id}}, currChild, valueList->itemWithoutBoundsCheck(i));
392 prevChild = currChild;
393 currChild = currChild->next();
396 state.styleMap().{{map_fill}}({{property_id}}, currChild, value);
397 currChild = currChild->next();
400 /* Reset all remaining layers to not have the property set. */
401 currChild->clear{{fill_type}}();
402 currChild = currChild->next();
407 {{ apply_fill_layer("CSSPropertyBackgroundAttachment", "Attachment") }}
408 {{ apply_fill_layer("CSSPropertyBackgroundBlendMode", "BlendMode") }}
409 {{ apply_fill_layer("CSSPropertyBackgroundClip", "Clip") }}
410 {{ apply_fill_layer("CSSPropertyBackgroundImage", "Image") }}
411 {{ apply_fill_layer("CSSPropertyBackgroundOrigin", "Origin") }}
412 {{ apply_fill_layer("CSSPropertyBackgroundPositionX", "XPosition") }}
413 {{ apply_fill_layer("CSSPropertyBackgroundPositionY", "YPosition") }}
414 {{ apply_fill_layer("CSSPropertyBackgroundRepeatX", "RepeatX") }}
415 {{ apply_fill_layer("CSSPropertyBackgroundRepeatY", "RepeatY") }}
416 {{ apply_fill_layer("CSSPropertyBackgroundSize", "Size") }}
417 {{ apply_fill_layer("CSSPropertyMaskSourceType", "MaskSourceType") }}
418 {{ apply_fill_layer("CSSPropertyWebkitBackgroundComposite", "Composite") }}
419 {{ apply_fill_layer("CSSPropertyWebkitMaskClip", "Clip") }}
420 {{ apply_fill_layer("CSSPropertyWebkitMaskComposite", "Composite") }}
421 {{ apply_fill_layer("CSSPropertyWebkitMaskImage", "Image") }}
422 {{ apply_fill_layer("CSSPropertyWebkitMaskOrigin", "Origin") }}
423 {{ apply_fill_layer("CSSPropertyWebkitMaskPositionX", "XPosition") }}
424 {{ apply_fill_layer("CSSPropertyWebkitMaskPositionY", "YPosition") }}
425 {{ apply_fill_layer("CSSPropertyWebkitMaskRepeatX", "RepeatX") }}
426 {{ apply_fill_layer("CSSPropertyWebkitMaskRepeatY", "RepeatY") }}
427 {{ apply_fill_layer("CSSPropertyWebkitMaskSize", "Size") }}
429 {%- macro apply_font(property_id, name_for_methods, initial, type_name) %}
430 {#- We specify the getters/setters here since they are on FontDescription
431 and not RenderStyle #}
432 {%- set getter = lower_first(name_for_methods) %}
433 {%- set setter = "set" + name_for_methods %}
434 {{ declare_initial_function(property_id) }}
436 state.fontBuilder().{{setter}}({{initial}});
439 {{ declare_inherit_function(property_id) }}
441 state.fontBuilder().{{setter}}(state.parentFontDescription().{{getter}}());
444 {{ declare_value_function(property_id) }}
446 if (!value->isPrimitiveValue())
448 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
449 state.fontBuilder().{{setter}}(static_cast<{{type_name}}>(*primitiveValue));
453 {{ apply_font("CSSPropertyFontStyle", "Italic", "FontItalicOff", "FontItalic") }}
454 {{ apply_font("CSSPropertyFontVariant", "SmallCaps", "FontSmallCapsOff", "FontSmallCaps") }}
455 {{ apply_font("CSSPropertyTextRendering", "TextRenderingMode", "AutoTextRendering", "TextRenderingMode") }}
456 {{ apply_font("CSSPropertyFontKerning", "Kerning", "FontDescription::AutoKerning", "FontDescription::Kerning") }}
457 {{ apply_font("CSSPropertyWebkitFontSmoothing", "FontSmoothing", "AutoSmoothing", "FontSmoothingMode") }}
459 {%- macro apply_value_number(property_id, id_for_minus_one) %}
460 {{ declare_value_function(property_id) }}
462 {%- set property = properties[property_id] %}
463 if (!value->isPrimitiveValue())
466 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
467 if (primitiveValue->getValueID() == {{id_for_minus_one}})
468 {{ set_value(property) }}(-1);
470 {{ set_value(property) }}(primitiveValue->getValue<{{property.type_name}}>(CSSPrimitiveValue::CSS_NUMBER));
474 {{ apply_value_number("CSSPropertyInternalMarqueeRepetition", "CSSValueInfinite") }}
476 {%- macro apply_value_shape(property_id) %}
477 {{ declare_value_function(property_id) }}
479 {%- set property = properties[property_id] %}
480 if (value->isPrimitiveValue()) {
481 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
482 if (primitiveValue->getValueID() == CSSValueAuto)
483 {{ set_value(property) }}(0);
484 else if (primitiveValue->getValueID() == CSSValueMarginBox
485 || primitiveValue->getValueID() == CSSValueBorderBox
486 || primitiveValue->getValueID() == CSSValuePaddingBox
487 || primitiveValue->getValueID() == CSSValueContentBox)
488 {{ set_value(property) }}(ShapeValue::createLayoutBoxValue(LayoutBox(*primitiveValue)));
489 else if (primitiveValue->getValueID() == CSSValueOutsideShape)
490 {{ set_value(property) }}(ShapeValue::createOutsideValue());
491 else if (primitiveValue->isShape()) {
492 {{ set_value(property) }}(ShapeValue::createShapeValue(basicShapeForValue(state, primitiveValue->getShapeValue())));
494 } else if (value->isImageValue() || value->isImageSetValue()) {
495 {{ set_value(property) }}(ShapeValue::createImageValue(state.styleImage({{property_id}}, value)));
500 {{ apply_value_shape("CSSPropertyShapeInside") }}
501 {{ apply_value_shape("CSSPropertyShapeOutside") }}
503 } // namespace WebCore